View difference between Paste ID: EJR4fTSe and C26sWULh
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Collections.Generic;
3-
namespace C_sharp_Light
3+
using System.Linq;
4
5
namespace CSharpLesson
6
{
7-
        static void Main(string[] args)
7+
8
    {
9-
            
9+
        public static void Main()
10
        {
11
            new CarService().Work();
12-
}
12+
13
    }
14
15
    public static class RandomStatic
16
    {
17
        static private Random _rand = new Random();
18
        static public int GetNext(int min, int max)
19
        {
20
            return _rand.Next(min, max);
21
        }
22
    }
23
    public static class Messager
24
    {
25
        static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay)
26
        {
27
            ConsoleColor defaultColor = Console.ForegroundColor;
28
            Console.ForegroundColor = color;
29
            Console.WriteLine(message);
30
            Console.ForegroundColor = defaultColor;
31
            if (delay)
32
                Console.ReadKey();
33
        }
34
    }
35
36
    class CarService
37
    {
38
        private int _money = 0;
39
        private List<MechanicalPart> _storageOfPart = new List<MechanicalPart>();
40
        private Client _client;
41
42
        public CarService()
43
        {
44
            for (int i = 0; i < 20; i++)
45
            {
46
                _storageOfPart.Add(new MechanicalPart(false));
47
            }
48
        }
49
        public void Work()
50
        {
51
            bool isOpen = true;
52
            while (isOpen)
53
            {
54
                ShowInfo();
55
56
                switch (Console.ReadKey(true).Key)
57
                {
58
                    case ConsoleKey.D1:
59
                        RepairCarClient();
60
                        break;
61
                    case ConsoleKey.D2:
62
                        ShowStorage();
63
                        Console.ReadKey();
64
                        break;
65
                    case ConsoleKey.D3:
66
                        DenialClinet();
67
                        break;
68
                    case ConsoleKey.Escape:
69
                        isOpen = false;
70
                        break;
71
                }
72
73
                TryToFinisCar();
74
            }
75
        }
76
        private void ShowInfo()
77
        {
78
            Console.Clear();
79
80
            GetClient();
81
82
            _client.Car.ShowBrokenPart();
83
            TryFindNeedfulPart();
84
            Console.WriteLine($"Деньги автосервиса {_money}");
85
86
            Console.WriteLine("\n1 - Починить машину клиента\n2 - Просмотреть склад\n3 - Отказать клиент\nEsc - выйти");
87
        }
88
        private void GetClient()
89
        {
90
            if (_client == null)
91
            {
92
                Messager.ShowMessageWithColor("У вас новый клиет", ConsoleColor.Green, false);
93
                _client = new Client();
94
            }
95
            else
96
            {
97
                Messager.ShowMessageWithColor("У вас все ещё старый клиет", ConsoleColor.Yellow, false);
98
            }
99
        }
100
        private void TryFindNeedfulPart()
101
        {
102
            List<MechanicalPart> sortList = _storageOfPart.Where(part => part.Name == _client.Car.GetTypePart()).ToList<MechanicalPart>();
103
            sortList = sortList.OrderBy(part => part.Cost).ToList<MechanicalPart>();
104
            if (sortList.Count > 0)
105
                Messager.ShowMessageWithColor($"Минимальная стоимость починки детали составляет {sortList[0].Cost} + 350 работае механика", ConsoleColor.White, false);
106
            else
107
                Messager.ShowMessageWithColor($"У вас нет необходимой детали, вам придется отказть клиенту", ConsoleColor.White, false);
108
        }
109
        private void RepairCarClient()
110
        {
111
            Console.Clear();
112
            _client.Car.ShowBrokenPart();
113
            ShowStorage();
114
115
            Console.Write("Введите индекс детали cо склада: ");
116
            int indexWorkingPart = Convert.ToInt32(Console.ReadLine());
117
118
            PositionCheck newPosition = _client.Car.ReplacePartWithReport(_storageOfPart[indexWorkingPart]);
119
            _client.Check.AddNewPosition(newPosition);
120
            if (newPosition.IsProfit)
121
            {
122
                _client.Check.AddNewPosition(new PositionCheck("Работа автомеханика", 350, true));
123
                _storageOfPart.RemoveAt(indexWorkingPart);
124
            }
125
        }
126
        private void ShowStorage()
127
        {
128
            int i = 0;
129
            foreach (var part in _storageOfPart)
130
            {
131
                Console.WriteLine($"{i}: {part.GetInfo()}");
132
                i++;
133
            }
134
        }
135
        private void TryToFinisCar()
136
        {
137
            if (_client != null)
138
            {
139
                if (_client.Car.CheckRepair())
140
                {
141
                    Console.Clear();
142
                    _client.Check.ShowResult();
143
                    _money += _client.GetMoney();
144
                    _client = null;
145
                }
146
            }
147
        }
148
        private void DenialClinet()
149
        {
150
            int mulct = 500;
151
            Messager.ShowMessageWithColor($"Вы отказали клиенту и вынуждены заплатить штраф в размере {mulct}", ConsoleColor.Red, true);
152
            _money -= mulct;
153
            _client = null;
154
        }
155
    }
156
    class Client
157
    {
158
        public Car Car { get; private set; } = new Car();
159
        public Check Check { get; private set; } = new Check();
160
        public int GetMoney()
161
        {
162
            return Check.GetCostWork();
163
        }
164
    }
165
    class Car
166
    {
167
        private MechanicalPart _brokenPart;
168
        public Car()
169
        {
170
            _brokenPart = new MechanicalPart(true);
171
        }
172
        public MechanicalPart.TypePart GetTypePart()
173
        {
174
            return _brokenPart.Name;
175
        }
176
        public void ShowBrokenPart()
177
        {
178
            Messager.ShowMessageWithColor($"Сломанная деталь - {_brokenPart.Name}", ConsoleColor.Red, false); ;
179
        }
180
        public PositionCheck ReplacePartWithReport(MechanicalPart workingPart)
181
        {
182
            if (ComparePart(workingPart))
183
            {
184
                _brokenPart = workingPart;
185
                Messager.ShowMessageWithColor("\nВы успешно заменили деталь. Деталь установлена, работа засена в чек", ConsoleColor.White, true);
186
                return new PositionCheck($"Замена: {workingPart.Name}", workingPart.Cost, true);
187
            }
188
            else
189
            {
190
                Messager.ShowMessageWithColor("\nЭто неправильная деталь. Она не установлена и вы получили штраф", ConsoleColor.White, true);
191
                return new PositionCheck($"Штраф - Неправильная замена: {_brokenPart.Name} ", 500, false);
192
            }
193
        }
194
        public bool CheckRepair()
195
        {
196
            return !_brokenPart.IsBroken;
197
        }
198
        private bool ComparePart(MechanicalPart newPart)
199
        {
200
            return _brokenPart.Name == newPart.Name;
201
        }
202
    }
203
    class Check
204
    {
205
        private List<PositionCheck> _positionChecks = new List<PositionCheck>();
206
        public void AddNewPosition(PositionCheck newPosition)
207
        {
208
            _positionChecks.Add(newPosition);
209
        }
210
        public void ShowResult()
211
        {
212
            int fullCost = 0;
213
            Messager.ShowMessageWithColor("===Чек из автосервиса===", ConsoleColor.White, false);
214
            foreach (var position in _positionChecks)
215
            {
216
                if (position.IsProfit)
217
                {
218
                    fullCost += position.CostWork;
219
                    Messager.ShowMessageWithColor(position.GetInfo(), ConsoleColor.Green, false);
220
                }
221
                else
222
                {
223
                    fullCost -= position.CostWork;
224
                    Messager.ShowMessageWithColor(position.GetInfo(), ConsoleColor.Red, false);
225
                }
226
            }
227
            Messager.ShowMessageWithColor($"Итоговая сумма = {fullCost}", ConsoleColor.Yellow, true);
228
        }
229
        public int GetCostWork()
230
        {
231
            int fullCost = 0;
232
            foreach (var position in _positionChecks)
233
            {
234
                if (position.IsProfit)
235
                    fullCost += position.CostWork;
236
                else
237
                    fullCost -= position.CostWork;
238
            }
239
240
            if (fullCost > 0)
241
                Messager.ShowMessageWithColor($"\nВы получили {fullCost} за работу", ConsoleColor.Green, true);
242
            else
243
                Messager.ShowMessageWithColor($"\nВы плохо сделали свою работу и вынуждены отдать свои деньги в размере {fullCost}", ConsoleColor.Red, true);
244
245
            return fullCost;
246
        }
247
    }
248
    class PositionCheck
249
    {
250
        public string NameWork { get; private set; }
251
        public int CostWork { get; private set; }
252
        public bool IsProfit { get; private set; }
253
        public PositionCheck(string nameWork, int costWork, bool isProfit)
254
        {
255
            NameWork = nameWork;
256
            CostWork = costWork;
257
            IsProfit = isProfit;
258
        }
259
        public string GetInfo()
260
        {
261
            string result = NameWork;
262
            for (int i = 0; i < 30 - result.Length; i++)
263
            {
264
                result += " ";
265
            }
266
            result += $"{CostWork}$";
267
            return result;
268
        }
269
    }
270
    class MechanicalPart
271
    {
272
        public enum TypePart
273
        {
274
            engine,
275
            cardan,
276
            steeringWheel,
277
            headLamp,
278
            wheel,
279
            carСandles
280
        }
281
        public TypePart Name { get; private set; }
282
        public int Cost { get; private set; }
283
        public bool IsBroken { get; private set; }
284
        public MechanicalPart(bool isBroken)
285
        {
286
            switch (RandomStatic.GetNext(0, 6))
287
            {
288
                case 0:
289
                    Name = TypePart.engine;
290
                    break;
291
                case 1:
292
                    Name = TypePart.cardan;
293
                    break;
294
                case 2:
295
                    Name = TypePart.steeringWheel;
296
                    break;
297
                case 3:
298
                    Name = TypePart.headLamp;
299
                    break;
300
                case 4:
301
                    Name = TypePart.wheel;
302
                    break;
303
                case 5:
304
                    Name = TypePart.carСandles;
305
                    break;
306
            }
307
            Cost = RandomStatic.GetNext(50, 500);
308
            IsBroken = isBroken;
309
        }
310
        public string GetInfo()
311
        {
312
            return $"{Name} - {Cost}$";
313
        }
314
    }
315
}
316