W1thr

Autoservice

Jun 14th, 2021 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Autoservice
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<Detail> details = new List<Detail>
  12.             {
  13.                 new Detail("A", 4),
  14.                 new Detail("B", 5),
  15.                 new Detail("C", 6),
  16.                 new Detail("D", 7),
  17.                 new Detail("E", 8),
  18.                 new Detail("F", 9),
  19.             };
  20.  
  21.             Station station = new Station(details);
  22.  
  23.             station.Work(details);
  24.         }
  25.     }
  26.  
  27.     class Station
  28.     {
  29.         private List<Detail> _availableDetails;
  30.         private static Random __random;
  31.         private float _balance;
  32.  
  33.         static Station()
  34.         {
  35.             __random = new Random();
  36.         }
  37.  
  38.         public Station(List<Detail> details)
  39.         {
  40.             _availableDetails = details;
  41.             _balance = 0;
  42.         }
  43.  
  44.         public void Work(List<Detail> details)
  45.         {
  46.             bool outOfDetails = false;
  47.             while (outOfDetails == false)
  48.             {
  49.                 VisualizeHeader();
  50.                 ReplaceDetail(InspectCar(details));
  51.  
  52.                 outOfDetails = _availableDetails.All(detail => detail.Amount == 0);
  53.  
  54.                 Console.Clear();
  55.             }
  56.             Console.WriteLine($"У вас закончились все детали. Зато вы заработали {_balance} денег!");
  57.             Console.ReadKey();
  58.         }
  59.  
  60.         private Detail InspectCar(List<Detail> details)
  61.         {
  62.             Detail detailToReplace = details[__random.Next(0, details.Count)];
  63.             Console.WriteLine($"Вам надо заменить деталь {detailToReplace.Label} за {detailToReplace.Price * 1.5}.");
  64.             return detailToReplace;
  65.         }
  66.  
  67.         private void ReplaceDetail(Detail detailToReplace)
  68.         {
  69.             Console.Write("Какую деталь поставить? ");
  70.             string requestedLabel = Console.ReadLine();
  71.  
  72.             Detail requstedDetail = _availableDetails.FirstOrDefault(detail => detail.Label == requestedLabel);
  73.  
  74.             if (requstedDetail != null)
  75.             {
  76.                 float coefficient = 0.5f;
  77.                 float workPrice = (requstedDetail.Price + requstedDetail.Price * coefficient);
  78.  
  79.                 if (requstedDetail.Amount > 0 && requstedDetail == detailToReplace)
  80.                     _balance += workPrice;
  81.                 else
  82.                     _balance -= workPrice;
  83.  
  84.                 requstedDetail.Use();
  85.  
  86.                 if (requstedDetail.Amount == 0)
  87.                     _availableDetails.Remove(requstedDetail);
  88.             }
  89.             else
  90.             {
  91.                 Console.WriteLine("Детали с таким названием нет!");
  92.             }
  93.  
  94.             Console.WriteLine(_balance);
  95.         }
  96.  
  97.         public void VisualizeHeader()
  98.         {
  99.             Console.SetCursorPosition(0, 0);
  100.             Console.WriteLine("----------Детали----------");
  101.  
  102.             Console.Write("Название   ");
  103.             foreach (var detail in _availableDetails)
  104.             {
  105.                 Console.Write($"|{detail.Label}|  ");
  106.             }
  107.             Console.WriteLine();
  108.  
  109.             Console.Write("Цена       ");
  110.             foreach (var detail in _availableDetails)
  111.             {
  112.                 Console.Write($"|{detail.Price}|  ");
  113.             }
  114.             Console.WriteLine();
  115.  
  116.             Console.Write("Количество ");
  117.             foreach (var detail in _availableDetails)
  118.             {
  119.                 Console.Write($"|{detail.Amount}|  ");
  120.             }
  121.             Console.WriteLine();
  122.             Console.WriteLine($"Баланс: {_balance}\n");
  123.         }
  124.     }
  125.  
  126.     class Detail
  127.     {
  128.         private static Random _random;
  129.  
  130.         public int Price { get; private set; }
  131.         public string Label { get; private set; }
  132.         public int Amount { get; private set; }
  133.  
  134.         static Detail()
  135.         {
  136.             _random = new Random();
  137.         }
  138.  
  139.         public Detail(string label, int price)
  140.         {
  141.             Label = label;
  142.             Price = price;
  143.  
  144.             int minAmount = 2; int maxAmount = 4;
  145.             Amount = _random.Next(minAmount, maxAmount + 1);
  146.         }
  147.  
  148.         public void Use()
  149.         {
  150.             Amount--;
  151.         }
  152.     }
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment