View difference between Paste ID: rUF8gS35 and Uiz2BiRN
SHOW: | | - or go back to the newest paste.
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;
26+
    private readonly Dictionary<Good, int> _goods;
27
28
    public Warehouse()
29
    {
30-
        _cells = new List<Cell>();
30+
        GoodEqualityCompare goodEqualityCompare = new();
31
        _goods = new(goodEqualityCompare);
32
    }
33
34
    public void Delive(Good good, int countGoods)
35
    {
36
        good = good ?? throw new ArgumentNullException();
37
38
        if (countGoods < 0)
39
            throw new ArgumentOutOfRangeException();
40-
        Cell newCell = new Cell(good, countGoods);
40+
41
        if (_goods.ContainsKey(good))
42-
        int cellIndex = _cells.FindIndex(cell => cell.Good == good);
42+
            _goods[good] += countGoods;
43
        else
44-
        if (cellIndex < 0)
44+
            _goods.Add(good, countGoods);
45-
            _cells.Add(newCell);
45+
46
47-
            _cells[cellIndex].Merge(newCell);
47+
    public bool CanIssue(Good good, int countGoods)
48
    {
49
        good = good ?? throw new ArgumentNullException();
50-
    public bool CanIssue(IReadOnlyCell issuedCell)
50+
51
        if (countGoods < 0)
52-
        issuedCell = issuedCell ?? throw new ArgumentNullException();
52+
53
54-
        int cellIndex = _cells.FindIndex(cell => cell.Good == issuedCell.Good);
54+
        if (_goods.TryGetValue(good, out int count))
55
            return count >= countGoods;
56-
        return (cellIndex > -1 && _cells[cellIndex].CountGoods >= issuedCell.CountGoods) == false;
56+
57
            return false;
58
    }
59-
    public void TransferForDelivery(IReadOnlyCell issuedCell)
59+
60
    public void TransferForDelivery(Good good, int countGoods)
61-
        issuedCell = issuedCell ?? throw new ArgumentNullException();
61+
62
        good = good ?? throw new ArgumentNullException();
63-
        int cellIndex = _cells.FindIndex(cell => cell.Good == issuedCell.Good);
63+
64
        if (countGoods < 0)
65-
        if (cellIndex > -1 && _cells[cellIndex].CountGoods >= issuedCell.CountGoods)
65+
66-
            _cells[cellIndex].Issue(issuedCell);
66+
67
        if (_goods.ContainsKey(good) && _goods[good] >= countGoods)
68
            _goods[good] -= countGoods;
69
        else
70
            throw new ArgumentOutOfRangeException();
71
    }
72-
    private Warehouse _warehouse;
72+
73
74-
    public Shop(Warehouse warehouse) 
74+
75
{
76-
        if (_warehouse == null)
76+
    private readonly Warehouse _warehouse;
77
78
    public Shop(Warehouse warehouse) => _warehouse = warehouse ?? throw new ArgumentNullException();
79-
        _warehouse = warehouse;
79+
    
80
    public Cart Cart()
81
    {
82
        return new Cart(_warehouse);
83
    }
84
}
85
86
public class Cart
87
{
88
    private readonly Dictionary<Good, int> _goods;
89
    private readonly IDeliveryWarehouse _warehouse;
90-
    private List<Cell> _cells;
90+
91-
    private IDeliveryWarehouse _warehouse;
91+
92
    {
93
        _warehouse = warehouse ?? throw new ArgumentNullException();
94
95
        GoodEqualityCompare goodEqualityCompare = new();
96-
        
96+
        _goods = new(goodEqualityCompare);
97-
        _cells = new List<Cell>();
97+
98
99
    public void Add(Good good, int countGoods)
100-
    public IReadOnlyList<IReadOnlyCell> Cells => _cells;
100+
101
        good = good ?? throw new ArgumentNullException();
102
103
        if (countGoods < 0)
104-
        if (good == null)
104+
105-
            throw new ArgumentNullException();        
105+
106
        if (_goods.ContainsKey(good))
107-
        if (countGoods < 0 )
107+
            _goods[good] += countGoods;
108
        else
109
            _goods.Add(good, countGoods);
110-
        Cell newCell = new Cell(good, countGoods);
110+
111
112-
        if (_warehouse.CanIssue(newCell) == false)
112+
113
    {
114
        foreach(var goods in _goods)
115-
        int cellIndex = _cells.FindIndex(cell => cell.Good == good);
115+
        {
116
            _warehouse.TransferForDelivery(goods.Key, goods.Value);
117-
        if (cellIndex < 0)
117+
        }
118-
            _cells.Add(newCell);
118+
119
        _goods.Clear();
120-
            _cells[cellIndex].Merge(newCell);
120+
121
    }
122
}
123
124
public class Good
125-
        foreach (Cell cell in _cells)
125+
126-
            _warehouse.TransferForDelivery(cell);
126+
127
    {
128-
        _cells.Clear();
128+
        if (string.IsNullOrEmpty(name))
129
            throw new ArgumentNullException();
130
131
        Name = name;
132
    }
133-
public class Cell : IReadOnlyCell
133+
134
    public string Name { get; }
135-
    public Cell(Good good, int countGoods)
135+
136-
    {        
136+
137-
        if (good == null)
137+
138
{
139
    public PayOrder() { }
140
141
    public string Paylink { get; }
142
}
143-
        Good = good;
143+
144-
        CountGoods = countGoods;
144+
145
{
146
    public bool CanIssue(Good good, int countGoods);
147-
    public Good Good {get; private set;}
147+
148-
    public int CountGoods { get; private set;}
148+
    public void TransferForDelivery(Good good, int countGoods);
149
}
150-
    public void Merge(Cell newCell)
150+
151
public class GoodEqualityCompare : IEqualityComparer<Good>
152-
        if (newCell.Good != Good)
152+
153-
            throw new InvalidOperationException();
153+
    public bool Equals(Good? good1, Good? good2)
154
    {
155-
        if (newCell.CountGoods < 0)
155+
        if (ReferenceEquals(good1, good2)) 
156
            return true;
157
158-
        CountGoods += newCell.CountGoods;
158+
        if (good1 == null || good2 == null)
159
            return false;
160
161-
    public void Issue(IReadOnlyCell newCell)
161+
        return good1.Name == good2.Name;
162
    }
163-
        if(newCell.Good != Good) 
163+
164-
            throw new InvalidOperationException();
164+
    public int GetHashCode(Good good) => good.Name.GetHashCode();
165
}