Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- {
- Menu menu = new Menu();
- menu.ShowMainMenu();
- }
- }
- class Menu
- {
- private AutoService _autoService;
- private Shop _shop;
- public Menu()
- {
- _autoService = new AutoService();
- _shop = new Shop();
- }
- public void ShowMainMenu()
- {
- while(_autoService.IsServiceWork())
- {
- _autoService.ShowBalance();
- Console.WriteLine();
- _autoService.ShowWarehouse();
- Console.WriteLine();
- _autoService.ShowBreakages();
- Console.WriteLine("\n[1] - приступить к починке\n" +
- "[2] - Заказать деталь в магазине\n" +
- "[3] - выход");
- Console.Write("Ввод: ");
- switch(Console.ReadLine())
- {
- case "1":
- _autoService.ChooseBrokenDetail();
- break;
- case "2":
- _autoService.SignContract(_shop);
- break;
- case "3":
- return;
- default:
- Console.WriteLine("Ошибка комманды!");
- break;
- }
- if(_autoService.IsCarRepaired())
- {
- _autoService.CreateNewClient();
- }
- _shop.CheckRefreshCounter();
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- class AutoService
- {
- private DetailWarehouse _detailsWarehouse;
- private Car _clientCar;
- private int _fine;
- public int WorkCost { get; private set; }
- public int Balance { get; private set; }
- public AutoService()
- {
- _detailsWarehouse = new DetailWarehouse();
- _clientCar = new Car();
- Balance = 2500;
- WorkCost = 5000;
- _fine = 2500;
- }
- public bool IsServiceWork()
- {
- if (IsWarehouseEmpty() || CheckNegativeBalance())
- {
- 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("Ввод: ");
- switch(Console.ReadLine())
- {
- case "1":
- RepairDetail("Двигатель");
- break;
- case "2":
- RepairDetail("Подвеска");
- break;
- case "3":
- RepairDetail("Проводка");
- break;
- case "4":
- RepairDetail("Кузов");
- break;
- }
- }
- public void SignContract(Shop shop)
- {
- if (Balance == 0)
- {
- Console.WriteLine("Ошибка! Нет предоплаты");
- return;
- }
- try
- {
- Balance -= shop.MakePrepayment(out string productName);
- _detailsWarehouse.AddDetail(shop.BuyProduct(productName));
- }
- catch
- {
- new Exception("Ошибка магазина!");
- }
- }
- public bool CheckNegativeBalance()
- {
- return Balance <= 0;
- }
- public bool IsWarehouseEmpty()
- {
- return _detailsWarehouse.IsSpaceDetailsEmpty();
- }
- public bool IsCarRepaired()
- {
- return _clientCar.IsDetailsBroken() == false;
- }
- public void CreateNewClient()
- {
- _clientCar = new Car();
- }
- public void ShowWarehouse()
- {
- _detailsWarehouse.ShowSpaceDetails();
- }
- private void RepairDetail(string detailName)
- {
- Detail detail = _clientCar.GetBrokenDetailByName(detailName);
- if (detail.IsBroken == false)
- {
- Console.WriteLine("Деталь в рабочем состоянии!");
- _detailsWarehouse.TakeDetail(detail.Name);
- GetFine();
- return;
- }
- _clientCar.ReplaceDetail(_detailsWarehouse.TakeDetail(detail.Name));
- GetProfit(_detailsWarehouse.GetPrice(detail.Name));
- }
- private void GetFine()
- {
- Balance -= _fine;
- }
- private void GetProfit(int detailPrice)
- {
- Balance += detailPrice + WorkCost;
- }
- }
- abstract class Warehouse
- {
- protected Dictionary<SpaceDetail, int> SpareDetails;
- public void ShowSpaceDetails()
- {
- foreach (var spareDetail in SpareDetails)
- {
- Console.WriteLine($"{spareDetail.Key.Name } - {spareDetail.Value}");
- }
- }
- public Detail TakeDetail(string detailName)
- {
- foreach (var requiredDetail in SpareDetails)
- {
- if (requiredDetail.Key.Name == detailName)
- {
- SpareDetails[requiredDetail.Key] -= 1;
- return requiredDetail.Key;
- }
- }
- throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе!");
- }
- public int GetPrice(string detailName)
- {
- foreach (var requiredDetail in SpareDetails)
- {
- if (requiredDetail.Key.Name == detailName)
- {
- return requiredDetail.Key.Price;
- }
- }
- throw new Exception("Ошибка! Деталь, которую вы хотите взять не храниться на складе! Цену получить нельзя!");
- }
- public bool IsSpaceDetailsEmpty()
- {
- foreach (var spareDetail in SpareDetails)
- {
- if (spareDetail.Value != 0)
- {
- return false;
- }
- }
- return true;
- }
- }
- class Shop : Warehouse
- {
- private int _maxProducts;
- public int RefreshCounter { get; private set; }
- public Shop()
- {
- _maxProducts = 7;
- RefreshCounter = 5;
- Detail Engine = new SpaceDetail("Двигатель", true, 10000);
- SpaceDetail spaceEngine = Engine as SpaceDetail;
- Detail Suspension = new SpaceDetail("Подвеска", true, 7000);
- SpaceDetail spaceSuspencion = Suspension as SpaceDetail;
- Detail Wiring = new SpaceDetail("Проводка", true, 5000);
- SpaceDetail spaceWiring = Wiring as SpaceDetail;
- Detail Body = new SpaceDetail("Кузов", true, 2500);
- SpaceDetail spaceBody = Body as SpaceDetail;
- SpareDetails = new Dictionary<SpaceDetail, int>
- {
- { spaceEngine, 7 },
- { spaceSuspencion, 7 },
- { spaceWiring, 7 },
- { spaceBody, 7 }
- };
- }
- public int MakePrepayment(out string productName)
- {
- Console.WriteLine("Какой товар вы хотите купить?\n" +
- "[1] - Двигатель\n" +
- "[2] - Подвеска\n" +
- "[3] - Проводка\n" +
- "[4] - Кузов\n" +
- "[5] - Выйти из каталога");
- Console.Write("Ввод: ");
- switch (Console.ReadLine())
- {
- case "1":
- productName = "Двигатель";
- return GetPrice("Двигатель");
- case "2":
- productName = "Подвеска";
- return GetPrice("Подвеска");
- case "3":
- productName = "Проводка";
- return GetPrice("Проводка");
- case "4":
- productName = "Кузов";
- return GetPrice("Кузов");
- }
- throw new Exception("Такой детали нет в каталоге!");
- }
- public Detail BuyProduct(string productName)
- {
- return TakeDetail(productName);
- }
- public void CheckRefreshCounter()
- {
- if(RefreshCounter == 0)
- {
- UpdateProducts();
- RefreshCounter = 6;
- }
- RefreshCounter -= 1;
- }
- private void UpdateProducts()
- {
- foreach (var product in SpareDetails)
- {
- if(product.Value != _maxProducts)
- {
- int productsNumber = _maxProducts - product.Value;
- SpareDetails[product.Key] += productsNumber;
- }
- }
- }
- }
- class DetailWarehouse : Warehouse
- {
- public DetailWarehouse()
- {
- Detail Engine = new SpaceDetail("Двигатель", true, 20000);
- SpaceDetail spaceEngine = Engine as SpaceDetail;
- Detail Suspension = new SpaceDetail("Подвеска", true, 15000);
- SpaceDetail spaceSuspencion = Suspension as SpaceDetail;
- Detail Wiring = new SpaceDetail("Проводка", true, 10000);
- SpaceDetail spaceWiring = Wiring as SpaceDetail;
- Detail Body = new SpaceDetail("Кузов", true, 5000);
- SpaceDetail spaceBody = Body as SpaceDetail;
- SpareDetails = new Dictionary<SpaceDetail, int>
- {
- { spaceEngine, 5 },
- { spaceSuspencion, 5 },
- { spaceWiring, 5 },
- { spaceBody, 5 }
- };
- }
- public void AddDetail(Detail detail)
- {
- foreach (var spareDetail in SpareDetails.ToArray())
- {
- if(spareDetail.Key.Name == detail.Name)
- {
- SpareDetails[spareDetail.Key] += 1;
- return;
- }
- }
- }
- }
- class Car
- {
- static private Random _random = new Random();
- private Detail _engine;
- private Detail _suspension;
- private Detail _wiring;
- private Detail _body;
- private List<Detail> _details;
- public Car()
- {
- _engine = new Detail("Двигатель", TryToBreak());
- _suspension = new Detail("Подвеска", TryToBreak());
- _wiring = new Detail("Проводка", TryToBreak());
- _body = new Detail("Кузов", TryToBreak());
- _details = new List<Detail>()
- {
- _engine,
- _suspension,
- _wiring,
- _body
- };
- CheckCreatedDetails();
- }
- public void ReplaceDetail(Detail newDetail)
- {
- for (int i = 0; i < _details.Count; i++)
- {
- if(_details[i].Name == newDetail.Name)
- {
- _details[i] = newDetail;
- }
- }
- }
- public Detail GetBrokenDetailByName(string detailName)
- {
- foreach (var brokenDetail in _details)
- {
- if (brokenDetail.Name == detailName)
- {
- 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<Detail> details = new List<Detail>();
- 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 bool IsBroken { get; protected set; }
- public Detail(string name, bool isBroken)
- {
- Name = name;
- IsBroken = isBroken;
- }
- public void BreakDetail()
- {
- IsBroken = true;
- }
- }
- class SpaceDetail : Detail
- {
- public int Price { get; private set; }
- public SpaceDetail(string name, bool isBroken, int price) : base(name, isBroken)
- {
- IsBroken = false;
- Price = price;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement