Anonim_999

CarService

Oct 14th, 2021 (edited)
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. namespace ConsoleApp2
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             CarService carService = new CarService();
  11.             carService.StartWork();
  12.         }
  13.     }
  14.  
  15.     class CarService
  16.     {
  17.         private Warehouse _warehouse;
  18.         private int _money;
  19.         private Random _random;
  20.         public CarService()
  21.         {
  22.             _random = new Random();
  23.             _warehouse = new Warehouse();
  24.             _money = _random.Next(10000,20000);
  25.         }
  26.  
  27.         public void StartWork()
  28.         {
  29.             bool isWorking = true;
  30.             string userInput;
  31.  
  32.             while (isWorking && _money > 0)
  33.             {
  34.                 Console.ReadKey();
  35.                 Console.Clear();
  36.                 Car car = new Car(_warehouse.GetDetail(_random.Next(0, _warehouse.GetCountDetails())));
  37.                 ShowInfo();
  38.                 Console.WriteLine("1.Продолжить\n2.Выход");
  39.                 userInput = Console.ReadLine();
  40.  
  41.                 switch (userInput)
  42.                 {
  43.                     case "1":
  44.                         StartService(car);
  45.                         break;
  46.                     case "2":
  47.                         isWorking = false;
  48.                         break;
  49.                     default:
  50.                         break;
  51.                 }
  52.             }
  53.  
  54.             if (_money <= 0)
  55.             {
  56.                 Console.WriteLine("Вы банкрот, игра окончена!");
  57.             }
  58.         }
  59.  
  60.         private void StartService(Car car)
  61.         {
  62.             bool isService = true;
  63.             string userInput;
  64.             int fine = 5000;
  65.             int priceWork = 5000;
  66.             int income = priceWork + car.Breaking.Price;
  67.  
  68.             while (isService && _money > 0)
  69.             {
  70.                 Console.ReadKey();
  71.                 Console.Clear();
  72.                 ShowInfo();
  73.                 car.ShowInfo();
  74.                 Console.WriteLine($"\nДоход если почините машину: {income}");
  75.                 Console.WriteLine("\n1.Заменить деталь\n2.Купить деталь\n3.Отказать клиенту");
  76.                 userInput = Console.ReadLine();
  77.  
  78.                 switch (userInput)
  79.                 {
  80.                     case "1":
  81.                         ReplaceDetail(car,fine,priceWork);
  82.                         isService = false;
  83.                         break;
  84.                     case "2":
  85.                         BuyDetail();
  86.                         break;
  87.                     case "3":
  88.                         Console.WriteLine($"Вы отказали клиенту и заплатили штраф в размере {fine}");
  89.                         _money -= fine;
  90.                         isService = false;
  91.                         break;
  92.                     default:
  93.                         break;
  94.                 }
  95.             }
  96.         }
  97.  
  98.         private void ReplaceDetail(Car car, int fine, int priceWork)
  99.         {
  100.             string userInput;
  101.             Console.WriteLine("\nКакую деталь заменить?");
  102.             userInput = Console.ReadLine();
  103.             int number;
  104.  
  105.             if (int.TryParse(userInput, out number) && _warehouse.CheckDetail(number - 1))
  106.             {
  107.                 int income = priceWork + _warehouse.GetDetail(number - 1).Price;
  108.  
  109.                 if (_warehouse.GetDetail(number - 1) == car.Breaking)
  110.                 {
  111.                     if (_warehouse.CheckAmountDetail(number - 1))
  112.                     {
  113.                         _warehouse.SubtractDetails(number - 1);
  114.                         _money += income;
  115.                         Console.WriteLine($"Деталь заменена успешно. Доход: {income}");
  116.                     }
  117.                     else
  118.                     {
  119.                         _money -= fine;
  120.                         Console.WriteLine($"Детали не было на складе, клиент уехал. Вы выплатили штраф в размере {fine}");
  121.                     }
  122.                 }
  123.                 else
  124.                 {
  125.                     fine += income;
  126.                     _money -= fine;
  127.                     Console.WriteLine($"Вы выбрали не ту деталь для замены, вы выплатели штраф в размере {fine}");
  128.                 }
  129.             }
  130.             else
  131.             {
  132.                 _money -= fine;
  133.                 Console.WriteLine($"Детали не было на складе, клиент уехал. Вы выплатили штраф в размере {fine}");
  134.             }
  135.         }
  136.  
  137.         private void BuyDetail()
  138.         {
  139.             string userInput;
  140.             Console.WriteLine("\nКакую деталь купить?");
  141.             userInput = Console.ReadLine();
  142.             int number;
  143.  
  144.             if (int.TryParse(userInput, out number) && _warehouse.CheckDetail(number - 1))
  145.             {
  146.                 if (_money > _warehouse.GetDetail(number - 1).Price)
  147.                 {
  148.                     _money -= _warehouse.GetDetail(number - 1).Price;
  149.                     Console.WriteLine($"Деталь {_warehouse.GetDetail(number - 1).Name} куплена");
  150.                     _warehouse.AddDetail(number - 1);
  151.                 }
  152.                 else
  153.                 {
  154.                     Console.WriteLine("Не хватило денег на деталь(((");
  155.                 }
  156.             }
  157.         }
  158.  
  159.         private void ShowInfo()
  160.         {
  161.             Console.WriteLine($"Автосервис:\nДеньги: {_money}");
  162.             _warehouse.ShowInfo();
  163.         }
  164.     }
  165.  
  166.     class Warehouse
  167.     {
  168.         private Dictionary<int, Detail> _details;
  169.         private List<string> _nameDetails;
  170.         private Dictionary<string, int> _detailsCounts;
  171.  
  172.         public Warehouse()
  173.         {
  174.             Random random = new Random();
  175.             _nameDetails = new List<string> { "Двигатель", "Трансмиссия", "Тормозная система"};
  176.             _details = new Dictionary<int,Detail>();
  177.             _detailsCounts = new Dictionary<string, int>();
  178.  
  179.             for (int i = 0; i < _nameDetails.Count; i++)
  180.             {
  181.                 _details.Add(i, new Detail(_nameDetails[i])) ;
  182.                 _detailsCounts.Add(_details[i].Name,random.Next(0,5));
  183.             }
  184.         }
  185.  
  186.         public void ShowInfo()
  187.         {
  188.             Console.WriteLine("Детали: ");
  189.             int index = 1;
  190.  
  191.             foreach (var detail in _details)
  192.             {
  193.                 Console.Write($"{index}. ");
  194.                 detail.Value.ShowInfo();
  195.                 Console.Write($" || Кол-во: {_detailsCounts[detail.Value.Name]}\n");
  196.                 index++;
  197.             }
  198.         }
  199.  
  200.         public Detail GetDetail(int index)
  201.         {
  202.             return _details[index];
  203.         }
  204.  
  205.         public bool CheckDetail(int index)
  206.         {
  207.             if (_details.ContainsKey(index))
  208.                 return true;
  209.             else
  210.             {
  211.                 Console.WriteLine("Нет такой детали нет на складе!");
  212.                 return false;
  213.             }
  214.         }
  215.  
  216.         public bool CheckAmountDetail(int index)
  217.         {
  218.             if (_detailsCounts[_nameDetails[index]] > 0)
  219.             {
  220.                 return true;
  221.             }
  222.             else
  223.             {
  224.                 return false;
  225.             }
  226.         }
  227.  
  228.         public int GetCountDetails()
  229.         {
  230.             return _details.Count;
  231.         }
  232.  
  233.         public void SubtractDetails(int index)
  234.         {
  235.             _detailsCounts[_nameDetails[index]]--;
  236.         }
  237.  
  238.         public void AddDetail(int index)
  239.         {
  240.             _detailsCounts[_nameDetails[index]]++;
  241.         }
  242.     }
  243.  
  244.     class Car
  245.     {
  246.         public Detail Breaking { get; private set; }
  247.  
  248.         public Car(Detail detail)
  249.         {
  250.             Breaking = detail;
  251.         }
  252.  
  253.         public void ShowInfo()
  254.         {
  255.             Console.WriteLine("Поломка у машины: ");
  256.             Breaking.ShowInfo();
  257.         }
  258.     }
  259.  
  260.     class Detail
  261.     {
  262.         public string Name { get; private set; }
  263.         public int Price { get; private set; }
  264.  
  265.         public Detail(string name)
  266.         {
  267.             Random random = new Random();
  268.             Name = name;
  269.             Price = random.Next(10000,20000);
  270.         }
  271.  
  272.         public void ShowInfo()
  273.         {
  274.             Console.Write($"Название детали: {Name} || Стоимость: {Price}");
  275.         }
  276.     }
  277. }
Add Comment
Please, Sign In to add comment