Advertisement
Rodunskiy

Untitled

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