Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class Program
- {
- public static void Main()
- {
- try
- {
- var people = GetPeople();
- var products = GetProducts();
- people = BuyProducts(people, products);
- PrintBuyersWithProducts(people);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- private static void PrintBuyersWithProducts(List<Person> people)
- {
- foreach (var person in people)
- {
- var boughtProducts = person.GetProducts();
- var result = boughtProducts.Any()
- ? string.Join(", ", boughtProducts.Select(p => p.Name).ToList())
- : "Nothing bought";
- Console.WriteLine($"{person.Name} - {result}");
- }
- }
- private static List<Person> BuyProducts(List<Person> people, List<Product> products)
- {
- while (true)
- {
- var purchase = Console.ReadLine();
- if (purchase == "END")
- {
- break;
- }
- var purchaseTokens = purchase.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- var buyer = people.FirstOrDefault(b => b.Name == purchaseTokens[0]);
- var product = products.FirstOrDefault(p => p.Name == purchaseTokens[1]);
- try
- {
- buyer.BuyProduct(product);
- Console.WriteLine($"{buyer.Name} bought {product.Name}");
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- return people;
- }
- private static List<Product> GetProducts()
- {
- var products = new List<Product>();
- var productsInfo = Console.ReadLine().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (var token in productsInfo)
- {
- var productInfo = token.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
- products.Add(new Product(productInfo[0], decimal.Parse(productInfo[1])));
- }
- return products;
- }
- private static List<Person> GetPeople()
- {
- var people = new List<Person>();
- var personsInfo = Console.ReadLine().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (var token in personsInfo)
- {
- var personInfo = token.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
- people.Add(new Person(personInfo[0], decimal.Parse(personInfo[1])));
- }
- return people;
- }
- }
- public class Person
- {
- private string name;
- private decimal money;
- private List<Product> bagOfProducts;
- public Person(string name, decimal money)
- {
- this.Name = name;
- this.Money = money;
- this.bagOfProducts = new List<Product>();
- }
- public string Name
- {
- get { return this.name; }
- private set
- {
- if (string.IsNullOrWhiteSpace(value))
- {
- throw new ArgumentException($"{nameof(Name)} cannot be empty");
- }
- this.name = value;
- }
- }
- private decimal Money
- {
- get { return this.money; }
- set
- {
- if (value < 0)
- {
- throw new ArgumentException($"{nameof(Money)} cannot be negative");
- }
- this.money = value;
- }
- }
- public IList<Product> GetProducts()
- {
- return this.bagOfProducts.AsReadOnly();
- }
- public void BuyProduct(Product product)
- {
- if (product.Cost > this.money)
- {
- throw new InvalidOperationException($"{this.name} can't afford {product.Name}");
- }
- this.money -= product.Cost;
- this.bagOfProducts.Add(product);
- }
- }
- public class Product
- {
- private string name;
- private decimal cost;
- public Product(string name, decimal cost)
- {
- this.Name = name;
- this.Cost = cost;
- }
- public string Name
- {
- get => this.name;
- private set
- {
- if (string.IsNullOrWhiteSpace(value))
- {
- throw new ArgumentException($"{nameof(Name)} cannot be empty");
- }
- this.name = value;
- }
- }
- public decimal Cost
- {
- get => this.cost;
- private set
- {
- if (value < 0)
- {
- throw new ArgumentException($"Money cannot be negative");
- }
- this.cost = value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment