Beispiel #1
0
        static void Main(string[] args)
        {
            var peopleCotainer   = new Dictionary <string, Person>();
            var productContainer = new Dictionary <string, Product>();

            string[] people = Console.ReadLine()
                              .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                foreach (var currentPerson in people)
                {
                    var tokens = currentPerson.Split('=');
                    var name   = tokens[0];
                    var money  = decimal.Parse(tokens[1]);

                    var person = new Person(name, money);
                    peopleCotainer.Add(name, person);
                }

                var products = Console.ReadLine()
                               .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var currentProduct in products)
                {
                    var tokens = currentProduct.Split('=');
                    var name   = tokens[0];
                    var price  = decimal.Parse(tokens[1]);

                    var product = new Product(name, price);
                    productContainer.Add(name, product);
                }

                var command = Console.ReadLine();

                while (!command.Equals("END"))
                {
                    var tokens = command
                                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var personName  = tokens[0];
                    var productName = tokens[1];

                    try
                    {
                        Person  person  = peopleCotainer[personName];
                        Product product = productContainer[productName];
                        person.BuyProducts(product);

                        Console.WriteLine($"{personName} bought {productName}");
                    }
                    catch (InvalidOperationException ioe)
                    {
                        Console.WriteLine(ioe.Message);
                    }

                    command = Console.ReadLine();
                }

                foreach (string person in peopleCotainer.Keys)
                {
                    string items  = string.Join(", ", peopleCotainer[person].BagOfProducts);
                    string result = !peopleCotainer[person].BagOfProducts.Any() ? "Nothing bought" : items;

                    Console.WriteLine($"{person} - {result}");
                }
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine(ae.Message);
            }
        }