Example #1
0
        public static void Main()
        {
            List <Person>  people   = new List <Person>();
            List <Product> products = new List <Product>();

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

            try
            {
                for (int i = 0; i < peopleInput.Length; i += 2)
                {
                    string  name  = peopleInput[i];
                    decimal money = decimal.Parse(peopleInput[i + 1]);

                    Person person = new Person(name, money);

                    people.Add(person);
                }

                for (int i = 0; i < productsInput.Length; i += 2)
                {
                    string  name = productsInput[i];
                    decimal cost = decimal.Parse(productsInput[i + 1]);

                    Product product = new Product(name, cost);

                    products.Add(product);
                }

                string command = Console.ReadLine();

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

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

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

                    Console.WriteLine(person.BuyProduct(product));

                    command = Console.ReadLine();
                }

                foreach (Person person in people)
                {
                    Console.WriteLine(person);
                }
            }
            catch (ArgumentException exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            string[] persons = Console.ReadLine()
                               .Split(new char[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries);

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

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

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

            try
            {
                for (int i = 0; i < persons.Length; i += 2)
                {
                    string name  = persons[i];
                    double money = double.Parse(persons[i + 1]);

                    allPersons.Add(new Person(name, money));
                }

                for (int i = 0; i < products.Length; i += 2)
                {
                    string name = products[i];
                    double cost = double.Parse(products[i + 1]);

                    allProducts.Add(new Product(name, cost));
                }

                string command = Console.ReadLine();

                while (command != "END")
                {
                    string[] tokens      = command.Split(' ');
                    string   currentName = tokens[0];
                    string   product     = tokens[1];

                    Person currentPerson = allPersons.FirstOrDefault(x => x.Name == currentName);

                    Product currentProduct = allProducts.FirstOrDefault(p => p.Name == product);

                    currentPerson.BuyProduct(currentProduct);

                    command = Console.ReadLine();
                }

                foreach (Person person in allPersons)
                {
                    Console.WriteLine(person.ToString());
                }
            }
            catch (Exception ae)
            {
                Console.WriteLine(ae.Message);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            string[] peoplesName = Console.ReadLine()
                                   .Split(";", StringSplitOptions.RemoveEmptyEntries)
                                   .ToArray();
            string[] productsName = Console.ReadLine()
                                    .Split(";", StringSplitOptions.RemoveEmptyEntries)
                                    .ToArray();

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

            try
            {
                for (int i = 0; i < peoplesName.Length; i++)
                {
                    string[] personInfo = peoplesName[i].Split("=");
                    Person   person     = new Person(personInfo[0], decimal.Parse(personInfo[1]));
                    people.Add(person);
                }

                for (int i = 0; i < productsName.Length; i++)
                {
                    string[] productInfo = productsName[i].Split("=");
                    Product  product     = new Product(productInfo[0], decimal.Parse(productInfo[1]));
                    allProducts.Add(product);
                }

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

                    Person  customer        = people.FirstOrDefault(x => x.Name == personName);
                    Product productPurchase = allProducts.FirstOrDefault(x => x.ProductName == productName);

                    try
                    {
                        customer.BuyProduct(productPurchase);
                        Console.WriteLine($"{customer.Name} bought {productPurchase.ProductName}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }

                people.ForEach(p => Console.WriteLine(p.ToString()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
        }
Example #4
0
        static void Main(string[] args)
        {
            List <Person>  peopleCollection  = new List <Person>();
            List <Product> productCollection = new List <Product>();

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

                for (int i = 0; i < people.Length; i += 2)
                {
                    string  name  = people[i];
                    decimal money = decimal.Parse(people[i + 1]);

                    Person person = new Person(name, money);

                    peopleCollection.Add(person);
                }


                for (int i = 0; i < products.Length; i += 2)
                {
                    string  name = products[i];
                    decimal cost = decimal.Parse(products[i + 1]);

                    Product product = new Product(name, cost);

                    productCollection.Add(product);
                }

                string purchase = Console.ReadLine();

                while (purchase != "END")
                {
                    string[] inputArguments = purchase.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    Person  buyer   = peopleCollection.FirstOrDefault(x => x.Name == inputArguments[0]);
                    Product product = productCollection.FirstOrDefault(y => y.Name == inputArguments[1]);

                    Console.WriteLine(buyer.BuyProduct(product));


                    purchase = Console.ReadLine();
                }

                foreach (var element in peopleCollection)
                {
                    Console.WriteLine(element);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #5
0
 private static void BuyProduct(Person person, Product product)
 {
     if (!person.BuyProduct(product))
     {
         Console.WriteLine($"{person.Name} can't afford {product.Name}");
     }
     else
     {
         Console.WriteLine($"{person.Name} bought {product.Name}");
     }
 }
Example #6
0
 public string Process(Person person, Product product)
 {
     if (person.Money >= product.Price)
     {
         person.BuyProduct(product);
         return($"{person.Name} bought {product.ProductName}");
     }
     else
     {
         return($"{person.Name} can't afford {product.ProductName}");
     }
 }
Example #7
0
        private static void Shopping(Person person, Product product)
        {
            bool boughtProduct = person.BuyProduct(product);

            if (boughtProduct)
            {
                Console.WriteLine($"{person.Name} bought {product.Name}");
            }
            else
            {
                Console.WriteLine($"{person.Name} can't afford {product.Name}");
            }
        }
Example #8
0
        static void Main(string[] args)
        {
            string[] allPeoplesAndMoney  = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);
            string[] allProductsAndCosts = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);

            List <Person> peoples = new List <Person>(allPeoplesAndMoney.Length);

            foreach (var people in allPeoplesAndMoney)
            {
                string[] currPersonNameAndMoney = people.Split("=");
                Person   currPerson             = new Person(currPersonNameAndMoney[0], double.Parse(currPersonNameAndMoney[1]));
                peoples.Add(currPerson);
            }

            List <Product> products = new List <Product>(allProductsAndCosts.Length);

            foreach (var product in allProductsAndCosts)
            {
                string[] currProductAndCost = product.Split("=");
                Product  currProduct        = new Product(currProductAndCost[0], double.Parse(currProductAndCost[1]));
                products.Add(currProduct);
            }

            string command = Console.ReadLine();

            while (command != "END")
            {
                string[] currComand  = command.Split();
                string   name        = currComand[0];
                string   product     = currComand[1];
                Person   currPerson  = peoples.FirstOrDefault(n => n.Name == name);
                Product  currProduct = products.FirstOrDefault(p => p.Name == product);
                peoples.Average(x => x.Money);
                if (currPerson.HasEnoughMoney(currProduct))
                {
                    currPerson.BuyProduct(currProduct);
                    Console.WriteLine($"{name} bought {product}");
                }
                else
                {
                    Console.WriteLine($"{name} can't afford {product}");
                }

                command = Console.ReadLine();
            }

            foreach (var person in peoples)
            {
                Console.WriteLine(person);
            }
        }
Example #9
0
        static void Main(string[] args)
        {
            List <string> peopleAndMoney = Console.ReadLine()
                                           .Split(";", StringSplitOptions.RemoveEmptyEntries)
                                           .ToList();
            List <string> productsAndCost = Console.ReadLine()
                                            .Split(";", StringSplitOptions.RemoveEmptyEntries)
                                            .ToList();

            try
            {
                List <Person>  peopleInStore = new List <Person>();
                List <Product> products      = new List <Product>();
                foreach (var item in peopleAndMoney)
                {
                    string  name   = item.Split("=", StringSplitOptions.RemoveEmptyEntries)[0];
                    decimal money  = decimal.Parse(item.Split("=", StringSplitOptions.RemoveEmptyEntries)[1]);
                    Person  person = new Person(name, money);
                    peopleInStore.Add(person);
                }
                foreach (var item in productsAndCost)
                {
                    string  name    = item.Split("=", StringSplitOptions.RemoveEmptyEntries)[0];
                    decimal cost    = decimal.Parse(item.Split("=", StringSplitOptions.RemoveEmptyEntries)[1]);
                    Product product = new Product(name, cost);
                    products.Add(product);
                }
                while (true)
                {
                    string input = Console.ReadLine();
                    if (input == "END")
                    {
                        break;
                    }
                    string  buyerName   = input.Split(" ")[0];
                    string  productName = input.Split(" ")[1];
                    Person  buyer       = peopleInStore.Find(x => x.Name == buyerName);
                    Product product     = products.Find(x => x.Name == productName);
                    buyer.BuyProduct(product);
                }
                foreach (var item in peopleInStore)
                {
                    Console.WriteLine(item);
                }
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine(ae.Message);
            }
        }
Example #10
0
        static void Main()
        {
            peoples  = new List <Person>();
            products = new List <Product>();

            string[] personsInput = Console.ReadLine()
                                    .Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string person in personsInput)
            {
                string[] per    = person.Split("=");
                string   name   = per[0];
                decimal  money  = decimal.Parse(per[1]);
                Person   people = new Person(name, money);
                peoples.Add(people);
            }

            string[] productInput = Console.ReadLine()
                                    .Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string articule in productInput)
            {
                string[] arg     = articule.Split("=");
                string   name    = arg[0];
                decimal  cost    = decimal.Parse(arg[1]);
                Product  product = new Product(name, cost);
                products.Add(product);
            }

            string commands;

            while ((commands = Console.ReadLine()) != "END")
            {
                string[] arguments   = commands.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                string   personName  = arguments[0];
                string   productName = arguments[1];
                if (peoples.Any(p => p.Name == personName) && products.Any(prod => prod.Name == productName))
                {
                    Person  currentPerson  = peoples.FirstOrDefault(p => p.Name == personName);
                    Product currentProduct = products.FirstOrDefault(p => p.Name == productName);
                    Console.WriteLine(currentPerson.BuyProduct(currentProduct));
                }
            }

            //Print Person Collection
            foreach (Person p in peoples)
            {
                Console.WriteLine(p.ToString());
            }
        }
Example #11
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 line = Console.ReadLine();
                if (line == "END")
                {
                    break;
                }
                string[] parts = line.Split();

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

                Person  person  = people[personName];
                Product product = products[productName];
                try
                {
                    person.BuyProduct(product);

                    Console.WriteLine($"{personName} bought {productName}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            foreach (var person in people.Values)
            {
                Console.WriteLine(person);
            }
        }
Example #12
0
        public void Run()
        {
            try
            {
                AddPeople();
                AddProducts();

                string ordersInput = Console.ReadLine();

                while (ordersInput != "END")
                {
                    string[] args = ordersInput
                                    .Split()
                                    .ToArray();

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

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

                    if (targetPerson != null && targetProduct != null)
                    {
                        targetPerson.BuyProduct(targetProduct);
                    }

                    ordersInput = Console.ReadLine();
                }

                foreach (var person in people)
                {
                    if (person.Products.Count == 0)
                    {
                        Console.WriteLine($"{person.Name} - Nothing bought");
                    }
                    else
                    {
                        Console.WriteLine($"{person.Name} - {string.Join(", ", person.Products.Select(p => p.Name))}");
                    }
                }
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #13
0
        public void Run()
        {
            try
            {
                string[] personsParameters  = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);
                string[] productsParameters = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);

                AddPerson(personsParameters);
                AddProduct(productsParameters);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            string command = Console.ReadLine();

            while (command != "END")
            {
                try
                {
                    string[] currentPurchase = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                    string   personName      = currentPurchase[0];
                    string   productName     = currentPurchase[1];

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

                    if (person != null && product != null)
                    {
                        person.BuyProduct(product);
                        Console.WriteLine($"{person.Name} bought {product.Name}");
                    }
                }
                catch (InvalidOperationException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                command = Console.ReadLine();
            }

            Console.WriteLine(string.Join(Environment.NewLine, people));
        }
Example #14
0
        static void Main(string[] args)
        {
            string[]       customersInfo = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);
            List <Person>  customers     = new List <Person>();
            List <Product> products      = new List <Product>();

            for (int i = 0; i < customersInfo.Length; i++)
            {
                var currentCustomer = customersInfo[i].Split("=");

                Person person = new Person(currentCustomer[0], decimal.Parse(currentCustomer[1]));
                customers.Add(person);
            }

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

            for (int i = 0; i < productsInfo.Length; i++)
            {
                var currentProduct = productsInfo[i].Split("=");

                Product product = new Product(currentProduct[0], decimal.Parse(currentProduct[1]));
                products.Add(product);
            }

            string input = String.Empty;

            while ((input = Console.ReadLine()) != "END")
            {
                var inputArgs = input.Split();

                Person  buyer        = customers.FirstOrDefault(x => x.Name == inputArgs[0]);
                Product productToBuy = products.FirstOrDefault(x => x.Name == inputArgs[1]);

                buyer.BuyProduct(productToBuy);
            }

            foreach (var item in customers)
            {
                Console.WriteLine(item);
            }
        }
Example #15
0
        public void Run()
        {
            try
            {
                ReadPersonsInput();
                ReadProductInput();
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            string command = Console.ReadLine();

            while (command != "END")
            {
                try
                {
                    string[] commandTokens = command.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
                    string   personName    = commandTokens[0];
                    string   productName   = commandTokens[1];

                    Person  person  = this.persons.FirstOrDefault(x => x.Name == personName);
                    Product product = this.products.FirstOrDefault(x => x.Name == productName);

                    if (person != null && product != null)
                    {
                        person.BuyProduct(product);
                        Console.WriteLine($"{person.Name} bought {product.Name}");
                    }
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                command = Console.ReadLine();
            }

            Console.WriteLine(String.Join(Environment.NewLine, this.persons));
        }
Example #16
0
        static void Main()
        {
            string[]      people     = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries), products = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);
            List <Person> personList = new List <Person>();

            foreach (string person in people)
            {
                personList.Add(new Person(person.Split("=")[0], double.Parse(person.Split("=")[1])));
            }
            List <Product> productList = new List <Product>();

            foreach (string product in products)
            {
                productList.Add(new Product(product.Split("=")[0], double.Parse(product.Split("=")[1])));
            }
            string[] command = Console.ReadLine().Split();
            while (command[0] != "END")
            {
                string  name        = command[0];
                string  product     = command[1];
                Person  currPerson  = personList.FirstOrDefault(n => n.Name == name);
                Product currProduct = productList.FirstOrDefault(p => p.ProductName == product);
                if (currPerson.HasEnoughMoney(currProduct))
                {
                    currPerson.BuyProduct(currProduct);
                    Console.WriteLine($"{name} bought {product}");
                }
                else
                {
                    Console.WriteLine($"{name} can't afford {product}");
                }
                command = Console.ReadLine().Split();
            }
            foreach (var person in personList)
            {
                Console.WriteLine(person);
            }
        }
Example #17
0
        public void Run()
        {
            try
            {
                this.ParsePeopleInput();
                this.ParseProductsInput();
                string command;
                while ((command = Console.ReadLine()) != "END")
                {
                    string[] cmdArgs = command
                                       .Split(" ", StringSplitOptions.RemoveEmptyEntries)
                                       .ToArray();
                    string personName  = cmdArgs[0];
                    string productName = cmdArgs[1];

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

                    if (person != null && product != null)
                    {
                        string result = person.BuyProduct(product);

                        Console.WriteLine(result);
                    }
                }
                foreach (Person person in this.people)
                {
                    Console.WriteLine(person);
                }
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine(ae.Message);
            }
        }
        public void Run()
        {
            AddPeople();

            AddProducts();

            string command = Console.ReadLine();

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

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

                try
                {
                    Person  currentPerson  = this.people.FirstOrDefault(x => x.Name == personName);
                    Product currentProduct = this.products.FirstOrDefault(x => x.Name == productName);

                    currentPerson.BuyProduct(currentProduct);

                    Console.WriteLine($"{currentPerson.Name} bought {currentProduct.Name}");
                }
                catch (InvalidOperationException ioe)
                {
                    Console.WriteLine(ioe.Message);
                }

                command = Console.ReadLine();
            }

            PrintOutput();
        }
        public void Run()
        {
            PeopleInput();
            ProductsInput();

            string command = Console.ReadLine();

            while (command != "END")
            {
                string[] commandSplitted = command.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToArray();

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

                try
                {
                    Person  thisPerson  = people.First(x => x.Name == personName);
                    Product thisProduct = products.First(x => x.Name == productName);

                    thisPerson.BuyProduct(thisProduct);

                    Console.WriteLine($"{thisPerson.Name} bought {thisProduct.Name}");
                }
                catch (InvalidOperationException ioe)
                {
                    Console.WriteLine(ioe.Message);
                }

                command = Console.ReadLine();
            }

            foreach (Person person in this.people)
            {
                Console.WriteLine(person);
            }
        }
Example #20
0
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(';', StringSplitOptions.RemoveEmptyEntries);

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

            foreach (var pair in input)
            {
                string[] tokens = pair.Split('=');

                try
                {
                    string  name  = tokens[0];
                    decimal money = decimal.Parse(tokens[1]);

                    Person person = new Person(name, money);
                    persons.Add(person);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

            input = Console.ReadLine().Split(';', StringSplitOptions.RemoveEmptyEntries);

            foreach (var pair in input)
            {
                string[] tokens = pair.Split('=');

                try
                {
                    string  name = tokens[0];
                    decimal cost = decimal.Parse(tokens[1]);

                    Product product = new Product(name, cost);
                    products.Add(product);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

            string command = Console.ReadLine();

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

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

                Person  person  = persons.Single(p => p.Name == personName);
                Product product = products.Single(p => p.Name == productName);

                if (!person.BuyProduct(product))
                {
                    Console.WriteLine($"{person.Name} can't afford {product.Name}");
                }
                else
                {
                    Console.WriteLine($"{person.Name} bought {product.Name}");
                }

                command = Console.ReadLine();
            }

            foreach (var person in persons)
            {
                string productsBought = person.Products.Count == 0 ? "Nothing bought" : String.Join(", ", person.Products.Select(p => p.Name));

                Console.WriteLine($"{person.Name} - {productsBought}");
            }
        }
Example #21
0
        public static void Main()
        {
            try
            {
                List <Person> people = new List <Person>();

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

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

                for (int i = 0; i < inputArg.Length; i++)
                {
                    string[] input = inputArg[i].Split("=");

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

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

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

                for (int i = 0; i < productInfo.Length; i++)
                {
                    string[] currentProductInfo = productInfo[i].Split("=");

                    string  name = currentProductInfo[0];
                    decimal cost = decimal.Parse(currentProductInfo[1]);

                    bagWithProducts.Add(new Product(name, cost));
                }

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

                    if (command.Equals("END"))
                    {
                        break;
                    }

                    string[] inputInfo = command.Split().ToArray();

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

                    Person person = people.FirstOrDefault(x => x.Name == personName);

                    Product currentProduct = bagWithProducts.FirstOrDefault(x => x.Name == productName);

                    person.BuyProduct(currentProduct);
                }

                foreach (var person in people)
                {
                    Console.WriteLine(person);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #22
0
        static void Main(string[] args)
        {
            string[] people   = Console.ReadLine().Split(new char[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
            string[] products = Console.ReadLine().Split(new char[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);

            Dictionary <string, Person>  allPersons  = new Dictionary <string, Person>();
            Dictionary <string, Product> allProducts = new Dictionary <string, Product>();


            try
            {
                foreach (var p in people)
                {
                    string[] peopleTokens = p.Split('=');
                    string   name         = peopleTokens[0];
                    decimal  money        = decimal.Parse(peopleTokens[1]);
                    if (!allPersons.ContainsKey(name))
                    {
                        Person newPerson = new Person(name, money);
                        allPersons.Add(name, newPerson);
                    }
                }
                foreach (var prod in products)
                {
                    string[] peopleTokens = prod.Split('=');
                    string   name         = peopleTokens[0];
                    decimal  cost         = decimal.Parse(peopleTokens[1]);
                    if (!allProducts.ContainsKey(name))
                    {
                        Product newProduct = new Product(name, cost);
                        allProducts.Add(name, newProduct);
                    }
                }


                string input = Console.ReadLine();

                while (input != "END")
                {
                    string[] inputTokens = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

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

                    Person  currentPerson  = allPersons[personName];
                    Product currentProduct = allProducts[productName];
                    try
                    {
                        currentPerson.BuyProduct(currentProduct);
                    }
                    catch (InvalidOperationException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    input = Console.ReadLine();
                }
            }
            catch (ArgumentException aex)
            {
                Console.WriteLine(aex.Message);
            }
            foreach (var per in allPersons.Values)
            {
                Console.WriteLine(per);
            }
        }
Example #23
0
        static void Main(string[] args)
        {
            string[] people = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);

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

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

            foreach (var personStr in people)
            {
                string[] input  = personStr.Split("=", StringSplitOptions.RemoveEmptyEntries);
                string   name   = input[0];
                double   money  = double.Parse(input[1]);
                Person   person = new Person(name, money);

                peopleList.Add(person);
            }

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

            foreach (var productStr in products)
            {
                string[] input   = productStr.Split("=", StringSplitOptions.RemoveEmptyEntries);
                string   name    = input[0];
                double   cost    = double.Parse(input[1]);
                Product  product = new Product(name, cost);

                productsList.Add(product);
            }

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

            while (inputCmnd[0] != "END")
            {
                string name        = inputCmnd[0];
                string productName = inputCmnd[1];

                Person  currPerson  = peopleList.Find(n => n.Name == name);
                Product currProduct = productsList.Find(n => n.Name == productName);

                currPerson.BuyProduct(currProduct);

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

            foreach (var person in peopleList)
            {
                Console.Write($"{person.Name} - ");
                if (person.BagOfProducts.Any())
                {
                    for (int i = 0; i < person.BagOfProducts.Count; i++)
                    {
                        if (i == person.BagOfProducts.Count - 1)
                        {
                            Console.WriteLine(person.BagOfProducts[i].Name);
                        }
                        else
                        {
                            Console.Write(person.BagOfProducts[i].Name + ", ");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Nothing bought");
                }
            }
        }
Example #24
0
        static void Main(string[] args)
        {
            string[] peopleInput = Console.ReadLine().Split(new[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < peopleInput.Length; i += 2)
            {
                string  name  = peopleInput[i];
                decimal money = decimal.Parse(peopleInput[i + 1]);
                try
                {
                    Person person = new Person(name, money);
                    people.Add(person);
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

            string[] productsInput = Console.ReadLine().Split(new[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < productsInput.Length; i += 2)
            {
                string  name = productsInput[i];
                decimal cost = decimal.Parse(productsInput[i + 1]);
                try
                {
                    Product product = new Product(name, cost);
                    products.Add(product);
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

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

                string[] purchasInput = input.Split();
                string   personName   = purchasInput[0];
                string   productName  = purchasInput[1];

                Person  person  = GetPerson(personName);
                Product product = GetProduct(productName);

                try
                {
                    person.BuyProduct(product);
                    Console.WriteLine($"{person.Name} bought {product.Name}");
                }
                catch (System.ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            foreach (var person in people)
            {
                Console.WriteLine(person);
            }
        }
Example #25
0
        static void Main(string[] args)
        {
            List <Person>  people   = new List <Person>();
            List <Product> products = new List <Product>();

            try
            {
                string[] person = Console.ReadLine().Split(';', StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < person.Length; i++)
                {
                    string[] tokens = person[i].Split('=', StringSplitOptions.RemoveEmptyEntries);

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

                    Person newPerson = new Person(name, money);

                    people.Add(newPerson);
                }

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

                for (int i = 0; i < product.Length; i++)
                {
                    string[] tokens = product[i].Split('=', StringSplitOptions.RemoveEmptyEntries);

                    string  name = tokens[0];
                    decimal cost = decimal.Parse(tokens[1]);

                    Product newProduct = new Product(name, cost);

                    products.Add(newProduct);
                }
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            string input = string.Empty;

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

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

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

                if (currentPerson.CanAfford(currentProduct))
                {
                    currentPerson.BuyProduct(currentProduct);
                }
                else
                {
                    Console.WriteLine($"{currentPerson.Name} can't afford {currentProduct.Name}");
                }
            }

            foreach (var person in people)
            {
                if (!person.Bag.Any())
                {
                    Console.WriteLine($"{person.Name} - Nothing bought");
                    continue;
                }

                Console.WriteLine($"{person.Name} - {string.Join(", ", person.Bag)}");
            }
        }
        public static void Main()
        {
            string[]       inputPeople = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            IList <Person> people      = new List <Person>();

            foreach (string person in inputPeople)
            {
                string[] args = person.Split('=');

                try
                {
                    Person currentPerson = new Person(args[0], decimal.Parse(args[1]));
                    people.Add(currentPerson);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                    return;
                }
            }

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

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

            foreach (string inputProduct in inputProducts)
            {
                string[] args = inputProduct.Split('=');

                try
                {
                    Product currentProduct = new Product(args[0], decimal.Parse(args[1]));
                    products.Add(currentProduct);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                    return;
                }
            }

            string input = Console.ReadLine();

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

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

                try
                {
                    currentPerson.BuyProduct(currentProduct);
                    Console.WriteLine($"{currentPerson.Name} bought {currentProduct.Name}");
                }
                catch (InvalidOperationException iope)
                {
                    Console.WriteLine(iope.Message);
                }

                input = Console.ReadLine();
            }

            foreach (Person person in people)
            {
                Console.WriteLine(person);
            }
        }
Example #27
0
        public static void Main()
        {
            string[] inputPersons = Console.ReadLine()
                                    .Split(new string[] { "=", ";" }, StringSplitOptions.RemoveEmptyEntries);

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

            try
            {
                List <Person> persons = new List <Person>();

                for (int i = 0; i < inputPersons.Length; i += 2)
                {
                    string currentPerson = inputPersons[i];
                    int    currentBudget = int.Parse(inputPersons[i + 1]);

                    Person person = new Person(currentPerson, currentBudget);
                    persons.Add(person);
                }

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

                for (int i = 0; i < inputProducts.Length; i += 2)
                {
                    string currentProduct = inputProducts[i];
                    int    currentSum     = int.Parse(inputProducts[i + 1]);

                    Product product = new Product(currentProduct, currentSum);
                    products.Add(product);
                }

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

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

                    string[] arguments = input
                                         .Split();

                    string currentPerson  = arguments[0];
                    string currentProduct = arguments[1];

                    Person  person  = persons.FirstOrDefault(x => x.Name == currentPerson);
                    Product product = products.FirstOrDefault(x => x.Name == currentProduct);

                    Console.WriteLine(person.BuyProduct(product));
                }

                foreach (var person in persons)
                {
                    Console.WriteLine(person);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #28
0
        public static void Main(string[] args)
        {
            List <Person>  people   = new List <Person>();
            List <Product> products = new List <Product>();

            string[] input = Console.ReadLine().Split(";");

            for (int i = 0; i < input.Length; i++)
            {
                string[] tempPersont = input[i].Split("=");

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

                try
                {
                    Person person = new Person(name, money);
                    people.Add(person);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Environment.Exit(0);
                }
            }

            input = Console.ReadLine().Split(";");

            for (int i = 0; i < input.Length; i++)
            {
                string[] tempProduct = input[i].Split("=");

                string  nameOfProduct = tempProduct[0];
                decimal price         = decimal.Parse(tempProduct[1]);

                try
                {
                    Product product = new Product(nameOfProduct, price);
                    products.Add(product);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Environment.Exit(0);
                }
            }


            input = Console.ReadLine().Split(" ");

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

                Person  person       = people.FirstOrDefault(x => x.Name == personName);
                Product productToBuy = products.FirstOrDefault(x => x.Name == productName);

                if (person.Money >= productToBuy.Cost)
                {
                    person.BuyProduct(productToBuy);
                    person.Money -= productToBuy.Cost;

                    Console.WriteLine($"{person.Name} bought {productToBuy.Name}");
                }
                else
                {
                    Console.WriteLine($"{person.Name} can't afford {productToBuy.Name}");
                }

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

            foreach (var person in people)
            {
                if (person.Products().Count > 0)
                {
                    Console.WriteLine($"{person.Name} - {string.Join(", ", person.Products().Select(x => x.Name))}");
                }
                else
                {
                    Console.WriteLine($"{person.Name} - Nothing bought");
                }
            }
        }
Example #29
0
        static void Main(string[] args)
        {
            List <Person>  people   = new List <Person>();
            List <Product> products = new List <Product>();

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

            tokens = Console.ReadLine().Split(';', 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 cmd = Console.ReadLine();

            while (cmd != "END")
            {
                string[] purchase = cmd
                                    .Split(' ', StringSplitOptions.RemoveEmptyEntries);

                string  name           = purchase[0];
                string  product        = purchase[1];
                Person  currentPerson  = people.Where(p => p.Name == name).FirstOrDefault();
                Product currentProduct = products.Where(p => p.Name == product).FirstOrDefault();
                if (currentPerson != null && currentProduct != null)
                {
                    currentPerson.BuyProduct(currentProduct);
                }

                cmd = Console.ReadLine();
            }

            foreach (Person p in people)
            {
                Console.WriteLine(p);
            }
        }
Example #30
0
        public static void Main()
        {
            List <Person>  people   = new List <Person>();
            List <Product> products = new List <Product>();

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

            for (int i = 0; i < namesInput.Length - 1; i += 2)
            {
                string name  = namesInput[i];
                double money = double.Parse(namesInput[i + 1]);
                try
                {
                    Person person = new Person(name, money);
                    people.Add(person);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                    return;
                }
            }

            for (int i = 0; i < productsInput.Length - 1; i += 2)
            {
                string name = productsInput[i];
                double cost = double.Parse(productsInput[i + 1]);
                try
                {
                    Product product = new Product(name, cost);
                    products.Add(product);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                    return;
                }
            }

            string input = Console.ReadLine();

            while (input != "END")
            {
                string[] inputParts  = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                string   personName  = inputParts[0];
                string   productName = inputParts[1];

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

                person.BuyProduct(product);
                input = Console.ReadLine();
            }

            foreach (Person person in people)
            {
                if (person.Products.Count == 0)
                {
                    Console.WriteLine($"{person.Name} - Nothing bought");
                }
                else
                {
                    Console.WriteLine($"{person.Name} - {string.Join(", ", person.Products)}");
                }
            }
        }