コード例 #1
0
        private static List <Repair> GetRepairs(List <string> repairArgs)
        {
            List <Repair> repairs = new List <Repair>();

            for (int i = 0; i < repairArgs.Count; i += 2)
            {
                string partName    = repairArgs[i];
                int    hoursWorked = int.Parse(repairArgs[i + 1]);

                Repair repair = new Repair(partName, hoursWorked);

                repairs.Add(repair);
            }

            return(repairs);
        }
コード例 #2
0
ファイル: Startup.cs プロジェクト: irinastoicheva/Csharp-OOP
        public static void Main()
        {
            string input = Console.ReadLine();
            Army   army  = new Army();

            while (input != "End")
            {
                string[] parts = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);

                string  typeSoldier = parts[0];
                int     id          = int.Parse(parts[1]);
                string  firstName   = parts[2];
                string  lastName    = parts[3];
                decimal salary;
                string  corps;

                switch (typeSoldier)
                {
                case "Private":
                    salary = decimal.Parse(parts[4]);
                    Private currentPrivate = new Private(id, firstName, lastName, salary);
                    army.AddSoldier(currentPrivate);
                    break;

                case "LieutenantGeneral":
                    salary = decimal.Parse(parts[4]);
                    int[] ids = new int[parts.Length - 5];
                    for (int i = 0; i < ids.Length; i++)
                    {
                        ids[i] = int.Parse(parts[i + 5]);
                    }

                    HashSet <Private> privates          = army.GetPrivates(ids);
                    LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(id, firstName, lastName, salary, privates);
                    army.AddSoldier(lieutenantGeneral);
                    break;

                case "Engineer":
                    salary = decimal.Parse(parts[4]);
                    corps  = parts[5];
                    if (corps == "Marines" || corps == "Airforces")
                    {
                        HashSet <Repair> repairs = new HashSet <Repair>();
                        for (int i = 6; i < parts.Length; i += 2)
                        {
                            Repair repair = new Repair(parts[i], int.Parse(parts[i + 1]));
                            if (repair != null)
                            {
                                repairs.Add(repair);
                            }
                        }

                        Engineer engineer = new Engineer(id, firstName, lastName, salary, corps, repairs);

                        army.AddSoldier(engineer);
                    }
                    break;

                case "Commando":
                    salary = decimal.Parse(parts[4]);
                    corps  = parts[5];
                    if (corps == "Marines" || corps == "Airforces")
                    {
                        HashSet <Mission> missions = new HashSet <Mission>();
                        for (int i = 6; i < parts.Length; i += 2)
                        {
                            Mission mission = new Mission(parts[i], parts[i + 1]);
                            if (mission != null)
                            {
                                missions.Add(mission);
                            }
                        }

                        Commando commando = new Commando(id, firstName, lastName, salary, corps, missions);
                        army.AddSoldier(commando);
                    }
                    break;

                case "Spy":
                    int codeNumber = int.Parse(parts[4]);
                    Spy spy        = new Spy(id, firstName, lastName, codeNumber);
                    army.AddSoldier(spy);
                    break;
                }

                input = Console.ReadLine();
            }

            Console.WriteLine(army);
        }
コード例 #3
0
        public static void Main(string[] args)
        {
            var allSoldiers = new List <ISoldier>();
            var privates    = new List <IPrivate>();

            string input = Console.ReadLine();

            while (input != "End")
            {
                string[] data      = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   title     = data[0];
                string   id        = data[1];
                string   firstName = data[2];
                string   lastName  = data[3];
                decimal  salary    = decimal.Parse(data[4]);

                switch (title)
                {
                case "Private":
                    var currentPrivate = new Private(id, firstName, lastName, salary);
                    privates.Add(currentPrivate);
                    allSoldiers.Add(currentPrivate);
                    break;

                case "LieutenantGeneral":
                    var currentLeutenant = new LieutenantGeneral(id, firstName, lastName, salary);
                    for (int i = 5; i < data.Length; i++)
                    {
                        currentLeutenant.AddPrivate(privates, data[i]);
                    }
                    allSoldiers.Add(currentLeutenant);
                    break;

                case "Engineer":
                    string engCorps = data[5];
                    if (engCorps == "Airforces" || engCorps == "Marines")
                    {
                        var currentEngineer = new Engineer(id, firstName, lastName, salary, engCorps);
                        for (int i = 6; i < data.Length; i += 2)
                        {
                            var repair = new Repair(data[i], int.Parse(data[i + 1]));
                            currentEngineer.AddRepair(repair);
                        }
                        if (currentEngineer.Corps != null)
                        {
                            allSoldiers.Add(currentEngineer);
                        }
                    }

                    break;

                case "Commando":
                    string commandoCorps = data[5];

                    if (commandoCorps == "Airforces" || commandoCorps == "Marines")
                    {
                        var currentCommando = new Commando(id, firstName, lastName, salary, commandoCorps);
                        for (int i = 6; i < data.Length; i += 2)
                        {
                            if (data[i + 1] == "inProgress" || data[i + 1] == "Finished")
                            {
                                var mission = new Mission(data[i], data[i + 1]);
                                currentCommando.AddMission(mission);
                            }
                        }
                        allSoldiers.Add(currentCommando);
                    }
                    break;

                case "Spy":
                    var currentSpy = new Spy(id, firstName, lastName, (int)salary);
                    allSoldiers.Add(currentSpy);
                    break;

                default:
                    break;
                }
                input = Console.ReadLine();
            }

            foreach (var soldier in allSoldiers)
            {
                Console.WriteLine(soldier.ToString());
            }
        }
