-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson46.cs
More file actions
260 lines (198 loc) · 5.56 KB
/
Lesson46.cs
File metadata and controls
260 lines (198 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Collections.Generic;
namespace Tired
{
class Program
{
static void Main(string[] args)
{
Kassa kassa = new Kassa();
UserInputMenu menu = new UserInputMenu(kassa);
menu.Update();
}
}
public class UserInputMenu
{
private const int QueueSleepTime = 2000;
private const string WordToExit = "выход";
private const string WordToStart = "начать обслуживание";
private string _wordToRead;
private Kassa _kassa;
public UserInputMenu(Kassa kassa)
{
_kassa = kassa;
}
public void Update()
{
while (_kassa != null && _wordToRead != WordToExit)
{
ShowMenu();
_wordToRead = Console.ReadLine();
Console.WriteLine();
switch (_wordToRead)
{
case WordToExit:
break;
case WordToStart:
AcceptClientCart();
break;
}
}
}
private void AcceptClientCart()
{
while (_kassa.ClientCount != 0)
{
Console.WriteLine("\n----Обслуживание клиента----");
Console.WriteLine("Итоговая сумма товаров клинта:"+_kassa.GetFirstClient().GetShoppigCart().GetCommonPrice()+" Р");
Console.WriteLine("Количество товаров в корзине клиента:" + _kassa.GetFirstClient().GetShoppigCart().ItemAmount);
Console.WriteLine("Счет клиента:" + _kassa.GetFirstClient().CurrentMoney + " Р");
Console.WriteLine("----Обслуживание клиента----");
if (_kassa.TryPayCart() == true)
{
Console.WriteLine("Клиент оплатил корзину товаров!");
}
else
{
Console.WriteLine("У клиента недостаточно средств на покупку товаров!\nСлучайный предмет был возвращен в магазин.");
_kassa.RemoveRandomItem();
}
Console.WriteLine();
System.Threading.Thread.Sleep(QueueSleepTime);
}
Console.WriteLine("Клиенты обслужены!");
}
private void ShowMenu()
{
Console.WriteLine("Система администрирования супермаркетом");
Console.WriteLine("Введите <" + WordToExit + ">, чтобы выйти из программы.");
Console.WriteLine("Введите <" + WordToStart + ">, чтобы начать обслуживание клиентов.");
Console.WriteLine("Клиентов в очереди:"+_kassa.ClientCount);
}
}
public class Kassa
{
private Queue<Client> _clients = new Queue<Client>();
public int ClientCount
{
get { return _clients.Count; }
}
public Kassa()
{
for(int i = 0; i < 8; i++)
{
_clients.Enqueue(new Client());
}
}
public bool TryPayCart()
{
Client client = _clients.Peek();
if( client.TryPay() == true)
{
_clients.Dequeue();
return true;
}
return false;
}
public void RemoveRandomItem() => _clients.Peek().RemoveRandomItemFromCart();
public Client GetFirstClient() => _clients.Peek();
}
public class Client
{
private ShoppingCart _shoppingCart = new ShoppingCart();
public int CurrentMoney { get; private set; } = 0;
public Client()
{
float priceFactor = 0.7f;
_shoppingCart.AddItem(ItemGiver.GiveRandomItems());
CurrentMoney = (int)(_shoppingCart.GetCommonPrice() * priceFactor);
}
public bool TryPay()
{
if ( CurrentMoney >= _shoppingCart.GetCommonPrice() )
{
CurrentMoney -= _shoppingCart.GetCommonPrice();
_shoppingCart.Clear();
return true;
}
return false;
}
public void RemoveRandomItemFromCart()
{
Item item = _shoppingCart.RemoveRandomItem();
CurrentMoney += item.Price;
}
public ShoppingCart GetShoppigCart() => _shoppingCart;
}
public class ShoppingCart
{
private List<Item> _itemList = new List<Item>();
public int ItemAmount
{
get
{
return _itemList.Count;
}
}
public void AddItem(Item item) => _itemList.Add(item);
public void AddItem(Item[] items) => _itemList.AddRange(items);
public void Clear() => _itemList.Clear();
public Item RemoveRandomItem()
{
if(_itemList.Count == 0)
{
return default;
}
Item randomItem = _itemList[new Random().Next(0, _itemList.Count)];
_itemList.Remove(randomItem);
return randomItem;
}
public int GetCommonPrice()
{
int price = 0;
foreach (Item item in _itemList)
{
price += item.Price;
}
return price;
}
}
public struct Item
{
public string Name { get; private set; }
public int Price { get; private set; }
public Item(string name, int price)
{
Name = name;
Price = price;
}
}
public static class ItemGiver
{
private static Item[] _items = {
new Item("Хлеб",35),
new Item("Молоко",84),
new Item("Мясо",321),
new Item("Бутылка воды",39),
new Item("Шоколад",110),
new Item("Чай",79),
new Item("Фисташки",240),
new Item("Кола",95),
new Item("Пицца",278),
new Item("Рис",67),
};
public static Item GiveRandomItem() => _items[new Random().Next(0, _items.Length)];
public static Item[] GiveRandomItems()
{
int randomMin = 5;
int randomMax = 15;
int randomLength = new Random().Next(randomMin, randomMax);
Item[] items = new Item[randomLength];
for (int i = 0; i < randomLength; i++)
{
items[i] = GiveRandomItem();
}
return items;
}
}
}