Advertisement
Guest User

13.1

a guest
Feb 16th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6.  
  7. public class Bread : ProductPrototype
  8. {
  9.     public Bread(decimal price) : base(price) { }
  10.  
  11.     public override ProductPrototype Clone()
  12.     {
  13.         return (ProductPrototype)this.MemberwiseClone();
  14.  
  15.     }
  16. }
  17.  
  18. public class Milk : ProductPrototype
  19. {
  20.     public Milk(decimal price) : base(price) { }
  21.  
  22.     public override ProductPrototype Clone()
  23.     {
  24.         return (ProductPrototype)this.MemberwiseClone();
  25.     }
  26. }
  27.  
  28.  
  29. public abstract class ProductPrototype
  30. {
  31.     public decimal Price { get; set; }
  32.  
  33.     public ProductPrototype(decimal price)
  34.     {
  35.         Price = price;
  36.     }
  37.  
  38.     public abstract ProductPrototype Clone();
  39. }
  40.  
  41.  
  42. public class Supermarket
  43. {
  44.     private Dictionary<string, ProductPrototype> _productList = new Dictionary<string, ProductPrototype>();
  45.  
  46.     public void AddProduct(string key, ProductPrototype productPrototype)
  47.     {
  48.         _productList.Add(key, productPrototype);
  49.     }
  50.  
  51.     public ProductPrototype GetProduct(string key)
  52.     {
  53.         return _productList[key].Clone();
  54.     }
  55. }
  56.  
  57.  
  58. class MainClass
  59. {
  60.     public static void Main(string[] args)
  61.     {
  62.  
  63.         var supermarket = new Supermarket();
  64.  
  65.         supermarket.AddProduct("Milk", new Milk(0.89m));
  66.         supermarket.AddProduct("Bread", new Bread(1.10m));
  67.  
  68.         decimal sourcePrice;
  69.         decimal currentPrice;
  70.  
  71.         var clonedMilk = (Milk)supermarket.GetProduct("Milk");        
  72.         sourcePrice = clonedMilk.Price;
  73.         clonedMilk.Price = 0.85m;
  74.         currentPrice = clonedMilk.Price;
  75.         Console.WriteLine(String.Format("Milk - {0} > {1}", sourcePrice, currentPrice));
  76.  
  77.         var clonedBread = (Bread)supermarket.GetProduct("Bread");
  78.         sourcePrice = clonedBread.Price;
  79.         clonedBread.Price = 0.99m;
  80.         currentPrice = clonedBread.Price;
  81.         Console.WriteLine(String.Format("Bread - {0} > {1}", sourcePrice, currentPrice));
  82.  
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement