Beispiel #1
0
        public static void Main()
        {
            try
            {
                List <Person>  people     = new List <Person>();
                List <Product> products   = new List <Product>();
                string[]       peopleData = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < peopleData.Length; i++)
                {
                    string[] args    = peopleData[i].Split("=");
                    string   name    = args[0];
                    decimal  money   = decimal.Parse(args[1]);
                    Person   current = new Person(name, money);
                    people.Add(current);
                }

                string[] productsData = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < productsData.Length; i++)
                {
                    string[] args    = productsData[i].Split("=");
                    string   name    = args[0];
                    decimal  cost    = decimal.Parse(args[1]);
                    Product  current = new Product(name, cost);
                    products.Add(current);
                }
                string command = Console.ReadLine();
                while (command != "END")
                {
                    string[] args        = command.Split(" ");
                    string   personName  = args[0];
                    string   productName = args[1];

                    Person  currentPerson  = people.FirstOrDefault(p => p.Name == personName);
                    Product currentProduct = products.FirstOrDefault(p => p.Name == productName);

                    if (currentProduct != null && currentPerson != null)
                    {
                        Console.WriteLine(currentPerson.Buy(currentProduct));
                    }

                    command = Console.ReadLine();
                }

                foreach (var person in people)
                {
                    Console.WriteLine(person);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }
        static void Main(string[] args)
        {
            try
            {
                string[] personStr = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);

                CreatePerson(personStr);

                string[] productStr = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);

                CreateProduct(productStr);

                while (true)
                {
                    string command = Console.ReadLine();
                    if (command == "END")
                    {
                        break;
                    }

                    string[] cmdArgs     = command.Split();
                    string   personName  = cmdArgs[0];
                    string   productName = cmdArgs[1];

                    Person  person  = people.FirstOrDefault(p => p.Name == personName);
                    Product product = products.FirstOrDefault(p => p.Name == productName);

                    if (person != null && product != null)
                    {
                        Console.WriteLine(person.Buy(product));
                    }
                }

                foreach (var person in people)
                {
                    Console.WriteLine(person);
                }
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine(ae.Message);
            }
        }
        public void Execute(string[] persons, string[] products)
        {
            try
            {
                List <Person>  listPerson    = new List <Person>();
                List <Product> productsInput = new List <Product>();

                ProcessPersonInput(listPerson, persons);
                ProcessProductsInput(productsInput, products);

                while (true)
                {
                    string command = Console.ReadLine();

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

                    string[] parts   = command.Split();
                    string   name    = parts[0];
                    string   product = parts[1];

                    Person  currentPerson  = Person.GetCurrentPerson(name, listPerson);
                    Product currentProduct = Person.GetCurrentProduct(product, productsInput);

                    if (Person.Buy(currentPerson, currentProduct))
                    {
                        Console.WriteLine($"{name} bought {product}");
                    }
                    else
                    {
                        Console.WriteLine($"{name} can't afford {product}");
                    }
                }

                foreach (var person in listPerson)
                {
                    if (person.Products.Count == 0)
                    {
                        Console.WriteLine($"{person.Name} - Nothing bought ");
                    }
                    else
                    {
                        var allProducts = person.Products;
                        Console.Write($"{person.Name} - ");

                        int index = 0;
                        foreach (var item in allProducts)
                        {
                            if (index == allProducts.Count - 1)
                            {
                                Console.Write($"{item.Name}");
                            }
                            else
                            {
                                Console.Write($"{item.Name}, ");
                            }
                            index++;
                        }

                        Console.WriteLine();
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }