Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Shopping_Spree
- {
- public class StartUp
- {
- static void Main(string[] args)
- {
- List<Person> people = new List<Person>();
- List<Product> products = new List<Product>();
- string[] peopleNameMoney = Console.ReadLine()
- .Split(";", StringSplitOptions.RemoveEmptyEntries)
- .ToArray();
- try
- {
- for (int i = 0; i < peopleNameMoney.Length; i++)
- {
- string[] parts = peopleNameMoney[i].Split("=", StringSplitOptions.RemoveEmptyEntries);
- Person person = new Person(parts[0], decimal.Parse(parts[1]));
- people.Add(person);
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- return;
- }
- try
- {
- string[] prodCost = Console.ReadLine()
- .Split(";", StringSplitOptions.RemoveEmptyEntries)
- .ToArray();
- for (int i = 0; i < prodCost.Length; i++)
- {
- string[] partsPoducts = prodCost[i].Split("=", StringSplitOptions.RemoveEmptyEntries);
- Product product = new Product(partsPoducts[0], decimal.Parse(partsPoducts[1]));
- products.Add(product);
- }
- }
- catch (Exception s)
- {
- Console.WriteLine(s.Message);
- return;
- }
- while (true)
- {
- string[] command = Console.ReadLine()
- .Split(' ', StringSplitOptions.RemoveEmptyEntries)
- .ToArray();
- if (command[0] == "END")
- {
- break;
- }
- foreach (var buyer in people )
- {
- if (buyer.Name == command[0])
- {
- foreach (var prod in products)
- {
- if (prod.Name == command[1])
- {
- buyer.BuyProduct(buyer, prod);
- }
- }
- }
- }
- }
- foreach (var item in people)
- {
- Console.WriteLine(item.ToString());
- }
- }
- }
- }
- \using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Text;
- using System.Threading;
- using System.Xml.Linq;
- namespace Shopping_Spree
- {
- public class Person
- {
- private decimal money;
- private string name;
- private List<string> bagOfProduct = new List<string>();
- public Person()
- {
- bagOfProduct = new List<string>();
- }
- public IReadOnlyCollection<string> BagOfProduct
- {
- get { return bagOfProduct.AsReadOnly(); }
- }
- public Person(string name)
- {
- Name = name;
- }
- public Person(string name, decimal money)
- {
- CheckValidation(name, money);
- Name = name;
- Money = money;
- }
- public decimal Money
- {
- get { return money; }
- private set { money = value; }
- }
- public string Name
- {
- get { return name; }
- private set { name = value; }
- }
- public void BuyProduct(Person name, Product prodName)
- {
- if (this.Money >= prodName.Cost)
- {
- bagOfProduct.Add(prodName.Name);
- this.Money -= prodName.Cost;
- Console.WriteLine($"{this.Name} bought {prodName.Name}");
- }
- else
- {
- Console.WriteLine($"{this.Name} can't afford {prodName.Name}");
- }
- }
- private void CheckValidation(string name, decimal money)
- {
- if (name == string.Empty)
- {
- throw new ArgumentException("Name cannot be empty");
- }
- if (money < 0)
- {
- throw new ArgumentException("Money cannot be negative");
- }
- }
- public override string ToString()
- {
- StringBuilder toPrint = new StringBuilder();
- toPrint.Append($"{this.name} - ");
- if (this.BagOfProduct.Count == 0)
- {
- return $"{this.name} - Nothing bought";
- }
- for (int i = 0; i < this.BagOfProduct.Count; i++)
- {
- if (i == this.BagOfProduct.Count -1)
- {
- toPrint.Append($"{bagOfProduct[i]}");
- }
- else
- {
- toPrint.Append($"{bagOfProduct[i]}, ");
- }
- }
- return $"{toPrint}";
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.Text;
- using System.Xml.Linq;
- namespace Shopping_Spree
- {
- public class Product
- {
- private string name;
- private decimal cost;
- private List<Product> allProducts;
- public Product()
- {
- allProducts = new List<Product>();
- }
- public IReadOnlyCollection <Product> all
- {
- get { return allProducts; }
- }
- public Product(string name, decimal cost)
- {
- CheckValidation(name, cost);
- Name = name;
- Cost = cost;
- }
- public string Name
- {
- get { return name; }
- private set { name = value; }
- }
- public decimal Cost
- {
- get {return cost;}
- private set { cost = value;}
- }
- private void CheckValidation(string name, decimal cost)
- {
- if (name == string.Empty)
- {
- throw new ArgumentException("Name cannot be empty");
- }
- if (cost < 0)
- {
- throw new ArgumentException("Money cannot be negative");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement