RedFlys

OnlineStore

Mar 25th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. Good iPhone12 = new Good("IPhone 12");
  2. Good iPhone11 = new Good("IPhone 11");
  3.  
  4. Warehouse warehouse = new Warehouse();
  5.  
  6. Shop shop = new Shop(warehouse);
  7.  
  8. warehouse.Delive(iPhone12, 10);
  9. warehouse.Delive(iPhone11, 1);
  10.  
  11. //Вывод всех товаров на складе с их остатком
  12.  
  13. Cart cart = shop.Cart();
  14. cart.Add(iPhone12, 4);
  15. cart.Add(iPhone11, 3); //при такой ситуации возникает ошибка так, как нет нужного количества товара на складе
  16.  
  17. //Вывод всех товаров в корзине
  18.  
  19. Console.WriteLine(cart.Order().Paylink);
  20.  
  21. cart.Add(iPhone12, 9); //Ошибка, после заказа со склада убираются заказанные товары
  22.  
  23.  
  24. public class Warehouse : IDeliveryWarehouse
  25. {
  26. private readonly List<Cell> _cells;
  27.  
  28. public Warehouse()
  29. {
  30. _cells = new List<Cell>();
  31. }
  32.  
  33. public void Delive(Good good, int countGoods)
  34. {
  35. good = good ?? throw new ArgumentNullException();
  36.  
  37. if (countGoods < 0)
  38. throw new ArgumentOutOfRangeException();
  39.  
  40. Cell newCell = new Cell(good, countGoods);
  41.  
  42. int cellIndex = _cells.FindIndex(cell => cell.Good == good);
  43.  
  44. if (cellIndex < 0)
  45. _cells.Add(newCell);
  46. else
  47. _cells[cellIndex].Merge(newCell);
  48. }
  49.  
  50. public bool CanIssue(IReadOnlyCell issuedCell)
  51. {
  52. issuedCell = issuedCell ?? throw new ArgumentNullException();
  53.  
  54. int cellIndex = _cells.FindIndex(cell => cell.Good == issuedCell.Good);
  55.  
  56. return (cellIndex > -1 && _cells[cellIndex].CountGoods >= issuedCell.CountGoods) == false;
  57. }
  58.  
  59. public void TransferForDelivery(IReadOnlyCell issuedCell)
  60. {
  61. issuedCell = issuedCell ?? throw new ArgumentNullException();
  62.  
  63. int cellIndex = _cells.FindIndex(cell => cell.Good == issuedCell.Good);
  64.  
  65. if (cellIndex > -1 && _cells[cellIndex].CountGoods >= issuedCell.CountGoods)
  66. _cells[cellIndex].Issue(issuedCell);
  67. }
  68. }
  69.  
  70. public class Shop
  71. {
  72. private Warehouse _warehouse;
  73.  
  74. public Shop(Warehouse warehouse)
  75. {
  76. if (_warehouse == null)
  77. throw new ArgumentNullException();
  78.  
  79. _warehouse = warehouse;
  80. }
  81.  
  82. public Cart Cart()
  83. {
  84. return new Cart(_warehouse);
  85. }
  86. }
  87.  
  88. public class Cart
  89. {
  90. private List<Cell> _cells;
  91. private IDeliveryWarehouse _warehouse;
  92.  
  93. public Cart(IDeliveryWarehouse warehouse)
  94. {
  95. _warehouse = warehouse ?? throw new ArgumentNullException();
  96.  
  97. _cells = new List<Cell>();
  98. }
  99.  
  100. public IReadOnlyList<IReadOnlyCell> Cells => _cells;
  101.  
  102. public void Add(Good good, int countGoods)
  103. {
  104. if (good == null)
  105. throw new ArgumentNullException();
  106.  
  107. if (countGoods < 0 )
  108. throw new ArgumentOutOfRangeException();
  109.  
  110. Cell newCell = new Cell(good, countGoods);
  111.  
  112. if (_warehouse.CanIssue(newCell) == false)
  113. throw new ArgumentOutOfRangeException();
  114.  
  115. int cellIndex = _cells.FindIndex(cell => cell.Good == good);
  116.  
  117. if (cellIndex < 0)
  118. _cells.Add(newCell);
  119. else
  120. _cells[cellIndex].Merge(newCell);
  121. }
  122.  
  123. public PayOrder Order()
  124. {
  125. foreach (Cell cell in _cells)
  126. _warehouse.TransferForDelivery(cell);
  127.  
  128. _cells.Clear();
  129. return new PayOrder();
  130. }
  131. }
  132.  
  133. public class Cell : IReadOnlyCell
  134. {
  135. public Cell(Good good, int countGoods)
  136. {
  137. if (good == null)
  138. throw new ArgumentNullException();
  139.  
  140. if (countGoods < 0)
  141. throw new ArgumentOutOfRangeException();
  142.  
  143. Good = good;
  144. CountGoods = countGoods;
  145. }
  146.  
  147. public Good Good {get; private set;}
  148. public int CountGoods { get; private set;}
  149.  
  150. public void Merge(Cell newCell)
  151. {
  152. if (newCell.Good != Good)
  153. throw new InvalidOperationException();
  154.  
  155. if (newCell.CountGoods < 0)
  156. throw new ArgumentOutOfRangeException();
  157.  
  158. CountGoods += newCell.CountGoods;
  159. }
  160.  
  161. public void Issue(IReadOnlyCell newCell)
  162. {
  163. if(newCell.Good != Good)
  164. throw new InvalidOperationException();
  165.  
  166. if (newCell.CountGoods < 0 || newCell.CountGoods > CountGoods)
  167. throw new ArgumentOutOfRangeException();
  168.  
  169. CountGoods -= newCell.CountGoods;
  170. }
  171. }
  172.  
  173. public class Good
  174. {
  175. public Good(string name)
  176. {
  177. if (name == null)
  178. throw new ArgumentNullException();
  179.  
  180. Name = name;
  181. }
  182.  
  183. public string Name { get; private set; }
  184. }
  185.  
  186. public class PayOrder
  187. {
  188. public PayOrder() { }
  189.  
  190. public string Paylink { get; private set; }
  191. }
  192.  
  193. public interface IReadOnlyCell
  194. {
  195. public Good Good { get; }
  196. public int CountGoods { get;}
  197. }
  198.  
  199. public interface IDeliveryWarehouse
  200. {
  201. public bool CanIssue(IReadOnlyCell cell);
  202.  
  203. public void TransferForDelivery(IReadOnlyCell cell);
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment