Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace ShoppingSpree
- {
- 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;
- }
- public string Name
- {
- get
- {
- return this.name;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- throw new ArgumentException($"{nameof(this.Name)} cannot be empty");
- }
- this.name = value;
- }
- }
- public decimal Money
- {
- get
- {
- return this.money;
- }
- set
- {
- if (value < 0)
- {
- throw new ArgumentException($"{nameof(this.Money)} cannot be negative");
- }
- this.money = value;
- }
- }
- public List<Product> BagOfProducts
- {
- get
- {
- return this.bagOfProducts;
- }
- set
- {
- value = new List<Product>();
- this.bagOfProducts = value;
- }
- }
- public void BuyProduct(Product product)
- {
- if (product.Cost > this.Money)
- {
- throw new InvalidOperationException($"{nameof(this.Name)} can't afford {nameof(product.Name)}");
- }
- this.Money -= product.Cost;
- this.bagOfProducts.Add(product);
- }
- }
- public class Product
- {
- private string productName;
- private decimal cost;
- public Product(string name, decimal cost)
- {
- this.Name = productName;
- this.Cost = cost;
- }
- public string Name
- {
- get
- {
- return this.productName;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- throw new ArgumentException($"{nameof(this.Name)} cannot be empty");
- }
- this.productName = value;
- }
- }
- public decimal Cost
- {
- get
- {
- return this.cost;
- }
- set
- {
- if (value < 0)
- {
- throw new ArgumentException($"{nameof(this.Cost)} cannot be nagative");
- }
- this.cost = value;
- }
- }
- }
- class ShoppingSpree
- {
- static void Main(string[] args)
- {
- try
- {
- string[] peopleInfo = Console.ReadLine().Split(new[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries);
- string[] produnctInfo = Console.ReadLine().Split(new[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries);
- List<Person> people = GetPeople(peopleInfo);
- List<Product> products = GetProducts(produnctInfo);
- string command = Console.ReadLine();
- while (command != "END")
- {
- string[] tokens = command.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- string name = tokens[0];
- string product = tokens[1];
- Person personToCheck = people.Where(p => p.Name == name).FirstOrDefault();
- Product productToCheck = products.Where(p => p.Name == product).FirstOrDefault();
- personToCheck.BuyProduct(productToCheck);
- command = Console.ReadLine();
- }
- foreach (var person in people)
- {
- if (person.BagOfProducts.Count > 0)
- {
- Console.WriteLine($"{person} - {string.Join(", ", person.BagOfProducts.Select(p => $"{p.Name}"))}");
- }
- else
- {
- Console.WriteLine($"{person.Name} - Nothing bought");
- }
- }
- }
- catch (ArgumentException e)
- {
- Console.WriteLine(e.Message);
- }
- }
- private static List<Product> GetProducts(string[] produnctInfo)
- {
- List<Product> products = new List<Product>();
- for (int i = 0; i < produnctInfo.Length; i += 2)
- {
- string name = produnctInfo[i];
- decimal cost = decimal.Parse(produnctInfo[i + 1]);
- Product product = new Product(name, cost);
- products.Add(product);
- }
- return products;
- }
- private static List<Person> GetPeople(string[] peopleInfo)
- {
- List<Person> people = new List<Person>();
- for (int i = 0; i < peopleInfo.Length; i += 2)
- {
- string name = peopleInfo[i];
- decimal money = decimal.Parse(peopleInfo[i + 1]);
- Person person = new Person(name, money);
- people.Add(person);
- }
- return people;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement