Nemesis_War

Автосервис

May 10th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.48 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 ConsoleApp15
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             bool worktime = true;
  14.             int userInput;
  15.             Player player = new Player(new AutoPart[] { new Wheel(), new Wheel(), new Wheel(), new GasTank(), new Engine(), new GasTank() }, 1000);
  16.             while (worktime)
  17.             {
  18.                 Car car = new Car();
  19.                 bool newCar = true;
  20.                 player.CheckMoney(ref worktime, ref newCar);
  21.                 while (newCar)
  22.                 {
  23.                     Console.Clear();
  24.                     player.ShowMoney();
  25.                     Console.WriteLine($"\nЧто желаете сделать:\nВыполнить диагностику - 1\nПочинить машину - 2\nОтказать в обслуживании - 3\nЗаказать делать - 4\nЗакрыть автосервис - 5");
  26.                     userInput = Convert.ToInt32(Console.ReadLine());
  27.                     switch (userInput)
  28.                     {
  29.                         case 1:
  30.                             car.Diagnostics();
  31.                             Console.ReadKey();
  32.                             break;
  33.                         case 2:
  34.                             player.ShowAllParts();
  35.                             player.PartForRepair(out string partForRepair);
  36.                             bool repairСonfirmed = car.CheckRepair(partForRepair);
  37.                             if (repairСonfirmed)
  38.                             {
  39.                                 player.SellPart(partForRepair);
  40.                                 newCar = false;
  41.                             }
  42.                             else
  43.                             {
  44.                                 player.Fine();
  45.                                 newCar = false;
  46.                             }
  47.                             break;
  48.                         case 3:
  49.                             player.Fine();
  50.                             newCar = false;
  51.                             break;
  52.                         case 4:
  53.                             Console.WriteLine("Для заказа:\nКолеса - нажмите 1\nТопливного бака - нажмите 2\nДвигателя - 3\nДля выхода - 0");
  54.                             userInput = Convert.ToInt32(Console.ReadLine());
  55.                             switch (userInput)
  56.                             {
  57.                                 case 1:
  58.                                     player.BuyPart(new Wheel());
  59.                                     break;
  60.                                 case 2:
  61.                                     player.BuyPart(new GasTank());
  62.                                     break;
  63.                                 case 3:
  64.                                     player.BuyPart(new Engine());
  65.                                     break;
  66.                                 default:
  67.                                     break;
  68.                             }
  69.                             break;
  70.                         case 5:
  71.                             worktime = false;
  72.                             newCar = false;
  73.                             break;
  74.                         default:
  75.                             Console.WriteLine("Вы ввели неверную команду - попрбуйте еще раз");
  76.                             break;
  77.                     }
  78.                 }
  79.             }
  80.             Console.WriteLine("Автосервис закрыт!");
  81.         }
  82.     }
  83.  
  84.     class Player
  85.     {
  86.         private AutoPart[] _autoPart;
  87.         private int _money;
  88.  
  89.         public Player(AutoPart[] autoPart, int money)
  90.         {
  91.             _autoPart = autoPart;
  92.             _money = money;
  93.         }
  94.  
  95.         public void ShowMoney()
  96.         {
  97.             Console.WriteLine($"У вас {_money} рублей");
  98.         }
  99.  
  100.         public void CheckMoney(ref bool worktime, ref bool newCar)
  101.         {
  102.             if (_money < 0)
  103.             {
  104.                 Console.WriteLine($"Вы банкрот.У вас {_money} рублей");
  105.                 worktime = false;
  106.                 newCar = false;
  107.             }
  108.         }
  109.  
  110.         public void ChangeMoney(int moneyChange)
  111.         {
  112.             _money += moneyChange;
  113.         }
  114.  
  115.         public void ShowAllParts()
  116.         {
  117.             for (int i = 0; i < _autoPart.Length; i++)
  118.             {
  119.                 Console.WriteLine($"Номер детали: {i} | Имя детали: {_autoPart[i].Name} | Стоимость детали : {_autoPart[i].Price} | Стоимость работы: {_autoPart[i].CostWork}");
  120.             }
  121.         }
  122.  
  123.         public void PartForRepair(out string partForRepair)
  124.         {
  125.             Console.WriteLine("Выберите деталь которую хотитье использовать для ремонта:");
  126.             partForRepair = _autoPart[Convert.ToInt32(Console.ReadLine())].Name;
  127.         }
  128.  
  129.         public void Fine()
  130.         {
  131.             _money -= 5000;
  132.         }
  133.  
  134.         public void SellPart(string partForRepair)
  135.         {
  136.             for (int i = 0; i < _autoPart.Length; i++)
  137.             {
  138.                 if (_autoPart[i].Name == partForRepair)
  139.                 {
  140.                     _autoPart[i].UsingDetail();
  141.                     ChangeMoney(_autoPart[i].Price + _autoPart[i].CostWork);
  142.                     DeleteEmpty(i);
  143.                     break;
  144.                 }
  145.             }
  146.         }
  147.  
  148.         public void DeleteEmpty(int emptyCell)
  149.         {
  150.             AutoPart[] tempAutoPart = new AutoPart[_autoPart.Length - 1];
  151.             for (int i = 0; i < tempAutoPart.Length; i++)
  152.             {
  153.                 if (i >= emptyCell)
  154.                 {
  155.                     tempAutoPart[i] = _autoPart[i + 1];
  156.                 }
  157.                 else
  158.                 {
  159.                     tempAutoPart[i] = _autoPart[i];
  160.                 }
  161.             }
  162.             _autoPart = tempAutoPart;
  163.         }
  164.  
  165.         public void BuyPart(AutoPart autoPart)
  166.         {
  167.             Console.WriteLine($"Вы точно хотите купить {autoPart.Name}? Стоимость покупки {autoPart.Price} рублей.\nДля подтвержения - нажмите 1\nДля отмены -2");
  168.             if (Convert.ToInt32(Console.ReadLine()) == 1)
  169.             {
  170.                 AutoPart[] tempAutoPart = new AutoPart[_autoPart.Length + 1];
  171.                 for (int i = 0; i < _autoPart.Length; i++)
  172.                 {
  173.                     tempAutoPart[i] = _autoPart[i];
  174.                 }
  175.                 tempAutoPart[tempAutoPart.Length - 1] = autoPart;
  176.                 _autoPart = tempAutoPart;
  177.                 ChangeMoney(-autoPart.Price);
  178.             }
  179.             else
  180.             {
  181.                 return;
  182.             }
  183.         }
  184.     }
  185.  
  186.     class AutoPart
  187.     {
  188.         public string Name { get; private set; }
  189.         public int CostWork { get; private set; }
  190.         public int Count { get; private set; }
  191.         public int Price { get; private set; }
  192.         public bool DetailWork { get; private set; }
  193.  
  194.         public AutoPart(string name, int constWork, int count, int price, bool work)
  195.         {
  196.             Name = name;
  197.             CostWork = constWork;
  198.             Count = count;
  199.             Price = price;
  200.             DetailWork = work;
  201.         }
  202.  
  203.         public void DetailStatus(bool status)
  204.         {
  205.             DetailWork = status;
  206.         }
  207.  
  208.         public void UsingDetail()
  209.         {
  210.             Count -= 1;
  211.         }
  212.     }
  213.  
  214.     class Wheel : AutoPart
  215.     {
  216.         public Wheel(string name = "Колесо", int costWork = 300, int count = 1, int price = 700, bool work = true) : base(name, costWork, count, price, work) { }
  217.     }
  218.  
  219.     class GasTank : AutoPart
  220.     {
  221.         public GasTank(string name = "Топливнй бак", int costWork = 500, int count = 1, int price = 1200, bool work = true) : base(name, costWork, count, price, work) { }
  222.     }
  223.  
  224.     class Engine : AutoPart
  225.     {
  226.         public Engine(string name = "Двигатель", int costWork = 1500, int count = 1, int price = 3000, bool work = true) : base(name, costWork, count, price, work) { }
  227.     }
  228.  
  229.     class Car
  230.     {
  231.         private bool _repared = false;
  232.         private Random _defectPart = new Random();
  233.         private AutoPart[] _partsCar = new AutoPart[] { new Wheel(), new Wheel(), new Wheel(), new Wheel(), new GasTank(), new Engine() };
  234.  
  235.         public Car()
  236.         {
  237.             DefectPart();
  238.         }
  239.  
  240.         public void DefectPart()
  241.         {
  242.             int random = _defectPart.Next(0, _partsCar.Length);
  243.             _partsCar[random].DetailStatus(false);
  244.         }
  245.  
  246.         public void Diagnostics()
  247.         {
  248.             for (int i = 0; i < _partsCar.Length; i++)
  249.             {
  250.                 Console.WriteLine($"Имя детали: {_partsCar[i].Name} | Статус детали {_partsCar[i].DetailWork}");
  251.             }
  252.         }
  253.  
  254.         public bool CheckRepair(string playerPartName)
  255.         {
  256.             for (int i = 0; i < _partsCar.Length; i++)
  257.             {
  258.                 if (_partsCar[i].Name == playerPartName && _partsCar[i].DetailWork == false)
  259.                 {
  260.                     _partsCar[i].DetailStatus(true);
  261.                     _repared = true;
  262.                 }
  263.             }
  264.             return _repared;
  265.         }
  266.     }
  267. }
Add Comment
Please, Sign In to add comment