Advertisement
TeT91

ДЗ Автосервис

Jun 13th, 2024 (edited)
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             CarService carService = new CarService();
  11.             carService.TryServeCars();
  12.             Console.ReadKey();
  13.         }
  14.     }
  15.  
  16.     class CarService
  17.     {
  18.         private Garage _garage;
  19.         private Storage _storage;
  20.  
  21.         private int _money;
  22.         private int _fine;
  23.         private int _installationPrice = 100;
  24.  
  25.         public CarService()
  26.         {
  27.             _garage = new Garage();
  28.             _storage = new Storage();
  29.  
  30.             _money = 1000;
  31.             _fine = 200;
  32.         }
  33.  
  34.         public void RepairDetail(Car car)
  35.         {
  36.             Detail detail = GetDetail(car);
  37.             DetailInfo detailInfo = _storage.GetDetailInfo(detail.Type);
  38.  
  39.             if (detailInfo.Count > 0)
  40.             {
  41.                 Detail newDetail = _storage.GetDetail(detail.Type);
  42.  
  43.                 if (detail.IsDamaged)
  44.                 {
  45.                     car.RemoveDetail(detail);
  46.                     car.AddDetail(newDetail);
  47.  
  48.                     int price = detailInfo.Price + _installationPrice;
  49.  
  50.                     Console.WriteLine($"Сломанная деталь успешно заменена. Сервис заработал {price}");
  51.  
  52.                     IncreaseMoney(price);
  53.                 }
  54.                 else
  55.                 {
  56.                     Console.WriteLine("Деталь не была сломана. Сервис потерял деталь");
  57.                 }
  58.             }
  59.             else
  60.             {
  61.                 Console.WriteLine("На складе не хватает нужных деталей");
  62.             }
  63.         }
  64.  
  65.         public void TryServeCars()
  66.         {
  67.             while (_garage.CarsInQueue != 0)
  68.             {
  69.                 Console.Clear();
  70.  
  71.                 Console.WriteLine($"Следующая машина. Машин в очереди {_garage.CarsInQueue}");
  72.  
  73.                 Car car = _garage.GetNextCar();
  74.  
  75.                 ShowEstimateInfo(car);
  76.  
  77.                 if (TryRefuseToRepair())
  78.                 {
  79.                     Console.WriteLine($"Отказ от ремонта стоил {_fine}");
  80.  
  81.                     DecreaseMoney(_fine);
  82.                 }
  83.                 else
  84.                 {
  85.                     bool isReparing = true;
  86.  
  87.                     while (isReparing)
  88.                     {
  89.                         Console.Clear();
  90.  
  91.                         ShowEstimateInfo(car);
  92.  
  93.                         RepairDetail(car);
  94.  
  95.                         if (car.IsRepaired)
  96.                         {
  97.                             isReparing = false;
  98.                             Console.WriteLine("Машина полностью готова");
  99.                         }
  100.                         else if (TryRefuseToRepair())
  101.                         {
  102.                             isReparing = false;
  103.  
  104.                             int currentFine = CalculateFine(car);
  105.                             Console.WriteLine($"Отказ от ремонта стоил {currentFine}");
  106.  
  107.                             DecreaseMoney(currentFine);
  108.                         }
  109.                     }
  110.                 }
  111.  
  112.                 Console.ReadKey();
  113.             }
  114.  
  115.             Console.WriteLine("Все машины обслужены");
  116.             Console.ReadKey();
  117.         }
  118.  
  119.         private int CalculateFine(Car car)
  120.         {
  121.             int value = 0;
  122.  
  123.             for (int i = 0; i < car.DetailsCount; i++)
  124.             {
  125.                 if (car.GetDetail(i).IsDamaged)
  126.                 {
  127.                     value += _fine;
  128.                 }
  129.             }
  130.  
  131.             return value;
  132.         }
  133.  
  134.         private bool TryRefuseToRepair()
  135.         {
  136.             const string CommandAgree = "n";
  137.             const string CommandRefuse = "y";
  138.  
  139.             bool isRunning = true;
  140.  
  141.             Console.WriteLine("Отказаться от ремонта? y/n");
  142.  
  143.             bool isRefused = false;
  144.  
  145.             while (isRunning)
  146.             {
  147.                 string userInput = Console.ReadLine();
  148.  
  149.                 switch (userInput)
  150.                 {
  151.                     case CommandAgree:
  152.                         isRefused = false;
  153.                         isRunning = false;
  154.                         break;
  155.  
  156.                     case CommandRefuse:
  157.                         isRefused = true;
  158.                         isRunning = false;
  159.                         break;
  160.  
  161.                     default:
  162.                         Console.WriteLine("Неверная команда");
  163.                         break;
  164.                 }
  165.             }
  166.  
  167.             return isRefused;
  168.         }
  169.  
  170.         private Detail GetDetail(Car car)
  171.         {
  172.             Detail detail = null;
  173.  
  174.             while (detail == null)
  175.             {
  176.                 Console.WriteLine("Какую деталь починить?");
  177.  
  178.                 string userInput = Console.ReadLine();
  179.  
  180.                 if (int.TryParse(userInput, out int result))
  181.                 {
  182.                     if (result >= 0 && result < car.DetailsCount)
  183.                     {
  184.                         detail = car.GetDetail(result);
  185.                     }
  186.                     else
  187.                     {
  188.                         Console.WriteLine("Неверная деталь");
  189.                     }
  190.                 }
  191.             }
  192.  
  193.             return detail;
  194.         }
  195.  
  196.         private void IncreaseMoney(int value)
  197.         {
  198.             _money += value;
  199.         }
  200.  
  201.         private void DecreaseMoney(int value)
  202.         {
  203.             _money -= value;
  204.         }
  205.  
  206.         private void ShowEstimateInfo(Car car)
  207.         {
  208.             ConsoleColor damagedColor = ConsoleColor.Red;
  209.             ConsoleColor repairedColor = ConsoleColor.Green;
  210.  
  211.             int totalPrice = 0;
  212.  
  213.             for (int i = 0; i < car.DetailsCount; i++)
  214.             {
  215.                 Detail detail = car.GetDetail(i);
  216.                 DetailInfo detailInfo = _storage.GetDetailInfo(detail.Type);
  217.  
  218.                 if (car.GetDetail(i).IsDamaged)
  219.                 {
  220.                     Console.ForegroundColor = damagedColor;
  221.                     Console.WriteLine($"{i} - {detailInfo.Type} - Поломка - Цена {detailInfo.Price} + цена установки {_installationPrice}. На складе {detailInfo.Count}");
  222.                     totalPrice += (detailInfo.Price + _installationPrice);
  223.                 }
  224.                 else
  225.                 {
  226.                     Console.ForegroundColor = repairedColor;
  227.                     Console.WriteLine($"{i} - {detailInfo.Type} - Исправна");
  228.                 }
  229.             }
  230.  
  231.             Console.ResetColor();
  232.  
  233.             Console.WriteLine($"Полная стоимость ремонта - {totalPrice}");
  234.         }
  235.     }
  236.  
  237.     class Garage
  238.     {
  239.         private Queue<Car> _cars;
  240.         private int _capacity;
  241.  
  242.         public Garage()
  243.         {
  244.             _cars = new Queue<Car>();
  245.             _capacity = 10;
  246.  
  247.             InitCars();
  248.         }
  249.  
  250.         public int CarsInQueue
  251.         {
  252.             get
  253.             {
  254.                 return _cars.Count;
  255.             }
  256.         }
  257.  
  258.         public Car GetNextCar()
  259.         {
  260.             return _cars.Dequeue();
  261.         }
  262.  
  263.         private void InitCars()
  264.         {
  265.             CarCreator carCreator = new CarCreator();
  266.  
  267.             for (int i = 0; i < _capacity; i++)
  268.             {
  269.                 _cars.Enqueue(carCreator.CreateCar());
  270.             }
  271.         }
  272.     }
  273.  
  274.     class CarCreator
  275.     {
  276.         public Car CreateCar()
  277.         {
  278.             int detailsCount = GenerateDetailsCount();
  279.  
  280.             Car car = new Car(detailsCount);
  281.  
  282.             List<Detail> details = CreateDetails(detailsCount);
  283.  
  284.             SetDetailsInCar(car, details);
  285.  
  286.             return car;
  287.         }
  288.  
  289.         private void SetDetailsInCar(Car car, List<Detail> details)
  290.         {
  291.             foreach (Detail detail in details)
  292.             {
  293.                 car.AddDetail(detail);
  294.             }
  295.         }
  296.  
  297.         private int GenerateDetailsCount()
  298.         {
  299.             int minCount = 5;
  300.             int maxCount = 10;
  301.  
  302.             return UserUtils.GenerateRandomNumber(minCount, maxCount);
  303.         }
  304.  
  305.         private List<Detail> CreateDetails(int count)
  306.         {
  307.             DetailFabric detailFabric = new DetailFabric();
  308.             List<Detail> details = new List<Detail>();
  309.  
  310.             for (int i = 0; i < count; i++)
  311.             {
  312.                 int id = UserUtils.GenerateRandomNumber(detailFabric.TypesCount);
  313.                 string type = detailFabric.GetType(id);
  314.  
  315.                 Detail detail = detailFabric.CreateDetail(type);
  316.  
  317.                 details.Add(detail);
  318.             }
  319.  
  320.             DamageDetails(details);
  321.  
  322.             return details;
  323.         }
  324.  
  325.         private void DamageDetails(List<Detail> details)
  326.         {
  327.             int maxPercent = 100;
  328.             int chance = 50;
  329.             bool hasDamagedDetails = false;
  330.  
  331.             for (int i = 0; i < details.Count; i++)
  332.             {
  333.                 int value = UserUtils.GenerateRandomNumber(maxPercent);
  334.  
  335.                 if (value > chance)
  336.                 {
  337.                     details[i].Damage();
  338.                     hasDamagedDetails = true;
  339.                 }
  340.             }
  341.  
  342.             if (hasDamagedDetails == false)
  343.             {
  344.                 int randomDetail = UserUtils.GenerateRandomNumber(details.Count);
  345.                 details[randomDetail].Damage();
  346.             }
  347.         }
  348.     }
  349.  
  350.     class Car
  351.     {
  352.         private List<Detail> _details;
  353.  
  354.         public Car(int detailsCount)
  355.         {
  356.             DetailsCount = detailsCount;
  357.             _details = new List<Detail>();
  358.         }
  359.  
  360.         public int DetailsCount { get; private set; }
  361.  
  362.         public bool IsRepaired
  363.         {
  364.             get
  365.             {
  366.                 bool isRepaired = true;
  367.  
  368.                 foreach (Detail detail in _details)
  369.                 {
  370.                     if (detail.IsDamaged)
  371.                     {
  372.                         isRepaired = false;
  373.                     }
  374.                 }
  375.  
  376.                 return isRepaired;
  377.             }
  378.         }
  379.  
  380.         public void AddDetail(Detail detail)
  381.         {
  382.             _details.Add(detail);
  383.         }
  384.  
  385.         public void RemoveDetail(Detail detail)
  386.         {
  387.             _details.Remove(detail);
  388.         }
  389.  
  390.         public Detail GetDetail(int id)
  391.         {
  392.             return _details[id];
  393.         }
  394.     }
  395.  
  396.     class Storage
  397.     {
  398.         private DetailFabric _detailFabric;
  399.         private List<DetailInfo> _detailsInfo;
  400.  
  401.         public Storage()
  402.         {
  403.             _detailFabric = new DetailFabric();
  404.             InitDetails();
  405.         }
  406.  
  407.         public Detail GetDetail(string type)
  408.         {
  409.             Detail detail = _detailFabric.CreateDetail(type);
  410.  
  411.             foreach (DetailInfo detailInfo in _detailsInfo)
  412.             {
  413.                 if (detailInfo.Type == type)
  414.                 {
  415.                     detailInfo.DecreaseCount();
  416.                 }
  417.             }
  418.  
  419.             return detail;
  420.         }
  421.  
  422.         public DetailInfo GetDetailInfo(string type)
  423.         {
  424.             DetailInfo tempInfo = null;
  425.  
  426.             foreach (DetailInfo detailInfo in _detailsInfo)
  427.             {
  428.                 if (detailInfo.Type == type)
  429.                 {
  430.                     tempInfo = detailInfo;
  431.                 }
  432.             }
  433.  
  434.             return tempInfo;
  435.         }
  436.  
  437.         private void InitDetails()
  438.         {
  439.             _detailsInfo = new List<DetailInfo>();
  440.  
  441.             for (int i = 0; i < _detailFabric.TypesCount; i++)
  442.             {
  443.                 string type = _detailFabric.GetType(i);
  444.                 _detailsInfo.Add(_detailFabric.CreateDetailInfo(type));
  445.             }
  446.         }
  447.     }
  448.  
  449.     class DetailFabric
  450.     {
  451.         private string[] _types;
  452.  
  453.         public DetailFabric()
  454.         {
  455.             InitTypes();
  456.         }
  457.  
  458.         public int TypesCount { get { return _types.Length; } }
  459.  
  460.         public DetailInfo CreateDetailInfo(string type)
  461.         {
  462.             int minPrice = 100;
  463.             int maxPrice = 500;
  464.             int price = UserUtils.GenerateRandomNumber(minPrice, maxPrice);
  465.  
  466.             int minCount = 15;
  467.             int maxCount = 30;
  468.             int count = UserUtils.GenerateRandomNumber(minCount, maxCount);
  469.  
  470.             return new DetailInfo(type, price, count);
  471.         }
  472.  
  473.         public Detail CreateDetail(string type)
  474.         {
  475.             return new Detail(type);
  476.         }
  477.  
  478.         public string GetType(int id)
  479.         {
  480.             return _types[id];
  481.         }
  482.  
  483.         private void InitTypes()
  484.         {
  485.             int typesCount = 4;
  486.             string typeName = "Тип_";
  487.             _types = new string[typesCount];
  488.  
  489.             for (int i = 0; i < _types.Length; i++)
  490.             {
  491.                 _types[i] = typeName + i;
  492.             }
  493.         }
  494.     }
  495.  
  496.     class DetailInfo
  497.     {
  498.         public DetailInfo(string type, int price, int count)
  499.         {
  500.             Type = type;
  501.             Price = price;
  502.             Count = count;
  503.         }
  504.  
  505.         public string Type { get; private set; }
  506.         public int Price { get; private set; }
  507.         public int Count { get; private set; }
  508.  
  509.         public void DecreaseCount()
  510.         {
  511.             Count--;
  512.         }
  513.     }
  514.  
  515.     class Detail
  516.     {
  517.         public Detail(string type)
  518.         {
  519.             Type = type;
  520.         }
  521.  
  522.         public bool IsDamaged { get; private set; }
  523.         public string Type { get; private set; }
  524.  
  525.         public void Damage()
  526.         {
  527.             IsDamaged = true;
  528.         }
  529.     }
  530.  
  531.     class UserUtils
  532.     {
  533.         private static Random s_random = new Random();
  534.  
  535.         public static int GenerateRandomNumber(int minValue, int maxValue)
  536.         {
  537.             return s_random.Next(minValue, maxValue);
  538.         }
  539.  
  540.         public static int GenerateRandomNumber(int maxValue)
  541.         {
  542.             return s_random.Next(maxValue);
  543.         }
  544.     }
  545. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement