Advertisement
Dr_Max_Experience

Task 13

Jul 26th, 2022 (edited)
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Reflection.Metadata.Ecma335;
  7. using System.Runtime.ConstrainedExecution;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace Task13
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             AutoService autoService = new AutoService();
  18.             autoService.Work();
  19.         }
  20.     }
  21.  
  22.     class AutoService
  23.     {
  24.         private float _money = 1000;
  25.         private float _jobMultiplier = 1.2f;
  26.         private List<Cell> _cellsParts = new List<Cell>();
  27.         private Queue<Car> _cars = new Queue<Car>();
  28.         private List<string> _cashiersCheck = new List<string>();
  29.  
  30.         public AutoService()
  31.         {
  32.             List<Part> parts = new List<Part>();
  33.  
  34.             CreateParts(parts);
  35.  
  36.             FillCells(parts);
  37.  
  38.             AddCarsToQueue(parts);
  39.         }
  40.  
  41.         public void Work()
  42.         {
  43.             bool isWork = true;
  44.             bool isMendCar;
  45.             int inspectionServiceableCar = 500;
  46.             int workPrice;
  47.  
  48.             while (isWork)
  49.             {
  50.                 Console.Clear();
  51.  
  52.                 isMendCar = true;
  53.                 workPrice = 0;
  54.                 int percent = 100;
  55.                 string userInput;
  56.                 float jobPercent = _jobMultiplier * percent - percent;
  57.  
  58.                 _cashiersCheck.Clear();
  59.  
  60.                 ShowInfo();
  61.  
  62.                 Console.WriteLine($"\nМашин в очереди: {_cars.Count}\nЦена за работу = {Math.Round(jobPercent)}% от суммы стоимости заменяемых деталей");
  63.                 Console.Write("Нажмите любую клавишу, чтобы обслужить следующую машину...");
  64.  
  65.                 Console.ReadKey();
  66.  
  67.                 if (GetServiceabilityCar())
  68.                 {
  69.                     Console.Clear();
  70.                     Console.WriteLine($"По результатам технического осмотра автомобиля, выяснено, что автомобиль исправен!\nЗа технический осмотр автомобиля ={inspectionServiceableCar}$");
  71.                     Console.ReadKey();
  72.  
  73.                     _money += inspectionServiceableCar;
  74.                 }
  75.                 else
  76.                 {
  77.                     while (isMendCar)
  78.                     {
  79.                         ShowInfo();
  80.                         _cars.Peek().ShowInfo();
  81.  
  82.                         Console.Write("\nВведите название детали, которую необходимо заменить: ");
  83.                         userInput = Console.ReadLine();
  84.  
  85.                         workPrice += GetMoneyForReplacingPart(TryReplacePart(userInput), userInput);
  86.  
  87.                         Console.WriteLine(_cashiersCheck.Last());
  88.  
  89.                         if (GetServiceabilityCar())
  90.                             isMendCar = false;
  91.                         else
  92.                             isMendCar = TryMendCar();
  93.                     }
  94.  
  95.                     ShowCashiersCheck(workPrice, GetServiceabilityCar());
  96.  
  97.                     _money += workPrice;
  98.                 }
  99.  
  100.                 _cars.Dequeue();
  101.  
  102.                 isWork = _cars.Count > 0;
  103.  
  104.                 if (isWork == false)
  105.                 {
  106.                     Console.WriteLine($"Все машины обслужены, конец рабочего дня!\nВыручка за сегодня {_money}$");
  107.                     Console.ReadKey();
  108.                 }
  109.  
  110.                 if (_money < 0)
  111.                 {
  112.                     isWork = false;
  113.                     Console.Write("Вы обанкротились! Конец бизнесу!");
  114.                     Console.ReadKey();
  115.                 }
  116.             }
  117.         }
  118.  
  119.         private bool GetServiceabilityCar()
  120.         {
  121.             return _cars.Peek().IsServiceable();
  122.         }
  123.  
  124.         private void ShowInfo()
  125.         {
  126.             Console.Clear();
  127.             Console.WriteLine($"Автосервис\nБаланс: {_money}$\n\nСклад:");
  128.  
  129.             foreach (var cell in _cellsParts)
  130.             {
  131.                 cell.ShowInfo();
  132.             }
  133.         }
  134.  
  135.         private bool TryReplacePart(string userInput)
  136.         {
  137.             if (FindDetail(userInput) && _cars.Peek().TryGetNotServiceablePart(userInput))
  138.             {
  139.                 TakePart(userInput);
  140.                 _cars.Peek().ReplacePart(userInput);
  141.                 return true;
  142.             }
  143.             else
  144.             {
  145.                 return false;
  146.             }
  147.         }
  148.  
  149.         private int GetMoneyForReplacingPart(bool IsReplacingPart, string userInput)
  150.         {
  151.             int workPrice = 0;
  152.             int penalty = 500;
  153.             int jobMultiplier;
  154.  
  155.             if (IsReplacingPart)
  156.             {
  157.                 workPrice = _cars.Peek().GetPricePart(userInput);
  158.                 jobMultiplier = (int)Math.Round(workPrice * _jobMultiplier - workPrice);
  159.  
  160.                 _cashiersCheck.Add($"Заменена {userInput}. Цена замены детали ={workPrice}$ + Цена за работу ={jobMultiplier}$");
  161.  
  162.                 workPrice += jobMultiplier;
  163.             }
  164.             else
  165.             {
  166.                 workPrice -= penalty;
  167.                 _cashiersCheck.Add($"Детали ({userInput}) нет, или такая деталь в машине исправна! Штраф ={penalty}$");
  168.             }
  169.  
  170.             return workPrice;
  171.         }
  172.  
  173.         private void ShowCashiersCheck(int workPrice, bool isServiceableCar)
  174.         {
  175.             int penalty = 1000;
  176.  
  177.             Console.Clear();
  178.  
  179.             if (isServiceableCar)
  180.             {
  181.                 Console.WriteLine($"~Чек-лист~\nАвтомобиль успешно починен!\n");
  182.                 ShowCashiersCheck(workPrice);
  183.             }
  184.             else
  185.             {
  186.                 Console.WriteLine($"~Чек-лист~\nАвтомобиль не был починен! Штраф ={penalty}$\n");
  187.                 ShowCashiersCheck(workPrice - penalty);
  188.                 _money -= penalty;
  189.             }
  190.  
  191.             Console.ReadKey();
  192.         }
  193.  
  194.         private void ShowCashiersCheck(int workPlace)
  195.         {
  196.             foreach (var checkString in _cashiersCheck)
  197.             {
  198.                 Console.WriteLine(checkString);
  199.             }
  200.  
  201.             Console.WriteLine($"\nИТОГО {workPlace}$");
  202.         }
  203.  
  204.         private bool TryMendCar()
  205.         {
  206.             const string CommandExit = "н";
  207.             const string CommandContinue = "д";
  208.             const string StringContinueOrExit = $"Продолжить чинить автомобиль ({CommandContinue}/{CommandExit}): ";
  209.             bool isMandCar = true;
  210.  
  211.             while (isMandCar)
  212.             {
  213.                 Console.Write(StringContinueOrExit);
  214.                 string userInput = Console.ReadLine();
  215.  
  216.                 switch (userInput)
  217.                 {
  218.                     case CommandContinue:
  219.                         return true;
  220.  
  221.                     case CommandExit:
  222.                         isMandCar = false;
  223.                         break;
  224.  
  225.                     default:
  226.                         Console.Write("такой команды нет!");
  227.                         Console.ReadKey();
  228.                         break;
  229.                 }
  230.             }
  231.  
  232.             return false;
  233.         }
  234.  
  235.         private bool FindDetail(string userInput)
  236.         {
  237.             foreach (var cell in _cellsParts)
  238.             {
  239.                 if (cell.TryFindDetail(userInput))
  240.                     if (cell.HavePart)
  241.                         return true;
  242.             }
  243.  
  244.             return false;
  245.         }
  246.  
  247.         private void TakePart(string userInput)
  248.         {
  249.             foreach (var cell in _cellsParts)
  250.             {
  251.                 if (cell.TryFindDetail(userInput))
  252.                     cell.TakePart();
  253.             }
  254.         }
  255.  
  256.         private void FillCells(List<Part> parts)
  257.         {
  258.             int minCountParts = 3;
  259.             int maxCountParts = 6;
  260.             Random random = new Random();
  261.  
  262.             foreach (var part in parts)
  263.             {
  264.                 _cellsParts.Add(new Cell(part, random.Next(minCountParts, maxCountParts)));
  265.             }
  266.         }
  267.  
  268.         private void CreateParts(List<Part> parts)
  269.         {
  270.             int minPricePart = 250;
  271.             int maxPricePart = 750;
  272.             Random random = new Random();
  273.  
  274.             parts.AddRange(new Part[] {
  275.                 new Part("деталь_1", random.Next(minPricePart, maxPricePart), true),
  276.                 new Part("деталь_2", random.Next(minPricePart, maxPricePart), true),
  277.                 new Part("деталь_3", random.Next(minPricePart, maxPricePart), true),
  278.                 new Part("деталь_4", random.Next(minPricePart, maxPricePart), true),
  279.                 new Part("деталь_5", random.Next(minPricePart, maxPricePart), true)
  280.             });
  281.         }
  282.  
  283.         private void AddCarsToQueue(List<Part> parts)
  284.         {
  285.             int minCountCars = 2;
  286.             int maxCountCars = 6;
  287.             Random random = new Random();
  288.  
  289.             int countCars = random.Next(minCountCars, maxCountCars);
  290.  
  291.             for (int i = 0; i < countCars; i++)
  292.             {
  293.                 _cars.Enqueue(new Car(parts));
  294.             }
  295.         }
  296.     }
  297.  
  298.     class Car
  299.     {
  300.         private List<Part> _parts = new List<Part>();
  301.  
  302.         public Car(List<Part> parts)
  303.         {
  304.             foreach (var part in parts)
  305.             {
  306.                 _parts.Add(new Part(part.Name, part.Price, SetRandomBoolValue()));
  307.             }
  308.         }
  309.  
  310.         public void ShowInfo()
  311.         {
  312.             Console.WriteLine("\n\n*Идёт обслуживание машины*\nСписок неисправных деталей:");
  313.  
  314.             var defectiveParts = _parts.Where(part => part.IsServiceable == false);
  315.  
  316.             foreach (var part in defectiveParts)
  317.             {
  318.                 Console.WriteLine($"{part.Name}");
  319.             }
  320.         }
  321.  
  322.         public bool IsServiceable()
  323.         {
  324.             var result = _parts.Where(part => part.IsServiceable == false);
  325.             return result.Count() == 0;
  326.         }
  327.  
  328.         public void ReplacePart(string userInput)
  329.         {
  330.             foreach (var part in _parts)
  331.             {
  332.                 if (part.Name == userInput)
  333.                     part.Replace();
  334.             }
  335.         }
  336.  
  337.         public bool TryGetNotServiceablePart(string userInput)
  338.         {
  339.             foreach (var part in _parts)
  340.             {
  341.                 if (part.Name == userInput && part.IsServiceable == false)
  342.                     return true;
  343.             }
  344.  
  345.             return false;
  346.         }
  347.  
  348.         public int GetPricePart(string userInput)
  349.         {
  350.             foreach (var part in _parts)
  351.             {
  352.                 if (part.Name == userInput)
  353.                     return part.Price;
  354.             }
  355.  
  356.             return 0;
  357.         }
  358.  
  359.         private bool SetRandomBoolValue()
  360.         {
  361.             int numberOfOutcomes = 2;
  362.             Random random = new Random();
  363.             return random.Next(0, numberOfOutcomes) == 1;
  364.         }
  365.     }
  366.  
  367.     class Cell
  368.     {
  369.         public Part Part { get; private set; }
  370.         public int Count { get; private set; }
  371.  
  372.         public bool HavePart => Count > 0;
  373.  
  374.         public Cell(Part part, int count)
  375.         {
  376.             Part = part;
  377.             Count = count;
  378.         }
  379.  
  380.         public void ShowInfo()
  381.         {
  382.             Console.WriteLine($"{Part.Name}: {Count} шт. ={Part.Price}$");
  383.         }
  384.  
  385.         public bool TryFindDetail(string userInput)
  386.         {
  387.             return Part.Name == userInput;
  388.         }
  389.  
  390.         public void TakePart()
  391.         {
  392.             Count--;
  393.         }
  394.     }
  395.  
  396.     class Part
  397.     {
  398.         public string Name { get; private set; }
  399.         public int Price { get; private set; }
  400.         public bool IsServiceable { get; private set; }
  401.  
  402.         public Part(string name, int price, bool isServiceable)
  403.         {
  404.             Name = name;
  405.             Price = price;
  406.             IsServiceable = isServiceable;
  407.         }
  408.  
  409.         public void Replace()
  410.         {
  411.             IsServiceable = true;
  412.         }
  413.     }
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement