yakovmonarh

Автосервис

Mar 11th, 2026
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.37 KB | None | 0 0
  1. using System.Text;
  2.  
  3. class Program
  4. {
  5.     internal static void Main()
  6.     {
  7.         var carService = new CarService(new Money(10000), new Warehouse(ProductFactory.Create()), CarFactory.Create());
  8.         var menu = new Menu(carService);
  9.         menu.Run();
  10.     }
  11. }
  12.  
  13. public interface IInsertActions
  14. {
  15.     Dictionary<string, Action> GetCommands();
  16.     Func<bool> ExitCommand { get; }
  17. }
  18.  
  19. public class Menu
  20. {
  21.     private const string HelpCommand = "help";
  22.     private const string ExitCommand = "exit";
  23.  
  24.     private readonly Dictionary<string, Action> _actionCommands;
  25.     private readonly Func<bool> _exitFunc;
  26.  
  27.     public Menu(IInsertActions insertActions)
  28.     {
  29.         _actionCommands = insertActions.GetCommands();
  30.         _exitFunc = insertActions.ExitCommand;
  31.     }
  32.  
  33.     public void Run()
  34.     {
  35.         bool isExit = false;
  36.  
  37.         while (isExit == false)
  38.         {
  39.             Console.Write($"Введите команду (список всех команд - '{HelpCommand}'): ");
  40.             string command = Console.ReadLine();
  41.  
  42.             switch (command)
  43.             {
  44.                 case ExitCommand:
  45.                     isExit = _exitFunc.Invoke();
  46.                     break;
  47.  
  48.                 case HelpCommand:
  49.                     Console.WriteLine($"{ExitCommand}, {string.Join(", ", _actionCommands.Keys)}");
  50.                     break;
  51.  
  52.                 default:
  53.                     ExecuteImportedMethod(command);
  54.                     break;
  55.             }
  56.         }
  57.  
  58.     }
  59.  
  60.     private void ExecuteImportedMethod(string command)
  61.     {
  62.         if (_actionCommands.ContainsKey(command))
  63.         {
  64.             _actionCommands[command].Invoke();
  65.         }
  66.         else
  67.         {
  68.             Console.WriteLine("Команда не найдена.");
  69.         }
  70.     }
  71.  
  72. }
  73.  
  74. public static class UserUtils
  75. {
  76.     private static Random s_random = new Random();
  77.  
  78.     public static int GenerateRandomNumber(int min, int max)
  79.     {
  80.         return s_random.Next(min, max);
  81.     }
  82.  
  83.     public static int GenerateRandomNumber(int length)
  84.     {
  85.         return s_random.Next(length);
  86.     }
  87.  
  88.     public static int ReadNumberConsole(int min = int.MinValue, int max = int.MaxValue)
  89.     {
  90.         int resultNumber;
  91.  
  92.         while (int.TryParse(Console.ReadLine(), out resultNumber) == false || resultNumber < min || resultNumber > max)
  93.         {
  94.             Console.Write("Неверный номер. Введите еще.");
  95.         }
  96.  
  97.         return resultNumber;
  98.     }
  99. }
  100.  
  101. public class Award
  102. {
  103.     public Award(int earned, int fine)
  104.     {
  105.         Earned = earned;
  106.         Fine = fine;
  107.     }
  108.  
  109.     public int Earned { get; }
  110.     public int Fine { get; }
  111. }
  112.  
  113. public class Car
  114. {
  115.     private readonly IPart[] _parts;
  116.  
  117.     public Car(string model, string serial, int yearProduction, IPart[] parts)
  118.     {
  119.         Model = model;
  120.         Serial = serial;
  121.         YearProduction = yearProduction;
  122.         _parts = parts;
  123.     }
  124.  
  125.     public int NumberDetails => _parts.Length;
  126.     public string Model { get; }
  127.     public string Serial { get; }
  128.     public int YearProduction { get; }
  129.  
  130.     public string GetInfo()
  131.     {
  132.         var info = new StringBuilder();
  133.  
  134.         for (int i = 0; i < _parts.Length; i++)
  135.         {
  136.             info.AppendLine($"{i}. {_parts[i].GetInfo()}");
  137.         }
  138.  
  139.         return info.ToString();
  140.     }
  141.  
  142.     public bool TryReplacePart(int idPartCar, PartBase partWarehouse, out string message)
  143.     {
  144.         IPart carPart = _parts[idPartCar];
  145.  
  146.         if (carPart == null || partWarehouse == null)
  147.         {
  148.             message = "Отсутствует одна или несколько деталей";
  149.             return false;
  150.         }
  151.  
  152.         if (carPart.Name != partWarehouse.Name)
  153.         {
  154.             message = "Деталь не подходит для замены.";
  155.             return false;
  156.         }
  157.  
  158.         if (carPart.IsBroken == false)
  159.         {
  160.             message = "Деталь не сломана - вы не получите оплату за ремонт целой детали.";
  161.             return false;
  162.         }
  163.  
  164.         _parts[idPartCar] = partWarehouse;
  165.         message = "Сломанная деталь успешно заменена на новую.";
  166.         return true;
  167.     }
  168.  
  169.     public PartBase[] GetCloneParts()
  170.     {
  171.         return _parts.Select(p => p.Clone()).ToArray();
  172.     }
  173. }
  174.  
  175. public class CarFactory
  176. {
  177.     public static Car[] Create()
  178.     {
  179.         return new Car[]
  180.         {
  181.             new Car("Audi", "A4", 2010, PartFactory.CreateParts()),
  182.             new Car("BMW", "X5", 2015, PartFactory.CreateParts()),
  183.             new Car("Mercedes", "S500", 2020, PartFactory.CreateParts())
  184.         };
  185.     }
  186. }
  187.  
  188. class CarService : IInsertActions
  189. {
  190.     private readonly Money _money;
  191.     private readonly Queue<Car> _cars;
  192.     private readonly Warehouse _warehouse;
  193.     private Repair _repair;
  194.  
  195.     public CarService(Money money, Warehouse warehouse, Car[] cars)
  196.     {
  197.         _money = money;
  198.         _cars = new Queue<Car>(cars);
  199.         _warehouse = warehouse;
  200.     }
  201.  
  202.     public Func<bool> ExitCommand => () => true;
  203.  
  204.     public Dictionary<string, Action> GetCommands()
  205.     {
  206.         return new Dictionary<string, Action>()
  207.         {
  208.             {"nextcar", GetInfoNextCar},
  209.             {"rejectcar", RejectCar},
  210.             {"repaircar", RepairCar},
  211.             {"parts", ShowMyParts},
  212.             {"balance", () => Console.WriteLine($"Баланс: {_money.Amount} руб.")}
  213.         };
  214.     }
  215.  
  216.     private void ShowMyParts()
  217.     {
  218.         Console.WriteLine("Детали в складе:");
  219.         Console.WriteLine(_warehouse.GetInfo());
  220.     }
  221.  
  222.     private void GetInfoNextCar()
  223.     {
  224.         Console.WriteLine("Авто из очереди:");
  225.  
  226.         if (_cars.TryPeek(out Car car) == false)
  227.         {
  228.             Console.WriteLine("В очереди нет машин.");
  229.             return;
  230.         }
  231.  
  232.         Console.WriteLine(car.GetInfo());
  233.     }
  234.  
  235.     private void RejectCar()
  236.     {
  237.         Order order = new Order();
  238.  
  239.         if (TryCalcMoney(order))
  240.         {
  241.             _cars.Dequeue();
  242.         }
  243.  
  244.     }
  245.  
  246.     private void RepairCar()
  247.     {
  248.         if (_repair != null)
  249.         {
  250.             Console.WriteLine("Текущий ремонт еще не закончен.");
  251.             return;
  252.         }
  253.  
  254.         if (_cars.TryDequeue(out Car car) == false)
  255.         {
  256.             Console.WriteLine("В очереди нет машин.");
  257.             return;
  258.         }
  259.  
  260.         Order order = new Order(car.GetCloneParts());
  261.  
  262.         _repair = new Repair(car, _warehouse);
  263.         Console.WriteLine("Авто отправлено на ремонт.");
  264.         _repair.Work();
  265.  
  266.         if (TryCalcMoney(order, car.GetCloneParts()))
  267.         {
  268.             _repair = null;
  269.         }
  270.         else
  271.         {
  272.             Console.WriteLine("Автосервис разорен.");
  273.         }
  274.     }
  275.  
  276.     private bool TryCalcMoney(Order order, PartBase[] parts = null)
  277.     {
  278.         Award award = order.CalculateMoney(parts);
  279.         _money.TryAdd(award.Earned);
  280.  
  281.         if (_money.TryRemove(award.Fine) == false)
  282.         {
  283.             Console.WriteLine("Автосервис не может оплатить штарф и поэтому разорен.");
  284.             return false;
  285.         }
  286.  
  287.         Console.WriteLine($"Штраф составил: {award.Fine} руб.");
  288.         Console.WriteLine($"Вы заработали: {award.Earned} руб.");
  289.         Console.WriteLine($"Баланс: {_money.Amount} руб.");
  290.         return true;
  291.     }
  292. }
  293.  
  294. public class Engine : PartBase
  295. {
  296.     public Engine(bool isBroken) : base(isBroken)
  297.     {
  298.     }
  299.  
  300.     public override string Name => "Двигатель";
  301.  
  302.     public override int Price => 100000;
  303.  
  304.     public override int PriceRepair => 10000;
  305.  
  306.     public override int Fine => 2000;
  307.  
  308.     public override PartBase Clone()
  309.     {
  310.         return new Engine(IsBroken);
  311.     }
  312. }
  313.  
  314. public class Filter : PartBase
  315. {
  316.     public Filter(bool isBroken) : base(isBroken)
  317.     {
  318.     }
  319.  
  320.     public override string Name => "Фильтр";
  321.  
  322.     public override int Price => 1000;
  323.  
  324.     public override int PriceRepair => 100;
  325.  
  326.     public override int Fine => 20;
  327.  
  328.     public override PartBase Clone()
  329.     {
  330.         return new Filter(IsBroken);
  331.     }
  332. }
  333.  
  334. public class Gearbox : PartBase
  335. {
  336.     public Gearbox(bool isBroken) : base(isBroken)
  337.     {
  338.     }
  339.  
  340.     public override string Name => "Коробка передач";
  341.  
  342.     public override int Price => 10000;
  343.  
  344.     public override int PriceRepair => 1000;
  345.  
  346.     public override int Fine => 200;
  347.  
  348.     public override PartBase Clone()
  349.     {
  350.         return new Gearbox(IsBroken);
  351.     }
  352. }
  353.  
  354. public interface IPart
  355. {
  356.     string Name { get; }
  357.  
  358.     bool IsBroken { get; }
  359.  
  360.     PartBase Clone();
  361.    
  362.     string GetInfo();
  363. }
  364.  
  365. public interface IPrice : IPart
  366. {
  367.     int Price { get; }
  368.  
  369.     int PriceRepair { get; }
  370.  
  371.     int Fine { get; }
  372. }
  373.  
  374. class Money
  375. {
  376.     public Money(int amount)
  377.     {
  378.         Amount = amount;
  379.     }
  380.  
  381.     public int Amount { get; private set; }
  382.  
  383.     public bool TryAdd(int amount)
  384.     {
  385.         if (amount < 0)
  386.         {
  387.             return false;
  388.         }
  389.  
  390.         Amount += amount;
  391.         return true;
  392.     }
  393.  
  394.     public bool TryRemove(int amount)
  395.     {
  396.         if (amount > Amount)
  397.         {
  398.             return false;
  399.         }
  400.  
  401.         Amount -= amount;
  402.         return true;
  403.     }
  404. }
  405.  
  406. public class Muffler : PartBase
  407. {
  408.     public Muffler(bool isBroken) : base(isBroken)
  409.     {
  410.     }
  411.  
  412.     public override string Name => "Глушитель";
  413.  
  414.     public override int Price => 1000;
  415.  
  416.     public override int PriceRepair => 100;
  417.  
  418.     public override int Fine => 20;
  419.  
  420.     public override PartBase Clone()
  421.     {
  422.         return new Muffler(IsBroken);
  423.     }
  424. }
  425.  
  426. public class Order
  427. {
  428.     private const int FixFine = 1000;
  429.  
  430.     private readonly Dictionary<int, Product> _brokenParts = new();
  431.  
  432.     public Order(IPrice[] parts)
  433.     {
  434.         for (int i = 0; i < parts.Length; i++)
  435.         {
  436.             var part = parts[i];
  437.  
  438.             if (part.IsBroken == false)
  439.             {
  440.                 continue;
  441.             }
  442.  
  443.             _brokenParts.Add(i, new Product((PartBase)part));
  444.         }
  445.     }
  446.  
  447.     public Order()
  448.     {
  449.        
  450.     }
  451.  
  452.     public bool IsRepair => _brokenParts.Count > 0;
  453.  
  454.     public Award CalculateMoney(PartBase[] carParts = null)
  455.     {
  456.         int earned = 0;
  457.         int fine = 0;
  458.  
  459.         if (IsRepair == false)
  460.         {
  461.             return new Award (0, FixFine);
  462.         }
  463.  
  464.         foreach (var product in _brokenParts)
  465.         {
  466.             if (carParts[product.Key].IsBroken)
  467.             {
  468.                 fine += product.Value.Fine;
  469.             }
  470.             else
  471.             {
  472.                 earned += product.Value.Price;
  473.             }
  474.         }
  475.  
  476.         return new Award(earned, fine);
  477.     }
  478.  
  479. }
  480.  
  481. public abstract class PartBase : IPart, IPrice
  482. {
  483.     public PartBase(bool isBroken)
  484.     {
  485.         IsBroken = isBroken;
  486.     }
  487.  
  488.     public abstract string Name { get; }
  489.     public bool IsBroken { get; }
  490.  
  491.     public abstract PartBase Clone();
  492.     public abstract int Price { get; }
  493.     public abstract int PriceRepair { get; }
  494.     public abstract int Fine { get; }
  495.  
  496.     public string GetInfo()
  497.     {
  498.         string status = IsBroken ? "сломана" : "работает";
  499.         return $"{Name}, состояние - {status}.";
  500.     }
  501. }
  502.  
  503. public class PartFactory
  504. {
  505.     public static IPart[] CreateParts()
  506.     {
  507.         const int minRange = 0;
  508.         const int maxRange = 2;
  509.        
  510.         IPart[] parts = new PartBase[]
  511.         {
  512.             new Wheel(UserUtils.GenerateRandomNumber(minRange, maxRange) > 0),
  513.             new Suspension(UserUtils.GenerateRandomNumber(minRange, maxRange) > 0),
  514.             new Muffler(UserUtils.GenerateRandomNumber(minRange, maxRange) > 0),
  515.             new Gearbox(UserUtils.GenerateRandomNumber(minRange, maxRange) > 0),
  516.             new Filter(UserUtils.GenerateRandomNumber(minRange, maxRange) > 0),
  517.             new Engine(UserUtils.GenerateRandomNumber(minRange, maxRange) > 0)
  518.         };
  519.  
  520.         if (parts.Where(p => p.IsBroken == true).Count() == 0)
  521.         {
  522.             parts[UserUtils.GenerateRandomNumber(0, parts.Length)] = new Wheel(true);
  523.         }
  524.  
  525.         return parts;
  526.     }
  527. }
  528.  
  529. public class Product
  530. {
  531.     private readonly PartBase _part;
  532.  
  533.     public Product(PartBase part, int count = 1)
  534.     {
  535.         _part = part;
  536.         Count = count;
  537.     }
  538.  
  539.     public int Count { get; private set; }
  540.     public int Price => _part.Price;
  541.     public int PriceRepair => _part.PriceRepair;
  542.     public int Fine => _part.Fine;
  543.     public string Name => _part.Name;
  544.  
  545.     public bool TryGetPart(out PartBase part)
  546.     {
  547.         if (Count == 0)
  548.         {
  549.             part = null;
  550.             return false;
  551.         }
  552.  
  553.         part = _part.Clone();
  554.         Count--;
  555.         return true;
  556.     }
  557. }
  558.  
  559. public class ProductFactory
  560. {
  561.     public static Product[] Create(int minRange = 1, int maxRange = 5)
  562.     {
  563.         return new Product[]
  564.         {
  565.             new Product(new Engine(false), UserUtils.GenerateRandomNumber(minRange, maxRange)),
  566.             new Product(new Filter(false), UserUtils.GenerateRandomNumber(minRange, maxRange)),
  567.             new Product(new Gearbox(false), UserUtils.GenerateRandomNumber(minRange, maxRange)),
  568.             new Product(new Muffler(false), UserUtils.GenerateRandomNumber(minRange, maxRange)),
  569.             new Product(new SparkPlug(false), UserUtils.GenerateRandomNumber(minRange, maxRange)),
  570.             new Product(new Suspension(false), UserUtils.GenerateRandomNumber(minRange, maxRange)),
  571.             new Product(new Wheel(false), UserUtils.GenerateRandomNumber(minRange, maxRange)),
  572.         };
  573.     }
  574. }
  575.  
  576. public class Repair
  577. {
  578.     private readonly Car _car;
  579.     private readonly Warehouse _warehouse;
  580.  
  581.     public Repair(Car car, Warehouse warehouse)
  582.     {
  583.         _car = car;
  584.         _warehouse = warehouse;
  585.     }
  586.  
  587.     public void Work()
  588.     {
  589.         const string ExitCommand = "exit";
  590.         const string PartCommand = "part";
  591.  
  592.         bool isExit = false;
  593.         string command = "";
  594.  
  595.         while (isExit == false)
  596.         {
  597.             Console.WriteLine($"{ExitCommand} - прекратить ремонт, {PartCommand} - заменить деталь.");
  598.             command = Console.ReadLine();
  599.  
  600.             switch (command)
  601.             {
  602.                 case PartCommand:
  603.                     ReplacPart();
  604.                     break;
  605.  
  606.                 case ExitCommand:
  607.                     isExit = true;
  608.                     break;
  609.  
  610.                 default:
  611.                     Console.WriteLine($"Неизвестная команда - \"{command}\".");
  612.                     break;
  613.             }
  614.         }
  615.  
  616.     }
  617.  
  618.     private void ReplacPart()
  619.     {
  620.         Console.WriteLine("Детали авто:");
  621.         Console.WriteLine(_car.GetInfo());
  622.         Console.Write("Введите номер детали авто: ");
  623.         int idPartCar = UserUtils.ReadNumberConsole(0, _car.NumberDetails);
  624.         Console.WriteLine();
  625.  
  626.         Console.WriteLine("Детали склада:");
  627.         Console.WriteLine();
  628.         Console.WriteLine(_warehouse.GetInfo());
  629.         Console.WriteLine();
  630.         Console.Write("Введите номер детали склада: ");
  631.         int idPartWarehouse = UserUtils.ReadNumberConsole(0, _warehouse.CountDetails);
  632.  
  633.         if (_warehouse.TryGetProduct(idPartWarehouse, out PartBase partWarehouse, out string errorGetPart) == false)
  634.         {
  635.             Console.WriteLine(errorGetPart);
  636.             return;
  637.         }
  638.  
  639.         if (_car.TryReplacePart(idPartCar, partWarehouse, out string messageReplacePart) == false)
  640.         {
  641.             Console.WriteLine($"Ошибка при замене детали: {messageReplacePart}");
  642.             return;
  643.         }
  644.  
  645.         Console.WriteLine(messageReplacePart);
  646.     }
  647. }
  648.  
  649. public class SparkPlug : PartBase
  650. {
  651.     public SparkPlug(bool isBroken) : base(isBroken)
  652.     {
  653.     }
  654.  
  655.     public override string Name => "Свеча зажигания";
  656.  
  657.     public override int Price => 100;
  658.  
  659.     public override int PriceRepair => 10;
  660.  
  661.     public override int Fine => 2;
  662.  
  663.     public override PartBase Clone()
  664.     {
  665.         return new SparkPlug(IsBroken);
  666.     }
  667. }
  668.  
  669. public class Suspension : PartBase
  670. {
  671.     public Suspension(bool isBroken) : base(isBroken)
  672.     {
  673.     }
  674.  
  675.     public override string Name => "Подвеска";
  676.  
  677.     public override int Price => 1000;
  678.  
  679.     public override int PriceRepair => 100;
  680.  
  681.     public override int Fine => 20;
  682.  
  683.     public override PartBase Clone()
  684.     {
  685.         return new Suspension(IsBroken);
  686.     }
  687. }
  688.  
  689. public class Warehouse
  690. {
  691.     private readonly Product[] _products;
  692.  
  693.     public Warehouse(Product[] products)
  694.     {
  695.         _products = products;
  696.     }
  697.  
  698.     public int CountDetails => _products.Sum(p => p.Count);
  699.  
  700.     public string GetInfo()
  701.     {
  702.         var info = new StringBuilder();
  703.  
  704.         for (int i = 0; i < _products.Length; i++)
  705.         {
  706.             info.AppendLine($"{i}. {_products[i].Name}, цена - {_products[i].Price}, цена замены - {_products[i].PriceRepair}, количество - {_products[i].Count} шт.");
  707.         }
  708.  
  709.         return info.ToString();
  710.     }
  711.  
  712.     public bool TryGetProduct(int idProduct, out PartBase part, out string errorMessage)
  713.     {
  714.         if (idProduct < 0 || idProduct >= _products.Length)
  715.         {
  716.             part = null;
  717.             errorMessage = $"Неверный номер детали: {idProduct}";
  718.             return false;
  719.         }
  720.  
  721.         if (_products[idProduct].TryGetPart(out part) == false)
  722.         {
  723.             errorMessage = "Детали закончились";
  724.             return false;
  725.         }
  726.  
  727.         errorMessage = string.Empty;
  728.         return true;
  729.     }
  730. }
  731.  
  732. public class Wheel : PartBase
  733. {
  734.     public Wheel(bool isBroken) : base(isBroken)
  735.     {
  736.     }
  737.  
  738.     public override string Name => "Колесо";
  739.  
  740.     public override int Price => 5000;
  741.  
  742.     public override int PriceRepair => 500;
  743.  
  744.     public override int Fine => 100;
  745.  
  746.     public override PartBase Clone()
  747.     {
  748.         return new Wheel(IsBroken);
  749.     }
  750. }
Advertisement
Add Comment
Please, Sign In to add comment