Advertisement
Guest User

Shopping Spree

a guest
Aug 9th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace ShoppingSpree
  8. {
  9.     public class Person
  10.     {
  11.         private string name;
  12.         private decimal money;
  13.         private List<Product> bagOfProducts;
  14.  
  15.         public Person(string name, decimal money)
  16.         {
  17.             this.Name = name;
  18.             this.Money = money;
  19.         }
  20.  
  21.         public string Name
  22.         {
  23.             get
  24.             {
  25.                 return this.name;
  26.             }
  27.  
  28.             set
  29.             {
  30.                 if (string.IsNullOrEmpty(value))
  31.                 {
  32.                     throw new ArgumentException($"{nameof(this.Name)} cannot be empty");
  33.                 }
  34.  
  35.                 this.name = value;
  36.             }
  37.         }
  38.  
  39.         public decimal Money
  40.         {
  41.             get
  42.             {
  43.                 return this.money;
  44.             }
  45.  
  46.             set
  47.             {
  48.                 if (value < 0)
  49.                 {
  50.                     throw new ArgumentException($"{nameof(this.Money)} cannot be negative");
  51.                 }
  52.  
  53.                 this.money = value;
  54.             }
  55.         }
  56.  
  57.         public List<Product> BagOfProducts
  58.         {
  59.             get
  60.             {
  61.                 return this.bagOfProducts;
  62.             }
  63.  
  64.             set
  65.             {
  66.                 value = new List<Product>();
  67.                 this.bagOfProducts = value;
  68.             }
  69.         }
  70.  
  71.         public void BuyProduct(Product product)
  72.         {
  73.             if (product.Cost > this.Money)
  74.             {
  75.                 throw new InvalidOperationException($"{nameof(this.Name)} can't afford {nameof(product.Name)}");
  76.             }
  77.  
  78.             this.Money -= product.Cost;
  79.             this.bagOfProducts.Add(product);
  80.         }
  81.     }
  82.  
  83.     public class Product
  84.     {
  85.         private string productName;
  86.         private decimal cost;
  87.  
  88.         public Product(string name, decimal cost)
  89.         {
  90.             this.Name = productName;
  91.             this.Cost = cost;
  92.         }
  93.  
  94.         public string Name
  95.         {
  96.             get
  97.             {
  98.                 return this.productName;
  99.             }
  100.  
  101.             set
  102.             {
  103.                 if (string.IsNullOrEmpty(value))
  104.                 {
  105.                     throw new ArgumentException($"{nameof(this.Name)} cannot be empty");
  106.                 }
  107.  
  108.                 this.productName = value;
  109.             }
  110.         }
  111.  
  112.         public decimal Cost
  113.         {
  114.             get
  115.             {
  116.                 return this.cost;
  117.             }
  118.  
  119.             set
  120.             {
  121.                 if (value < 0)
  122.                 {
  123.                     throw new ArgumentException($"{nameof(this.Cost)} cannot be nagative");
  124.                 }
  125.  
  126.                 this.cost = value;
  127.             }
  128.         }
  129.     }
  130.  
  131.     class ShoppingSpree
  132.     {
  133.         static void Main(string[] args)
  134.         {
  135.             try
  136.             {
  137.                 string[] peopleInfo = Console.ReadLine().Split(new[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries);
  138.                 string[] produnctInfo = Console.ReadLine().Split(new[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries);
  139.  
  140.                 List<Person> people = GetPeople(peopleInfo);
  141.                 List<Product> products = GetProducts(produnctInfo);
  142.  
  143.                 string command = Console.ReadLine();
  144.  
  145.                 while (command != "END")
  146.                 {
  147.                     string[] tokens = command.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  148.                     string name = tokens[0];
  149.                     string product = tokens[1];
  150.  
  151.                     Person personToCheck = people.Where(p => p.Name == name).FirstOrDefault();
  152.                     Product productToCheck = products.Where(p => p.Name == product).FirstOrDefault();
  153.  
  154.                     personToCheck.BuyProduct(productToCheck);
  155.  
  156.                     command = Console.ReadLine();
  157.                 }
  158.  
  159.                 foreach (var person in people)
  160.                 {
  161.                     if (person.BagOfProducts.Count > 0)
  162.                     {
  163.                         Console.WriteLine($"{person} - {string.Join(", ", person.BagOfProducts.Select(p => $"{p.Name}"))}");
  164.                     }
  165.                     else
  166.                     {
  167.                         Console.WriteLine($"{person.Name} - Nothing bought");
  168.                     }
  169.                 }
  170.             }
  171.             catch (ArgumentException e)
  172.             {
  173.                 Console.WriteLine(e.Message);
  174.             }
  175.         }
  176.  
  177.         private static List<Product> GetProducts(string[] produnctInfo)
  178.         {
  179.             List<Product> products = new List<Product>();
  180.  
  181.             for (int i = 0; i < produnctInfo.Length; i += 2)
  182.             {
  183.                 string name = produnctInfo[i];
  184.                 decimal cost = decimal.Parse(produnctInfo[i + 1]);
  185.              
  186.                     Product product = new Product(name, cost);
  187.  
  188.                     products.Add(product);              
  189.             }
  190.  
  191.             return products;
  192.         }
  193.  
  194.         private static List<Person> GetPeople(string[] peopleInfo)
  195.         {
  196.             List<Person> people = new List<Person>();
  197.  
  198.             for (int i = 0; i < peopleInfo.Length; i += 2)
  199.             {
  200.                 string name = peopleInfo[i];
  201.                 decimal money = decimal.Parse(peopleInfo[i + 1]);
  202.              
  203.                     Person person = new Person(name, money);
  204.  
  205.                     people.Add(person);            
  206.             }
  207.  
  208.             return people;
  209.         }
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement