lovelyvook

Unit_49

Aug 16th, 2024 (edited)
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Ijunior
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Game game = new Game();
  12.             game.Work();
  13.         }
  14.     }
  15.  
  16.     class Game
  17.     {
  18.         private Service _service = new Service();
  19.  
  20.         public void Work()
  21.         {
  22.             const string CommandServiceCar = "1";
  23.             const string CommandShowStorage = "2";
  24.             const string CommandExit = "3";
  25.  
  26.             bool isWork = true;
  27.  
  28.             while (isWork)
  29.             {
  30.                 Console.WriteLine("Машин в очереди: " + _service.CarsCount);
  31.  
  32.                 Console.Write($"\n{CommandServiceCar} - обслужить машину" +
  33.                               $"\n{CommandShowStorage} - посмотреть детали на складе" +
  34.                               $"\n{CommandExit} - выйти" +
  35.                               $"\nВведите номер: ");
  36.  
  37.                 switch (Console.ReadLine())
  38.                 {
  39.                     case CommandServiceCar:
  40.                         _service.FixCar();
  41.                         break;
  42.  
  43.                     case CommandShowStorage:
  44.                         _service.ShowStorageInfo();
  45.                         break;
  46.  
  47.                     case CommandExit:
  48.                         isWork = false;
  49.                         break;
  50.  
  51.                     default:
  52.                         Console.WriteLine("Некорректный ввод");
  53.                         break;
  54.                 }
  55.  
  56.                 if (_service.CarsCount == 0)
  57.                 {
  58.                     Console.WriteLine("Вы починили все машины");
  59.                     isWork = false;
  60.                 }
  61.  
  62.                 Console.ReadKey();
  63.                 Console.Clear();
  64.             }
  65.         }
  66.     }
  67.  
  68.     class Service
  69.     {
  70.         private FactoryDetails _factoryDetails;
  71.         private Storage _storage;
  72.         private Queue<Car> _cars;
  73.         private int _money;
  74.         private int _priceRepair;
  75.         private int _peneltyFix;
  76.         private int _penaltyDetail;
  77.  
  78.         public Service()
  79.         {
  80.             _factoryDetails = new FactoryDetails();
  81.             _storage = new Storage(_factoryDetails);
  82.             _cars = new Queue<Car>();
  83.             CreateCars();
  84.  
  85.             _money = 1000;
  86.             _priceRepair = 100;
  87.             _peneltyFix = 100;
  88.             _penaltyDetail = 50;
  89.         }
  90.  
  91.         public int CarsCount => _cars.Count;
  92.  
  93.         public void FixCar()
  94.         {
  95.             const string CommandCancel = "exit";
  96.  
  97.             bool isWork = true;
  98.             bool isPenalty = false;
  99.             Car car = _cars.Dequeue();
  100.  
  101.             Console.Clear();
  102.             Console.Write($"Для начала ремонта нажмите любую клавишу, для отмены введите '{CommandCancel}' : ");
  103.  
  104.             if (Console.ReadLine().ToLower() == CommandCancel)
  105.             {
  106.                 int countFixPenalty = 1;
  107.                 PayPenalty(_peneltyFix, countFixPenalty);
  108.             }
  109.             else
  110.             {
  111.                 while (isWork)
  112.                 {
  113.                     if (car.HasBrokenDetail() > 0)
  114.                     {
  115.                         Console.Clear();
  116.                         Console.WriteLine($"Деньги мастерской: {_money}\n\n");
  117.                         car.ShowInfo();
  118.                         ReplaceDetail(car, ref isPenalty);
  119.  
  120.                         if (isPenalty)
  121.                         {
  122.                             PayPenalty(_penaltyDetail, car.HasBrokenDetail());
  123.                             isWork = false;
  124.                         }
  125.  
  126.                         Console.ReadKey();
  127.                     }
  128.                     else
  129.                     {
  130.                         isWork = false;
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.  
  136.         public void ReplaceDetail(Car car, ref bool isPenalty)
  137.         {
  138.             const string CommandCancel = "exit";
  139.  
  140.             Console.Write($"Введите номер детали для замены или '{CommandCancel}' для окончания ремонта: ");
  141.             string userInput = Console.ReadLine();
  142.  
  143.             if (int.TryParse(userInput, out int index))
  144.             {
  145.                 if (index > 0 && index <= car.DetailsCount)
  146.                 {
  147.                     index--;
  148.                     Detail detail = car.GetDetail(index);
  149.                     bool isBroken = detail.IsBroken;
  150.  
  151.                     if (_storage.TryGetDetail(ref detail))
  152.                     {
  153.                         car.ReplaceDetail(index, detail);
  154.                         Console.WriteLine($"Вы заменили деталь {detail.Name}");
  155.  
  156.                         if (isBroken)
  157.                             EarnMoney(detail.Price);
  158.                         else
  159.                             Console.WriteLine("Вы не получаете денег, деталь была исправна");
  160.                     }
  161.                     else
  162.                     {
  163.                         Console.WriteLine("На складе отсутствует эта деталь");
  164.                     }
  165.                 }
  166.                 else
  167.                 {
  168.                     Console.WriteLine("Такого номера нет");
  169.                 }
  170.             }
  171.             else if (userInput.ToLower() == CommandCancel)
  172.             {
  173.                 isPenalty = true;
  174.             }
  175.             else
  176.             {
  177.                 Console.WriteLine("Некорректный ввод");
  178.             }
  179.         }
  180.  
  181.         public void ShowStorageInfo()
  182.         {
  183.             _storage.ShowInfo();
  184.         }
  185.  
  186.         private void EarnMoney(int detailPrice)
  187.         {
  188.             _money += detailPrice + _priceRepair;
  189.             Console.WriteLine("Вы заработали " + (detailPrice + _priceRepair));
  190.         }
  191.  
  192.         private void PayPenalty(int penalty, int count)
  193.         {
  194.             _money -= penalty * count;
  195.             Console.WriteLine("Вы заплатили неустойку в размере: " + (penalty * count));
  196.         }
  197.  
  198.         private void CreateCars()
  199.         {
  200.             int count = 10;
  201.  
  202.             for (int i = 0; i < count; i++)
  203.             {
  204.                 _cars.Enqueue(new Car(_factoryDetails.CopyList()));
  205.             }
  206.         }
  207.     }
  208.  
  209.     class Storage
  210.     {
  211.         private FactoryDetails _factoryDetails;
  212.         private List<Box> _boxes;
  213.  
  214.         public Storage(FactoryDetails factoryDetails)
  215.         {
  216.             _factoryDetails = factoryDetails;
  217.             _boxes = new List<Box>();
  218.             CreateBoxes();
  219.         }
  220.  
  221.         public void CreateBoxes()
  222.         {
  223.             int minDetails = 1;
  224.             int maxDetails = 10;
  225.  
  226.             for (int i = 0; i < _factoryDetails.DetailsCount; i++)
  227.             {
  228.                 _boxes.Add(new Box(_factoryDetails.CopyDetail(i), Utils.GetRandomNumber(minDetails, maxDetails)));
  229.             }
  230.         }
  231.  
  232.         public bool TryGetDetail(ref Detail detail)
  233.         {
  234.             for (int i = _boxes.Count - 1; i >= 0; i--)
  235.             {
  236.                 if (_boxes[i].Detail.Name == detail.Name)
  237.                 {
  238.                     _boxes[i].ReduceDetail();
  239.                     detail = _boxes[i].Detail;
  240.  
  241.                     if (_boxes[i].Count == 0)
  242.                     {
  243.                         _boxes.RemoveAt(i);
  244.                     }
  245.  
  246.                     return true;
  247.                 }
  248.             }
  249.  
  250.             return false;
  251.         }
  252.  
  253.         public void ShowInfo()
  254.         {
  255.             foreach (Box box in _boxes)
  256.             {
  257.                 box.ShowInfo();
  258.             }
  259.         }
  260.     }
  261.  
  262.     class Box
  263.     {
  264.         public Box(Detail detail, int count)
  265.         {
  266.             Detail = detail;
  267.             Count = count;
  268.         }
  269.  
  270.         public Detail Detail { get; }
  271.         public int Count { get; private set; }
  272.  
  273.         public void ShowInfo()
  274.         {
  275.             Console.WriteLine($"{Detail.Name} - {Count} штук");
  276.         }
  277.  
  278.         public void ReduceDetail()
  279.         {
  280.             Count--;
  281.         }
  282.     }
  283.  
  284.     class FactoryDetails
  285.     {
  286.         private List<Detail> _details = new List<Detail>();
  287.  
  288.         public FactoryDetails()
  289.         {
  290.             Create();
  291.         }
  292.  
  293.         public int DetailsCount => _details.Count;
  294.  
  295.         public List<Detail> CopyList()
  296.         {
  297.             List<Detail> copiedDetails = new List<Detail>();
  298.  
  299.             foreach (Detail detail in _details)
  300.             {
  301.                 copiedDetails.Add(new Detail(detail.Name, detail.Price));
  302.             }
  303.  
  304.             return copiedDetails;
  305.         }
  306.  
  307.         public Detail CopyDetail(int index)
  308.         {
  309.             return _details[index];
  310.         }
  311.  
  312.         private void Create()
  313.         {
  314.             int minPrice = 100;
  315.             int maxPrice = 400;
  316.  
  317.             string[] names = new string[]
  318.             {
  319.                 "генератор", "бензонасос", "патрубки",
  320.                 "термостат", "распредвал", "коленвал",
  321.                 "сцепление", "маховик", "глушитель"
  322.             };
  323.  
  324.             for (int i = 0; i < names.Length; i++)
  325.             {
  326.                 int price = Utils.GetRandomNumber(minPrice, maxPrice);
  327.                 _details.Add(new Detail(names[i], price));
  328.             }
  329.         }
  330.     }
  331.  
  332.     class Car
  333.     {
  334.         private List<Detail> _details;
  335.  
  336.         public Car(List<Detail> details)
  337.         {
  338.             _details = details;
  339.             BrokeRandomDetails();
  340.         }
  341.  
  342.         public int DetailsCount => _details.Count;
  343.  
  344.         public void ShowInfo()
  345.         {
  346.             for (int i = 0; i < _details.Count; i++)
  347.             {
  348.                 Console.Write(i + 1 + " ");
  349.                 _details[i].ShowInfo();
  350.             }
  351.         }
  352.  
  353.         public int HasBrokenDetail()
  354.         {
  355.             int count = 0;
  356.  
  357.             foreach (Detail detail in _details)
  358.             {
  359.                 if (detail.IsBroken)
  360.                     count++;
  361.             }
  362.  
  363.             return count;
  364.         }
  365.  
  366.         public Detail GetDetail(int index)
  367.         {
  368.             return _details.ElementAt(index);
  369.         }
  370.  
  371.         public void ReplaceDetail(int index, Detail detail)
  372.         {
  373.             _details.RemoveAt(index);
  374.             _details.Add(detail);
  375.         }
  376.  
  377.         private void BrokeRandomDetails()
  378.         {
  379.             int minPercent = 0;
  380.             int maxPercent = 4;
  381.             int countBrokenDetails = 0;
  382.  
  383.             foreach (Detail detail in _details)
  384.             {
  385.                 if (Utils.GetRandomNumber(minPercent, maxPercent) == 0)
  386.                 {
  387.                     detail.MakeBroken();
  388.                     countBrokenDetails++;
  389.                 }
  390.             }
  391.  
  392.             if (countBrokenDetails == 0)
  393.                 _details[Utils.GetRandomNumber(0, _details.Count - 1)].MakeBroken();
  394.         }
  395.     }
  396.  
  397.     class Detail
  398.     {
  399.         public Detail(string name, int price)
  400.         {
  401.             Name = name;
  402.             Price = price;
  403.             IsBroken = false;
  404.         }
  405.  
  406.         public string Name { get; }
  407.         public int Price { get; }
  408.         public bool IsBroken { get; private set; }
  409.  
  410.         public void MakeBroken()
  411.         {
  412.             IsBroken = true;
  413.         }
  414.  
  415.         public void ShowInfo()
  416.         {
  417.             if (IsBroken)
  418.                 Console.WriteLine($"{Name} - деталь сломана, стоимость замены - {Price} монет");
  419.             else
  420.                 Console.WriteLine($"{Name} - деталь исправна");
  421.         }
  422.     }
  423.  
  424.     class Utils
  425.     {
  426.         private static Random s_random = new Random();
  427.  
  428.         public static int GetRandomNumber(int minValue, int maxValue)
  429.         {
  430.             return s_random.Next(minValue, maxValue);
  431.         }
  432.     }
  433. }
Advertisement
Add Comment
Please, Sign In to add comment