Aliendreamer

shopping oop basic

May 18th, 2018
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.74 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class Program
  7. {
  8.     public static void Main()
  9.     {
  10.         try
  11.         {
  12.             var people = GetPeople();
  13.             var products = GetProducts();
  14.  
  15.             people = BuyProducts(people, products);
  16.             PrintBuyersWithProducts(people);
  17.         }
  18.         catch (Exception e)
  19.         {
  20.             Console.WriteLine(e.Message);
  21.         }
  22.     }
  23.  
  24.     private static void PrintBuyersWithProducts(List<Person> people)
  25.     {
  26.         foreach (var person in people)
  27.         {
  28.             var boughtProducts = person.GetProducts();
  29.             var result = boughtProducts.Any()
  30.                         ? string.Join(", ", boughtProducts.Select(p => p.Name).ToList())
  31.                         : "Nothing bought";
  32.             Console.WriteLine($"{person.Name} - {result}");
  33.         }
  34.     }
  35.  
  36.     private static List<Person> BuyProducts(List<Person> people, List<Product> products)
  37.     {
  38.         while (true)
  39.         {
  40.             var purchase = Console.ReadLine();
  41.             if (purchase == "END")
  42.             {
  43.                 break;
  44.             }
  45.  
  46.             var purchaseTokens = purchase.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  47.             var buyer = people.FirstOrDefault(b => b.Name == purchaseTokens[0]);
  48.             var product = products.FirstOrDefault(p => p.Name == purchaseTokens[1]);
  49.  
  50.             try
  51.             {
  52.                 buyer.BuyProduct(product);
  53.                 Console.WriteLine($"{buyer.Name} bought {product.Name}");
  54.             }
  55.             catch (Exception e)
  56.             {
  57.                 Console.WriteLine(e.Message);
  58.             }
  59.         }
  60.  
  61.         return people;
  62.     }
  63.  
  64.     private static List<Product> GetProducts()
  65.     {
  66.         var products = new List<Product>();
  67.  
  68.         var productsInfo = Console.ReadLine().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  69.         foreach (var token in productsInfo)
  70.         {
  71.             var productInfo = token.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
  72.             products.Add(new Product(productInfo[0], decimal.Parse(productInfo[1])));
  73.         }
  74.  
  75.         return products;
  76.     }
  77.  
  78.     private static List<Person> GetPeople()
  79.     {
  80.         var people = new List<Person>();
  81.  
  82.         var personsInfo = Console.ReadLine().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  83.         foreach (var token in personsInfo)
  84.         {
  85.             var personInfo = token.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
  86.             people.Add(new Person(personInfo[0], decimal.Parse(personInfo[1])));
  87.         }
  88.  
  89.         return people;
  90.     }
  91. }
  92.  
  93. public class Person
  94. {
  95.     private string        name;
  96.     private decimal       money;
  97.     private List<Product> bagOfProducts;
  98.  
  99.     public Person(string name, decimal money)
  100.     {
  101.         this.Name = name;
  102.         this.Money = money;
  103.         this.bagOfProducts = new List<Product>();
  104.     }
  105.  
  106.     public string Name
  107.     {
  108.         get { return this.name; }
  109.         private set
  110.         {
  111.             if (string.IsNullOrWhiteSpace(value))
  112.             {
  113.                 throw new ArgumentException($"{nameof(Name)} cannot be empty");
  114.             }
  115.             this.name = value;
  116.         }
  117.     }
  118.  
  119.     private decimal Money
  120.     {
  121.         get { return this.money; }
  122.         set
  123.         {
  124.             if (value < 0)
  125.             {
  126.                 throw new ArgumentException($"{nameof(Money)} cannot be negative");
  127.             }
  128.             this.money = value;
  129.         }
  130.     }
  131.  
  132.     public IList<Product> GetProducts()
  133.     {
  134.         return this.bagOfProducts.AsReadOnly();
  135.     }
  136.  
  137.     public void BuyProduct(Product product)
  138.     {
  139.         if (product.Cost > this.money)
  140.         {
  141.             throw new InvalidOperationException($"{this.name} can't afford {product.Name}");
  142.         }
  143.  
  144.         this.money -= product.Cost;
  145.         this.bagOfProducts.Add(product);
  146.     }
  147. }
  148.  
  149.  
  150.  
  151. public class Product
  152. {
  153.     private string  name;
  154.     private decimal cost;
  155.  
  156.     public Product(string name, decimal cost)
  157.     {
  158.         this.Name = name;
  159.         this.Cost = cost;
  160.     }
  161.  
  162.     public string Name
  163.     {
  164.         get => this.name;
  165.         private set
  166.         {
  167.             if (string.IsNullOrWhiteSpace(value))
  168.             {
  169.                 throw new ArgumentException($"{nameof(Name)} cannot be empty");
  170.             }
  171.             this.name = value;
  172.         }
  173.     }
  174.  
  175.     public decimal Cost
  176.     {
  177.         get => this.cost;
  178.         private set
  179.         {
  180.             if (value < 0)
  181.             {
  182.                 throw new ArgumentException($"Money cannot be negative");
  183.             }
  184.             this.cost = value;
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment