Advertisement
Torgach

FixService

May 11th, 2021
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Service
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Menu menu = new Menu();
  14.             menu.ShowMainMenu();
  15.         }
  16.     }
  17.  
  18. class Menu
  19.     {
  20.         private AutoService _autoService;
  21.         private Shop _shop;
  22.  
  23.         public Menu()
  24.         {
  25.             _autoService = new AutoService();
  26.             _shop = new Shop();
  27.         }
  28.  
  29.         public void ShowMainMenu()
  30.         {
  31.             while(_autoService.IsServiceWork())
  32.             {
  33.                 _autoService.ShowBalance();
  34.                 Console.WriteLine();
  35.                 _autoService.ShowWarehouse();
  36.                 Console.WriteLine();
  37.                 _autoService.ShowBreakages();
  38.  
  39.                 Console.WriteLine("\n[1] - приступить к починке\n" +
  40.                     "[2] - Заказать деталь в магазине\n" +
  41.                     "[3] - выход");
  42.                 Console.Write("Ввод: ");
  43.  
  44.                 switch(Console.ReadLine())
  45.                 {
  46.                     case "1":
  47.                         _autoService.ChooseBrokenDetail();
  48.                         break;
  49.                     case "2":
  50.                         _autoService.SignContract(_shop);
  51.                         break;
  52.                     case "3":
  53.                         return;
  54.                     default:
  55.                         Console.WriteLine("Ошибка комманды!");
  56.                         break;
  57.                 }
  58.  
  59.                 if(_autoService.IsCarRepaired())
  60.                 {
  61.                     _autoService.CreateNewClient();
  62.                 }
  63.  
  64.                 _shop.CheckRefreshCounter();
  65.  
  66.                 Console.ReadKey();
  67.                 Console.Clear();
  68.             }
  69.         }
  70.     }
  71.  
  72.  class AutoService
  73.     {
  74.         private DetailWarehouse _detailsWarehouse;
  75.         private Car _clientCar;
  76.         private int _fine;
  77.  
  78.         public int WorkCost { get; private set; }
  79.         public int Balance { get; private set; }
  80.  
  81.         public AutoService()
  82.         {
  83.             _detailsWarehouse = new DetailWarehouse();
  84.             _clientCar = new Car();
  85.             Balance = 2500;
  86.             WorkCost = 5000;
  87.             _fine = 2500;
  88.         }
  89.  
  90.         public bool IsServiceWork()
  91.         {
  92.             if (IsWarehouseEmpty() || CheckNegativeBalance())
  93.             {
  94.                 Console.WriteLine("Запасов больше нет! Закрываем гараж.");
  95.                 return false;
  96.             }
  97.             return true;
  98.         }
  99.  
  100.         public void ShowBalance()
  101.         {
  102.             Console.WriteLine($"Баланс: {Balance}");
  103.         }
  104.  
  105.         public void ShowBreakages()
  106.         {
  107.             _clientCar.ShowBrokenDetails();
  108.         }
  109.  
  110.         public void ChooseBrokenDetail()
  111.         {
  112.             Console.WriteLine("Выберите деталь, которую вы хотите починить: \n" +
  113.                 "[1] - Двигатель\n" +
  114.                 "[2] - Подвеска\n" +
  115.                 "[3] - Проводка\n" +
  116.                 "[4] - Кузов");
  117.             Console.Write("Ввод: ");
  118.  
  119.             switch(Console.ReadLine())
  120.             {
  121.                 case "1":
  122.                     RepairDetail("Двигатель");
  123.                     break;
  124.                 case "2":
  125.                     RepairDetail("Подвеска");
  126.                     break;
  127.                 case "3":
  128.                     RepairDetail("Проводка");
  129.                     break;
  130.                 case "4":
  131.                     RepairDetail("Кузов");
  132.                     break;
  133.             }
  134.         }
  135.        
  136.         public void SignContract(Shop shop)
  137.         {
  138.             if (Balance == 0)
  139.             {
  140.                 Console.WriteLine("Ошибка! Нет предоплаты");
  141.                 return;
  142.             }
  143.  
  144.             try
  145.             {
  146.                 Balance -= shop.MakePrepayment(out string productName);
  147.                 _detailsWarehouse.AddDetail(shop.BuyProduct(productName));
  148.             }
  149.             catch
  150.             {
  151.                 new Exception("Ошибка магазина!");
  152.             }
  153.         }
  154.  
  155.         public bool CheckNegativeBalance()
  156.         {
  157.             return Balance <= 0;
  158.         }
  159.  
  160.         public bool IsWarehouseEmpty()
  161.         {
  162.             return _detailsWarehouse.IsSpaceDetailsEmpty();
  163.         }
  164.  
  165.         public bool IsCarRepaired()
  166.         {
  167.             return _clientCar.IsDetailsBroken() == false;
  168.         }
  169.         public void CreateNewClient()
  170.         {
  171.             _clientCar = new Car();
  172.         }
  173.  
  174.         public void ShowWarehouse()
  175.         {
  176.             _detailsWarehouse.ShowSpaceDetails();
  177.         }
  178.  
  179.         private void RepairDetail(string detailName)
  180.         {
  181.             Detail detail = _clientCar.GetBrokenDetailByName(detailName);
  182.  
  183.             if (detail.IsBroken == false)
  184.             {
  185.                 Console.WriteLine("Деталь в рабочем состоянии!");
  186.                 _detailsWarehouse.TakeDetail(detail.Name);
  187.                 GetFine();
  188.                 return;
  189.             }
  190.  
  191.             _clientCar.ReplaceDetail(_detailsWarehouse.TakeDetail(detail.Name));
  192.             GetProfit(_detailsWarehouse.GetPrice(detail.Name));
  193.         }
  194.  
  195.         private void GetFine()
  196.         {
  197.             Balance -= _fine;
  198.         }
  199.  
  200.         private void GetProfit(int detailPrice)
  201.         {
  202.             Balance += detailPrice + WorkCost;
  203.         }
  204.     }
  205.  
  206.  
  207. abstract class Warehouse
  208.     {
  209.         protected Dictionary<SpaceDetail, int> SpareDetails;
  210.  
  211.         public void ShowSpaceDetails()
  212.         {
  213.             foreach (var spareDetail in SpareDetails)
  214.             {
  215.                 Console.WriteLine($"{spareDetail.Key.Name } - {spareDetail.Value}");
  216.             }
  217.         }
  218.  
  219.         public Detail TakeDetail(string detailName)
  220.         {
  221.             foreach (var requiredDetail in SpareDetails)
  222.             {
  223.                 if (requiredDetail.Key.Name == detailName)
  224.                 {
  225.                     SpareDetails[requiredDetail.Key] -= 1;
  226.                     return requiredDetail.Key;
  227.                 }
  228.             }
  229.  
  230.             throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе!");
  231.         }
  232.  
  233.         public int GetPrice(string detailName)
  234.         {
  235.             foreach (var requiredDetail in SpareDetails)
  236.             {
  237.                 if (requiredDetail.Key.Name == detailName)
  238.                 {
  239.                     return requiredDetail.Key.Price;
  240.                 }
  241.             }
  242.             throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе! Цену получить нельзя!");
  243.         }
  244.  
  245.         public bool IsSpaceDetailsEmpty()
  246.         {
  247.             foreach (var spareDetail in SpareDetails)
  248.             {
  249.                 if (spareDetail.Value != 0)
  250.                 {
  251.                     return false;
  252.                 }
  253.             }
  254.             return true;
  255.         }
  256.     }
  257.  
  258. class Shop : Warehouse
  259.     {
  260.         private int _maxProducts;
  261.  
  262.         public int RefreshCounter { get; private set; }
  263.  
  264.         public Shop()
  265.         {
  266.             _maxProducts = 7;
  267.             RefreshCounter = 5;
  268.  
  269.             Detail Engine = new SpaceDetail("Двигатель", true, 10000);
  270.             SpaceDetail spaceEngine = Engine as SpaceDetail;
  271.  
  272.             Detail Suspension = new SpaceDetail("Подвеска", true, 7000);
  273.             SpaceDetail spaceSuspencion = Suspension as SpaceDetail;
  274.  
  275.             Detail Wiring = new SpaceDetail("Проводка", true, 5000);
  276.             SpaceDetail spaceWiring = Wiring as SpaceDetail;
  277.  
  278.             Detail Body = new SpaceDetail("Кузов", true, 2500);
  279.             SpaceDetail spaceBody = Body as SpaceDetail;
  280.  
  281.             SpareDetails = new Dictionary<SpaceDetail, int>
  282.             {
  283.                 { spaceEngine, 7 },
  284.                 { spaceSuspencion, 7 },
  285.                 { spaceWiring, 7 },
  286.                 { spaceBody, 7 }
  287.             };
  288.         }
  289.  
  290.         public int MakePrepayment(out string productName)
  291.         {
  292.             Console.WriteLine("Какой товар вы хотите купить?\n" +
  293.                     "[1] - Двигатель\n" +
  294.                     "[2] - Подвеска\n" +
  295.                     "[3] - Проводка\n" +
  296.                     "[4] - Кузов\n" +
  297.                     "[5] - Выйти из каталога");
  298.             Console.Write("Ввод: ");
  299.  
  300.             switch (Console.ReadLine())
  301.             {
  302.                 case "1":
  303.                     productName = "Двигатель";
  304.                     return GetPrice("Двигатель");
  305.                 case "2":
  306.                     productName = "Подвеска";
  307.                     return GetPrice("Подвеска");
  308.                 case "3":
  309.                     productName = "Проводка";
  310.                     return GetPrice("Проводка");
  311.                 case "4":
  312.                     productName = "Кузов";
  313.                     return GetPrice("Кузов");
  314.             }
  315.  
  316.             throw new Exception("Такой детали нет в каталоге!");
  317.         }
  318.  
  319.         public Detail BuyProduct(string productName)
  320.         {
  321.             return TakeDetail(productName);
  322.         }
  323.  
  324.         public void CheckRefreshCounter()
  325.         {
  326.             if(RefreshCounter == 0)
  327.             {
  328.                 UpdateProducts();
  329.                 RefreshCounter = 6;
  330.             }
  331.  
  332.             RefreshCounter -= 1;
  333.         }
  334.  
  335.         private void UpdateProducts()
  336.         {
  337.             foreach (var product in SpareDetails)
  338.             {
  339.                 if(product.Value != _maxProducts)
  340.                 {
  341.                     int productsNumber = _maxProducts - product.Value;
  342.                     SpareDetails[product.Key] += productsNumber;
  343.                 }
  344.             }
  345.         }
  346.     }
  347.  
  348. class DetailWarehouse : Warehouse
  349.     {
  350.         public DetailWarehouse()
  351.         {
  352.             Detail Engine = new SpaceDetail("Двигатель", true, 20000);
  353.             SpaceDetail spaceEngine = Engine as SpaceDetail;
  354.  
  355.             Detail Suspension = new SpaceDetail("Подвеска", true, 15000);
  356.             SpaceDetail spaceSuspencion = Suspension as SpaceDetail;
  357.  
  358.             Detail Wiring = new SpaceDetail("Проводка", true, 10000);
  359.             SpaceDetail spaceWiring = Wiring as SpaceDetail;
  360.  
  361.             Detail Body = new SpaceDetail("Кузов", true, 5000);
  362.             SpaceDetail spaceBody = Body as SpaceDetail;
  363.  
  364.             SpareDetails = new Dictionary<SpaceDetail, int>
  365.             {
  366.                 { spaceEngine, 5 },
  367.                 { spaceSuspencion, 5 },
  368.                 { spaceWiring, 5 },
  369.                 { spaceBody, 5 }
  370.             };
  371.         }
  372.  
  373.         public void AddDetail(Detail detail)
  374.         {
  375.             foreach (var spareDetail in SpareDetails.ToArray())
  376.             {
  377.                 if(spareDetail.Key.Name == detail.Name)
  378.                 {
  379.                     SpareDetails[spareDetail.Key] += 1;
  380.                     return;
  381.                 }
  382.             }
  383.         }
  384.     }
  385.  
  386.  class Car
  387.     {
  388.         static private Random _random = new Random();
  389.  
  390.         private Detail _engine;
  391.         private Detail _suspension;
  392.         private Detail _wiring;
  393.         private Detail _body;
  394.  
  395.         private List<Detail> _details;
  396.  
  397.         public Car()
  398.         {
  399.             _engine = new Detail("Двигатель", TryToBreak());
  400.             _suspension = new Detail("Подвеска", TryToBreak());
  401.             _wiring = new Detail("Проводка", TryToBreak());
  402.             _body = new Detail("Кузов", TryToBreak());
  403.  
  404.             _details = new List<Detail>()
  405.             {
  406.                 _engine,
  407.                 _suspension,
  408.                 _wiring,
  409.                 _body
  410.             };
  411.  
  412.             CheckCreatedDetails();
  413.         }
  414.  
  415.         public void ReplaceDetail(Detail newDetail)
  416.         {
  417.             for (int i = 0; i < _details.Count; i++)
  418.             {
  419.                 if(_details[i].Name == newDetail.Name)
  420.                 {
  421.                     _details[i] = newDetail;
  422.                 }
  423.             }
  424.         }
  425.  
  426.         public Detail GetBrokenDetailByName(string detailName)
  427.         {
  428.             foreach (var brokenDetail in _details)
  429.             {
  430.                 if (brokenDetail.Name == detailName)
  431.                 {
  432.                     return brokenDetail;
  433.                 }
  434.             }
  435.  
  436.             throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе!");
  437.         }
  438.  
  439.         public bool IsDetailsBroken()
  440.         {
  441.             foreach (var detail in _details)
  442.             {
  443.                 if (detail.IsBroken)
  444.                     return true;
  445.             }
  446.             return false;
  447.         }
  448.  
  449.         public void ShowBrokenDetails()
  450.         {
  451.             foreach (var brokenDetail in _details)
  452.             {
  453.                 if (brokenDetail.IsBroken)
  454.                 {
  455.                     Console.WriteLine($"{brokenDetail.Name} в критическом состоянии!");
  456.                 }
  457.             }
  458.         }
  459.  
  460.         private bool TryToBreak()
  461.         {
  462.             return Convert.ToBoolean(_random.Next(0, 2));
  463.         }
  464.  
  465.         private void CheckCreatedDetails()
  466.         {
  467.             List<Detail> details = new List<Detail>();
  468.  
  469.             foreach (var detail in _details)
  470.             {
  471.                 if(detail.IsBroken == false)
  472.                 {
  473.                     details.Add(detail);
  474.                 }
  475.                 else
  476.                 {
  477.                     return;
  478.                 }
  479.             }
  480.             details[_random.Next(0, details.Count)].BreakDetail();
  481.         }
  482.     }
  483.  
  484. class Detail
  485.     {
  486.         public string Name { get; protected set; }
  487.         public bool IsBroken { get; protected set; }
  488.  
  489.         public Detail(string name, bool isBroken)
  490.         {
  491.             Name = name;
  492.             IsBroken = isBroken;
  493.         }
  494.  
  495.         public void BreakDetail()
  496.         {
  497.             IsBroken = true;
  498.         }
  499.     }
  500.  
  501. class SpaceDetail : Detail
  502.     {
  503.         public int Price { get; private set; }
  504.  
  505.         public SpaceDetail(string name, bool isBroken, int price) : base(name, isBroken)
  506.         {
  507.             IsBroken = false;
  508.             Price = price;
  509.         }
  510.     }
  511.  
  512. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement