Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Good iPhone12 = new Good("IPhone 12");
- Good iPhone11 = new Good("IPhone 11");
- Warehouse warehouse = new Warehouse();
- Shop shop = new Shop(warehouse);
- warehouse.Delive(iPhone12, 10);
- warehouse.Delive(iPhone11, 1);
- //Вывод всех товаров на складе с их остатком
- Cart cart = shop.Cart();
- cart.Add(iPhone12, 4);
- cart.Add(iPhone11, 3); //при такой ситуации возникает ошибка так, как нет нужного количества товара на складе
- //Вывод всех товаров в корзине
- Console.WriteLine(cart.Order().Paylink);
- cart.Add(iPhone12, 9); //Ошибка, после заказа со склада убираются заказанные товары
- public class Warehouse : IDeliveryWarehouse
- {
- private readonly List<Cell> _cells;
- public Warehouse()
- {
- _cells = new List<Cell>();
- }
- public void Delive(Good good, int countGoods)
- {
- good = good ?? throw new ArgumentNullException();
- if (countGoods < 0)
- throw new ArgumentOutOfRangeException();
- Cell newCell = new Cell(good, countGoods);
- int cellIndex = _cells.FindIndex(cell => cell.Good == good);
- if (cellIndex < 0)
- _cells.Add(newCell);
- else
- _cells[cellIndex].Merge(newCell);
- }
- public bool CanIssue(IReadOnlyCell issuedCell)
- {
- issuedCell = issuedCell ?? throw new ArgumentNullException();
- int cellIndex = _cells.FindIndex(cell => cell.Good == issuedCell.Good);
- return (cellIndex > -1 && _cells[cellIndex].CountGoods >= issuedCell.CountGoods) == false;
- }
- public void TransferForDelivery(IReadOnlyCell issuedCell)
- {
- issuedCell = issuedCell ?? throw new ArgumentNullException();
- int cellIndex = _cells.FindIndex(cell => cell.Good == issuedCell.Good);
- if (cellIndex > -1 && _cells[cellIndex].CountGoods >= issuedCell.CountGoods)
- _cells[cellIndex].Issue(issuedCell);
- }
- }
- public class Shop
- {
- private Warehouse _warehouse;
- public Shop(Warehouse warehouse)
- {
- if (_warehouse == null)
- throw new ArgumentNullException();
- _warehouse = warehouse;
- }
- public Cart Cart()
- {
- return new Cart(_warehouse);
- }
- }
- public class Cart
- {
- private List<Cell> _cells;
- private IDeliveryWarehouse _warehouse;
- public Cart(IDeliveryWarehouse warehouse)
- {
- _warehouse = warehouse ?? throw new ArgumentNullException();
- _cells = new List<Cell>();
- }
- public IReadOnlyList<IReadOnlyCell> Cells => _cells;
- public void Add(Good good, int countGoods)
- {
- if (good == null)
- throw new ArgumentNullException();
- if (countGoods < 0 )
- throw new ArgumentOutOfRangeException();
- Cell newCell = new Cell(good, countGoods);
- if (_warehouse.CanIssue(newCell) == false)
- throw new ArgumentOutOfRangeException();
- int cellIndex = _cells.FindIndex(cell => cell.Good == good);
- if (cellIndex < 0)
- _cells.Add(newCell);
- else
- _cells[cellIndex].Merge(newCell);
- }
- public PayOrder Order()
- {
- foreach (Cell cell in _cells)
- _warehouse.TransferForDelivery(cell);
- _cells.Clear();
- return new PayOrder();
- }
- }
- public class Cell : IReadOnlyCell
- {
- public Cell(Good good, int countGoods)
- {
- if (good == null)
- throw new ArgumentNullException();
- if (countGoods < 0)
- throw new ArgumentOutOfRangeException();
- Good = good;
- CountGoods = countGoods;
- }
- public Good Good {get; private set;}
- public int CountGoods { get; private set;}
- public void Merge(Cell newCell)
- {
- if (newCell.Good != Good)
- throw new InvalidOperationException();
- if (newCell.CountGoods < 0)
- throw new ArgumentOutOfRangeException();
- CountGoods += newCell.CountGoods;
- }
- public void Issue(IReadOnlyCell newCell)
- {
- if(newCell.Good != Good)
- throw new InvalidOperationException();
- if (newCell.CountGoods < 0 || newCell.CountGoods > CountGoods)
- throw new ArgumentOutOfRangeException();
- CountGoods -= newCell.CountGoods;
- }
- }
- public class Good
- {
- public Good(string name)
- {
- if (name == null)
- throw new ArgumentNullException();
- Name = name;
- }
- public string Name { get; private set; }
- }
- public class PayOrder
- {
- public PayOrder() { }
- public string Paylink { get; private set; }
- }
- public interface IReadOnlyCell
- {
- public Good Good { get; }
- public int CountGoods { get;}
- }
- public interface IDeliveryWarehouse
- {
- public bool CanIssue(IReadOnlyCell cell);
- public void TransferForDelivery(IReadOnlyCell cell);
- }
Advertisement
Add Comment
Please, Sign In to add comment