コード例 #4
0
ファイル: StartUp.cs プロジェクト: vankatalp360/C-OOP-Basics
        static void Main(string[] args)
        {
            var soldiers = new List <ISoldier>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                var tokens      = input.Split();
                var soldierType = tokens[0];
                int id          = int.Parse(tokens[1]);
                var firstName   = tokens[2];
                var lastName    = tokens[3];
                var salary      = decimal.Parse(tokens[4]);

                ISoldier soldier = null;

                try
                {
                    switch (soldierType)
                    {
                    case "Private":
                        soldier = new Private(id, firstName, lastName, salary);
                        break;

                    case "LeutenantGeneral":
                        var leutenant = new LeutenantGeneral(id, firstName, lastName, salary);

                        for (int i = 5; i < tokens.Length; i++)
                        {
                            var      privateId = int.Parse(tokens[i]);
                            ISoldier @private  = soldiers.FirstOrDefault(p => p.Id == privateId);
                            leutenant.AddPivate(@private);
                        }

                        soldier = leutenant;
                        break;

                    case "Engineer":
                        var engineerCorps = tokens[5];
                        var enginner      = new Engineer(id, firstName, lastName, salary, engineerCorps);

                        for (int i = 6; i < tokens.Length; i++)
                        {
                            var     partName    = tokens[i];
                            var     hoursWorked = int.Parse(tokens[++i]);
                            IRepair repair      = new Repair(partName, hoursWorked);
                            enginner.AddRepair(repair);
                        }

                        soldier = enginner;
                        break;

                    case "Commando":
                        var commandoCorps = tokens[5];
                        var commando      = new Commando(id, firstName, lastName, salary, commandoCorps);

                        for (int i = 6; i < tokens.Length; i++)
                        {
                            var codeName     = tokens[i];
                            var missionState = tokens[++i];
                            try
                            {
                                IMission mission = new Mission(codeName, missionState);
                                commando.AddMission(mission);
                            }
                            catch { }
                        }

                        soldier = commando;
                        break;

                    case "Spy":
                        int codeNumber = (int)salary;
                        soldier = new Spy(id, firstName, lastName, codeNumber);
                        break;

                    default:
                        throw new ArgumentException("Invalid soldier type!");
                    }
                    soldiers.Add(soldier);
                }
                catch (Exception)
                {
                }
            }

            foreach (var s in soldiers)
            {
                Console.WriteLine(s);
            }
        }
コード例 #5
0
        static void Main(string[] args)
        {
            var input         = Console.ReadLine();
            var myArmy        = new List <Soldier>();
            var myPrivateArmy = new List <Private>();

            while (input != "End")
            {
                var currentSoldier = input.Split(" ").ToArray();
                var typeOfSoldier  = currentSoldier[0];
                var id             = currentSoldier[1];
                var firstName      = currentSoldier[2];
                var lastName       = currentSoldier[3];

                switch (typeOfSoldier)
                {
                case "Private":
                    var salary         = decimal.Parse(currentSoldier[4]);
                    var currentPrivate = new Private(id, firstName, lastName, salary);
                    myArmy.Add(currentPrivate);
                    myPrivateArmy.Add(currentPrivate);
                    break;

                case "LieutenantGeneral":
                    salary = decimal.Parse(currentSoldier[4]);
                    var general = new LieutenantGeneral(id, firstName, lastName, salary);

                    if (currentSoldier.Length > 4)
                    {
                        for (int i = 5; i < currentSoldier.Length; i++)
                        {
                            var currentId = currentSoldier[i];

                            var foundPrivate = myPrivateArmy.FirstOrDefault(x => x.Id == currentId);

                            if (foundPrivate != null)
                            {
                                general.Privates.Add(foundPrivate);
                            }
                        }
                    }
                    myArmy.Add(general);
                    break;

                case "Spy":
                    var codeName = int.Parse(currentSoldier[4]);
                    var spy      = new Spy(id, firstName, lastName, codeName);
                    myArmy.Add(spy);
                    break;

                case "Engineer":
                    salary = decimal.Parse(currentSoldier[4]);
                    var corps = currentSoldier[5];

                    try
                    {
                        var engineer = new Engineer(id, firstName, lastName, salary, corps);
                        if (currentSoldier.Length > 7)
                        {
                            for (int i = 6; i < currentSoldier.Length; i += 2)
                            {
                                var currentRepair = new Repair(currentSoldier[i], int.Parse(currentSoldier[i + 1]));
                                engineer.Repairs.Add(currentRepair);
                            }
                        }

                        myArmy.Add(engineer);
                    }
                    catch (Exception ex)
                    {
                        input = Console.ReadLine();
                        continue;
                    }
                    break;

                case "Commando":
                    salary = decimal.Parse(currentSoldier[4]);
                    corps  = currentSoldier[5];

                    try
                    {
                        var commando = new Commando(id, firstName, lastName, salary, corps);

                        if (currentSoldier.Length > 7)
                        {
                            for (int i = 6; i < currentSoldier.Length; i += 2)
                            {
                                try
                                {
                                    var currentMission = new Mission(currentSoldier[i], currentSoldier[i + 1]);
                                    commando.Missions.Add(currentMission);
                                }
                                catch (Exception)
                                {
                                    continue;
                                }
                            }
                        }

                        myArmy.Add(commando);
                    }
                    catch (Exception)
                    {
                        input = Console.ReadLine();
                        continue;
                    }
                    break;
                }

                input = Console.ReadLine();
            }

            foreach (var soldier in myArmy)
            {
                Console.WriteLine(soldier.ToString());
            }
        }