Askor

Hw38

Dec 18th, 2021 (edited)
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.46 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 CarService
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             CarService carService = new CarService();
  14.             carService.Work();
  15.         }
  16.     }
  17.  
  18.     class CarService
  19.     {
  20.         private Storage _storage = new Storage();
  21.         private Queue<Car> _cars = new Queue<Car>();
  22.  
  23.         public int Money { get; private set; }
  24.  
  25.         public CarService()
  26.         {
  27.             int carCount = 100;
  28.             Money = 0;
  29.             CreateCarsQueue(carCount);
  30.         }
  31.  
  32.         public void Work()
  33.         {
  34.             bool _isWorking = true;
  35.             ConsoleKeyInfo consoleKey;
  36.  
  37.             Console.WriteLine("Press any button to start");
  38.             consoleKey = Console.ReadKey();
  39.             Console.Clear();
  40.  
  41.             while (_isWorking)
  42.             {
  43.                 if (_cars.Count > 0 && consoleKey.Key != ConsoleKey.Escape)
  44.                 {
  45.                     _storage.ShowInfo();
  46.                     Console.WriteLine($"Balance - {Money}$\n");
  47.                     ReciveTheOrder();
  48.                 }
  49.                 else
  50.                 {
  51.                     _isWorking = false;
  52.                 }
  53.  
  54.                 Console.WriteLine("\nPress any button to continue");
  55.                 consoleKey = Console.ReadKey();
  56.                 Console.Clear();
  57.             }
  58.         }
  59.  
  60.         private void ReciveTheOrder()
  61.         {
  62.             Car car = _cars.Peek();
  63.             bool isServiced = true;
  64.  
  65.             Console.WriteLine("Need to repair:");
  66.             car.ShowInfo();
  67.             Console.WriteLine();
  68.  
  69.             Console.WriteLine("1 - Accept repair | 2 - Reject repair");
  70.             Console.Write("Input: ");
  71.  
  72.             switch (Console.ReadLine())
  73.             {
  74.                 case "1":
  75.                     Fix(car);
  76.                     break;
  77.                 case "2":
  78.                     Console.WriteLine("You rejected the repair and received a fine");
  79.                     TakeFine();
  80.                     break;
  81.                 default:
  82.                     Console.WriteLine("Wrong action");
  83.                     isServiced = false;
  84.                     break;
  85.             }
  86.  
  87.             if(isServiced == true)
  88.             {
  89.                 _cars.Dequeue();
  90.             }
  91.         }
  92.  
  93.         private void Fix(Car car)
  94.         {
  95.             Detail detail;
  96.  
  97.             Console.Write("Choose detail: ");
  98.             detail = _storage.ChooseElement(GetNumber() - 1);
  99.  
  100.             if(detail != null && detail.Name == car.BrokenElement.Name)
  101.             {
  102.                 _storage.RemoveElement(detail);
  103.                 TakePaid(detail);
  104.                 Console.WriteLine("Fixed");
  105.             }
  106.             else
  107.             {
  108.                 TakeFine();
  109.                 Console.WriteLine("Wrong detail");
  110.             }
  111.         }
  112.  
  113.         private void TakeFine()
  114.         {
  115.             int _priceForFine = 300;
  116.  
  117.             Money -= _priceForFine;
  118.         }
  119.  
  120.         private void TakePaid(Detail detail)
  121.         {
  122.             int _priceForRepair = 50;
  123.  
  124.             Money += detail.Price + _priceForRepair;
  125.         }
  126.  
  127.         private void CreateCarsQueue(int size)
  128.         {
  129.             Random random = new Random();
  130.             int randomCount;
  131.  
  132.             for (int i = 0; i < size; i++)
  133.             {
  134.                 randomCount = random.Next();
  135.                 _cars.Enqueue(new Car(randomCount));
  136.             }
  137.         }
  138.  
  139.         private int GetNumber()
  140.         {
  141.             int number;
  142.             string userInput;
  143.  
  144.             do
  145.             {
  146.                 userInput = Console.ReadLine();
  147.             }
  148.             while (int.TryParse(userInput, out number) == false);
  149.  
  150.             return number;
  151.         }
  152.     }
  153.  
  154.     class Storage
  155.     {
  156.         private DatabaseOfDetails _dataBase;
  157.         private List<Detail> _details;
  158.        
  159.         public Storage()
  160.         {
  161.             _dataBase = new DatabaseOfDetails();
  162.             _details = new List<Detail>();
  163.            
  164.             Fill();
  165.         }
  166.  
  167.         public void ShowInfo()
  168.         {
  169.             int countOfDetails;
  170.             int index = 1;
  171.  
  172.             Console.WriteLine("\n------------------------Storage--------------------------");
  173.  
  174.             foreach (Detail detail in _details.Distinct())
  175.             {
  176.                 countOfDetails = _details.Where(x => x == detail).Count();
  177.                 detail.ShowInfo(countOfDetails, index);
  178.  
  179.                 index++;
  180.             }
  181.  
  182.             Console.WriteLine("------------------------Storage--------------------------\n");
  183.         }
  184.  
  185.         public Detail ChooseElement(int index)
  186.         {
  187.             if (index >= 0 && index < _details.Distinct().Count())
  188.             {
  189.                 Detail detail = _details.Distinct().ElementAt(index);
  190.  
  191.                 Console.WriteLine($"You choose - {detail.Name}");
  192.                 return detail;
  193.             }
  194.             else
  195.             {
  196.                 return null;
  197.             }
  198.         }
  199.  
  200.         public void RemoveElement(Detail detail)
  201.         {
  202.             if(_details.Contains(detail))
  203.             {
  204.                 _details.Remove(detail);
  205.             }
  206.             else
  207.             {
  208.                 Console.WriteLine("Error |remove element|");
  209.             }
  210.         }
  211.  
  212.         private void Fill()
  213.         {
  214.             int countOfDetails = 5;
  215.  
  216.             foreach (Detail detail in _dataBase.GetElementsList())
  217.             {
  218.                 for (int i = 0; i < countOfDetails; i++)
  219.                 {
  220.                     _details.Add(detail);
  221.                 }
  222.             }
  223.         }
  224.     }
  225.  
  226.     class Car
  227.     {
  228.         private DatabaseOfDetails _dataBase;
  229.  
  230.         public Detail BrokenElement { get; private set; }
  231.  
  232.         public Car(int randomCount)
  233.         {
  234.             _dataBase = new DatabaseOfDetails();
  235.             BrokenElement = _dataBase.GetRandomDetail(randomCount);
  236.         }
  237.  
  238.         public void ShowInfo()
  239.         {
  240.             BrokenElement.ShowInfo();
  241.         }
  242.     }
  243.  
  244.     class Detail
  245.     {
  246.         public string Name { get; private set; }
  247.  
  248.         public int Price { get; private set; }
  249.  
  250.         public Detail(string name, int price)
  251.         {
  252.             Name = name;
  253.             Price = price;
  254.         }
  255.  
  256.         public void ShowInfo()
  257.         {
  258.             Console.WriteLine($"Element - {Name} | Price - {Price}$");
  259.         }
  260.  
  261.         public void ShowInfo(int count, int index)
  262.         {
  263.             Console.WriteLine($"{index}. Element - {Name} | Price - {Price}$ | Count - {count}");
  264.         }
  265.     }
  266.  
  267.     class DatabaseOfDetails
  268.     {
  269.         private Dictionary<string, int> _details = new Dictionary<string, int>();
  270.  
  271.         public DatabaseOfDetails()
  272.         {
  273.             Create();
  274.         }
  275.  
  276.         public Detail GetRandomDetail(int randomCount)
  277.         {
  278.             Random random = new Random(randomCount);
  279.             Detail detail;
  280.             string name;
  281.             int price;
  282.  
  283.             string[] detailsName = GetDetailsName();
  284.  
  285.             int detailIndex = random.Next(0, detailsName.Length);
  286.  
  287.             name = detailsName[detailIndex];
  288.             price = _details[name];
  289.  
  290.  
  291.             detail = new Detail(name, price);
  292.  
  293.             return detail;
  294.         }
  295.  
  296.         public List<Detail> GetElementsList()
  297.         {
  298.             List<Detail> details = new List<Detail>();
  299.  
  300.             foreach (KeyValuePair<string, int> keyValue in _details)
  301.             {
  302.                 details.Add(new Detail(keyValue.Key, keyValue.Value));
  303.             }
  304.  
  305.             return details;
  306.         }
  307.  
  308.         private void Create()
  309.         {
  310.             _details.Add("battery", 5);
  311.             _details.Add("brakes", 7);
  312.             _details.Add("clutch", 13);
  313.             _details.Add("engine", 30);
  314.             _details.Add("gear box", 23);
  315.             _details.Add("radiator", 9);
  316.         }
  317.  
  318.         private string[] GetDetailsName()
  319.         {
  320.             Dictionary<string, int>.KeyCollection keyCollection = _details.Keys;
  321.  
  322.             string[] detailsName = new string[_details.Count];
  323.             int detailIndex = 0;
  324.  
  325.             foreach (string name in keyCollection)
  326.             {
  327.                 detailsName[detailIndex] = name;
  328.                 detailIndex++;
  329.             }
  330.  
  331.             return detailsName;
  332.         }
  333.     }
  334. }
Add Comment
Please, Sign In to add comment