public static void Main()
        {
            string[] people     = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
            var      peopleFill = new List <Person>();

            foreach (var perosn in people)
            {
                var     personSp = perosn.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                string  name     = personSp[0];
                decimal money    = decimal.Parse(personSp[1]);

                try
                {
                    Person some = new Person(name, money, new List <Product>());
                    peopleFill.Add(some);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }
            string[] product  = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
            var      products = new List <Product>();

            foreach (var item in product)
            {
                var     itemSp      = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                string  nameProduct = itemSp[0];
                decimal cost        = decimal.Parse(itemSp[1]);

                try
                {
                    Product someProduct = new Product(nameProduct, cost);
                    products.Add(someProduct);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

            string command = string.Empty;

            while ((command = Console.ReadLine()) != "END")
            {
                string[] comSp = command.Split().ToArray();

                string nameOfPerson = comSp[0];
                string prodBuy      = comSp[1];


                Person  personB  = peopleFill.Where(p => p.Name == nameOfPerson).First();
                Product productB = products.Where(p => p.Name == prodBuy).First();

                if (personB.Money >= productB.Cost)
                {
                    personB.Money -= productB.Cost;
                    personB.AddProductToBag(productB);
                    Console.WriteLine("{0} bought {1}", personB.Name, productB.Name);
                }
                else
                {
                    Console.WriteLine("{0} can't afford {1}", personB.Name, productB.Name);
                }
            }
            foreach (Person man in peopleFill)
            {
                if (man.SeeBag().Count() > 0)
                {
                    Console.WriteLine("{0} - {1}", man.Name, String.Join(", ", man.SeeBag().Select(p => p.Name)));
                }
                else
                {
                    Console.WriteLine("{0} - Nothing bought", man.Name);
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            List <Person>  persons  = new List <Person>();
            List <Product> products = new List <Product>();

            string[] tokens = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string token in tokens)
            {
                string[] tok   = token.Split('=');
                string   name  = tok[0].Trim();
                decimal  money = decimal.Parse(tok[1].Trim());
                try
                {
                    persons.Add(new Person(name, money));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

            tokens = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string token in tokens)
            {
                string[] tok  = token.Split('=');
                string   name = tok[0].Trim();
                decimal  cost = decimal.Parse(tok[1].Trim());
                try
                {
                    products.Add(new Product(name, cost));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

            string command = Console.ReadLine();

            while (command != "END")
            {
                tokens = command.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                string  personName  = tokens[0];
                string  productName = tokens[1];
                Person  person      = persons.Where(p => p.Name == personName).First();
                Product product     = products.Where(p => p.Name == productName).First();

                if (person.Money >= product.Cost)
                {
                    person.Money -= product.Cost;
                    person.AddProductToBag(product);
                    Console.WriteLine("{0} bought {1}", person.Name, product.Name);
                }
                else
                {
                    Console.WriteLine("{0} can't afford {1}", person.Name, product.Name);
                }

                command = Console.ReadLine();
            }

            foreach (Person person in persons)
            {
                if (person.SeeBag().Count > 0)
                {
                    Console.WriteLine("{0} - {1}", person.Name, String.Join(", ", person.SeeBag().Select(p => p.Name)));
                }
                else
                {
                    Console.WriteLine("{0} - Nothing bought", person.Name);
                }
            }
        }