static void Main() { string[] input = Console.ReadLine() .Split(new char[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries) .ToArray(); List <Person> people = GetPeople(input); input = Console.ReadLine() .Split(new char[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries) .ToArray(); List <Product> products = GetProducts(input); string[] command = Console.ReadLine() .Split(" ") .ToArray(); while (command[0]?.ToLower() != "end") { string currentPersonName = command[0]; string currentProductName = command[1]; Person currentPerson = people.FirstOrDefault(n => n.Name == currentPersonName); Product currentProduct = products.FirstOrDefault(n => n.Name == currentProductName); if (currentPerson != null && currentProduct != null) { Console.WriteLine(currentPerson.CanAfford(currentProduct)); } command = Console.ReadLine() .Split(" ") .ToArray(); } people.ForEach(p => Console.WriteLine(p)); }
static void Main(string[] args) { string[] people = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); string[] products = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); List <Person> peopleList = new List <Person>(); List <Product> productList = new List <Product>(); foreach (var p in people) { string[] tokens = p.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); string name = tokens[0]; decimal money = decimal.Parse(tokens[1]); try { Person person = new Person(name, money); peopleList.Add(person); } catch (Exception ex) { Console.WriteLine(ex.Message); Environment.Exit(0); } } foreach (var p in products) { string[] tokens = p.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); string productName = tokens[0]; decimal cost = decimal.Parse(tokens[1]); try { Product product = new Product(productName, cost); productList.Add(product); } catch (Exception ex) { Console.WriteLine(ex.Message); Environment.Exit(0); } } string commands = Console.ReadLine(); while (commands != "END") { string[] split = commands.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string name = split[0]; string product = split[1]; var currentPerson = peopleList.Where(a => a.Name == name); var currentProduct = productList.Where(a => a.Name == product); foreach (var person in currentPerson) { foreach (var prod in currentProduct) { person.CanAfford(person, prod); } } commands = Console.ReadLine(); } foreach (var person in peopleList) { person.PrintAllPeople(person); } }
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)}"); } }