Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace CSLight
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- CarService carService = new CarService();
- carService.TryServeCars();
- Console.ReadKey();
- }
- }
- class CarService
- {
- private Garage _garage;
- private Storage _storage;
- private int _money;
- private int _fine;
- private int _installationPrice = 100;
- public CarService()
- {
- _garage = new Garage();
- _storage = new Storage();
- _money = 1000;
- _fine = 200;
- }
- public void RepairDetail(Car car)
- {
- Detail detail = GetDetail(car);
- DetailInfo detailInfo = _storage.GetDetailInfo(detail.Type);
- if (detailInfo.Count > 0)
- {
- Detail newDetail = _storage.GetDetail(detail.Type);
- if (detail.IsDamaged)
- {
- car.RemoveDetail(detail);
- car.AddDetail(newDetail);
- int price = detailInfo.Price + _installationPrice;
- Console.WriteLine($"Сломанная деталь успешно заменена. Сервис заработал {price}");
- IncreaseMoney(price);
- }
- else
- {
- Console.WriteLine("Деталь не была сломана. Сервис потерял деталь");
- }
- }
- else
- {
- Console.WriteLine("На складе не хватает нужных деталей");
- }
- }
- public void TryServeCars()
- {
- while (_garage.CarsInQueue != 0)
- {
- Console.Clear();
- Console.WriteLine($"Следующая машина. Машин в очереди {_garage.CarsInQueue}");
- Car car = _garage.GetNextCar();
- ShowEstimateInfo(car);
- if (TryRefuseToRepair())
- {
- Console.WriteLine($"Отказ от ремонта стоил {_fine}");
- DecreaseMoney(_fine);
- }
- else
- {
- bool isReparing = true;
- while (isReparing)
- {
- Console.Clear();
- ShowEstimateInfo(car);
- RepairDetail(car);
- if (car.IsRepaired)
- {
- isReparing = false;
- Console.WriteLine("Машина полностью готова");
- }
- else if (TryRefuseToRepair())
- {
- isReparing = false;
- int currentFine = CalculateFine(car);
- Console.WriteLine($"Отказ от ремонта стоил {currentFine}");
- DecreaseMoney(currentFine);
- }
- }
- }
- Console.ReadKey();
- }
- Console.WriteLine("Все машины обслужены");
- Console.ReadKey();
- }
- private int CalculateFine(Car car)
- {
- int value = 0;
- for (int i = 0; i < car.DetailsCount; i++)
- {
- if (car.GetDetail(i).IsDamaged)
- {
- value += _fine;
- }
- }
- return value;
- }
- private bool TryRefuseToRepair()
- {
- const string CommandAgree = "n";
- const string CommandRefuse = "y";
- bool isRunning = true;
- Console.WriteLine("Отказаться от ремонта? y/n");
- bool isRefused = false;
- while (isRunning)
- {
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandAgree:
- isRefused = false;
- isRunning = false;
- break;
- case CommandRefuse:
- isRefused = true;
- isRunning = false;
- break;
- default:
- Console.WriteLine("Неверная команда");
- break;
- }
- }
- return isRefused;
- }
- private Detail GetDetail(Car car)
- {
- Detail detail = null;
- while (detail == null)
- {
- Console.WriteLine("Какую деталь починить?");
- string userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int result))
- {
- if (result >= 0 && result < car.DetailsCount)
- {
- detail = car.GetDetail(result);
- }
- else
- {
- Console.WriteLine("Неверная деталь");
- }
- }
- }
- return detail;
- }
- private void IncreaseMoney(int value)
- {
- _money += value;
- }
- private void DecreaseMoney(int value)
- {
- _money -= value;
- }
- private void ShowEstimateInfo(Car car)
- {
- ConsoleColor damagedColor = ConsoleColor.Red;
- ConsoleColor repairedColor = ConsoleColor.Green;
- int totalPrice = 0;
- for (int i = 0; i < car.DetailsCount; i++)
- {
- Detail detail = car.GetDetail(i);
- DetailInfo detailInfo = _storage.GetDetailInfo(detail.Type);
- if (car.GetDetail(i).IsDamaged)
- {
- Console.ForegroundColor = damagedColor;
- Console.WriteLine($"{i} - {detailInfo.Type} - Поломка - Цена {detailInfo.Price} + цена установки {_installationPrice}. На складе {detailInfo.Count}");
- totalPrice += (detailInfo.Price + _installationPrice);
- }
- else
- {
- Console.ForegroundColor = repairedColor;
- Console.WriteLine($"{i} - {detailInfo.Type} - Исправна");
- }
- }
- Console.ResetColor();
- Console.WriteLine($"Полная стоимость ремонта - {totalPrice}");
- }
- }
- class Garage
- {
- private Queue<Car> _cars;
- private int _capacity;
- public Garage()
- {
- _cars = new Queue<Car>();
- _capacity = 10;
- InitCars();
- }
- public int CarsInQueue
- {
- get
- {
- return _cars.Count;
- }
- }
- public Car GetNextCar()
- {
- return _cars.Dequeue();
- }
- private void InitCars()
- {
- CarCreator carCreator = new CarCreator();
- for (int i = 0; i < _capacity; i++)
- {
- _cars.Enqueue(carCreator.CreateCar());
- }
- }
- }
- class CarCreator
- {
- public Car CreateCar()
- {
- int detailsCount = GenerateDetailsCount();
- Car car = new Car(detailsCount);
- List<Detail> details = CreateDetails(detailsCount);
- SetDetailsInCar(car, details);
- return car;
- }
- private void SetDetailsInCar(Car car, List<Detail> details)
- {
- foreach (Detail detail in details)
- {
- car.AddDetail(detail);
- }
- }
- private int GenerateDetailsCount()
- {
- int minCount = 5;
- int maxCount = 10;
- return UserUtils.GenerateRandomNumber(minCount, maxCount);
- }
- private List<Detail> CreateDetails(int count)
- {
- DetailFabric detailFabric = new DetailFabric();
- List<Detail> details = new List<Detail>();
- for (int i = 0; i < count; i++)
- {
- int id = UserUtils.GenerateRandomNumber(detailFabric.TypesCount);
- string type = detailFabric.GetType(id);
- Detail detail = detailFabric.CreateDetail(type);
- details.Add(detail);
- }
- DamageDetails(details);
- return details;
- }
- private void DamageDetails(List<Detail> details)
- {
- int maxPercent = 100;
- int chance = 50;
- bool hasDamagedDetails = false;
- for (int i = 0; i < details.Count; i++)
- {
- int value = UserUtils.GenerateRandomNumber(maxPercent);
- if (value > chance)
- {
- details[i].Damage();
- hasDamagedDetails = true;
- }
- }
- if (hasDamagedDetails == false)
- {
- int randomDetail = UserUtils.GenerateRandomNumber(details.Count);
- details[randomDetail].Damage();
- }
- }
- }
- class Car
- {
- private List<Detail> _details;
- public Car(int detailsCount)
- {
- DetailsCount = detailsCount;
- _details = new List<Detail>();
- }
- public int DetailsCount { get; private set; }
- public bool IsRepaired
- {
- get
- {
- bool isRepaired = true;
- foreach (Detail detail in _details)
- {
- if (detail.IsDamaged)
- {
- isRepaired = false;
- }
- }
- return isRepaired;
- }
- }
- public void AddDetail(Detail detail)
- {
- _details.Add(detail);
- }
- public void RemoveDetail(Detail detail)
- {
- _details.Remove(detail);
- }
- public Detail GetDetail(int id)
- {
- return _details[id];
- }
- }
- class Storage
- {
- private DetailFabric _detailFabric;
- private List<DetailInfo> _detailsInfo;
- public Storage()
- {
- _detailFabric = new DetailFabric();
- InitDetails();
- }
- public Detail GetDetail(string type)
- {
- Detail detail = _detailFabric.CreateDetail(type);
- foreach (DetailInfo detailInfo in _detailsInfo)
- {
- if (detailInfo.Type == type)
- {
- detailInfo.DecreaseCount();
- }
- }
- return detail;
- }
- public DetailInfo GetDetailInfo(string type)
- {
- DetailInfo tempInfo = null;
- foreach (DetailInfo detailInfo in _detailsInfo)
- {
- if (detailInfo.Type == type)
- {
- tempInfo = detailInfo;
- }
- }
- return tempInfo;
- }
- private void InitDetails()
- {
- _detailsInfo = new List<DetailInfo>();
- for (int i = 0; i < _detailFabric.TypesCount; i++)
- {
- string type = _detailFabric.GetType(i);
- _detailsInfo.Add(_detailFabric.CreateDetailInfo(type));
- }
- }
- }
- class DetailFabric
- {
- private string[] _types;
- public DetailFabric()
- {
- InitTypes();
- }
- public int TypesCount { get { return _types.Length; } }
- public DetailInfo CreateDetailInfo(string type)
- {
- int minPrice = 100;
- int maxPrice = 500;
- int price = UserUtils.GenerateRandomNumber(minPrice, maxPrice);
- int minCount = 15;
- int maxCount = 30;
- int count = UserUtils.GenerateRandomNumber(minCount, maxCount);
- return new DetailInfo(type, price, count);
- }
- public Detail CreateDetail(string type)
- {
- return new Detail(type);
- }
- public string GetType(int id)
- {
- return _types[id];
- }
- private void InitTypes()
- {
- int typesCount = 4;
- string typeName = "Тип_";
- _types = new string[typesCount];
- for (int i = 0; i < _types.Length; i++)
- {
- _types[i] = typeName + i;
- }
- }
- }
- class DetailInfo
- {
- public DetailInfo(string type, int price, int count)
- {
- Type = type;
- Price = price;
- Count = count;
- }
- public string Type { get; private set; }
- public int Price { get; private set; }
- public int Count { get; private set; }
- public void DecreaseCount()
- {
- Count--;
- }
- }
- class Detail
- {
- public Detail(string type)
- {
- Type = type;
- }
- public bool IsDamaged { get; private set; }
- public string Type { get; private set; }
- public void Damage()
- {
- IsDamaged = true;
- }
- }
- class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int minValue, int maxValue)
- {
- return s_random.Next(minValue, maxValue);
- }
- public static int GenerateRandomNumber(int maxValue)
- {
- return s_random.Next(maxValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement