Advertisement
MaoChessy

Task 38 - fix

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