Exemple #1
0
        private static void CreateEngeneer(string[] tokens, int id, string firstName, string lastName, decimal salary, string corp)
        {
            List <IRepair> repairs = new List <IRepair>();

            if (IsValidCorp(corp))
            {
                for (int i = 6; i < tokens.Length; i += 2)
                {
                    string repairPart  = tokens[i];
                    int    repairHours = int.Parse(tokens[i + 1]);

                    Repair repair = new Repair(repairPart, repairHours);
                    repairs.Add(repair);
                }
                Engineer engineer = new Engineer(id, firstName, lastName, salary, corp, repairs);
                Console.WriteLine(engineer.ToString());
            }
        }
Exemple #2
0
        public static void Main()
        {
            List <Private> privates = new List <Private>();
            Corps          corps;

            while (true)
            {
                string[] token = Console.ReadLine().Split();

                if (token[0] == "End")
                {
                    break;
                }

                if (token[0] == "Private")
                {
                    Private privatee = new Private(token[1], token[2], token[3], decimal.Parse(token[4]));
                    privates.Add(privatee);
                    Console.WriteLine(privatee.ToString());
                }
                else if (token[0] == "LieutenantGeneral")
                {
                    LieutenantGeneral lieutenantGeneral = new LieutenantGeneral(token[1], token[2], token[3], decimal.Parse(token[4]));

                    if (token.Length >= 5)
                    {
                        for (int i = 5; i < token.Length; i++)
                        {
                            string  privateId = token[i];
                            Private privatee  = privates.FirstOrDefault(x => x.Id == privateId);
                            lieutenantGeneral.Privates.Add(privatee);
                        }
                    }

                    Console.WriteLine(lieutenantGeneral.ToString());
                }
                else if (token[0] == "Engineer")
                {
                    if (Enum.TryParse(token[5], out corps))
                    {
                        Engineer engineer = new Engineer(token[1], token[2], token[3], decimal.Parse(token[4]), corps);

                        if (token.Length >= 6)
                        {
                            for (int i = 6; i < token.Length; i += 2)
                            {
                                string repairPartName  = token[i];
                                int    repairPartHours = int.Parse(token[i + 1]);

                                Repair repair = new Repair(repairPartName, repairPartHours);
                                engineer.Repairs.Add(repair);
                            }
                        }

                        Console.WriteLine(engineer.ToString());
                    }
                }
                else if (token[0] == "Commando")
                {
                    if (Enum.TryParse(token[5], out corps))
                    {
                        Commando commando = new Commando(token[1], token[2], token[3], decimal.Parse(token[4]), corps);

                        if (token.Length >= 6)
                        {
                            for (int i = 6; i < token.Length; i += 2)
                            {
                                string missionName     = token[i];
                                string missionProgress = token[i + 1];

                                if (Enum.TryParse(missionProgress, out MissionState missionState))
                                {
                                    Mission mission = new Mission(missionName, missionState);
                                    commando.Missions.Add(mission);
                                }
                            }

                            Console.WriteLine(commando.ToString());
                        }
                    }
                }
                else if (token[0] == "Spy")
                {
                    Spy spy = new Spy(token[1], token[2], token[3], int.Parse(token[4]));
                    Console.WriteLine(spy);
                }
                else
                {
                    throw new ArgumentException("Invalid Type of Soldier!");
                }
            }
        }
        static void Main(string[] args)
        {
            var input = Console.ReadLine().Split(" ").ToArray();

            List <Soldier> soldiers = new List <Soldier>();
            List <Private> privates = new List <Private>();

            while (input[0] != "End")
            {
                switch (input[0])
                {
                case "Private":
                {
                    int    id        = int.Parse(input[1]);
                    string firstName = input[2];
                    string lastName  = input[3];
                    double salary    = double.Parse(input[4]);

                    Private soldier = new Private(id, firstName, lastName, salary);
                    soldiers.Add(soldier);
                    privates.Add(soldier);

                    break;
                }

                case "Commando":
                {
                    int    id        = int.Parse(input[1]);
                    string firstName = input[2];
                    string lastName  = input[3];
                    double salary    = double.Parse(input[4]);
                    string cops      = input[5];

                    if (cops == "Airforces" || cops == "Marines")
                    {
                        Commando soldier = new Commando(id, firstName, lastName, salary, cops);

                        for (int i = 6; i < input.Length; i += 2)
                        {
                            if (input[i + 1] == "inProgress" || input[i + 1] == "finished")
                            {
                                Mission mission = new Mission(input[i], input[i + 1]);

                                soldier.Missions.Add(mission);
                            }
                        }
                        soldiers.Add(soldier);
                    }


                    break;
                }

                case "Spy":
                {
                    int    id         = int.Parse(input[1]);
                    string firstName  = input[2];
                    string lastName   = input[3];
                    int    codeNumber = int.Parse(input[4]);

                    Spy spy = new Spy(id, firstName, lastName, codeNumber);

                    soldiers.Add(spy);

                    break;
                }

                case "LieutenantGeneral":
                {
                    int    id        = int.Parse(input[1]);
                    string firstName = input[2];
                    string lastName  = input[3];
                    double salary    = double.Parse(input[4]);


                    LeutenantGeneral soldier = new LeutenantGeneral(id, firstName, lastName, salary);

                    for (int i = 5; i < input.Length; i++)
                    {
                        Private solderToAdd = privates.FirstOrDefault(s => s.Id == int.Parse(input[i]));


                        soldier.Privates.Add(solderToAdd);
                    }
                    soldiers.Add(soldier);

                    break;
                }

                case "Engineer":
                {
                    int    id        = int.Parse(input[1]);
                    string firstName = input[2];
                    string lastName  = input[3];
                    double salary    = double.Parse(input[4]);
                    string cops      = input[5];

                    Engineer soldier = new Engineer(id, firstName, lastName, salary, cops);

                    for (int i = 6; i < input.Length; i += 2)
                    {
                        Repear repear = new Repear(input[i], int.Parse(input[i + 1]));

                        soldier.Repears.Add(repear);
                    }
                    soldiers.Add(soldier);

                    break;
                }
                }

                input = Console.ReadLine().Split(" ").ToArray();
            }

            foreach (var soldier in soldiers)
            {
                Console.WriteLine(soldier.ToString());
            }
        }
        private static void GetAndPrintCurArmyMan(List <Private> privates, string[] tokens)
        {
            switch (tokens[0])
            {
            case "Private":
            {
                Private privatex = new Private
                                       (int.Parse(tokens[1]), tokens[2], tokens[3], decimal.Parse(tokens[4]));
                privates.Add(privatex);

                Console.WriteLine(privatex.ToString());

                break;
            }

            case "LieutenantGeneral":
            {
                LieutenantGeneral general = new LieutenantGeneral
                                                (int.Parse(tokens[1]), tokens[2], tokens[3], decimal.Parse(tokens[4]));

                for (int i = 5; i < tokens.Length; i++)
                {
                    var privateToAdd = privates.Find(x => x.Id == int.Parse(tokens[i]));
                    general.Privates.Add(privateToAdd);
                }

                Console.WriteLine(general.ToString());
                break;
            }

            case "Engineer":
            {
                try
                {
                    Engineer engineer = new Engineer
                                            (int.Parse(tokens[1]), tokens[2], tokens[3], decimal.Parse(tokens[4]), tokens[5]);

                    GetRepairs(tokens, engineer);

                    Console.WriteLine(engineer.ToString());
                    break;
                }
                catch (ArgumentException)
                {
                    break;
                }
            }

            case "Commando":
            {
                try
                {
                    Commando commando = new Commando
                                            (int.Parse(tokens[1]), tokens[2], tokens[3], decimal.Parse(tokens[4]), tokens[5]);

                    GetMissions(tokens, commando);

                    Console.WriteLine(commando.ToString());
                    break;
                }
                catch (ArgumentException)
                {
                    break;
                }
            }

            case "Spy":
            {
                Spy spy = new Spy
                              (int.Parse(tokens[1]), tokens[2], tokens[3], int.Parse(tokens[4]));

                Console.WriteLine(spy.ToString());

                break;
            }
            }
        }