Advertisement
Rodunskiy

Untitled

Aug 1st, 2023
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         CarService carService = new CarService();
  9.  
  10.         bool isWorking = true;
  11.  
  12.         while (isWorking)
  13.         {
  14.             carService.Work();
  15.         }
  16.     }
  17. }
  18.  
  19. class CarService
  20. {
  21.     private DetailsStorage _detailsStorage;
  22.     private Car _car;
  23.  
  24.     private int _breakageIndex;
  25.     private int _moneyBalance = 0;
  26.  
  27.     public CarService()
  28.     {
  29.         _detailsStorage = new DetailsStorage();
  30.         _car = new Car();
  31.     }
  32.  
  33.  
  34.     public void Work()
  35.     {
  36.         _car = Clone();
  37.         int sumFine = 10;
  38.         Detail breakDetail = TakeBreakDetail();
  39.         int priceForTheWork = breakDetail.Price + breakDetail.PriceForRepairs;
  40.  
  41.         _detailsStorage.ShowDetails();
  42.  
  43.         Console.SetCursorPosition(0, 14);
  44.         Console.WriteLine($"Банк:{_moneyBalance}\nВ автосервис заехал автомобиль.\n\n|Сломалась деталь {breakDetail.Name}\n|Цена за работу:{priceForTheWork} монет\n\nВведите название детали для замены или если нужной детали нет, введите noDetails и заплатите штраф.");
  45.  
  46.         string userInput = Console.ReadLine();
  47.  
  48.         if (userInput == "noDetails")
  49.         {
  50.             _moneyBalance -= sumFine;
  51.         }
  52.         else
  53.         {
  54.             if (breakDetail.Name == userInput)
  55.             {
  56.                 Detail newDetail = TakeDetail(userInput);
  57.  
  58.                 _moneyBalance += priceForTheWork;
  59.  
  60.                 Console.WriteLine($"Починка прошла успешно! Автосервис заработал {priceForTheWork} монет.");
  61.  
  62.                 ReturnNewDetail(newDetail);
  63.             }
  64.             else
  65.             {
  66.                 Detail temporaryDetail = TakeDetail(userInput);
  67.  
  68.                 _moneyBalance -= breakDetail.Price;
  69.  
  70.                 Console.WriteLine($"Вы заменили не ту деталь. Вам придется возместить клиенту {breakDetail.Price} монет.");
  71.             }
  72.         }
  73.  
  74.         Console.ReadKey();
  75.         Console.Clear();
  76.     }
  77.  
  78.     public Detail TakeDetail(string detailName)
  79.     {
  80.         return _detailsStorage.GiveDetail(detailName);
  81.     }
  82.  
  83.     public Detail TakeBreakDetail()
  84.     {
  85.        return _car.GiveBreakDetail();
  86.     }
  87.  
  88.     public void ReturnNewDetail(Detail detail)
  89.     {
  90.         _car.TakeNewDetail(detail);
  91.     }
  92.     public Car Clone()
  93.     {
  94.         return new Car();
  95.     }
  96. }
  97.  
  98. class DetailsStorage
  99. {
  100.     private static Random _random = new Random();
  101.  
  102.     private List<Detail> _details;
  103.  
  104.     public DetailsStorage()
  105.     {
  106.         _details = new List<Detail>();
  107.  
  108.         AddToDetails(new Wheel());
  109.         AddToDetails(new Conditioner());
  110.         AddToDetails(new Transmission());
  111.     }
  112.  
  113.     public void AddToDetails(Detail detail)
  114.     {
  115.         int minIndex = 1;
  116.         int maxIndex = 5;
  117.         int maxDetails = _random.Next(minIndex, maxIndex);
  118.  
  119.         for (int i = 0; i < maxDetails; i++)
  120.         {
  121.             _details.Add(detail.Clone());
  122.         }
  123.     }
  124.  
  125.     public Detail GiveDetail(string detailName)
  126.     {
  127.         Detail temporaryDetail = null;
  128.  
  129.         foreach (var detail in _details)
  130.         {
  131.             if (detail.Name == detailName)
  132.             {
  133.                 temporaryDetail = detail;
  134.                 break;
  135.             }
  136.         }
  137.  
  138.         _details.Remove(temporaryDetail);
  139.  
  140.         return temporaryDetail;
  141.     }
  142.  
  143.     public void ShowDetails()
  144.     {
  145.         Console.WriteLine("Кол-во деталей на складе:");
  146.  
  147.         foreach (var detail in _details)
  148.         {
  149.             detail.ShowInfo();
  150.         }
  151.     }
  152.  
  153. }
  154.  
  155. class Detail
  156. {
  157.  
  158.     public Detail(string name, int price,int priceForRepairs, bool condition)
  159.     {
  160.         Name = name;
  161.         Price = price;
  162.         PriceForRepairs = priceForRepairs;
  163.         Condition = condition;
  164.     }
  165.  
  166.     public string Name { get; private set; }
  167.     public int Price { get; private set; }
  168.     public int PriceForRepairs { get; private set; }
  169.     public bool Condition { get; private set; }
  170.  
  171.     public virtual Detail Clone()
  172.     {
  173.         return new Detail(Name, Price, PriceForRepairs, Condition);
  174.     }
  175.  
  176.     public void ShowInfo()
  177.     {
  178.         Console.WriteLine($"{Name}|Цена:{Price}");
  179.     }
  180.  
  181.     public void BreakDetail()
  182.     {
  183.         Condition = false;
  184.     }
  185. }
  186.  
  187. class Wheel : Detail
  188. {
  189.     public override Detail Clone()
  190.     {
  191.         return new Wheel();
  192.     }
  193.  
  194.     public Wheel() : base ("Колесо", 10, 20, true) { }
  195. }
  196.  
  197. class Conditioner : Detail
  198. {
  199.     public override Detail Clone()
  200.     {
  201.         return new Conditioner();
  202.     }
  203.  
  204.     public Conditioner() : base("Кондиционер", 15, 30, true) { }
  205. }
  206.  
  207. class Transmission : Detail
  208. {
  209.     public override Detail Clone()
  210.     {
  211.         return new Transmission();
  212.     }
  213.  
  214.     public Transmission() : base("Коробка передач", 30, 60, true) { }
  215. }
  216.  
  217. class Car
  218. {
  219.     private List<Detail> _details;
  220.  
  221.     public Car()
  222.     {
  223.         _details = new List<Detail>() { new Wheel(), new Conditioner(), new Transmission() };
  224.         BreakCar();
  225.     }
  226.  
  227.     public void BreakCar()
  228.     {
  229.         int randomIndex = Utils.GetRandomNumber(_details.Count - 1);
  230.  
  231.         _details[randomIndex].BreakDetail();
  232.     }
  233.  
  234.     public void TakeNewDetail(Detail detail)
  235.     {
  236.         _details.Add(detail);
  237.     }
  238.  
  239.     public Detail GiveBreakDetail()
  240.     {
  241.         Detail temporaryDetail = null;
  242.  
  243.         foreach (Detail detail in _details)
  244.         {
  245.             if(detail.Condition == false)
  246.             {
  247.                 temporaryDetail = detail;
  248.                 break;
  249.             }
  250.         }
  251.  
  252.         _details.Remove(temporaryDetail);
  253.  
  254.         return temporaryDetail;
  255.     }
  256. }
  257.  
  258. class Utils
  259. {
  260.     private static Random _random = new Random();
  261.  
  262.     public static int GetRandomNumber(int max)
  263.     {
  264.         return _random.Next(max + 1);
  265.     }
  266.  
  267.     public static int ConvertToInt()
  268.     {
  269.         int templateNumber;
  270.         string userInput = string.Empty;
  271.  
  272.         while (int.TryParse(userInput, out templateNumber) == false)
  273.         {
  274.             userInput = Console.ReadLine();
  275.         }
  276.  
  277.         return templateNumber;
  278.     }
  279. }
  280.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement