illiden

AutoService

Apr 8th, 2020
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.62 KB | None | 0 0
  1. using System;
  2.  
  3. namespace AutoService
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Service service = new Service(new Store(), new Storage(), new RandomClientGenerator());
  10.             ServiceMenu serviceMenu = new ServiceMenu(service);
  11.             serviceMenu.MainMenu();
  12.         }
  13.     }
  14.  
  15.     class ServiceMenu
  16.     {
  17.         private Service _service;
  18.  
  19.         public ServiceMenu(Service service)
  20.         {
  21.             _service = service;
  22.         }
  23.  
  24.         public void MainMenu()
  25.         {
  26.             bool work = true;
  27.  
  28.             while (work)
  29.             {
  30.                 Console.Clear();
  31.                 Console.WriteLine("Автосервис ПочиниМошинку");
  32.                 Console.WriteLine("\nПриехал клиент, ему нужно починить " + _service.GetClient());
  33.                 Console.WriteLine("\nВыберите вариант действия: " +
  34.                                     "\n1. Зайти в магазин" +
  35.                                     "\n2. Зайти на склад" +
  36.                                     "\n3. Обслужить клиента " +
  37.                                     "\n4. Отказаться от клиента" +
  38.                                     "\n5. Выход");
  39.  
  40.                 string result = Console.ReadLine();
  41.                 switch (result)
  42.                 {
  43.                     case "1":
  44.                         StoreMenu();
  45.                         break;
  46.                     case "2":
  47.                         Console.WriteLine("У вас на складе лежат " + _service.GetWallet() + " и следующие детали:");
  48.                         Console.WriteLine(_service.GetStorage());
  49.                         break;
  50.                     case "3":
  51.                         ServeMenu();
  52.                         break;
  53.                     case "4":
  54.                         _service.RefuseClient();
  55.                         _service.NextClient();
  56.                         Console.WriteLine("Вы отказались обслуживать клиента и получаете штраф! Ваш баланс " + _service.GetWallet());
  57.                         break;
  58.                     case "5":
  59.                         work = false;
  60.                         Console.WriteLine("Приходи на работу завтра в то же время! И не опаздывай, бездельник!");
  61.                         break;
  62.                     default:
  63.                         Console.WriteLine("Такой команды не существует");
  64.                         break;
  65.                 }
  66.                 Console.ReadKey();
  67.             }
  68.         }
  69.  
  70.         public void StoreMenu()
  71.         {
  72.             bool storeWork = true;
  73.  
  74.             while (storeWork)
  75.             {
  76.                 Console.Clear();
  77.                 Console.WriteLine("Вы в магазине, ваш баланс - " + _service.GetWallet() + ". \nВыберите, что вы хотите сделать: " +
  78.                                "\n1. Открыть список товаров" +
  79.                                "\n2. Купить деталь по номеру из списка" +
  80.                                "\n3. Выход");
  81.                 string storeResult = Console.ReadLine();
  82.                 switch (storeResult)
  83.                 {
  84.                     case "1":
  85.                         Console.Clear();
  86.                         Console.WriteLine("Список доступных товаров: \n" + _service.GetStore());
  87.                         break;
  88.                     case "2":
  89.                         Console.Write("Введите номер покупаемой детали: ");
  90.                         if (_service.SellDetail(Convert.ToInt32(Console.ReadLine())))
  91.                         {
  92.                             Console.WriteLine("Поздравляем с новым приобретением!");
  93.                         }
  94.                         else
  95.                         {
  96.                             Console.WriteLine("Покупка не удалась! Проверьте баланс и номер детали");
  97.                         }
  98.                         break;
  99.                     case "3":
  100.                         storeWork = false;
  101.                         break;
  102.                 }
  103.                 Console.ReadKey();
  104.             }
  105.         }
  106.  
  107.         public void ServeMenu()
  108.         {
  109.             Console.Clear();
  110.             Console.WriteLine(_service.GetStorage());
  111.             Console.Write("Выедите номер детали: ");
  112.             Detail detail = _service.TakeDetailToStorage(Convert.ToInt32(Console.ReadLine()));
  113.             if(detail != null)
  114.             {
  115.                 if (_service.ServeClient(detail))
  116.                 {
  117.                     Console.WriteLine("Клиент обслужен! Ваш баланс: " + _service.GetWallet());
  118.                 }
  119.                 else
  120.                 {
  121.                     Console.WriteLine("Клиент обслужен неверно, вы платите ущерб! Ваш баланс: " + _service.GetWallet());
  122.                 }
  123.             }
  124.             else
  125.             {
  126.                 Console.WriteLine("Вы ввели неверный номер, что расценивается как отказ от клиента!");
  127.                 _service.RefuseClient();
  128.                 Console.WriteLine("Вы отказались обслуживать клиента и получаете штраф! Ваш баланс " + _service.GetWallet());
  129.             }
  130.             _service.NextClient();
  131.         }
  132.     }
  133.  
  134.     class Service
  135.     {
  136.         private Store _store;
  137.         private Storage _storage;
  138.         private RandomClientGenerator _randomClientGenerator;
  139.         private CheckList _currentClient;
  140.         private int _penalty = 1000;
  141.         private int _wallet = 20000;
  142.  
  143.         public Service(Store store, Storage storage, RandomClientGenerator randomClientGenerator)
  144.         {
  145.             _store = store;
  146.             _storage = storage;
  147.             _randomClientGenerator = randomClientGenerator;
  148.             _currentClient = randomClientGenerator.CreateClient();
  149.         }
  150.  
  151.         public int GetWallet()
  152.         {
  153.             return _wallet;
  154.         }
  155.  
  156.         public string GetClient()
  157.         {
  158.             return _currentClient.GetBrokenDetail() + ", за починку заплятят " + _currentClient.GetRepairCost();
  159.         }
  160.  
  161.         public bool ServeClient(Detail detail)
  162.         {
  163.             _storage.RemoveDetail(detail);
  164.             if (detail.Name == _currentClient.GetBrokenDetail())
  165.             {
  166.                 _wallet += _currentClient.GetRepairCost();
  167.                 _currentClient = null;
  168.                 return true;
  169.             }
  170.             else
  171.             {
  172.                 _wallet -= _currentClient.GetDamage(detail);
  173.                 _currentClient = null;
  174.                 return false;
  175.             }
  176.         }
  177.  
  178.         public void RefuseClient()
  179.         {
  180.             _currentClient = null;
  181.             _wallet -= _penalty;
  182.         }
  183.  
  184.         public void NextClient()
  185.         {
  186.             _currentClient = _randomClientGenerator.CreateClient();
  187.         }
  188.  
  189.         public string GetStore()
  190.         {
  191.             return _store.GetStoreList();
  192.         }
  193.  
  194.         public bool SellDetail(int index)
  195.         {
  196.             if(_store.CheckIndex(index))
  197.             {
  198.                 Detail sellDetail = _store.SellDetail(index);
  199.                 if (_wallet < sellDetail.Price)
  200.                 {
  201.                     return false;
  202.                 }
  203.                 else
  204.                 {
  205.                     _wallet -= sellDetail.Price;
  206.                     _storage.AddDetail(sellDetail);
  207.                     return true;
  208.                 }
  209.             }
  210.             else
  211.             {
  212.                 return false;
  213.             }
  214.         }
  215.  
  216.         public string GetStorage()
  217.         {
  218.             return _storage.GetStorageList();
  219.         }
  220.  
  221.         public Detail TakeDetailToStorage(int index)
  222.         {
  223.             return index < _storage.GetStorageDetails().Length ? _storage.GetStorageDetails()[index - 1] : null;
  224.         }
  225.     }
  226.  
  227.     class Store
  228.     {
  229.         private Detail[] _storeDetails = { new Wheel(), new Brakes(), new Engine(),
  230.                                       new Glass(), new HeadLight(), new SteeringWheel(),
  231.                                       new Door(), new Tire(), new CarGrip()};
  232.  
  233.         public string GetStoreList()
  234.         {
  235.             string detailsList = "";
  236.             for (int i = 0; i < _storeDetails.Length; i++)
  237.             {
  238.                 detailsList += (i + 1) + ". " + _storeDetails[i].Name + " - " + _storeDetails[i].Price + "\n";
  239.             }
  240.             return detailsList;
  241.         }
  242.  
  243.         public bool CheckIndex(int index)
  244.         {
  245.             return index < _storeDetails.Length ? true : false;
  246.         }
  247.  
  248.         public Detail SellDetail(int index)
  249.         {
  250.             return _storeDetails[index - 1];
  251.         }
  252.     }
  253.  
  254.     class Storage
  255.     {
  256.         private Detail[] _storageDetails = { new Wheel(), new Brakes(), new Engine(),
  257.                                              new Glass(), new HeadLight(), new SteeringWheel(),
  258.                                              new Door(), new Tire(), new CarGrip(),
  259.                                              new Wheel(), new Brakes(), new Engine(),
  260.                                              new Glass(), new HeadLight(), new SteeringWheel(),
  261.                                              new Door(), new Tire(), new CarGrip()};
  262.  
  263.         public void AddDetail(Detail newDetail)
  264.         {
  265.             Detail[] newStorageDetails = new Detail[_storageDetails.Length + 1];
  266.             for (int i = 0; i < _storageDetails.Length; i++)
  267.             {
  268.                 newStorageDetails[i] = _storageDetails[i];
  269.             }
  270.             newStorageDetails[_storageDetails.Length] = newDetail;
  271.             _storageDetails = newStorageDetails;
  272.         }
  273.  
  274.         public void RemoveDetail(Detail removeDetail)
  275.         {
  276.             Detail[] newStorageDetails = new Detail[_storageDetails.Length - 1];
  277.             int removeDetailIndex = 0;
  278.             for (int i = 0; i < _storageDetails.Length; i++)
  279.             {
  280.                 if(_storageDetails[i].Name == removeDetail.Name)
  281.                 {
  282.                     removeDetailIndex = i;
  283.                     break;
  284.                 }
  285.             }
  286.             int index = 0;
  287.             for (int i = 0; i < newStorageDetails.Length; i++)
  288.             {
  289.                 if(index == removeDetailIndex)
  290.                 {
  291.                     index++;
  292.                 }
  293.                 newStorageDetails[i] = _storageDetails[index];
  294.                 index++;
  295.             }
  296.             _storageDetails = newStorageDetails;
  297.         }
  298.  
  299.         public string GetStorageList()
  300.         {
  301.             string detailList = "";
  302.             for (int i = 0; i < _storageDetails.Length; i++)
  303.             {
  304.                 detailList += (i + 1) + ". " + _storageDetails[i].Name + "\n";
  305.             }
  306.             return detailList;
  307.         }
  308.  
  309.         public Detail[] GetStorageDetails()
  310.         {
  311.             return _storageDetails;
  312.         }
  313.     }
  314.  
  315.     class RandomClientGenerator
  316.     {
  317.         private Detail[] _details = { new Wheel(), new Brakes(), new Engine(),
  318.                                       new Glass(), new HeadLight(), new SteeringWheel(),
  319.                                       new Door(), new Tire(), new CarGrip()};
  320.         private Random _random = new Random();
  321.  
  322.         public CheckList CreateClient()
  323.         {
  324.             return new CheckList(_details[_random.Next(0, _details.Length)]);
  325.         }
  326.     }
  327.  
  328.     class CheckList
  329.     {
  330.         private Detail _brokenDetail;
  331.  
  332.         public CheckList(Detail brokenDeatil)
  333.         {
  334.             _brokenDetail = brokenDeatil;
  335.         }
  336.  
  337.         public string GetBrokenDetail()
  338.         {
  339.             return _brokenDetail.Name;
  340.         }
  341.  
  342.         public int GetRepairCost()
  343.         {
  344.             return _brokenDetail.Price + _brokenDetail.PriceForWork;
  345.         }
  346.  
  347.         public int GetDamage(Detail replacementDetail)
  348.         {
  349.             return replacementDetail.PriceForDamage + _brokenDetail.Price;
  350.         }
  351.     }
  352.  
  353.     class CarGrip : Detail
  354.     {
  355.         public CarGrip()
  356.         {
  357.             Name = "Сцепление";
  358.             Price = 3000;
  359.             PriceForWork = 1500;
  360.             PriceForDamage = 300;
  361.         }
  362.     }
  363.  
  364.     class Tire : Detail
  365.     {
  366.         public Tire()
  367.         {
  368.             Name = "Покрышка";
  369.             Price = 3000;
  370.             PriceForWork = 1500;
  371.             PriceForDamage = 700;
  372.         }
  373.     }
  374.  
  375.     class Door : Detail
  376.     {
  377.         public Door()
  378.         {
  379.             Name = "Дверь";
  380.             Price = 6000;
  381.             PriceForWork = 1500;
  382.             PriceForDamage = 800;
  383.         }
  384.     }
  385.  
  386.     class SteeringWheel : Detail
  387.     {
  388.         public SteeringWheel()
  389.         {
  390.             Name = "Руль";
  391.             Price = 7500;
  392.             PriceForWork = 2000;
  393.             PriceForDamage = 1000;
  394.         }
  395.     }
  396.  
  397.     class HeadLight : Detail
  398.     {
  399.         public HeadLight()
  400.         {
  401.             Name = "Фары";
  402.             Price = 1500;
  403.             PriceForWork = 1000;
  404.             PriceForDamage = 500;
  405.         }
  406.     }
  407.  
  408.     class Glass : Detail
  409.     {
  410.         public Glass()
  411.         {
  412.             Name = "Стекло";
  413.             Price = 1000;
  414.             PriceForWork = 5000;
  415.             PriceForDamage = 2000;
  416.         }
  417.     }
  418.  
  419.     class Engine : Detail
  420.     {
  421.         public Engine()
  422.         {
  423.             Name = "Двигатель";
  424.             Price = 30000;
  425.             PriceForWork = 10000;
  426.             PriceForDamage = 5000;
  427.         }
  428.     }
  429.  
  430.     class Brakes : Detail
  431.     {
  432.         public Brakes()
  433.         {
  434.             Name = "Тормоза";
  435.             Price = 4000;
  436.             PriceForWork = 1000;
  437.             PriceForDamage = 500;
  438.         }
  439.     }
  440.  
  441.     class Wheel : Detail
  442.     {
  443.         public Wheel()
  444.         {
  445.             Name = "Колесо";
  446.             Price = 2000;
  447.             PriceForWork = 500;
  448.             PriceForDamage = 200;
  449.         }
  450.     }
  451.  
  452.     abstract class Detail
  453.     {
  454.         public string Name { get; protected set; }
  455.         public int Price { get; protected set; }
  456.         public int PriceForWork { get; protected set; }
  457.         public int PriceForDamage { get; protected set; }
  458.     }
  459. }
Advertisement
Add Comment
Please, Sign In to add comment