Advertisement
Guest User

Untitled

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