Beispiel #1
0
        static void Main(string[] args)
        {
            var Person  = new Person();
            var Product = new Product();

            string[] Input = Console.ReadLine().Split(';', '=');
            for (int i = 0; i < Input.Length; i += 2)
            {
                Person.Add(Input[i], decimal.Parse(Input[i + 1]));
            }

            string[] InputProducts = Console.ReadLine().Split(';', '=');
            for (int i = 0; i < InputProducts.Length - 1; i += 2)
            {
                Product.AddProduct(InputProducts[i], decimal.Parse(InputProducts[i + 1]));
            }
            var Boughts = new Dictionary <string, List <string> >();

            while (true)
            {
                string[] BuyStuff = Console.ReadLine().Split();
                if (BuyStuff[0] == "END")
                {
                    break;
                }
                decimal money = Person.GetMoney(BuyStuff[0]);
                decimal cost  = Product.GetCost(BuyStuff[1]);
                if (money >= cost)
                {
                    Console.WriteLine(BuyStuff[0] + " bought " + BuyStuff[1]);
                    Person.SetMoney(BuyStuff[0], cost);
                    if (!Boughts.ContainsKey(BuyStuff[0]))
                    {
                        Boughts.Add(BuyStuff[0], new List <string>());
                    }
                    Boughts[BuyStuff[0]].Add(BuyStuff[1]);
                }
                else
                {
                    if (!Boughts.ContainsKey(BuyStuff[0]))
                    {
                        Boughts.Add(BuyStuff[0], new List <string>());
                    }
                    Console.WriteLine(BuyStuff[0] + " can't afford " + BuyStuff[1]);
                }
            }
            foreach (var person in Boughts)
            {
                if (person.Value.Count < 1)
                {
                    Console.WriteLine(person.Key + " - Nothing bought");
                }
                else
                {
                    Console.WriteLine(person.Key + " - " + String.Join(", ", person.Value));
                }
            }
        }
Beispiel #2
0
        static void Main()
        {
            try
            {
                string[] peopleArray = Console.ReadLine().Split(';', StringSplitOptions.RemoveEmptyEntries);

                List <Person> people = new List <Person>();


                for (int i = 0; i < peopleArray.Length; i++)
                {
                    string[] personData = peopleArray[i].Split('=');
                    string   name       = personData[0];
                    decimal  money      = decimal.Parse(personData[1]);

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

                List <Product> products = new List <Product>();

                string[] productsArray = Console.ReadLine().Split(';', StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < productsArray.Length; i++)
                {
                    string[] productData = productsArray[i].Split('=');
                    string   name        = productData[0];
                    decimal  cost        = decimal.Parse(productData[1]);

                    Product product = new Product(name, cost);
                    products.Add(product);
                }

                string command = Console.ReadLine();
                while (command != "END")
                {
                    string[] purchaseData = command.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                    string   personName   = purchaseData[0];
                    string   productName  = purchaseData[1];

                    Person  neededPerson  = people.FirstOrDefault(x => x.Name == personName);
                    Product neededProduct = products.FirstOrDefault(x => x.Name == productName);

                    if (neededPerson != null && neededProduct != null)
                    {
                        if (neededPerson.Money >= neededProduct.Cost)
                        {
                            neededPerson.Money -= neededProduct.Cost;
                            neededPerson.Add(neededProduct.Name);
                            Console.WriteLine($"{neededPerson.Name} bought {neededProduct.Name}");
                        }
                        else
                        {
                            Console.WriteLine($"{neededPerson.Name} can't afford {neededProduct.Name}");
                        }
                    }
                    command = Console.ReadLine();
                }

                foreach (var person in people)
                {
                    if (person.Products.Count > 0)
                    {
                        Console.WriteLine($"{person.Name} - {string.Join(", ", person.Products)}");
                    }
                    else
                    {
                        Console.WriteLine($"{person.Name} - Nothing bought");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }