Advertisement
desislava_topuzakova

Shop

Apr 18th, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Shop
  6. {
  7. class Shop
  8. {
  9. //полета
  10. private Dictionary<string, Product> availableProducts;
  11.  
  12. //методи
  13. public Shop()
  14. {
  15. availableProducts = new Dictionary<string, Product>();
  16. }
  17.  
  18. public Dictionary<string, Product> AvailableProducts
  19. {
  20. get
  21. {
  22. return availableProducts;
  23. }
  24.  
  25. set
  26. {
  27. availableProducts = value;
  28. }
  29. }
  30.  
  31. //sell
  32. public void Sell(string code, double quantity)
  33. {
  34. if (availableProducts.ContainsKey(code))
  35. {
  36. //имам продукта
  37. Product product = availableProducts[code];
  38. if(product.Quantity >= quantity)
  39. {
  40. //имаме количество
  41. product.Quantity = product.Quantity - quantity;
  42. availableProducts[code] = product;
  43. }
  44. else
  45. {
  46. //нямаме количество
  47. throw new Exception("Not enough quantity.");
  48. }
  49. }
  50. else
  51. {
  52. //нямам продукта
  53. throw new Exception("Not available product.");
  54. }
  55. }
  56.  
  57. public void Add (string code, string name, double price, double quantity)
  58. {
  59. if (availableProducts.ContainsKey(code))
  60. {
  61. //имаме го вече
  62. Product product = new Product(name, code, price, quantity);
  63. availableProducts[code] = product;
  64. }
  65. else
  66. {
  67. //нямаме го и трябва да добавим нов продукт
  68. Product product = new Product(name, code, price, quantity);
  69. availableProducts.Add(code, product);
  70. }
  71. }
  72.  
  73. public void Update (string code, double quantity)
  74. {
  75. if (availableProducts.ContainsKey(code))
  76. {
  77. //имам наличен продукта
  78. Product product = availableProducts[code];
  79. product.Quantity = product.Quantity + quantity;
  80. availableProducts[code] = product;
  81.  
  82. }
  83. else
  84. {
  85. //нямам наличен продукта
  86. throw new Exception("Please add your product first!");
  87. }
  88. }
  89.  
  90. public void Print()
  91. {
  92. foreach (var pair in availableProducts)
  93. {
  94. Product product = pair.Value;
  95. Console.WriteLine($"Product name: {product.Name}, Code: {product.Code}, " +
  96. $"Price: {product.Price}, Quantity: {product.Quantity}");
  97. }
  98. }
  99.  
  100. public double Calculate()
  101. {
  102. double sum = 0;
  103.  
  104. foreach(var pair in availableProducts)
  105. {
  106. Product product = pair.Value;
  107. sum += product.Quantity * product.Price;
  108. }
  109.  
  110. return sum;
  111. }
  112.  
  113.  
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement