Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace CSharpLesson
- {
- class Program
- {
- public static void Main()
- {
- new CarService().Work();
- }
- }
- public static class RandomStatic
- {
- static private Random _rand = new Random();
- static public int GetNext(int min, int max)
- {
- return _rand.Next(min, max);
- }
- }
- public static class Messager
- {
- static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay)
- {
- ConsoleColor defaultColor = Console.ForegroundColor;
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ForegroundColor = defaultColor;
- if (delay)
- Console.ReadKey();
- }
- }
- class CarService
- {
- private int _money = 0;
- private List<MechanicalPart> _storageOfPart = new List<MechanicalPart>();
- private Client _client;
- public CarService()
- {
- for (int i = 0; i < 20; i++)
- {
- _storageOfPart.Add(new MechanicalPart(false));
- }
- }
- public void Work()
- {
- bool isOpen = true;
- while (isOpen)
- {
- ShowInfo();
- switch (Console.ReadKey(true).Key)
- {
- case ConsoleKey.D1:
- RepairCarClient();
- break;
- case ConsoleKey.D2:
- ShowStorage();
- Console.ReadKey();
- break;
- case ConsoleKey.D3:
- DenialClinet();
- break;
- case ConsoleKey.Escape:
- isOpen = false;
- break;
- }
- TryToFinisCar();
- }
- }
- private void ShowInfo()
- {
- Console.Clear();
- GetClient();
- _client.Car.ShowBrokenPart();
- TryFindNeedfulPart();
- Console.WriteLine($"Деньги автосервиса {_money}");
- Console.WriteLine("\n1 - Починить машину клиента\n2 - Просмотреть склад\n3 - Отказать клиент\nEsc - выйти");
- }
- private void GetClient()
- {
- if (_client == null)
- {
- Messager.ShowMessageWithColor("У вас новый клиет", ConsoleColor.Green, false);
- _client = new Client();
- }
- else
- {
- Messager.ShowMessageWithColor("У вас все ещё старый клиет", ConsoleColor.Yellow, false);
- }
- }
- private void TryFindNeedfulPart()
- {
- List<MechanicalPart> sortList = _storageOfPart.Where(part => part.Name == _client.Car.GetTypePart()).ToList<MechanicalPart>();
- sortList = sortList.OrderBy(part => part.Cost).ToList<MechanicalPart>();
- if (sortList.Count > 0)
- Messager.ShowMessageWithColor($"Минимальная стоимость починки детали составляет {sortList[0].Cost} + 350 работае механика", ConsoleColor.White, false);
- else
- Messager.ShowMessageWithColor($"У вас нет необходимой детали, вам придется отказть клиенту", ConsoleColor.White, false);
- }
- private void RepairCarClient()
- {
- Console.Clear();
- _client.Car.ShowBrokenPart();
- ShowStorage();
- Console.Write("Введите индекс детали cо склада: ");
- int indexWorkingPart = Convert.ToInt32(Console.ReadLine());
- PositionCheck newPosition = _client.Car.ReplacePartWithReport(_storageOfPart[indexWorkingPart]);
- _client.Check.AddNewPosition(newPosition);
- if (newPosition.IsProfit)
- {
- _client.Check.AddNewPosition(new PositionCheck("Работа автомеханика", 350, true));
- _storageOfPart.RemoveAt(indexWorkingPart);
- }
- }
- private void ShowStorage()
- {
- int i = 0;
- foreach (var part in _storageOfPart)
- {
- Console.WriteLine($"{i}: {part.GetInfo()}");
- i++;
- }
- }
- private void TryToFinisCar()
- {
- if (_client != null)
- {
- if (_client.Car.CheckRepair())
- {
- Console.Clear();
- _client.Check.ShowResult();
- _money += _client.GetMoney();
- _client = null;
- }
- }
- }
- private void DenialClinet()
- {
- int mulct = 500;
- Messager.ShowMessageWithColor($"Вы отказали клиенту и вынуждены заплатить штраф в размере {mulct}", ConsoleColor.Red, true);
- _money -= mulct;
- _client = null;
- }
- }
- class Client
- {
- public Car Car { get; private set; } = new Car();
- public Check Check { get; private set; } = new Check();
- public int GetMoney()
- {
- return Check.GetCostWork();
- }
- }
- class Car
- {
- private MechanicalPart _brokenPart;
- public Car()
- {
- _brokenPart = new MechanicalPart(true);
- }
- public MechanicalPart.TypePart GetTypePart()
- {
- return _brokenPart.Name;
- }
- public void ShowBrokenPart()
- {
- Messager.ShowMessageWithColor($"Сломанная деталь - {_brokenPart.Name}", ConsoleColor.Red, false); ;
- }
- public PositionCheck ReplacePartWithReport(MechanicalPart workingPart)
- {
- if (ComparePart(workingPart))
- {
- _brokenPart = workingPart;
- Messager.ShowMessageWithColor("\nВы успешно заменили деталь. Деталь установлена, работа засена в чек", ConsoleColor.White, true);
- return new PositionCheck($"Замена: {workingPart.Name}", workingPart.Cost, true);
- }
- else
- {
- Messager.ShowMessageWithColor("\nЭто неправильная деталь. Она не установлена и вы получили штраф", ConsoleColor.White, true);
- return new PositionCheck($"Штраф - Неправильная замена: {_brokenPart.Name} ", 500, false);
- }
- }
- public bool CheckRepair()
- {
- return !_brokenPart.IsBroken;
- }
- private bool ComparePart(MechanicalPart newPart)
- {
- return _brokenPart.Name == newPart.Name;
- }
- }
- class Check
- {
- private List<PositionCheck> _positionChecks = new List<PositionCheck>();
- public void AddNewPosition(PositionCheck newPosition)
- {
- _positionChecks.Add(newPosition);
- }
- public void ShowResult()
- {
- int fullCost = 0;
- Messager.ShowMessageWithColor("===Чек из автосервиса===", ConsoleColor.White, false);
- foreach (var position in _positionChecks)
- {
- if (position.IsProfit)
- {
- fullCost += position.CostWork;
- Messager.ShowMessageWithColor(position.GetInfo(), ConsoleColor.Green, false);
- }
- else
- {
- fullCost -= position.CostWork;
- Messager.ShowMessageWithColor(position.GetInfo(), ConsoleColor.Red, false);
- }
- }
- Messager.ShowMessageWithColor($"Итоговая сумма = {fullCost}", ConsoleColor.Yellow, true);
- }
- public int GetCostWork()
- {
- int fullCost = 0;
- foreach (var position in _positionChecks)
- {
- if (position.IsProfit)
- fullCost += position.CostWork;
- else
- fullCost -= position.CostWork;
- }
- if (fullCost > 0)
- Messager.ShowMessageWithColor($"\nВы получили {fullCost} за работу", ConsoleColor.Green, true);
- else
- Messager.ShowMessageWithColor($"\nВы плохо сделали свою работу и вынуждены отдать свои деньги в размере {fullCost}", ConsoleColor.Red, true);
- return fullCost;
- }
- }
- class PositionCheck
- {
- public string NameWork { get; private set; }
- public int CostWork { get; private set; }
- public bool IsProfit { get; private set; }
- public PositionCheck(string nameWork, int costWork, bool isProfit)
- {
- NameWork = nameWork;
- CostWork = costWork;
- IsProfit = isProfit;
- }
- public string GetInfo()
- {
- string result = NameWork;
- for (int i = 0; i < 30 - result.Length; i++)
- {
- result += " ";
- }
- result += $"{CostWork}$";
- return result;
- }
- }
- class MechanicalPart
- {
- public enum TypePart
- {
- engine,
- cardan,
- steeringWheel,
- headLamp,
- wheel,
- carСandles
- }
- public TypePart Name { get; private set; }
- public int Cost { get; private set; }
- public bool IsBroken { get; private set; }
- public MechanicalPart(bool isBroken)
- {
- switch (RandomStatic.GetNext(0, 6))
- {
- case 0:
- Name = TypePart.engine;
- break;
- case 1:
- Name = TypePart.cardan;
- break;
- case 2:
- Name = TypePart.steeringWheel;
- break;
- case 3:
- Name = TypePart.headLamp;
- break;
- case 4:
- Name = TypePart.wheel;
- break;
- case 5:
- Name = TypePart.carСandles;
- break;
- }
- Cost = RandomStatic.GetNext(50, 500);
- IsBroken = isBroken;
- }
- public string GetInfo()
- {
- return $"{Name} - {Cost}$";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement