Advertisement
social1986

Untitled

Feb 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Person
  5. {
  6.     private string name;
  7.     private decimal money;
  8.     private List<Product> products;
  9.  
  10.     public Person(string name, decimal money)
  11.     {
  12.         this.Name = name;
  13.         this.Money = money;
  14.         products = new List<Product>();
  15.     }
  16.  
  17.     public List<Product> Products
  18.     {
  19.         get { return products; }
  20.         private set { products = value; }
  21.     }
  22.  
  23.  
  24.     public decimal Money
  25.     {
  26.         get { return money; }
  27.         private set
  28.         {
  29.             Validator.ValidatingMoney(value);
  30.             this.money = value;
  31.         }
  32.     }
  33.  
  34.  
  35.     public string Name
  36.     {
  37.         get { return name; }
  38.         private set
  39.         {
  40.            Validator.ValidatingName(value);
  41.             this.name = value;
  42.         }
  43.     }  
  44.  
  45.     public void AddProduct(Product product)
  46.     {
  47.         if (product.Cost <= money)
  48.         {
  49.             products.Add(product);
  50.             this.money -= product.Cost;
  51.         }
  52.         else
  53.         {
  54.             throw new ArgumentException($"{name} can't afford {product.Name}");
  55.         }
  56.     }
  57.  
  58.     public override string ToString()
  59.     {
  60.         return $"{name} - {string.Join(", ", products)}";
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement