Exemplo n.º 1
0
        public void Run()
        {
            while (true)
            {
                try
                {
                    var command = this.reader.ReadLine();

                    if (command == "End")
                    {
                        break;
                    }

                    var args = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);

                    if (args[0] == "Private")
                    {
                        var @private = new Private(int.Parse(args[1]), args[2], args[3], decimal.Parse(args[4]));
                        this.privates.Add(@private);
                        this.soldiers.Add(@private);
                    }
                    else if (args[0] == "LieutenantGeneral")
                    {
                        var lieutenant = new LieutenantGeneral(int.Parse(args[1]), args[2], args[3], decimal.Parse(args[4]));
                        var privateIds = args.Skip(5).ToArray();

                        foreach (var privateId in privateIds)
                        {
                            var find = this.privates.Find(x => x.Id == int.Parse(privateId));

                            if (find != null)
                            {
                                lieutenant.AddPrivate(find);
                            }
                        }

                        this.soldiers.Add(lieutenant);
                    }
                    else if (args[0] == "Engineer")
                    {
                        var repairs  = args.Skip(6).ToArray();
                        var engineer = new Engineer(int.Parse(args[1]), args[2], args[3], decimal.Parse(args[4]), args[5]);

                        for (int i = 6; i < args.Length; i += 2)
                        {
                            try
                            {
                                engineer.AddRepair(new Repair(args[i], int.Parse(args[i + 1])));
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }

                        this.soldiers.Add(engineer);
                    }
                    else if (args[0] == "Commando")
                    {
                        var commando = new Commando(int.Parse(args[1]), args[2], args[3], decimal.Parse(args[4]), args[5]);

                        for (int i = 6; i < args.Length; i += 2)
                        {
                            try
                            {
                                commando.AddMission(new Mission(args[i], args[i + 1]));
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }

                        this.soldiers.Add(commando);
                    }
                    else if (args[0] == "Spy")
                    {
                        var spy = new Spy(int.Parse(args[1]), args[2], args[3], int.Parse(args[4]));
                        this.soldiers.Add(spy);
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }

            PrintAllSoldiers();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //var leu = new LieutenantGeneral("hdhs","hdhs","hsh");

            ////cannot happen because of level of protecton leu.ListOfPrivates.Clear();
            //var priv = new Private("hdhs", "hdhs", "hsh");
            ////everything is alright with the private
            //var eng = new Engineer("hdhs", "hdhs", "hsh");
            ////everything is alright with the engineer
            //var com = new Commando("hdhs", "hdhs", "hsh");
            ////check lists in com=mission complete

            //var spy = new Spy("hdhs", "hdhs", "hsh",123);
            ////everything is alright with the spy

            string         command;
            List <Private> privates = new List <Private>();

            while ((command = Console.ReadLine()) != "End")
            {
                string[] commandToArr = command.Split().ToArray();
                string   cmdType      = commandToArr[0];

                if (cmdType == "Private")
                {
                    string  id            = commandToArr[1];
                    string  firstName     = commandToArr[2];
                    string  lastName      = commandToArr[3];
                    decimal salary        = decimal.Parse(commandToArr[4]);
                    Private privatePerson = new Private(id, firstName, lastName, salary);
                    privates.Add(privatePerson);
                    Console.WriteLine(privatePerson.ToString());
                }
                else if (cmdType == "LieutenantGeneral")
                {
                    string            id        = commandToArr[1];
                    string            firstName = commandToArr[2];
                    string            lastName  = commandToArr[3];
                    decimal           salary    = decimal.Parse(commandToArr[4]);
                    LieutenantGeneral gen       = new LieutenantGeneral(id, firstName, lastName, salary);
                    for (int i = 5; i < commandToArr.Length; i++)
                    {
                        string  privatesId  = commandToArr[i];
                        Private toBeWritten = privates.FirstOrDefault(x => x.Id == privatesId);
                        gen.ListOfPrivates.Add(toBeWritten);
                    }
                    Console.WriteLine(gen.ToString());
                }
                else if (cmdType == "Engineer")
                {
                    string  id        = commandToArr[1];
                    string  firstName = commandToArr[2];
                    string  lastName  = commandToArr[3];
                    decimal salary    = decimal.Parse(commandToArr[4]);
                    string  corps     = commandToArr[5];
                    try
                    {
                        Engineer engineer = new Engineer(id, firstName, lastName, salary, corps);
                        for (int i = 6; i < commandToArr.Length; i += 2)
                        {
                            string partName    = commandToArr[i];
                            int    hoursWorked = int.Parse(commandToArr[i + 1]);
                            Repair repair      = new Repair(partName, hoursWorked);
                            engineer.ListOfRepairs.Add(repair);
                        }
                        Console.WriteLine(engineer.ToString());
                    }
                    catch (Exception)
                    {
                    }
                }
                else if (cmdType == "Commando")
                {
                    string  id        = commandToArr[1];
                    string  firstName = commandToArr[2];
                    string  lastName  = commandToArr[3];
                    decimal salary    = decimal.Parse(commandToArr[4]);
                    string  corps     = commandToArr[5];
                    try
                    {
                        Commando commando = new Commando(id, firstName, lastName, salary, corps);
                        for (int i = 6; i < commandToArr.Length; i += 2)
                        {
                            string missionCodeName = commandToArr[i];
                            string state           = commandToArr[i + 1];
                            try
                            {
                                Mission mission = new Mission(missionCodeName, state);
                                commando.ListOfMissions.Add(mission);
                            }
                            catch (Exception)
                            {
                            }
                        }
                        Console.WriteLine(commando.ToString());
                    }
                    catch (Exception)
                    {
                    }
                }
                else if (cmdType == "Spy")
                {
                    string id         = commandToArr[1];
                    string firstName  = commandToArr[2];
                    string lastName   = commandToArr[3];
                    int    codeNumber = int.Parse(commandToArr[4]);
                    Spy    spy        = new Spy(id, firstName, lastName, codeNumber);
                    Console.WriteLine(spy);
                }
            }
        }