SHOW:
|
|
- or go back to the newest paste.
1 | using System; | |
2 | using System.Collections.Generic; | |
3 | using System.Linq; | |
4 | using System.Text; | |
5 | using System.Threading.Tasks; | |
6 | ||
7 | namespace Service | |
8 | { | |
9 | class Program | |
10 | { | |
11 | static void Main(string[] args) | |
12 | { | |
13 | ServiceMenu menu = new ServiceMenu(); | |
14 | menu.Run(); | |
15 | } | |
16 | } | |
17 | ||
18 | class ServiceMenu | |
19 | { | |
20 | private AutoService _autoService; | |
21 | private Shop _shop; | |
22 | ||
23 | public ServiceMenu() | |
24 | { | |
25 | _autoService = new AutoService(); | |
26 | _shop = new Shop(); | |
27 | } | |
28 | ||
29 | public void Run() | |
30 | { | |
31 | while(_autoService.IsServiceWork()) | |
32 | { | |
33 | Show(); | |
34 | ||
35 | if(ChooseItem()) | |
36 | { | |
37 | TryCreateNewClient(); | |
38 | _shop.TryRefreshProducts(); | |
39 | } | |
40 | else | |
41 | { | |
42 | return; | |
43 | } | |
44 | ||
45 | Console.ReadKey(); | |
46 | Console.Clear(); | |
47 | } | |
48 | } | |
49 | ||
50 | public void Show() | |
51 | { | |
52 | _autoService.ShowBalance(); | |
53 | Console.WriteLine(); | |
54 | _autoService.ShowWarehouse(); | |
55 | Console.WriteLine(); | |
56 | _autoService.ShowBreakages(); | |
57 | ||
58 | Console.WriteLine("\n[1] - приступить к починке\n" + | |
59 | "[2] - Заказать деталь в магазине\n" + | |
60 | "[3] - выход"); | |
61 | Console.Write("Ввод: "); | |
62 | } | |
63 | ||
64 | private bool ChooseItem() | |
65 | { | |
66 | switch (Console.ReadLine()) | |
67 | { | |
68 | case "1": | |
69 | _autoService.ChooseBrokenDetail(); | |
70 | break; | |
71 | case "2": | |
72 | _autoService.SignContract(_shop); | |
73 | break; | |
74 | case "3": | |
75 | return false; | |
76 | default: | |
77 | Console.WriteLine("Ошибка комманды!"); | |
78 | break; | |
79 | } | |
80 | return true; | |
81 | } | |
82 | ||
83 | private void TryCreateNewClient() | |
84 | { | |
85 | if (_autoService.IsCarRepaired) | |
86 | { | |
87 | _autoService.CreateNewClient(); | |
88 | } | |
89 | } | |
90 | } | |
91 | ||
92 | class AutoService | |
93 | { | |
94 | private DetailWarehouse _detailsWarehouse; | |
95 | private Car _clientCar; | |
96 | private int _fine; | |
97 | ||
98 | public int WorkCost { get; private set; } | |
99 | public int Balance { get; private set; } | |
100 | public bool IsBalanceNegaive | |
101 | { | |
102 | get | |
103 | { | |
104 | return Balance <= 0; | |
105 | } | |
106 | } | |
107 | public bool IsWarehouseEmpty | |
108 | { | |
109 | get | |
110 | { | |
111 | return _detailsWarehouse.IsSpaceDetailsEmpty(); | |
112 | } | |
113 | } | |
114 | public bool IsCarRepaired | |
115 | { | |
116 | get | |
117 | { | |
118 | return _clientCar.IsDetailsBroken() == false; | |
119 | } | |
120 | } | |
121 | ||
122 | public AutoService() | |
123 | { | |
124 | _detailsWarehouse = new DetailWarehouse(); | |
125 | _clientCar = new Car(); | |
126 | Balance = 2500; | |
127 | WorkCost = 5000; | |
128 | _fine = 2500; | |
129 | } | |
130 | ||
131 | public bool IsServiceWork() | |
132 | { | |
133 | if (IsWarehouseEmpty) | |
134 | { | |
135 | Console.WriteLine("Запасов больше нет! Закрываем гараж."); | |
136 | return false; | |
137 | } | |
138 | ||
139 | if(IsBalanceNegaive) | |
140 | { | |
141 | Console.WriteLine("Отрицательный баланс! Закрываем гараж."); | |
142 | return false; | |
143 | } | |
144 | return true; | |
145 | } | |
146 | ||
147 | public void ShowBalance() | |
148 | { | |
149 | Console.WriteLine($"Баланс: {Balance}"); | |
150 | } | |
151 | ||
152 | public void ShowBreakages() | |
153 | { | |
154 | _clientCar.ShowBrokenDetails(); | |
155 | } | |
156 | ||
157 | public void ChooseBrokenDetail() | |
158 | { | |
159 | Console.WriteLine("Выберите деталь, которую вы хотите починить: \n" + | |
160 | "[1] - Двигатель\n" + | |
161 | "[2] - Подвеска\n" + | |
162 | "[3] - Проводка\n" + | |
163 | "[4] - Кузов"); | |
164 | Console.Write("Ввод: "); | |
165 | ||
166 | if (int.TryParse(Console.ReadLine(), out int productNumber) == false | |
167 | && productNumber > _detailsWarehouse.GetMaxPartsNumber()) | |
168 | { | |
169 | Console.WriteLine("Ошибка! Такой детали нет."); | |
170 | return; | |
171 | } | |
172 | ||
173 | --productNumber; | |
174 | DetailType detailType = (DetailType)productNumber; | |
175 | ||
176 | RepairDetail(detailType); | |
177 | } | |
178 | ||
179 | public void SignContract(Shop shop) | |
180 | { | |
181 | if (Balance == 0) | |
182 | { | |
183 | Console.WriteLine("Ошибка! Нет предоплаты"); | |
184 | return; | |
185 | } | |
186 | ||
187 | shop.Show(); | |
188 | ||
189 | if (shop.IsDetailChosen(out DetailType detailType) == false) | |
190 | { | |
191 | return; | |
192 | } | |
193 | ||
194 | Balance -= shop.MakePrepayment(detailType); | |
195 | _detailsWarehouse.AddDetail(shop.BuyProduct(detailType)); | |
196 | } | |
197 | ||
198 | public void CreateNewClient() | |
199 | { | |
200 | _clientCar = new Car(); | |
201 | } | |
202 | ||
203 | public void ShowWarehouse() | |
204 | { | |
205 | _detailsWarehouse.ShowSpaceDetails(); | |
206 | } | |
207 | ||
208 | private void RepairDetail(DetailType detailType) | |
209 | { | |
210 | Detail detail = _clientCar.GetBrokenDetailByType(detailType); | |
211 | ||
212 | if (detail.IsBroken == false) | |
213 | { | |
214 | Console.WriteLine("Деталь в рабочем состоянии!"); | |
215 | _detailsWarehouse.TakeDetail(detail.DetailType); | |
216 | GetFine(); | |
217 | return; | |
218 | } | |
219 | ||
220 | _clientCar.ReplaceDetail(_detailsWarehouse.TakeDetail(detail.DetailType)); | |
221 | GetProfit(_detailsWarehouse.GetPrice(detail.DetailType)); | |
222 | } | |
223 | ||
224 | private void GetFine() | |
225 | { | |
226 | Balance -= _fine; | |
227 | } | |
228 | ||
229 | private void GetProfit(int detailPrice) | |
230 | { | |
231 | Balance += detailPrice + WorkCost; | |
232 | } | |
233 | } | |
234 | ||
235 | class Car | |
236 | { | |
237 | private static Random _random = new Random(); | |
238 | ||
239 | private List<Detail> _details; | |
240 | ||
241 | public Car() | |
242 | { | |
243 | _details = new List<Detail>() | |
244 | { | |
245 | new Detail("Двигатель", DetailType.Engine, TryToBreak()), | |
246 | new Detail("Подвеска", DetailType.Suspension, TryToBreak()), | |
247 | new Detail("Проводка", DetailType.Wiring, TryToBreak()), | |
248 | new Detail("Кузов", DetailType.Body, TryToBreak()) | |
249 | }; | |
250 | ||
251 | CheckCreatedDetails(); | |
252 | } | |
253 | ||
254 | public void ReplaceDetail(Detail newDetail) | |
255 | { | |
256 | for (int i = 0; i < _details.Count; i++) | |
257 | { | |
258 | if(_details[i].DetailType == newDetail.DetailType) | |
259 | { | |
260 | _details[i] = newDetail; | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
265 | public Detail GetBrokenDetailByType(DetailType detailType) | |
266 | { | |
267 | foreach (var brokenDetail in _details) | |
268 | { | |
269 | if (brokenDetail.DetailType == detailType) | |
270 | { | |
271 | return brokenDetail; | |
272 | } | |
273 | } | |
274 | ||
275 | throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе!"); | |
276 | } | |
277 | ||
278 | public bool IsDetailsBroken() | |
279 | { | |
280 | foreach (var detail in _details) | |
281 | { | |
282 | if (detail.IsBroken) | |
283 | return true; | |
284 | } | |
285 | return false; | |
286 | } | |
287 | ||
288 | public void ShowBrokenDetails() | |
289 | { | |
290 | foreach (var brokenDetail in _details) | |
291 | { | |
292 | if (brokenDetail.IsBroken) | |
293 | { | |
294 | Console.WriteLine($"{brokenDetail.Name} в критическом состоянии!"); | |
295 | } | |
296 | } | |
297 | } | |
298 | ||
299 | private bool TryToBreak() | |
300 | { | |
301 | return Convert.ToBoolean(_random.Next(0, 2)); | |
302 | } | |
303 | ||
304 | private void CheckCreatedDetails() | |
305 | { | |
306 | List<Detail> details = new List<Detail>(); | |
307 | ||
308 | foreach (var detail in _details) | |
309 | { | |
310 | if(detail.IsBroken == false) | |
311 | { | |
312 | details.Add(detail); | |
313 | } | |
314 | else | |
315 | { | |
316 | return; | |
317 | } | |
318 | } | |
319 | details[_random.Next(0, details.Count)].BreakDetail(); | |
320 | } | |
321 | } | |
322 | ||
323 | class Detail | |
324 | { | |
325 | public string Name { get; protected set; } | |
326 | public DetailType DetailType { get; protected set; } | |
327 | public bool IsBroken { get; protected set; } | |
328 | ||
329 | public Detail(string name, DetailType detailType, bool isBroken) | |
330 | { | |
331 | Name = name; | |
332 | DetailType = detailType; | |
333 | IsBroken = isBroken; | |
334 | } | |
335 | ||
336 | public void BreakDetail() | |
337 | { | |
338 | IsBroken = true; | |
339 | } | |
340 | } | |
341 | ||
342 | enum DetailType | |
343 | { | |
344 | Engine, | |
345 | Suspension, | |
346 | Wiring, | |
347 | Body | |
348 | } | |
349 | ||
350 | class SpareDetail : Detail | |
351 | { | |
352 | public int Price { get; private set; } | |
353 | ||
354 | public SpareDetail(string name, DetailType detailType, bool isBroken, int price) : base(name, detailType, isBroken) | |
355 | { | |
356 | Name = name; | |
357 | IsBroken = false; | |
358 | Price = price; | |
359 | } | |
360 | } | |
361 | ||
362 | abstract class Warehouse | |
363 | { | |
364 | protected Dictionary<SpareDetail, int> _spareDetails; | |
365 | ||
366 | public void ShowSpaceDetails() | |
367 | { | |
368 | foreach (var spareDetail in _spareDetails) | |
369 | { | |
370 | Console.WriteLine($"{spareDetail.Key.Name } - {spareDetail.Value}"); | |
371 | } | |
372 | } | |
373 | ||
374 | public Detail TakeDetail(DetailType detailType) | |
375 | { | |
376 | foreach (var requiredDetail in _spareDetails) | |
377 | { | |
378 | if (requiredDetail.Key.DetailType == detailType) | |
379 | { | |
380 | _spareDetails[requiredDetail.Key] -= 1; | |
381 | return requiredDetail.Key; | |
382 | } | |
383 | } | |
384 | ||
385 | throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе!"); | |
386 | } | |
387 | ||
388 | public int GetPrice(DetailType detailType) | |
389 | { | |
390 | foreach (var requiredDetail in _spareDetails) | |
391 | { | |
392 | if (requiredDetail.Key.DetailType == detailType) | |
393 | { | |
394 | return requiredDetail.Key.Price; | |
395 | } | |
396 | } | |
397 | throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе! Цену получить нельзя!"); | |
398 | } | |
399 | ||
400 | public bool IsSpaceDetailsEmpty() | |
401 | { | |
402 | foreach (var spareDetail in _spareDetails) | |
403 | { | |
404 | if (spareDetail.Value != 0) | |
405 | { | |
406 | return false; | |
407 | } | |
408 | } | |
409 | return true; | |
410 | } | |
411 | } | |
412 | ||
413 | class DetailWarehouse : Warehouse | |
414 | { | |
415 | public DetailWarehouse() | |
416 | { | |
417 | Detail Engine = new SpareDetail("Двигатель", DetailType.Engine, true, 20000); | |
418 | SpareDetail spaceEngine = Engine as SpareDetail; | |
419 | ||
420 | Detail Suspension = new SpareDetail("Подвеска", DetailType.Suspension, true, 15000); | |
421 | SpareDetail spaceSuspencion = Suspension as SpareDetail; | |
422 | ||
423 | Detail Wiring = new SpareDetail("Проводка", DetailType.Wiring, true, 10000); | |
424 | SpareDetail spaceWiring = Wiring as SpareDetail; | |
425 | ||
426 | Detail Body = new SpareDetail("Кузов", DetailType.Body, true, 5000); | |
427 | SpareDetail spaceBody = Body as SpareDetail; | |
428 | ||
429 | _spareDetails = new Dictionary<SpareDetail, int> | |
430 | { | |
431 | { spaceEngine, 5 }, | |
432 | { spaceSuspencion, 5 }, | |
433 | { spaceWiring, 5 }, | |
434 | { spaceBody, 5 } | |
435 | }; | |
436 | } | |
437 | ||
438 | public void AddDetail(Detail detail) | |
439 | { | |
440 | foreach (var spareDetail in _spareDetails.ToArray()) | |
441 | { | |
442 | if(spareDetail.Key.DetailType == detail.DetailType) | |
443 | { | |
444 | _spareDetails[spareDetail.Key] += 1; | |
445 | return; | |
446 | } | |
447 | } | |
448 | } | |
449 | ||
450 | public int GetMaxPartsNumber() | |
451 | { | |
452 | return _spareDetails.Keys.Count; | |
453 | } | |
454 | } | |
455 | ||
456 | class Shop : Warehouse | |
457 | { | |
458 | private int _maxProducts; | |
459 | ||
460 | public int RefreshCounter { get; private set; } | |
461 | ||
462 | public Shop() | |
463 | { | |
464 | _maxProducts = 7; | |
465 | RefreshCounter = 5; | |
466 | ||
467 | Detail Engine = new SpareDetail("Двигатель", DetailType.Engine, true, 10000); | |
468 | SpareDetail spaceEngine = Engine as SpareDetail; | |
469 | ||
470 | Detail Suspension = new SpareDetail("Подвеска", DetailType.Suspension, true, 7000); | |
471 | SpareDetail spaceSuspencion = Suspension as SpareDetail; | |
472 | ||
473 | Detail Wiring = new SpareDetail("Проводка", DetailType.Wiring, true, 5000); | |
474 | SpareDetail spaceWiring = Wiring as SpareDetail; | |
475 | ||
476 | Detail Body = new SpareDetail("Кузов", DetailType.Body, true, 2500); | |
477 | SpareDetail spaceBody = Body as SpareDetail; | |
478 | ||
479 | _spareDetails = new Dictionary<SpareDetail, int> | |
480 | { | |
481 | { spaceEngine, 7 }, | |
482 | { spaceSuspencion, 7 }, | |
483 | { spaceWiring, 7 }, | |
484 | { spaceBody, 7 } | |
485 | }; | |
486 | } | |
487 | ||
488 | public void Show() | |
489 | { | |
490 | Console.WriteLine("\nКакой товар вы хотите купить?\n" + | |
491 | "[1] - Двигатель\n" + | |
492 | "[2] - Подвеска\n" + | |
493 | "[3] - Проводка\n" + | |
494 | "[4] - Кузов\n" + | |
495 | "[exit] - Выйти из каталога"); | |
496 | Console.Write("Ввод: "); | |
497 | } | |
498 | ||
499 | public bool IsDetailChosen(out DetailType detailType) | |
500 | { | |
501 | detailType = 0; | |
502 | ||
503 | string userInput = Console.ReadLine(); | |
504 | ||
505 | if(userInput == "exit") | |
506 | { | |
507 | return false; | |
508 | } | |
509 | ||
510 | if (int.TryParse(userInput, out int numberProduct) && numberProduct <= _maxProducts) | |
511 | { | |
512 | --numberProduct; | |
513 | detailType = (DetailType)numberProduct; | |
514 | return true; | |
515 | } | |
516 | return false; | |
517 | } | |
518 | ||
519 | public int MakePrepayment(DetailType detailType) | |
520 | { | |
521 | return GetPrice(detailType); | |
522 | } | |
523 | ||
524 | public Detail BuyProduct(DetailType detailType) | |
525 | { | |
526 | return TakeDetail(detailType); | |
527 | } | |
528 | ||
529 | public void TryRefreshProducts() | |
530 | { | |
531 | if (RefreshCounter == 0) | |
532 | { | |
533 | UpdateProducts(); | |
534 | RefreshCounter = 6; | |
535 | } | |
536 | RefreshCounter -= 1; | |
537 | } | |
538 | ||
539 | private void UpdateProducts() | |
540 | { | |
541 | Dictionary<SpareDetail, int> tempList = new Dictionary<SpareDetail, int>(_spareDetails); | |
542 | ||
543 | foreach (var product in tempList) | |
544 | { | |
545 | if (product.Value != _maxProducts) | |
546 | { | |
547 | _spareDetails[product.Key] = _maxProducts; | |
548 | } | |
549 | } | |
550 | } | |
551 | } | |
552 | } |