Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Advertisement
vovanhik_24

#50

Apr 4th, 2025 (edited)
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CarService
  6. {
  7.     public class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             AutoService service = new AutoService();
  12.             service.Run();
  13.         }
  14.     }
  15.  
  16.     public class Detail
  17.     {
  18.         public Detail(string name, decimal price, bool isBroken)
  19.         {
  20.             Name = name;
  21.             Price = price;
  22.             IsBroken = isBroken;
  23.         }
  24.  
  25.         public string Name { get; private set; }
  26.         public decimal Price { get; private set; }
  27.         public bool IsBroken { get; private set; }
  28.     }
  29.  
  30.     public class DetailStorage
  31.     {
  32.         public DetailStorage(Detail detail, int amount = 1)
  33.         {
  34.             Detail = detail;
  35.             Amount = amount;
  36.         }
  37.  
  38.         public Detail Detail { get; private set; }
  39.         public int Amount { get; private set; }
  40.  
  41.         public int SetAmount(int amount)
  42.         {
  43.             return Amount += amount;
  44.         }
  45.     }
  46.  
  47.     public class AutoServiceStorage
  48.     {
  49.         private readonly List<DetailStorage> _partsInStock;
  50.  
  51.         public AutoServiceStorage()
  52.         {
  53.             _partsInStock = new List<DetailStorage>
  54.             {
  55.                 new DetailStorage(new Detail("Двигатель", 5000, false), UserUtils.GenerateRandom(10)),
  56.                 new DetailStorage(new Detail("Тормоза", 1200, false), UserUtils.GenerateRandom(10)),
  57.                 new DetailStorage(new Detail("Фара", 800, false), UserUtils.GenerateRandom(10)),
  58.                 new DetailStorage(new Detail("Аккумулятор", 2500, false), UserUtils.GenerateRandom(10))
  59.             };
  60.         }
  61.  
  62.         public bool PartsInStock(string partName)
  63.         {
  64.             DetailStorage StorageItem = _partsInStock.FirstOrDefault(i => i.Detail.Name == partName);
  65.  
  66.             if (StorageItem == null || StorageItem.Amount <= 0)
  67.             {
  68.                 Console.WriteLine($"Нет нужной детали: {partName}");
  69.                 return false;
  70.             }
  71.  
  72.             return true;
  73.         }
  74.  
  75.         public bool DecreasePartAmount(string partName)
  76.         {
  77.             DetailStorage StorageItem = _partsInStock.FirstOrDefault(i => i.Detail.Name == partName);
  78.  
  79.             if (StorageItem != null && StorageItem.Amount > 0)
  80.             {
  81.                 StorageItem.SetAmount(-1);
  82.                 return true;
  83.             }
  84.  
  85.             return false;
  86.         }
  87.  
  88.         public List<DetailStorage> GetAllParts()
  89.         {
  90.             return _partsInStock;
  91.         }
  92.     }
  93.  
  94.     public class Car
  95.     {
  96.         public Car(string model, List<Detail> detail)
  97.         {
  98.             Model = model;
  99.             Detail = detail;
  100.             IsRepairStarted = false;
  101.         }
  102.  
  103.         public string Model { get; private set; }
  104.         public List<Detail> Detail { get; private set; }
  105.         public bool IsRepairStarted { get; private set; }
  106.  
  107.         public List<Detail> GetBrokenParts()
  108.         {
  109.             return Detail.Where(i => i.IsBroken).ToList();
  110.         }
  111.  
  112.         public bool IsFullyRepaired()
  113.         {
  114.             return !Detail.Any(i => i.IsBroken);
  115.         }
  116.  
  117.         public void StartRepair()
  118.         {
  119.             IsRepairStarted = true;
  120.         }
  121.  
  122.         public bool ReplacePart(Detail newPart)
  123.         {
  124.             Detail brokenPart = Detail.FirstOrDefault(i => i.Name == newPart.Name);
  125.  
  126.             if (brokenPart != null)
  127.             {
  128.                 Detail.Remove(brokenPart);
  129.                 Detail.Add(newPart);
  130.  
  131.                 return true;
  132.             }
  133.  
  134.             return false;
  135.         }
  136.     }
  137.  
  138.     public class CarCreator
  139.     {
  140.         private readonly List<Car> _cars;
  141.  
  142.         public CarCreator()
  143.         {
  144.             _cars = new List<Car>
  145.             {
  146.                 new Car("Toyota", new List<Detail>
  147.                 {
  148.                     new Detail("Двигатель", 500m, true),
  149.                     new Detail("Фара", 100m, true),
  150.                     new Detail("Тормоза", 150m, false)
  151.                 }),
  152.                 new Car("Honda", new List<Detail>
  153.                 {
  154.                     new Detail("Аккумулятор", 500m, true),
  155.                     new Detail("Двигатель", 100m, false)
  156.                 })
  157.             };
  158.         }
  159.  
  160.         public List<Car> GetCars()
  161.         {
  162.             return _cars;
  163.         }
  164.     }
  165.  
  166.     public class AutoService
  167.     {
  168.         private const decimal RepairFEE = 1000;
  169.         private const decimal CancelBeforeRepairFee = 2000;
  170.         private const decimal CancelDuringRepairFeePerPart = 600;
  171.  
  172.         private readonly CarCreator _carCreator;
  173.         private readonly Queue<Car> _cars;
  174.         private readonly AutoServiceStorage _storage;
  175.         private decimal _balance;
  176.  
  177.         public AutoService()
  178.         {
  179.             _cars = new Queue<Car>();
  180.             _carCreator = new CarCreator();
  181.             _storage = new AutoServiceStorage();
  182.             _balance = UserUtils.GenerateRandom(10000);
  183.  
  184.             AddCarsToQueue();
  185.         }
  186.  
  187.         public void Run()
  188.         {
  189.             const string CommandToRepair = "1";
  190.             const string CommandToCancelRepair = "2";
  191.             const string CommandToExit = "3";
  192.  
  193.             bool isWorking = true;
  194.  
  195.             while (isWorking && _cars.Count > 0 && _balance > 0)
  196.             {
  197.                 Console.Clear();
  198.                 ShowServiceStatus();
  199.  
  200.                 Car currentCar = _cars.Peek();
  201.  
  202.                 Console.WriteLine($"\nТекущая машина: {currentCar.Model}");
  203.                 ShowBrokenParts(currentCar);
  204.  
  205.                 Console.WriteLine("\nВыберите действие:");
  206.                 Console.WriteLine("1 - Отремонтировать деталь");
  207.                 Console.WriteLine("2 - Отказаться от ремонта");
  208.                 Console.WriteLine("3 - Выйти");
  209.  
  210.                 Console.Write("Введите номер команды: ");
  211.                 string userInput = Console.ReadLine();
  212.  
  213.                 switch (userInput)
  214.                 {
  215.                     case CommandToRepair:
  216.                         Console.Write("Введите название детали для ремонта: ");
  217.                         string partName = Console.ReadLine();
  218.  
  219.                         RepairPart(currentCar, partName);
  220.                         Pause();
  221.                         break;
  222.  
  223.                     case CommandToCancelRepair:
  224.                         CancelRepair(currentCar);
  225.                         Pause();
  226.                         break;
  227.  
  228.                     case CommandToExit:
  229.                         isWorking = false;
  230.                         break;
  231.  
  232.                     default:
  233.                         Console.WriteLine("Некорректный ввод");
  234.                         Pause();
  235.                         break;
  236.                 }
  237.             }
  238.  
  239.             Console.WriteLine("\nРабота сервиса завершена.");
  240.             Console.WriteLine($"Итоговый баланс: {_balance}");
  241.         }
  242.  
  243.         private void Pause()
  244.         {
  245.             Console.WriteLine("\nНажмите любую клавишу для продолжения...");
  246.             Console.ReadKey();
  247.         }
  248.  
  249.         private void AddCarsToQueue()
  250.         {
  251.             List<Car> cars = _carCreator.GetCars();
  252.  
  253.             foreach (Car car in cars)
  254.             {
  255.                 _cars.Enqueue(car);
  256.                 Console.WriteLine($"Машина {car.Model} добавлена в очередь");
  257.             }
  258.         }
  259.  
  260.         private void ShowBrokenParts(Car car)
  261.         {
  262.             List<Detail> brokenParts = car.GetBrokenParts();
  263.             Console.WriteLine($"Сломанные детали в {car.Model}:");
  264.  
  265.             foreach (Detail part in brokenParts)
  266.             {
  267.                 Console.WriteLine($"- {part.Name} (Цена: {part.Price})");
  268.             }
  269.  
  270.             if (brokenParts.Count == 0)
  271.             {
  272.                 Console.WriteLine("Все детали исправны.");
  273.             }
  274.         }
  275.  
  276.         private void CancelRepair(Car car)
  277.         {
  278.             decimal penalty;
  279.  
  280.             if (car.IsRepairStarted == false)
  281.             {
  282.                 penalty = CancelBeforeRepairFee;
  283.                 Console.WriteLine($"В ремонте было отказано. Штраф за отказ от услуг: {penalty}");
  284.             }
  285.             else
  286.             {
  287.                 int brokenPartsCount = car.GetBrokenParts().Count;
  288.                 penalty = brokenPartsCount * CancelDuringRepairFeePerPart;
  289.  
  290.                 Console.WriteLine($"Отказ от ремонта во время работы. Штраф: {penalty} ({brokenPartsCount} сломанных деталей)");
  291.             }
  292.  
  293.             _balance -= penalty;
  294.             RemoveCarQueue(car);
  295.         }
  296.  
  297.         private bool RepairPart(Car car, string partName)
  298.         {
  299.             car.StartRepair();
  300.  
  301.             List<Detail> brokenParts = car.GetBrokenParts();
  302.             Detail partToRepair = brokenParts.FirstOrDefault(i => i.Name.Equals(partName, StringComparison.OrdinalIgnoreCase));
  303.  
  304.             if (partToRepair == null)
  305.             {
  306.                 Console.WriteLine($"Деталь {partName} не сломана или не существует");
  307.                 return false;
  308.             }
  309.  
  310.             if (!_storage.PartsInStock(partName))
  311.             {
  312.                 Console.WriteLine($"Нет нужной детали на складе: {partName}");
  313.                 return false;
  314.             }
  315.  
  316.             Detail newPart = new Detail(partName, partToRepair.Price, false);
  317.  
  318.             if (car.ReplacePart(newPart))
  319.             {
  320.                 _storage.DecreasePartAmount(partName);
  321.  
  322.                 _balance += partToRepair.Price + RepairFEE;
  323.                 Console.WriteLine($"Деталь {partName} заменена. Прибыль: {partToRepair.Price + RepairFEE}");
  324.  
  325.                 if (car.IsFullyRepaired())
  326.                 {
  327.                     RemoveCarQueue(car);
  328.                     Console.WriteLine($"Машина {car.Model} полностью отремонтирована и удалена из очереди");
  329.                 }
  330.  
  331.                 return true;
  332.             }
  333.  
  334.             return false;
  335.         }
  336.  
  337.         private void ShowServiceStatus()
  338.         {
  339.             Console.WriteLine($"\nТекущий баланс: {_balance}");
  340.  
  341.             Console.WriteLine("\nМашины в очереди:");
  342.             if (_cars.Count == 0)
  343.             {
  344.                 Console.WriteLine("Очередь пуста.");
  345.             }
  346.             else
  347.             {
  348.                 foreach (Car car in _cars)
  349.                 {
  350.                     Console.WriteLine($"- {car.Model} (Сломанных деталей: {car.GetBrokenParts().Count})");
  351.                 }
  352.             }
  353.  
  354.             Console.WriteLine("\nДетали на складе:");
  355.             foreach (DetailStorage part in _storage.GetAllParts())
  356.             {
  357.                 Console.WriteLine($"- {part.Detail.Name}: {part.Amount} шт. (Цена: {part.Detail.Price})");
  358.             }
  359.         }
  360.  
  361.         private void RemoveCarQueue(Car car)
  362.         {
  363.             Queue<Car> newQueue = new Queue<Car>(_cars.Where(i => i != car));
  364.             _cars.Clear();
  365.  
  366.             foreach (Car i in newQueue)
  367.             {
  368.                 _cars.Enqueue(i);
  369.             }
  370.         }
  371.     }
  372.  
  373.     class UserUtils
  374.     {
  375.         private static Random s_random = new Random();
  376.  
  377.         public static int GenerateRandom(int maxValue)
  378.         {
  379.             return s_random.Next(0, maxValue + 1);
  380.         }
  381.  
  382.         public static int GenerateRandom(int minValue, int maxValue)
  383.         {
  384.             return s_random.Next(minValue, maxValue + 1);
  385.         }
  386.  
  387.         public static int BaseRandomGenerator(int maxValue)
  388.         {
  389.             return s_random.Next(0, maxValue);
  390.         }
  391.  
  392.         public static int BaseRandomGenerator(int minValue, int maxValue)
  393.         {
  394.             return s_random.Next(minValue, maxValue);
  395.         }
  396.     }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement