Advertisement
illiden

AutoService v2

Apr 9th, 2020
945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.56 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.Wallet + " и следующие детали:");
  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.Wallet);
  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.Wallet + ". \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.Wallet);
  118.                 }
  119.                 else
  120.                 {
  121.                     Console.WriteLine("Клиент обслужен неверно, вы платите ущерб! Ваш баланс: " + _service.Wallet);
  122.                 }
  123.             }
  124.             else
  125.             {
  126.                 Console.WriteLine("Вы ввели неверный номер, что расценивается как отказ от клиента!");
  127.                 _service.RefuseClient();
  128.                 Console.WriteLine("Вы отказались обслуживать клиента и получаете штраф! Ваш баланс " + _service.Wallet);
  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.  
  142.         public int Wallet { get; private set; }
  143.  
  144.         public Service(Store store, Storage storage, RandomClientGenerator randomClientGenerator, int wallet = 20000)
  145.         {
  146.             _store = store;
  147.             _storage = storage;
  148.             _randomClientGenerator = randomClientGenerator;
  149.             _currentClient = randomClientGenerator.CreateClient();
  150.             Wallet = wallet;
  151.         }
  152.  
  153.         public string GetClient()
  154.         {
  155.             return _currentClient.GetBrokenDetail() + ", за починку заплятят " + _currentClient.GetRepairCost();
  156.         }
  157.  
  158.         public bool ServeClient(Detail detail)
  159.         {
  160.             _storage.RemoveDetail(detail);
  161.             if (detail.Name == _currentClient.GetBrokenDetail())
  162.             {
  163.                 Wallet += _currentClient.GetRepairCost();
  164.                 _currentClient = null;
  165.                 return true;
  166.             }
  167.             else
  168.             {
  169.                 Wallet -= _currentClient.GetDamage(detail);
  170.                 _currentClient = null;
  171.                 return false;
  172.             }
  173.         }
  174.  
  175.         public void RefuseClient()
  176.         {
  177.             _currentClient = null;
  178.             Wallet -= _penalty;
  179.         }
  180.  
  181.         public void NextClient()
  182.         {
  183.             _currentClient = _randomClientGenerator.CreateClient();
  184.         }
  185.  
  186.         public string GetStore()
  187.         {
  188.             return _store.GetStoreList();
  189.         }
  190.  
  191.         public bool SellDetail(int index)
  192.         {
  193.             if(_store.CheckIndex(index))
  194.             {
  195.                 Detail sellDetail = _store.SellDetail(index);
  196.                 if (Wallet < sellDetail.Price)
  197.                 {
  198.                     return false;
  199.                 }
  200.                 else
  201.                 {
  202.                     Wallet -= sellDetail.Price;
  203.                     _storage.AddDetail(sellDetail);
  204.                     return true;
  205.                 }
  206.             }
  207.             else
  208.             {
  209.                 return false;
  210.             }
  211.         }
  212.  
  213.         public string GetStorage()
  214.         {
  215.             return _storage.GetStorageList();
  216.         }
  217.  
  218.         public Detail TakeDetailToStorage(int index)
  219.         {
  220.             return index < _storage.StorageDetails.Length ? _storage.StorageDetails[index - 1] : null;
  221.         }
  222.     }
  223.  
  224.     class Store
  225.     {
  226.         private Detail[] _storeDetails = { new Wheel(), new Brakes(), new Engine(),
  227.                                       new Glass(), new HeadLight(), new SteeringWheel(),
  228.                                       new Door(), new Tire(), new CarGrip()};
  229.  
  230.         public string GetStoreList()
  231.         {
  232.             string detailsList = "";
  233.             for (int i = 0; i < _storeDetails.Length; i++)
  234.             {
  235.                 detailsList += (i + 1) + ". " + _storeDetails[i].Name + " - " + _storeDetails[i].Price + "\n";
  236.             }
  237.             return detailsList;
  238.         }
  239.  
  240.         public bool CheckIndex(int index)
  241.         {
  242.             return index < _storeDetails.Length ? true : false;
  243.         }
  244.  
  245.         public Detail SellDetail(int index)
  246.         {
  247.             return _storeDetails[index - 1];
  248.         }
  249.     }
  250.  
  251.     class Storage
  252.     {
  253.         public Detail[] StorageDetails { get; private set; } = { new Wheel(), new Brakes(), new Engine(),
  254.                                                                  new Glass(), new HeadLight(), new SteeringWheel(),
  255.                                                                  new Door(), new Tire(), new CarGrip(),
  256.                                                                  new Wheel(), new Brakes(), new Engine(),
  257.                                                                  new Glass(), new HeadLight(), new SteeringWheel(),
  258.                                                                  new Door(), new Tire(), new CarGrip() };
  259.  
  260.         public void AddDetail(Detail newDetail)
  261.         {
  262.             Detail[] newStorageDetails = new Detail[StorageDetails.Length + 1];
  263.             for (int i = 0; i < StorageDetails.Length; i++)
  264.             {
  265.                 newStorageDetails[i] = StorageDetails[i];
  266.             }
  267.             newStorageDetails[StorageDetails.Length] = newDetail;
  268.             StorageDetails = newStorageDetails;
  269.         }
  270.  
  271.         public void RemoveDetail(Detail removeDetail)
  272.         {
  273.             Detail[] newStorageDetails = new Detail[StorageDetails.Length - 1];
  274.             int removeDetailIndex = 0;
  275.             for (int i = 0; i < StorageDetails.Length; i++)
  276.             {
  277.                 if(StorageDetails[i].Name == removeDetail.Name)
  278.                 {
  279.                     removeDetailIndex = i;
  280.                     break;
  281.                 }
  282.             }
  283.             int index = 0;
  284.             for (int i = 0; i < newStorageDetails.Length; i++)
  285.             {
  286.                 if(index == removeDetailIndex)
  287.                 {
  288.                     index++;
  289.                 }
  290.                 newStorageDetails[i] = StorageDetails[index];
  291.                 index++;
  292.             }
  293.             StorageDetails = newStorageDetails;
  294.         }
  295.  
  296.         public string GetStorageList()
  297.         {
  298.             string detailList = "";
  299.             for (int i = 0; i < StorageDetails.Length; i++)
  300.             {
  301.                 detailList += (i + 1) + ". " + StorageDetails[i].Name + "\n";
  302.             }
  303.             return detailList;
  304.         }
  305.     }
  306.  
  307.     class RandomClientGenerator
  308.     {
  309.         private Detail[] _details = { new Wheel(), new Brakes(), new Engine(),
  310.                                       new Glass(), new HeadLight(), new SteeringWheel(),
  311.                                       new Door(), new Tire(), new CarGrip()};
  312.         private Random _random = new Random();
  313.  
  314.         public CheckList CreateClient()
  315.         {
  316.             return new CheckList(_details[_random.Next(0, _details.Length)]);
  317.         }
  318.     }
  319.  
  320.     class CheckList
  321.     {
  322.         private Detail _brokenDetail;
  323.  
  324.         public CheckList(Detail brokenDeatil)
  325.         {
  326.             _brokenDetail = brokenDeatil;
  327.         }
  328.  
  329.         public string GetBrokenDetail()
  330.         {
  331.             return _brokenDetail.Name;
  332.         }
  333.  
  334.         public int GetRepairCost()
  335.         {
  336.             return _brokenDetail.Price + _brokenDetail.PriceForWork;
  337.         }
  338.  
  339.         public int GetDamage(Detail replacementDetail)
  340.         {
  341.             return replacementDetail.PriceForDamage + _brokenDetail.Price;
  342.         }
  343.     }
  344.  
  345.     class CarGrip : Detail
  346.     {
  347.         public CarGrip()
  348.         {
  349.             Name = "Сцепление";
  350.             Price = 3000;
  351.             PriceForWork = 1500;
  352.             PriceForDamage = 300;
  353.         }
  354.     }
  355.  
  356.     class Tire : Detail
  357.     {
  358.         public Tire()
  359.         {
  360.             Name = "Покрышка";
  361.             Price = 3000;
  362.             PriceForWork = 1500;
  363.             PriceForDamage = 700;
  364.         }
  365.     }
  366.  
  367.     class Door : Detail
  368.     {
  369.         public Door()
  370.         {
  371.             Name = "Дверь";
  372.             Price = 6000;
  373.             PriceForWork = 1500;
  374.             PriceForDamage = 800;
  375.         }
  376.     }
  377.  
  378.     class SteeringWheel : Detail
  379.     {
  380.         public SteeringWheel()
  381.         {
  382.             Name = "Руль";
  383.             Price = 7500;
  384.             PriceForWork = 2000;
  385.             PriceForDamage = 1000;
  386.         }
  387.     }
  388.  
  389.     class HeadLight : Detail
  390.     {
  391.         public HeadLight()
  392.         {
  393.             Name = "Фары";
  394.             Price = 1500;
  395.             PriceForWork = 1000;
  396.             PriceForDamage = 500;
  397.         }
  398.     }
  399.  
  400.     class Glass : Detail
  401.     {
  402.         public Glass()
  403.         {
  404.             Name = "Стекло";
  405.             Price = 1000;
  406.             PriceForWork = 5000;
  407.             PriceForDamage = 2000;
  408.         }
  409.     }
  410.  
  411.     class Engine : Detail
  412.     {
  413.         public Engine()
  414.         {
  415.             Name = "Двигатель";
  416.             Price = 30000;
  417.             PriceForWork = 10000;
  418.             PriceForDamage = 5000;
  419.         }
  420.     }
  421.  
  422.     class Brakes : Detail
  423.     {
  424.         public Brakes()
  425.         {
  426.             Name = "Тормоза";
  427.             Price = 4000;
  428.             PriceForWork = 1000;
  429.             PriceForDamage = 500;
  430.         }
  431.     }
  432.  
  433.     class Wheel : Detail
  434.     {
  435.         public Wheel()
  436.         {
  437.             Name = "Колесо";
  438.             Price = 2000;
  439.             PriceForWork = 500;
  440.             PriceForDamage = 200;
  441.         }
  442.     }
  443.  
  444.     abstract class Detail
  445.     {
  446.         public string Name { get; protected set; }
  447.         public int Price { get; protected set; }
  448.         public int PriceForWork { get; protected set; }
  449.         public int PriceForDamage { get; protected set; }
  450.     }
  451. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement