Advertisement
KostaUrumov

Untitled

Nov 8th, 2022
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Shopping_Spree
  6. {
  7.     public class StartUp
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<Person> people = new List<Person>();
  12.             List<Product> products = new List<Product>();
  13.             string[] peopleNameMoney = Console.ReadLine()
  14.                 .Split(";", StringSplitOptions.RemoveEmptyEntries)
  15.                 .ToArray();
  16.             try
  17.             {
  18.                 for (int i = 0; i < peopleNameMoney.Length; i++)
  19.                 {
  20.                     string[] parts = peopleNameMoney[i].Split("=", StringSplitOptions.RemoveEmptyEntries);
  21.                     Person person = new Person(parts[0], decimal.Parse(parts[1]));
  22.                     people.Add(person);
  23.                 }
  24.  
  25.             }
  26.             catch (Exception e)
  27.             {
  28.  
  29.                 Console.WriteLine(e.Message);
  30.                 return;
  31.             }
  32.  
  33.  
  34.             try
  35.             {
  36.                 string[] prodCost = Console.ReadLine()
  37.                 .Split(";", StringSplitOptions.RemoveEmptyEntries)
  38.                 .ToArray();
  39.                 for (int i = 0; i < prodCost.Length; i++)
  40.                 {
  41.                     string[] partsPoducts = prodCost[i].Split("=", StringSplitOptions.RemoveEmptyEntries);
  42.                     Product product = new Product(partsPoducts[0], decimal.Parse(partsPoducts[1]));
  43.                     products.Add(product);
  44.                 }
  45.  
  46.             }
  47.             catch (Exception s)
  48.             {
  49.  
  50.                 Console.WriteLine(s.Message);
  51.                 return;
  52.             }
  53.  
  54.            
  55.  
  56.             while (true)
  57.             {
  58.                 string[] command = Console.ReadLine()
  59.                     .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  60.                     .ToArray();
  61.                 if (command[0] == "END")
  62.                 {
  63.                     break;
  64.                 }
  65.                 foreach (var buyer in people )
  66.                 {
  67.                     if (buyer.Name == command[0])
  68.                     {
  69.                         foreach (var prod in products)
  70.                         {
  71.                             if (prod.Name == command[1])
  72.                             {
  73.                                 buyer.BuyProduct(buyer, prod);
  74.                                
  75.                             }
  76.                         }
  77.                     }    
  78.                 }
  79.             }
  80.  
  81.  
  82.             foreach (var item in people)
  83.             {
  84.                 Console.WriteLine(item.ToString());
  85.  
  86.             }
  87.  
  88.                        
  89.         }
  90.     }
  91. }
  92.  
  93.  
  94. \using System;
  95. using System.Collections.Generic;
  96. using System.Data;
  97. using System.Text;
  98. using System.Threading;
  99. using System.Xml.Linq;
  100.  
  101. namespace Shopping_Spree
  102. {
  103.     public class Person
  104.     {
  105.         private decimal money;
  106.         private string name;
  107.        
  108.         private List<string> bagOfProduct = new List<string>();
  109.  
  110.         public Person()
  111.         {
  112.  
  113.             bagOfProduct = new List<string>();
  114.         }
  115.  
  116.        
  117.  
  118.         public IReadOnlyCollection<string> BagOfProduct
  119.         {
  120.             get { return bagOfProduct.AsReadOnly(); }
  121.         }
  122.        
  123.  
  124.         public Person(string name)
  125.         {
  126.             Name = name;
  127.         }
  128.  
  129.         public Person(string name, decimal money)
  130.         {
  131.             CheckValidation(name, money);
  132.             Name = name;
  133.             Money = money;
  134.            
  135.         }
  136.  
  137.        
  138.         public decimal Money
  139.         {
  140.             get { return money; }
  141.             private set { money = value; }
  142.         }
  143.         public string Name
  144.         {
  145.             get { return name; }
  146.             private set { name = value; }
  147.         }
  148.  
  149.         public void BuyProduct(Person name, Product prodName)
  150.         {
  151.             if (this.Money >= prodName.Cost)
  152.             {
  153.                 bagOfProduct.Add(prodName.Name);
  154.                 this.Money -= prodName.Cost;
  155.                 Console.WriteLine($"{this.Name} bought {prodName.Name}");
  156.             }
  157.             else
  158.             {
  159.                 Console.WriteLine($"{this.Name} can't afford {prodName.Name}");
  160.             }
  161.                
  162.            
  163.         }
  164.  
  165.         private void CheckValidation(string name, decimal money)
  166.         {
  167.             if (name == string.Empty)
  168.             {
  169.                 throw new ArgumentException("Name cannot be empty");
  170.             }
  171.             if (money < 0)
  172.             {
  173.                 throw new ArgumentException("Money cannot be negative");
  174.             }
  175.         }
  176.  
  177.         public override string ToString()
  178.         {
  179.             StringBuilder toPrint = new StringBuilder();
  180.             toPrint.Append($"{this.name} - ");
  181.             if (this.BagOfProduct.Count == 0)
  182.             {
  183.                 return $"{this.name} - Nothing bought";
  184.             }
  185.  
  186.             for (int i = 0; i < this.BagOfProduct.Count; i++)
  187.             {
  188.                 if (i ==  this.BagOfProduct.Count -1)
  189.                 {
  190.                     toPrint.Append($"{bagOfProduct[i]}");
  191.                 }
  192.                 else
  193.                 {
  194.                     toPrint.Append($"{bagOfProduct[i]}, ");
  195.                 }
  196.             }
  197.             return $"{toPrint}";
  198.         }
  199.  
  200.  
  201.  
  202.     }
  203. }
  204.  
  205.  
  206. using System;
  207. using System.Collections.Generic;
  208. using System.Dynamic;
  209. using System.Text;
  210. using System.Xml.Linq;
  211.  
  212. namespace Shopping_Spree
  213. {
  214.     public class Product
  215.     {
  216.         private string name;
  217.         private decimal cost;
  218.         private List<Product> allProducts;
  219.  
  220.         public Product()
  221.         {
  222.             allProducts = new List<Product>();
  223.  
  224.         }
  225.         public IReadOnlyCollection <Product> all
  226.         {
  227.             get { return allProducts; }
  228.  
  229.         }
  230.  
  231.         public Product(string name, decimal cost)
  232.         {
  233.             CheckValidation(name, cost);
  234.             Name = name;
  235.             Cost = cost;
  236.  
  237.         }
  238.  
  239.         public string Name
  240.         {
  241.             get { return name; }
  242.             private set { name = value; }
  243.         }
  244.  
  245.         public decimal Cost
  246.         {
  247.             get {return cost;}
  248.             private set { cost = value;}
  249.         }
  250.         private void CheckValidation(string name, decimal cost)
  251.         {
  252.             if (name == string.Empty)
  253.             {
  254.                 throw new ArgumentException("Name cannot be empty");
  255.             }
  256.             if (cost < 0)
  257.             {
  258.                 throw new ArgumentException("Money cannot be negative");
  259.             }
  260.         }
  261.  
  262.  
  263.  
  264.     }
  265. }
  266.  
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement