Vapio

task37

Jun 9th, 2021 (edited)
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         AutoReapirShop autoReapirShop = new AutoReapirShop();
  10.         bool isWorking = true;
  11.         int sleepTime = 200;
  12.  
  13.         while (isWorking)
  14.         {
  15.             isWorking = autoReapirShop.Update();
  16.             Thread.Sleep(sleepTime);
  17.         }
  18.     }
  19. }
  20.  
  21. public class AutoReapirShop
  22. {
  23.     private Random _random;
  24.     private Auto _customerCar;
  25.     private Dictionary<string, int> _partsStorage;
  26.     private string[] _partsName;
  27.  
  28.     private int _turn;
  29.  
  30.     private Screen _screen;
  31.     private bool _isWorking;
  32.  
  33.     private int _moneyAmount = 1000;
  34.     private int _moneyForCar;
  35.     private int _storageDetailsAmountMinimum = 0;
  36.     private int _storageDetailsAmountMaximum = 10;
  37.     private int _penalty = 200;
  38.  
  39.     public AutoReapirShop()
  40.     {
  41.         _random = new Random();
  42.         _partsStorage = new Dictionary<string, int>();
  43.         _screen = Screen.Round;
  44.         _isWorking = true;
  45.         _turn = 1;
  46.         _moneyForCar = 0;
  47.         CreateCustomerCar();
  48.         CreateStorage();
  49.     }
  50.  
  51.     public bool Update()
  52.     {
  53.         if (_isWorking)
  54.         {
  55.             switch (_screen)
  56.             {
  57.                 case Screen.Round:
  58.                     PrintScreenRound();
  59.                     break;
  60.                 case Screen.Shop:
  61.                     PrintScreenShop();
  62.                     break;
  63.                 case Screen.Car:
  64.                     PrintScreenRepairCar();
  65.                     break;
  66.                 case Screen.Detail:
  67.                     PrintScreenRepairDetail();
  68.                     break;
  69.             }
  70.  
  71.             if (IsBankrout())
  72.             {
  73.                 Console.WriteLine("You are banckroupt");
  74.                 _isWorking = false;
  75.             }
  76.         }
  77.  
  78.         return _isWorking;
  79.     }
  80.  
  81.     private bool IsBankrout()
  82.     {
  83.         return _moneyAmount < 0;
  84.     }
  85.  
  86.     private void PrintScreenRound()
  87.     {
  88.         bool isTyping = true;
  89.         string input;
  90.  
  91.         do
  92.         {
  93.             PrintState();
  94.  
  95.             Console.WriteLine("\n0. Go to Shop.");
  96.             Console.WriteLine("1. Go to Car.");
  97.             Console.WriteLine("2. Exit.");
  98.  
  99.             input = Console.ReadLine();
  100.            
  101.             if (input.Equals("0"))
  102.             {
  103.                 isTyping = false;
  104.                 _screen = Screen.Shop;
  105.             }
  106.             else if (input.Equals("1"))
  107.             {
  108.                 isTyping = false;
  109.                 _screen = Screen.Car;
  110.             }
  111.             else if (input.Equals("2"))
  112.             {
  113.                 isTyping = false;
  114.                 _isWorking = false;
  115.             }
  116.         }
  117.         while (isTyping);
  118.     }
  119.  
  120.     private void PrintScreenShop()
  121.     {
  122.         bool isTyping = true;
  123.         int partsIndex;
  124.  
  125.         do
  126.         {
  127.             PrintState();
  128.             PrintParts(ShowFormat.Car);
  129.             PrintParts(ShowFormat.Storage);
  130.  
  131.             Console.WriteLine("Input index of detail to buy or q to exit");
  132.  
  133.             string input = Console.ReadLine();
  134.             isTyping = !int.TryParse(input, out partsIndex);
  135.  
  136.             if (input.Equals("q"))
  137.             {
  138.                 isTyping = false;
  139.                 _screen = Screen.Round;
  140.             }
  141.             else if (partsIndex <= 0 || partsIndex >= _partsName.Length)
  142.             {
  143.                 isTyping = true;
  144.             }
  145.             else
  146.             {
  147.                 if (_moneyAmount <= 0)
  148.                 {
  149.                     Console.WriteLine("You haven't enought money");
  150.                 }
  151.                 else
  152.                 {
  153.                     ++_partsStorage[_partsName[partsIndex - 1]];
  154.                     _moneyAmount -= _customerCar.GetPriceDetail(_partsName[partsIndex - 1]);
  155.                 }
  156.             }
  157.         }
  158.         while (isTyping);
  159.     }
  160.  
  161.     private void PrintScreenRepairCar()
  162.     {
  163.         bool isTyping = true;
  164.         int chooseIndex;
  165.  
  166.         do
  167.         {
  168.             PrintState();
  169.  
  170.             Console.WriteLine("\nBroken Details ");
  171.             _customerCar.ShowBrokenDetails();
  172.  
  173.             Console.WriteLine("\n0. Repair Detail.");
  174.             Console.WriteLine("1. Next Car.");
  175.  
  176.             isTyping = !int.TryParse(Console.ReadLine(), out chooseIndex);
  177.  
  178.             if (chooseIndex == 0)
  179.             {
  180.                 _screen = Screen.Detail;
  181.             }
  182.             else if (chooseIndex == 1)
  183.             {
  184.                 if (_customerCar.DetailsBrokenAmount <= 0)
  185.                 {
  186.                     _moneyAmount += _moneyForCar;
  187.                     _moneyForCar = 0;
  188.                 }
  189.                 else
  190.                 {
  191.                     _moneyAmount -= _penalty;
  192.                 }
  193.  
  194.                 _screen = Screen.Round;
  195.                 ++_turn;
  196.                 CreateCustomerCar();
  197.             }
  198.             else
  199.             {
  200.                 isTyping = true;
  201.             }
  202.         }
  203.         while (isTyping);
  204.     }
  205.  
  206.     private void PrintScreenRepairDetail()
  207.     {
  208.         string partName;
  209.         int partNameIndex;
  210.         int indexDetail;
  211.         bool isTyping = true;
  212.  
  213.         do
  214.         {
  215.             PrintState();
  216.             PrintParts(ShowFormat.Parts);
  217.  
  218.             Console.WriteLine("\nInput index of name part or q to exit.");
  219.  
  220.             partName = Console.ReadLine();
  221.             isTyping = !int.TryParse(partName, out partNameIndex);
  222.  
  223.             if (partName.Equals("q"))
  224.             {
  225.                 isTyping = false;
  226.                 _screen = Screen.Car;
  227.             }
  228.             else if (partNameIndex <= 0 || partNameIndex > _partsName.Length)
  229.             {
  230.                 isTyping = true;
  231.             }
  232.             else
  233.             {
  234.                 if (_partsStorage[_partsName[partNameIndex - 1]] > 0)
  235.                 {
  236.                     int amountItems = _customerCar.GetAmountItemsPart(_partsName[partNameIndex - 1]);
  237.  
  238.                     Console.WriteLine($"\nInput a index of part from 1 to {amountItems}.");
  239.                     isTyping = int.TryParse(Console.ReadLine(), out indexDetail);
  240.  
  241.                     if (indexDetail <= 0 || indexDetail > amountItems)
  242.                     {
  243.                         isTyping = false;
  244.                     }
  245.                     else
  246.                     {
  247.                         bool isRepairable = _customerCar.RepairDetail(_partsName[partNameIndex - 1], indexDetail - 1);
  248.                         --_partsStorage[_partsName[partNameIndex - 1]];
  249.  
  250.                         if (isRepairable)
  251.                             _moneyForCar += _customerCar.GetPriceRepair(_partsName[partNameIndex - 1]);
  252.                         else
  253.                             _moneyForCar -= _customerCar.GetPriceRepair(_partsName[partNameIndex - 1]);
  254.                     }
  255.                 }
  256.                 else
  257.                 {
  258.                     Console.WriteLine("You haven't details in storage.");
  259.                 }
  260.             }
  261.         }
  262.         while (isTyping);
  263.        
  264.     }
  265.  
  266.     private void PrintState()
  267.     {
  268.         Console.Clear();
  269.         Console.WriteLine($"Turn : {_turn}");
  270.         Console.WriteLine($"Your money : {_moneyAmount}");
  271.     }
  272.  
  273.     private void PrintParts(ShowFormat showFormat)
  274.     {
  275.         string result = "\n" + showFormat.ToString() + ": \n";
  276.  
  277.         for (int i = 0; i < _partsName.Length; ++i)
  278.         {
  279.             if (showFormat == ShowFormat.Parts)
  280.                 result += String.Format($"{i + 1}. - {_partsName[i]}\n");
  281.             else if (showFormat == ShowFormat.Car)
  282.                 result += String.Format($"{i + 1}. {_partsName[i]} - {_customerCar.GetPriceDetail(_partsName[i])}\n");
  283.             else if (showFormat == ShowFormat.Storage)
  284.                 result += String.Format($"{i + 1}. {_partsName[i]} - {_partsStorage[_partsName[i]]}\n");
  285.         }
  286.        
  287.         Console.WriteLine(result);
  288.     }
  289.  
  290.     private void CreateCustomerCar()
  291.     {
  292.         _customerCar = new Auto(_random);
  293.     }
  294.  
  295.     private void CreateStorage()
  296.     {
  297.         _partsName = _customerCar.GetNamesParts();
  298.         int detailsAmount;
  299.  
  300.         for (int i = 0; i < _partsName.Length; ++i)
  301.         {
  302.             detailsAmount = _random.Next(_storageDetailsAmountMinimum, _storageDetailsAmountMaximum);
  303.             _partsStorage.Add(_partsName[i], detailsAmount);
  304.         }
  305.     }
  306. }
  307.  
  308. public enum Screen
  309. {
  310.     Round,
  311.     Shop,
  312.     Car,
  313.     Detail
  314. }
  315.  
  316. public enum ShowFormat
  317. {
  318.     Parts,
  319.     Car,
  320.     Storage
  321. }
  322.  
  323. public class Auto
  324. {
  325.     private Dictionary<string, Part> _parts;
  326.     private Random _random;
  327.    
  328.     public int DetailsBrokenAmount { get; private set; }
  329.  
  330.     public Auto(Random random)
  331.     {
  332.         _random = random;
  333.         CreateParts();
  334.     }
  335.  
  336.     public int GetPriceDetail(string partName)
  337.     {
  338.         return _parts[partName].GetPriceDetail();
  339.     }
  340.  
  341.     public int GetPriceService(string partName)
  342.     {
  343.         return _parts[partName].GetPriceService();
  344.     }
  345.  
  346.     public int GetAmountItemsPart(string partName)
  347.     {
  348.         return _parts[partName].GetItemsAmount();
  349.     }
  350.  
  351.     public string[] GetNamesParts()
  352.     {
  353.         string[] partNames = new string[_parts.Count];
  354.         int i = 0;
  355.  
  356.         foreach(KeyValuePair<string, Part> part in _parts)
  357.         {
  358.             partNames[i] = part.Key;
  359.             ++i;
  360.         }
  361.  
  362.         return partNames;
  363.     }
  364.  
  365.     public int GetPriceRepair(string partName)
  366.     {
  367.         return _parts[partName].GetPriceService() + _parts[partName].GetPriceDetail();
  368.     }
  369.  
  370.     public bool RepairDetail(string partName, int detailIndex)
  371.     {
  372.         bool isRepaired = _parts[partName].Repair(detailIndex);
  373.  
  374.         if (isRepaired)
  375.             --DetailsBrokenAmount;
  376.  
  377.         return isRepaired;
  378.     }
  379.  
  380.     public void ShowBrokenDetails()
  381.     {
  382.         foreach (KeyValuePair<string, Part> part in _parts)
  383.             for (int i = 0; i < part.Value.GetItemsAmount(); ++i)
  384.                 if (part.Value.IsBrokenDetail(i))
  385.                     Console.WriteLine($"{part.Key} ({i + 1}) - " +
  386.                                       $"{part.Value.GetPriceDetail() + part.Value.GetPriceService()}");
  387.     }
  388.  
  389.     private void CreateParts()
  390.     {
  391.         _parts = new Dictionary<string, Part>
  392.         {
  393.             { "Engine", new Engine(_random) },
  394.             { "Tire", new Tires(_random) },
  395.             { "Cell", new Cells(_random) },
  396.             { "Salon", new Salon(_random) },
  397.             { "Head Light", new LightsHead(_random) },
  398.             { "Back Light", new LightsBack(_random) }
  399.         };
  400.  
  401.         foreach (KeyValuePair<string, Part> part in _parts)
  402.             DetailsBrokenAmount += part.Value.DetailsBrokenAmount;
  403.     }
  404. }
  405.  
  406. public class Engine : Part
  407. {
  408.     public Engine(Random random) :
  409.            base(random)
  410.     {
  411.         ItemsAmount = 1;
  412.         PriceDetailMinimum = 500;
  413.         PriceDetailMaximum = 1500;
  414.         PriceServiceMinimum = 200;
  415.         PriceServiceMaximum = 900;
  416.         NamePart = "Engine";
  417.         CreateItems();
  418.     }
  419. }
  420.  
  421. public class Cells : Part
  422. {
  423.     public Cells (Random random) :
  424.            base (random)
  425.     {
  426.         ItemsAmount = 4;
  427.         PriceDetailMinimum = 80;
  428.         PriceDetailMaximum = 200;
  429.         PriceServiceMinimum = 80;
  430.         PriceServiceMaximum = 120;
  431.         NamePart = "Cell";
  432.         CreateItems();
  433.     }
  434. }
  435.  
  436. public class Tires : Part
  437. {
  438.  
  439.     public Tires (Random random) :
  440.            base (random)
  441.     {
  442.         ItemsAmount = 4;
  443.         PriceDetailMinimum = 100;
  444.         PriceDetailMaximum = 250;
  445.         PriceServiceMinimum = 80;
  446.         PriceServiceMaximum = 140;
  447.         NamePart = "Tire";
  448.         CreateItems();
  449.     }
  450. }
  451.  
  452. public class Salon : Part
  453. {
  454.     public Salon (Random random) :
  455.            base (random)
  456.     {
  457.         ItemsAmount = 1;
  458.         PriceDetailMinimum = 600;
  459.         PriceDetailMaximum = 900;
  460.         PriceServiceMinimum = 400;
  461.         PriceServiceMaximum = 600;
  462.         NamePart = "Salon";
  463.         CreateItems();
  464.     }
  465. }
  466.  
  467. public class LightsHead : Part
  468. {
  469.     public LightsHead(Random random) :
  470.            base (random)
  471.     {
  472.         ItemsAmount = 2;
  473.         PriceDetailMinimum = 100;
  474.         PriceDetailMaximum = 300;
  475.         PriceServiceMinimum = 80;
  476.         PriceServiceMaximum = 250;
  477.         NamePart = "Head Light";
  478.         CreateItems();
  479.     }
  480. }
  481.  
  482. public class LightsBack : Part
  483. {
  484.     public LightsBack(Random random) : base(random)
  485.     {
  486.         ItemsAmount = 2;
  487.         PriceDetailMinimum = 120;
  488.         PriceDetailMaximum = 300;
  489.         PriceServiceMinimum = 80;
  490.         PriceServiceMaximum = 250;
  491.         NamePart = "Back Light";
  492.         CreateItems();
  493.     }
  494. }
  495.  
  496. public abstract class Part
  497. {
  498.     protected Random Random;
  499.     protected List<Item> Items;
  500.     protected int ItemsAmount;
  501.     protected string NamePart;
  502.     protected int PriceDetailMinimum;
  503.     protected int PriceDetailMaximum;
  504.     protected int PriceServiceMinimum;
  505.     protected int PriceServiceMaximum;
  506.     protected int RangePercent;
  507.     protected int BrokenPrecent;
  508.  
  509.     public int DetailsBrokenAmount { get; private set; }
  510.  
  511.     public string GetName()
  512.     {
  513.         return NamePart;
  514.     }
  515.  
  516.     public int GetPriceDetail()
  517.     {
  518.         return Items[Items.Count - 1].PriceDetail;    
  519.     }
  520.  
  521.     public int GetPriceService()
  522.     {
  523.         return Items[Items.Count - 1].PriceService;
  524.     }
  525.  
  526.     public int GetItemsAmount()
  527.     {
  528.         return ItemsAmount;
  529.     }
  530.  
  531.     public bool Repair(int index)
  532.     {
  533.         bool isBroken = Items[index].IsBroken;
  534.         if (isBroken)
  535.             Items[index].Repair();
  536.  
  537.         return isBroken;
  538.     }
  539.     public bool IsBrokenDetail(int index)
  540.     {
  541.         return Items[index].IsBroken;
  542.     }
  543.  
  544.     public Part(Random random)
  545.     {
  546.         Random = random;
  547.         DetailsBrokenAmount = 0;
  548.     }
  549.  
  550.     protected void CreateItems()
  551.     {
  552.         Items = new List<Item>();
  553.         RangePercent = 100;
  554.         BrokenPrecent = 50;
  555.  
  556.         bool isBroken;
  557.         Item item;
  558.  
  559.         int priceDetail = Random.Next(PriceDetailMinimum, PriceDetailMaximum);
  560.         int priceService = Random.Next(PriceServiceMinimum, PriceServiceMaximum);
  561.  
  562.         for (int i = 0; i < ItemsAmount; ++i)
  563.         {
  564.             isBroken = Random.Next(RangePercent) > BrokenPrecent;
  565.  
  566.             if (isBroken)
  567.                 ++DetailsBrokenAmount;
  568.  
  569.             item = new Item(priceService, priceDetail, isBroken);
  570.             Items.Add(item);
  571.         }
  572.     }
  573. }
  574.  
  575. public class Item
  576. {
  577.     public int PriceService { get; private set; }
  578.     public int PriceDetail { get; private set; }
  579.     public bool IsBroken { get; private set; }
  580.  
  581.     public Item(int priceService, int priceDetail, bool isBroken)
  582.     {
  583.         PriceService = priceService;
  584.         PriceDetail = priceDetail;
  585.         IsBroken = isBroken;
  586.     }
  587.  
  588.     public void Repair()
  589.     {
  590.         IsBroken = false;
  591.     }
  592. }
  593.  
Add Comment
Please, Sign In to add comment