using System; using System.Collections.Generic; using System.Threading; public class Program { public static void Main() { AutoReapirShop autoReapirShop = new AutoReapirShop(); bool isWorking = true; int sleepTime = 200; while (isWorking) { isWorking = autoReapirShop.Update(); Thread.Sleep(sleepTime); } } } public class AutoReapirShop { private Random _random; private Auto _customerCar; private Dictionary _partsStorage; private string[] _partsName; private int _turn; private Screen _screen; private bool _isWorking; private int _moneyAmount = 1000; private int _moneyForCar; private int _storageDetailsAmountMinimum = 0; private int _storageDetailsAmountMaximum = 10; private int _penalty = 200; public AutoReapirShop() { _random = new Random(); _partsStorage = new Dictionary(); _screen = Screen.Round; _isWorking = true; _turn = 1; _moneyForCar = 0; CreateCustomerCar(); CreateStorage(); } public bool Update() { if (_isWorking) { switch (_screen) { case Screen.Round: PrintScreenRound(); break; case Screen.Shop: PrintScreenShop(); break; case Screen.Car: PrintScreenRepairCar(); break; case Screen.Detail: PrintScreenRepairDetail(); break; } if (IsBankrout()) { Console.WriteLine("You are banckroupt"); _isWorking = false; } } return _isWorking; } private bool IsBankrout() { return _moneyAmount < 0; } private void PrintScreenRound() { bool isTyping = true; string input; do { PrintState(); Console.WriteLine("\n0. Go to Shop."); Console.WriteLine("1. Go to Car."); Console.WriteLine("2. Exit."); input = Console.ReadLine(); if (input.Equals("0")) { isTyping = false; _screen = Screen.Shop; } else if (input.Equals("1")) { isTyping = false; _screen = Screen.Car; } else if (input.Equals("2")) { isTyping = false; _isWorking = false; } } while (isTyping); } private void PrintScreenShop() { bool isTyping = true; int partsIndex; do { PrintState(); PrintParts(ShowFormat.Car); PrintParts(ShowFormat.Storage); Console.WriteLine("Input index of detail to buy or q to exit"); string input = Console.ReadLine(); isTyping = !int.TryParse(input, out partsIndex); if (input.Equals("q")) { isTyping = false; _screen = Screen.Round; } else if (partsIndex <= 0 || partsIndex >= _partsName.Length) { isTyping = true; } else { if (_moneyAmount <= 0) { Console.WriteLine("You haven't enought money"); } else { ++_partsStorage[_partsName[partsIndex - 1]]; _moneyAmount -= _customerCar.GetPriceDetail(_partsName[partsIndex - 1]); } } } while (isTyping); } private void PrintScreenRepairCar() { bool isTyping = true; int chooseIndex; do { PrintState(); Console.WriteLine("\nBroken Details "); _customerCar.ShowBrokenDetails(); Console.WriteLine("\n0. Repair Detail."); Console.WriteLine("1. Next Car."); isTyping = !int.TryParse(Console.ReadLine(), out chooseIndex); if (chooseIndex == 0) { _screen = Screen.Detail; } else if (chooseIndex == 1) { if (_customerCar.DetailsBrokenAmount <= 0) { _moneyAmount += _moneyForCar; _moneyForCar = 0; } else { _moneyAmount -= _penalty; } _screen = Screen.Round; ++_turn; CreateCustomerCar(); } else { isTyping = true; } } while (isTyping); } private void PrintScreenRepairDetail() { string partName; int partNameIndex; int indexDetail; bool isTyping = true; do { PrintState(); PrintParts(ShowFormat.Parts); Console.WriteLine("\nInput index of name part or q to exit."); partName = Console.ReadLine(); isTyping = !int.TryParse(partName, out partNameIndex); if (partName.Equals("q")) { isTyping = false; _screen = Screen.Car; } else if (partNameIndex <= 0 || partNameIndex > _partsName.Length) { isTyping = true; } else { if (_partsStorage[_partsName[partNameIndex - 1]] > 0) { int amountItems = _customerCar.GetAmountItemsPart(_partsName[partNameIndex - 1]); Console.WriteLine($"\nInput a index of part from 1 to {amountItems}."); isTyping = int.TryParse(Console.ReadLine(), out indexDetail); if (indexDetail <= 0 || indexDetail > amountItems) { isTyping = false; } else { bool isRepairable = _customerCar.RepairDetail(_partsName[partNameIndex - 1], indexDetail - 1); --_partsStorage[_partsName[partNameIndex - 1]]; if (isRepairable) _moneyForCar += _customerCar.GetPriceRepair(_partsName[partNameIndex - 1]); else _moneyForCar -= _customerCar.GetPriceRepair(_partsName[partNameIndex - 1]); } } else { Console.WriteLine("You haven't details in storage."); } } } while (isTyping); } private void PrintState() { Console.Clear(); Console.WriteLine($"Turn : {_turn}"); Console.WriteLine($"Your money : {_moneyAmount}"); } private void PrintParts(ShowFormat showFormat) { string result = "\n" + showFormat.ToString() + ": \n"; for (int i = 0; i < _partsName.Length; ++i) { if (showFormat == ShowFormat.Parts) result += String.Format($"{i + 1}. - {_partsName[i]}\n"); else if (showFormat == ShowFormat.Car) result += String.Format($"{i + 1}. {_partsName[i]} - {_customerCar.GetPriceDetail(_partsName[i])}\n"); else if (showFormat == ShowFormat.Storage) result += String.Format($"{i + 1}. {_partsName[i]} - {_partsStorage[_partsName[i]]}\n"); } Console.WriteLine(result); } private void CreateCustomerCar() { _customerCar = new Auto(_random); } private void CreateStorage() { _partsName = _customerCar.GetNamesParts(); int detailsAmount; for (int i = 0; i < _partsName.Length; ++i) { detailsAmount = _random.Next(_storageDetailsAmountMinimum, _storageDetailsAmountMaximum); _partsStorage.Add(_partsName[i], detailsAmount); } } } public enum Screen { Round, Shop, Car, Detail } public enum ShowFormat { Parts, Car, Storage } public class Auto { private Dictionary _parts; private Random _random; public int DetailsBrokenAmount { get; private set; } public Auto(Random random) { _random = random; CreateParts(); } public int GetPriceDetail(string partName) { return _parts[partName].GetPriceDetail(); } public int GetPriceService(string partName) { return _parts[partName].GetPriceService(); } public int GetAmountItemsPart(string partName) { return _parts[partName].GetItemsAmount(); } public string[] GetNamesParts() { string[] partNames = new string[_parts.Count]; int i = 0; foreach(KeyValuePair part in _parts) { partNames[i] = part.Key; ++i; } return partNames; } public int GetPriceRepair(string partName) { return _parts[partName].GetPriceService() + _parts[partName].GetPriceDetail(); } public bool RepairDetail(string partName, int detailIndex) { bool isRepaired = _parts[partName].Repair(detailIndex); if (isRepaired) --DetailsBrokenAmount; return isRepaired; } public void ShowBrokenDetails() { foreach (KeyValuePair part in _parts) for (int i = 0; i < part.Value.GetItemsAmount(); ++i) if (part.Value.IsBrokenDetail(i)) Console.WriteLine($"{part.Key} ({i + 1}) - " + $"{part.Value.GetPriceDetail() + part.Value.GetPriceService()}"); } private void CreateParts() { _parts = new Dictionary { { "Engine", new Engine(_random) }, { "Tire", new Tires(_random) }, { "Cell", new Cells(_random) }, { "Salon", new Salon(_random) }, { "Head Light", new LightsHead(_random) }, { "Back Light", new LightsBack(_random) } }; foreach (KeyValuePair part in _parts) DetailsBrokenAmount += part.Value.DetailsBrokenAmount; } } public class Engine : Part { public Engine(Random random) : base(random) { ItemsAmount = 1; PriceDetailMinimum = 500; PriceDetailMaximum = 1500; PriceServiceMinimum = 200; PriceServiceMaximum = 900; NamePart = "Engine"; CreateItems(); } } public class Cells : Part { public Cells (Random random) : base (random) { ItemsAmount = 4; PriceDetailMinimum = 80; PriceDetailMaximum = 200; PriceServiceMinimum = 80; PriceServiceMaximum = 120; NamePart = "Cell"; CreateItems(); } } public class Tires : Part { public Tires (Random random) : base (random) { ItemsAmount = 4; PriceDetailMinimum = 100; PriceDetailMaximum = 250; PriceServiceMinimum = 80; PriceServiceMaximum = 140; NamePart = "Tire"; CreateItems(); } } public class Salon : Part { public Salon (Random random) : base (random) { ItemsAmount = 1; PriceDetailMinimum = 600; PriceDetailMaximum = 900; PriceServiceMinimum = 400; PriceServiceMaximum = 600; NamePart = "Salon"; CreateItems(); } } public class LightsHead : Part { public LightsHead(Random random) : base (random) { ItemsAmount = 2; PriceDetailMinimum = 100; PriceDetailMaximum = 300; PriceServiceMinimum = 80; PriceServiceMaximum = 250; NamePart = "Head Light"; CreateItems(); } } public class LightsBack : Part { public LightsBack(Random random) : base(random) { ItemsAmount = 2; PriceDetailMinimum = 120; PriceDetailMaximum = 300; PriceServiceMinimum = 80; PriceServiceMaximum = 250; NamePart = "Back Light"; CreateItems(); } } public abstract class Part { protected Random Random; protected List Items; protected int ItemsAmount; protected string NamePart; protected int PriceDetailMinimum; protected int PriceDetailMaximum; protected int PriceServiceMinimum; protected int PriceServiceMaximum; protected int RangePercent; protected int BrokenPrecent; public int DetailsBrokenAmount { get; private set; } public string GetName() { return NamePart; } public int GetPriceDetail() { return Items[Items.Count - 1].PriceDetail; } public int GetPriceService() { return Items[Items.Count - 1].PriceService; } public int GetItemsAmount() { return ItemsAmount; } public bool Repair(int index) { bool isBroken = Items[index].IsBroken; if (isBroken) Items[index].Repair(); return isBroken; } public bool IsBrokenDetail(int index) { return Items[index].IsBroken; } public Part(Random random) { Random = random; DetailsBrokenAmount = 0; } protected void CreateItems() { Items = new List(); RangePercent = 100; BrokenPrecent = 50; bool isBroken; Item item; int priceDetail = Random.Next(PriceDetailMinimum, PriceDetailMaximum); int priceService = Random.Next(PriceServiceMinimum, PriceServiceMaximum); for (int i = 0; i < ItemsAmount; ++i) { isBroken = Random.Next(RangePercent) > BrokenPrecent; if (isBroken) ++DetailsBrokenAmount; item = new Item(priceService, priceDetail, isBroken); Items.Add(item); } } } public class Item { public int PriceService { get; private set; } public int PriceDetail { get; private set; } public bool IsBroken { get; private set; } public Item(int priceService, int priceDetail, bool isBroken) { PriceService = priceService; PriceDetail = priceDetail; IsBroken = isBroken; } public void Repair() { IsBroken = false; } }