Exemple #1
0
        private static void BuyingProducts()
        {
            while (true)
            {
                string inputLine = Console.ReadLine().Trim();
                if (inputLine == "END")
                {
                    return;
                }

                string[] purchase         = inputLine.Split(' ');
                string   buyer            = purchase[0];
                string   purchasedProduct = purchase[1];

                Person currentPerson = listOfPersons
                                       .FirstOrDefault(p => p.PersonName == buyer);

                decimal price = productList
                                .FirstOrDefault(p => p.ProductName == purchasedProduct)
                                .ProductPrice;

                Product newProduct = new Product(purchasedProduct, price);

                Console.WriteLine(currentPerson.AddProduct(newProduct));
            }
        }
        static void Main(string[] args)
        {
            List <Person>  personsList  = new List <Person>();
            List <Product> productsList = new List <Product>();

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

            for (int i = 0; i < persons.Length; i++)
            {
                string[] personInfo = persons[i].Split("=", StringSplitOptions.RemoveEmptyEntries);

                string  name  = personInfo[0];
                decimal money = decimal.Parse(personInfo[1]);

                Person person = new Person(name, money);

                personsList.Add(person);
            }

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

            for (int i = 0; i < products.Length; i++)
            {
                string[] productInfo = products[i].Split("=", StringSplitOptions.RemoveEmptyEntries);

                string  productName  = productInfo[0];
                decimal productPrice = decimal.Parse(productInfo[1]);

                Product currentProduct = new Product(productName, productPrice);

                productsList.Add(currentProduct);
            }

            string command = string.Empty;

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

                string person  = cmdArgs[0];
                string product = cmdArgs[1];

                Person currentPerson = personsList.Find(x => x.Name == person);

                Product currentProduct = productsList.Find(x => x.Name == product);

                if (personsList.Contains(currentPerson))
                {
                    if (productsList.Contains(currentProduct))
                    {
                        currentPerson.AddProduct(currentProduct);
                    }
                }
            }

            foreach (var item in personsList)
            {
                Console.WriteLine(item);
            }
        }
        static void Main(string[] args)
        {
            string[] peopleInfo = Console.ReadLine()
                                  .Split(new char[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries);

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

            string[] command = Console.ReadLine().Split();

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

            try
            {
                for (int i = 0; i < peopleInfo.Length; i += 2)
                {
                    string  personName  = peopleInfo[i];
                    decimal personMoney = decimal.Parse(peopleInfo[i + 1]);

                    Person person = new Person(personName, personMoney);
                    persons.Add(person);
                }

                for (int i = 0; i < productsInfo.Length; i += 2)
                {
                    string  productName = productsInfo[i];
                    decimal productCost = decimal.Parse(productsInfo[i + 1]);

                    Product product = new Product(productName, productCost);
                    products.Add(product);
                }
                while (command[0] != "END")
                {
                    string shoppingPerson  = command[0];
                    string shoppingProduct = command[1];

                    Person  person  = persons.FirstOrDefault(x => x.Name == shoppingPerson);
                    Product product = products.FirstOrDefault(x => x.Name == shoppingProduct);

                    person.AddProduct(product);

                    command = Console.ReadLine().Split();
                }

                foreach (var person in persons)
                {
                    Console.WriteLine(person.ToString());
                }
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemple #4
0
        static void Main(string[] args)
        {
            var people  = new Dictionary <string, Person>();
            var product = new Dictionary <string, Product>();

            try
            {
                people  = ReadPeople();
                product = Readproduct();
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }


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

                var parts = line.Split();

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

                Person  person   = people[personName];
                Product products = product[productName];


                try
                {
                    person.AddProduct(products);

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

            foreach (var person in people.Values)
            {
                Console.WriteLine(person);
            }
        }
Exemple #5
0
        static void Main(string[] args)
        {
            Dictionary <string, Person>  people   = new Dictionary <string, Person>();
            Dictionary <string, Product> products = new Dictionary <string, Product>();

            try
            {
                people   = ReadPeople();
                products = ReadProducts();
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

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

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

                string[] purchaseData = command.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   personName   = purchaseData[0];
                string   productName  = purchaseData[1];

                Person  person  = people[personName];
                Product product = products[productName];

                try
                {
                    person.AddProduct(product);
                    Console.WriteLine($"{person.Name} bought {product.Name}");
                }
                catch (InvalidOperationException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            foreach (var person in people.Values)
            {
                Console.WriteLine(person);
            }
        }
Exemple #6
0
        public void Run()
        {
            try
            {
                string[] inputPeople = Console.ReadLine()
                                       .Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

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

                AddPerson(inputPeople);
                AddProduct(inputProducts);

                string command = Console.ReadLine();

                while (command != "END")
                {
                    string[] information = command.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                    string personName  = information[0];
                    string productName = information[1];

                    Person  targetPerson  = this.people.FirstOrDefault(p => p.Name == personName);
                    Product targetProduct = this.products.FirstOrDefault(p => p.Name == productName);

                    if (targetProduct != null && targetPerson != null)
                    {
                        Console.WriteLine(targetPerson.AddProduct(targetProduct));
                    }

                    command = Console.ReadLine();
                }

                foreach (Person person in this.people)
                {
                    Console.WriteLine(person.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public static void Main(string[] args)
        {
            string[] peopleArgs   = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
            string[] productsArgs = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

            HashSet <Person>  people   = new HashSet <Person>();
            HashSet <Product> products = new HashSet <Product>();

            AddAllPeople(peopleArgs, people);
            AddAllProducts(productsArgs, products);

            string command = Console.ReadLine();

            while (command.ToLower() != "end")
            {
                string[] commandArgs = command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                string   personName  = commandArgs[0];
                string   productName = commandArgs[1];

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

                if (currentPerson.CanAffordProduct(currentProduct.Cost))
                {
                    currentPerson.AddProduct(currentProduct);
                    Console.WriteLine($"{personName} bought {productName}");
                }
                else
                {
                    Console.WriteLine($"{personName} can't afford {productName}");
                }

                command = Console.ReadLine();
            }

            foreach (Person person in people)
            {
                Console.WriteLine(person);
            }
        }
Exemple #8
0
        private static void Bought(IList <Person> persons, IList <Product> products)
        {
            string input = Console.ReadLine();

            while (input != "END")
            {
                string[] data        = input.Split(" ");
                string   personName  = data[0].Trim();
                string   productName = data[1].Trim();

                Person  person  = persons.First(p => p.Name == personName);
                Product product = products.First(p => p.Name == productName);
                if (person.AddProduct(product))
                {
                    Console.WriteLine($"{personName} bought {productName}");
                }
                else
                {
                    Console.WriteLine($"{personName} can't afford {productName}");
                }

                input = Console.ReadLine();
            }
        }
Exemple #9
0
        static void Main(string[] args)
        {
            List <Person> persons = new List <Person>();

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

            try
            {
                List <string> personsInfo = Console.ReadLine()
                                            .Split(';', StringSplitOptions.RemoveEmptyEntries)
                                            .ToList();

                for (int i = 0; i < personsInfo.Count; i++)
                {
                    List <string> currentPersonInfo = personsInfo[i]
                                                      .Split('=')
                                                      .ToList();

                    string personName  = currentPersonInfo[0];
                    double personMoney = double.Parse(currentPersonInfo[1]);

                    persons.Add(new Person(personName, personMoney));
                }

                List <string> productsInfo = Console.ReadLine()
                                             .Split(';', StringSplitOptions.RemoveEmptyEntries)
                                             .ToList();

                for (int j = 0; j < productsInfo.Count; j++)
                {
                    List <string> currentProductInfo = productsInfo[j]
                                                       .Split('=')
                                                       .ToList();

                    string productName = currentProductInfo[0];
                    double productCost = double.Parse(currentProductInfo[1]);

                    products.Add(new Product(productName, productCost));
                }

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

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

                    List <string> commandInfo = command
                                                .Split(' ', StringSplitOptions.RemoveEmptyEntries)
                                                .ToList();

                    string personName  = commandInfo[0];
                    string productName = commandInfo[1];

                    Person  foundPerson  = persons.Where(x => x.Name == personName).FirstOrDefault();
                    Product foundProduct = products.Where(x => x.Name == productName).FirstOrDefault();

                    if (foundPerson != null && foundProduct != null)
                    {
                        foundPerson.AddProduct(foundProduct);
                    }
                }

                foreach (var person in persons)
                {
                    if (person.Products.Count > 0)
                    {
                        List <string> boughtProducts = person.Products.Select(x => x.Name).ToList();

                        Console.WriteLine($"{person.Name} - {string.Join(", ",boughtProducts)}");
                    }
                    else
                    {
                        Console.WriteLine($"{person.Name} - Nothing bought ");
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
Exemple #10
0
        static void Main(string[] args)
        {
            string[] players = Console.ReadLine()
                               .Split(";", StringSplitOptions.RemoveEmptyEntries)
                               .ToArray();

            List <Person>  listPlayers  = new List <Person>();
            List <Product> listProducts = new List <Product>();

            bool isEverythingValid = true;

            try
            {
                for (int i = 0; i < players.Length; i++)
                {
                    string[] playerInfo = players[i].Split("=", StringSplitOptions.RemoveEmptyEntries)
                                          .ToArray();
                    string  playerName   = playerInfo[0];
                    decimal playersMoney = decimal.Parse(playerInfo[1]);

                    Person currentPerson = new Person(playerName, playersMoney);

                    listPlayers.Add(currentPerson);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }


            string[] products = Console.ReadLine()
                                .Split(";", StringSplitOptions.RemoveEmptyEntries)
                                .ToArray();
            try
            {
                for (int i = 0; i < products.Length; i++)
                {
                    string[] productInfo = products[i].Split("=", StringSplitOptions.RemoveEmptyEntries)
                                           .ToArray();
                    string  productName  = productInfo[0];
                    decimal productPrice = decimal.Parse(productInfo[1]);

                    Product currentProduct = new Product(productName, productPrice);

                    listProducts.Add(currentProduct);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }



            if (isEverythingValid)
            {
                string command = string.Empty;

                while ((command = Console.ReadLine()) != "END")
                {
                    string[] cmdArgs = command.Split(" ", StringSplitOptions.RemoveEmptyEntries)
                                       .ToArray();

                    string personName  = cmdArgs[0];
                    string productName = cmdArgs[1];

                    Person currentPerson = listPlayers.Find(x => x.Name == personName);

                    Product currentProduct = listProducts.Find(x => x.Name == productName);

                    if (currentPerson.Money >= currentProduct.Cost && listPlayers.Contains(currentPerson) && listProducts.Contains(currentProduct))
                    {
                        currentPerson.AddProduct(currentProduct);
                        currentPerson.DecreaseMoney(currentProduct);
                        Console.WriteLine($"{currentPerson.Name} bought {currentProduct.Name}");
                    }
                    else
                    {
                        Console.WriteLine($"{currentPerson.Name} can't afford {currentProduct.Name}");
                    }
                }
            }

            if (isEverythingValid)
            {
                foreach (var item in listPlayers)
                {
                    if (item.Count == 0)
                    {
                        Console.WriteLine($"{item.Name} - Nothing bought");
                    }
                    else
                    {
                        Console.WriteLine(item);
                    }
                }
            }
        }
Exemple #11
0
        public static void Main(string[] args)
        {
            var peopleInput   = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);
            var productsInput = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);

            try
            {
                foreach (var item in peopleInput)
                {
                    var    personInput = item.Split("=");
                    Person person      = new Person(personInput[0], decimal.Parse(personInput[1]));

                    People.AddPerson(person);
                }

                foreach (var item in productsInput)
                {
                    var     productInput = item.Split("=");
                    Product product      = new Product(productInput[0], decimal.Parse(productInput[1]));

                    Products.AddProduct(product);
                }
                string input = Console.ReadLine();
                while (input != "END")
                {
                    try
                    {
                        string[] splitInput = input.Split(" ");

                        Person person = People.GetPersonByName(splitInput[0]);

                        Product product = Products.GetProductByName(splitInput[1]);

                        if (person.Money - product.Cost >= 0)
                        {
                            person.AddProduct(product);
                            person.SpendMoney(product.Cost);

                            Console.WriteLine($"{person.Name} bought {product.Name}");
                        }
                        else
                        {
                            throw new ArgumentException(String.Format(ExceptionMessages.NotEnoughMoneyException, person.Name, product.Name));
                        }
                    }
                    catch (ArgumentException ae)
                    {
                        Console.WriteLine(ae.Message);
                    }
                    input = Console.ReadLine();
                }
                try
                {
                    foreach (var person in People.Collection)
                    {
                        if (person.Products.Count > 0)
                        {
                            Console.WriteLine($"{person.Name} - {string.Join(", ", person.Products)}");
                        }
                        else
                        {
                            throw new ArgumentException(String.Format(ExceptionMessages.EmptyProductsCollectionException, person.Name));
                        }
                    }
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine(ae.Message);
            }
        }
Exemple #12
0
        public static void Main()
        {
            string[]       people         = Console.ReadLine().Split(new char[] { '=', ';' });
            string[]       products       = Console.ReadLine().Split(new char[] { '=', ';' });
            string         command        = Console.ReadLine();
            List <Person>  listOfPeople   = new List <Person>();
            List <Product> listOfProducts = new List <Product>();

            for (int i = 0; i < people.Length; i += 2)
            {
                try
                {
                    listOfPeople.Add(new Person(people[i], decimal.Parse(people[i + 1])));
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                    return;
                }
            }

            for (int i = 0; i < products.Length; i += 2)
            {
                try
                {
                    listOfProducts.Add(new Product(products[i], decimal.Parse(products[i + 1])));
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                    return;
                }
            }

            while (command != "END")
            {
                string[] token = command.Split();

                Person  person  = listOfPeople.FirstOrDefault(x => x.Name == token[0]);
                Product product = listOfProducts.FirstOrDefault(x => x.Name == token[1]);

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

                command = Console.ReadLine();
            }

            foreach (var person in listOfPeople)
            {
                if (person.CountOfProducts().Count > 0)
                {
                    Console.WriteLine($"{person.Name} - {string.Join(", ", person.CountOfProducts().Select(x => x.Name))}");
                }
                else
                {
                    Console.WriteLine($"{person.Name} - Nothing bought");
                }
            }
        }