using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Service { class Program { static void Main(string[] args) { ServiceMenu menu = new ServiceMenu(); menu.Run(); } } class ServiceMenu { private AutoService _autoService; private Shop _shop; public ServiceMenu() { _autoService = new AutoService(); _shop = new Shop(); } public void Run() { while(_autoService.IsServiceWork()) { Show(); if(ChooseItem()) { TryCreateNewClient(); _shop.TryRefreshProducts(); } else { return; } Console.ReadKey(); Console.Clear(); } } public void Show() { _autoService.ShowBalance(); Console.WriteLine(); _autoService.ShowWarehouse(); Console.WriteLine(); _autoService.ShowBreakages(); Console.WriteLine("\n[1] - приступить к починке\n" + "[2] - Заказать деталь в магазине\n" + "[3] - выход"); Console.Write("Ввод: "); } private bool ChooseItem() { switch (Console.ReadLine()) { case "1": _autoService.ChooseBrokenDetail(); break; case "2": _autoService.SignContract(_shop); break; case "3": return false; default: Console.WriteLine("Ошибка комманды!"); break; } return true; } private void TryCreateNewClient() { if (_autoService.IsCarRepaired) { _autoService.CreateNewClient(); } } } class AutoService { private DetailWarehouse _detailsWarehouse; private Car _clientCar; private int _fine; public int WorkCost { get; private set; } public int Balance { get; private set; } public bool IsBalanceNegaive { get { return Balance <= 0; } } public bool IsWarehouseEmpty { get { return _detailsWarehouse.IsSpaceDetailsEmpty(); } } public bool IsCarRepaired { get { return _clientCar.IsDetailsBroken() == false; } } public AutoService() { _detailsWarehouse = new DetailWarehouse(); _clientCar = new Car(); Balance = 2500; WorkCost = 5000; _fine = 2500; } public bool IsServiceWork() { if (IsWarehouseEmpty) { Console.WriteLine("Запасов больше нет! Закрываем гараж."); return false; } if(IsBalanceNegaive) { Console.WriteLine("Отрицательный баланс! Закрываем гараж."); return false; } return true; } public void ShowBalance() { Console.WriteLine($"Баланс: {Balance}"); } public void ShowBreakages() { _clientCar.ShowBrokenDetails(); } public void ChooseBrokenDetail() { Console.WriteLine("Выберите деталь, которую вы хотите починить: \n" + "[1] - Двигатель\n" + "[2] - Подвеска\n" + "[3] - Проводка\n" + "[4] - Кузов"); Console.Write("Ввод: "); if (int.TryParse(Console.ReadLine(), out int productNumber) == false && productNumber > _detailsWarehouse.GetMaxPartsNumber()) { Console.WriteLine("Ошибка! Такой детали нет."); return; } --productNumber; DetailType detailType = (DetailType)productNumber; RepairDetail(detailType); } public void SignContract(Shop shop) { if (Balance == 0) { Console.WriteLine("Ошибка! Нет предоплаты"); return; } shop.Show(); if (shop.IsDetailChosen(out DetailType detailType) == false) { return; } Balance -= shop.MakePrepayment(detailType); _detailsWarehouse.AddDetail(shop.BuyProduct(detailType)); } public void CreateNewClient() { _clientCar = new Car(); } public void ShowWarehouse() { _detailsWarehouse.ShowSpaceDetails(); } private void RepairDetail(DetailType detailType) { Detail detail = _clientCar.GetBrokenDetailByType(detailType); if (detail.IsBroken == false) { Console.WriteLine("Деталь в рабочем состоянии!"); _detailsWarehouse.TakeDetail(detail.DetailType); GetFine(); return; } _clientCar.ReplaceDetail(_detailsWarehouse.TakeDetail(detail.DetailType)); GetProfit(_detailsWarehouse.GetPrice(detail.DetailType)); } private void GetFine() { Balance -= _fine; } private void GetProfit(int detailPrice) { Balance += detailPrice + WorkCost; } } class Car { private static Random _random = new Random(); private List _details; public Car() { _details = new List() { new Detail("Двигатель", DetailType.Engine, TryToBreak()), new Detail("Подвеска", DetailType.Suspension, TryToBreak()), new Detail("Проводка", DetailType.Wiring, TryToBreak()), new Detail("Кузов", DetailType.Body, TryToBreak()) }; CheckCreatedDetails(); } public void ReplaceDetail(Detail newDetail) { for (int i = 0; i < _details.Count; i++) { if(_details[i].DetailType == newDetail.DetailType) { _details[i] = newDetail; } } } public Detail GetBrokenDetailByType(DetailType detailType) { foreach (var brokenDetail in _details) { if (brokenDetail.DetailType == detailType) { return brokenDetail; } } throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе!"); } public bool IsDetailsBroken() { foreach (var detail in _details) { if (detail.IsBroken) return true; } return false; } public void ShowBrokenDetails() { foreach (var brokenDetail in _details) { if (brokenDetail.IsBroken) { Console.WriteLine($"{brokenDetail.Name} в критическом состоянии!"); } } } private bool TryToBreak() { return Convert.ToBoolean(_random.Next(0, 2)); } private void CheckCreatedDetails() { List details = new List(); foreach (var detail in _details) { if(detail.IsBroken == false) { details.Add(detail); } else { return; } } details[_random.Next(0, details.Count)].BreakDetail(); } } class Detail { public string Name { get; protected set; } public DetailType DetailType { get; protected set; } public bool IsBroken { get; protected set; } public Detail(string name, DetailType detailType, bool isBroken) { Name = name; DetailType = detailType; IsBroken = isBroken; } public void BreakDetail() { IsBroken = true; } } enum DetailType { Engine, Suspension, Wiring, Body } class SpareDetail : Detail { public int Price { get; private set; } public SpareDetail(string name, DetailType detailType, bool isBroken, int price) : base(name, detailType, isBroken) { Name = name; IsBroken = false; Price = price; } } abstract class Warehouse { protected Dictionary _spareDetails; public void ShowSpaceDetails() { foreach (var spareDetail in _spareDetails) { Console.WriteLine($"{spareDetail.Key.Name } - {spareDetail.Value}"); } } public Detail TakeDetail(DetailType detailType) { foreach (var requiredDetail in _spareDetails) { if (requiredDetail.Key.DetailType == detailType) { _spareDetails[requiredDetail.Key] -= 1; return requiredDetail.Key; } } throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе!"); } public int GetPrice(DetailType detailType) { foreach (var requiredDetail in _spareDetails) { if (requiredDetail.Key.DetailType == detailType) { return requiredDetail.Key.Price; } } throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе! Цену получить нельзя!"); } public bool IsSpaceDetailsEmpty() { foreach (var spareDetail in _spareDetails) { if (spareDetail.Value != 0) { return false; } } return true; } } class DetailWarehouse : Warehouse { public DetailWarehouse() { Detail Engine = new SpareDetail("Двигатель", DetailType.Engine, true, 20000); SpareDetail spaceEngine = Engine as SpareDetail; Detail Suspension = new SpareDetail("Подвеска", DetailType.Suspension, true, 15000); SpareDetail spaceSuspencion = Suspension as SpareDetail; Detail Wiring = new SpareDetail("Проводка", DetailType.Wiring, true, 10000); SpareDetail spaceWiring = Wiring as SpareDetail; Detail Body = new SpareDetail("Кузов", DetailType.Body, true, 5000); SpareDetail spaceBody = Body as SpareDetail; _spareDetails = new Dictionary { { spaceEngine, 5 }, { spaceSuspencion, 5 }, { spaceWiring, 5 }, { spaceBody, 5 } }; } public void AddDetail(Detail detail) { foreach (var spareDetail in _spareDetails.ToArray()) { if(spareDetail.Key.DetailType == detail.DetailType) { _spareDetails[spareDetail.Key] += 1; return; } } } public int GetMaxPartsNumber() { return _spareDetails.Keys.Count; } } class Shop : Warehouse { private int _maxProducts; public int RefreshCounter { get; private set; } public Shop() { _maxProducts = 7; RefreshCounter = 5; Detail Engine = new SpareDetail("Двигатель", DetailType.Engine, true, 10000); SpareDetail spaceEngine = Engine as SpareDetail; Detail Suspension = new SpareDetail("Подвеска", DetailType.Suspension, true, 7000); SpareDetail spaceSuspencion = Suspension as SpareDetail; Detail Wiring = new SpareDetail("Проводка", DetailType.Wiring, true, 5000); SpareDetail spaceWiring = Wiring as SpareDetail; Detail Body = new SpareDetail("Кузов", DetailType.Body, true, 2500); SpareDetail spaceBody = Body as SpareDetail; _spareDetails = new Dictionary { { spaceEngine, 7 }, { spaceSuspencion, 7 }, { spaceWiring, 7 }, { spaceBody, 7 } }; } public void Show() { Console.WriteLine("\nКакой товар вы хотите купить?\n" + "[1] - Двигатель\n" + "[2] - Подвеска\n" + "[3] - Проводка\n" + "[4] - Кузов\n" + "[exit] - Выйти из каталога"); Console.Write("Ввод: "); } public bool IsDetailChosen(out DetailType detailType) { detailType = 0; string userInput = Console.ReadLine(); if(userInput == "exit") { return false; } if (int.TryParse(userInput, out int numberProduct) && numberProduct <= _maxProducts) { --numberProduct; detailType = (DetailType)numberProduct; return true; } return false; } public int MakePrepayment(DetailType detailType) { return GetPrice(detailType); } public Detail BuyProduct(DetailType detailType) { return TakeDetail(detailType); } public void TryRefreshProducts() { if (RefreshCounter == 0) { UpdateProducts(); RefreshCounter = 6; } RefreshCounter -= 1; } private void UpdateProducts() { Dictionary tempList = new Dictionary(_spareDetails); foreach (var product in tempList) { if (product.Value != _maxProducts) { _spareDetails[product.Key] = _maxProducts; } } } } }