Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace CarService
- {
- public class Program
- {
- static void Main(string[] args)
- {
- AutoService service = new AutoService();
- service.Run();
- }
- }
- public class Detail
- {
- public Detail(string name, decimal price, bool isBroken)
- {
- Name = name;
- Price = price;
- IsBroken = isBroken;
- }
- public string Name { get; private set; }
- public decimal Price { get; private set; }
- public bool IsBroken { get; private set; }
- }
- public class DetailStorage
- {
- public DetailStorage(Detail detail, int amount = 1)
- {
- Detail = detail;
- Amount = amount;
- }
- public Detail Detail { get; private set; }
- public int Amount { get; private set; }
- public int SetAmount(int amount)
- {
- return Amount += amount;
- }
- }
- public class AutoServiceStorage
- {
- private readonly List<DetailStorage> _partsInStock;
- public AutoServiceStorage()
- {
- _partsInStock = new List<DetailStorage>
- {
- new DetailStorage(new Detail("Двигатель", 5000, false), UserUtils.GenerateRandom(10)),
- new DetailStorage(new Detail("Тормоза", 1200, false), UserUtils.GenerateRandom(10)),
- new DetailStorage(new Detail("Фара", 800, false), UserUtils.GenerateRandom(10)),
- new DetailStorage(new Detail("Аккумулятор", 2500, false), UserUtils.GenerateRandom(10))
- };
- }
- public bool PartsInStock(string partName)
- {
- DetailStorage StorageItem = _partsInStock.FirstOrDefault(i => i.Detail.Name == partName);
- if (StorageItem == null || StorageItem.Amount <= 0)
- {
- Console.WriteLine($"Нет нужной детали: {partName}");
- return false;
- }
- return true;
- }
- public bool DecreasePartAmount(string partName)
- {
- DetailStorage StorageItem = _partsInStock.FirstOrDefault(i => i.Detail.Name == partName);
- if (StorageItem != null && StorageItem.Amount > 0)
- {
- StorageItem.SetAmount(-1);
- return true;
- }
- return false;
- }
- public List<DetailStorage> GetAllParts()
- {
- return _partsInStock;
- }
- }
- public class Car
- {
- public Car(string model, List<Detail> detail)
- {
- Model = model;
- Detail = detail;
- IsRepairStarted = false;
- }
- public string Model { get; private set; }
- public List<Detail> Detail { get; private set; }
- public bool IsRepairStarted { get; private set; }
- public List<Detail> GetBrokenParts()
- {
- return Detail.Where(i => i.IsBroken).ToList();
- }
- public bool IsFullyRepaired()
- {
- return !Detail.Any(i => i.IsBroken);
- }
- public void StartRepair()
- {
- IsRepairStarted = true;
- }
- public bool ReplacePart(Detail newPart)
- {
- Detail brokenPart = Detail.FirstOrDefault(i => i.Name == newPart.Name);
- if (brokenPart != null)
- {
- Detail.Remove(brokenPart);
- Detail.Add(newPart);
- return true;
- }
- return false;
- }
- }
- public class CarCreator
- {
- private readonly List<Car> _cars;
- public CarCreator()
- {
- _cars = new List<Car>
- {
- new Car("Toyota", new List<Detail>
- {
- new Detail("Двигатель", 500m, true),
- new Detail("Фара", 100m, true),
- new Detail("Тормоза", 150m, false)
- }),
- new Car("Honda", new List<Detail>
- {
- new Detail("Аккумулятор", 500m, true),
- new Detail("Двигатель", 100m, false)
- })
- };
- }
- public List<Car> GetCars()
- {
- return _cars;
- }
- }
- public class AutoService
- {
- private const decimal RepairFEE = 1000;
- private const decimal CancelBeforeRepairFee = 2000;
- private const decimal CancelDuringRepairFeePerPart = 600;
- private readonly CarCreator _carCreator;
- private readonly Queue<Car> _cars;
- private readonly AutoServiceStorage _storage;
- private decimal _balance;
- public AutoService()
- {
- _cars = new Queue<Car>();
- _carCreator = new CarCreator();
- _storage = new AutoServiceStorage();
- _balance = UserUtils.GenerateRandom(10000);
- AddCarsToQueue();
- }
- public void Run()
- {
- const string CommandToRepair = "1";
- const string CommandToCancelRepair = "2";
- const string CommandToExit = "3";
- bool isWorking = true;
- while (isWorking && _cars.Count > 0 && _balance > 0)
- {
- Console.Clear();
- ShowServiceStatus();
- Car currentCar = _cars.Peek();
- Console.WriteLine($"\nТекущая машина: {currentCar.Model}");
- ShowBrokenParts(currentCar);
- Console.WriteLine("\nВыберите действие:");
- Console.WriteLine("1 - Отремонтировать деталь");
- Console.WriteLine("2 - Отказаться от ремонта");
- Console.WriteLine("3 - Выйти");
- Console.Write("Введите номер команды: ");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandToRepair:
- Console.Write("Введите название детали для ремонта: ");
- string partName = Console.ReadLine();
- RepairPart(currentCar, partName);
- Pause();
- break;
- case CommandToCancelRepair:
- CancelRepair(currentCar);
- Pause();
- break;
- case CommandToExit:
- isWorking = false;
- break;
- default:
- Console.WriteLine("Некорректный ввод");
- Pause();
- break;
- }
- }
- Console.WriteLine("\nРабота сервиса завершена.");
- Console.WriteLine($"Итоговый баланс: {_balance}");
- }
- private void Pause()
- {
- Console.WriteLine("\nНажмите любую клавишу для продолжения...");
- Console.ReadKey();
- }
- private void AddCarsToQueue()
- {
- List<Car> cars = _carCreator.GetCars();
- foreach (Car car in cars)
- {
- _cars.Enqueue(car);
- Console.WriteLine($"Машина {car.Model} добавлена в очередь");
- }
- }
- private void ShowBrokenParts(Car car)
- {
- List<Detail> brokenParts = car.GetBrokenParts();
- Console.WriteLine($"Сломанные детали в {car.Model}:");
- foreach (Detail part in brokenParts)
- {
- Console.WriteLine($"- {part.Name} (Цена: {part.Price})");
- }
- if (brokenParts.Count == 0)
- {
- Console.WriteLine("Все детали исправны.");
- }
- }
- private void CancelRepair(Car car)
- {
- decimal penalty;
- if (car.IsRepairStarted == false)
- {
- penalty = CancelBeforeRepairFee;
- Console.WriteLine($"В ремонте было отказано. Штраф за отказ от услуг: {penalty}");
- }
- else
- {
- int brokenPartsCount = car.GetBrokenParts().Count;
- penalty = brokenPartsCount * CancelDuringRepairFeePerPart;
- Console.WriteLine($"Отказ от ремонта во время работы. Штраф: {penalty} ({brokenPartsCount} сломанных деталей)");
- }
- _balance -= penalty;
- RemoveCarQueue(car);
- }
- private bool RepairPart(Car car, string partName)
- {
- car.StartRepair();
- List<Detail> brokenParts = car.GetBrokenParts();
- Detail partToRepair = brokenParts.FirstOrDefault(i => i.Name.Equals(partName, StringComparison.OrdinalIgnoreCase));
- if (partToRepair == null)
- {
- Console.WriteLine($"Деталь {partName} не сломана или не существует");
- return false;
- }
- if (!_storage.PartsInStock(partName))
- {
- Console.WriteLine($"Нет нужной детали на складе: {partName}");
- return false;
- }
- Detail newPart = new Detail(partName, partToRepair.Price, false);
- if (car.ReplacePart(newPart))
- {
- _storage.DecreasePartAmount(partName);
- _balance += partToRepair.Price + RepairFEE;
- Console.WriteLine($"Деталь {partName} заменена. Прибыль: {partToRepair.Price + RepairFEE}");
- if (car.IsFullyRepaired())
- {
- RemoveCarQueue(car);
- Console.WriteLine($"Машина {car.Model} полностью отремонтирована и удалена из очереди");
- }
- return true;
- }
- return false;
- }
- private void ShowServiceStatus()
- {
- Console.WriteLine($"\nТекущий баланс: {_balance}");
- Console.WriteLine("\nМашины в очереди:");
- if (_cars.Count == 0)
- {
- Console.WriteLine("Очередь пуста.");
- }
- else
- {
- foreach (Car car in _cars)
- {
- Console.WriteLine($"- {car.Model} (Сломанных деталей: {car.GetBrokenParts().Count})");
- }
- }
- Console.WriteLine("\nДетали на складе:");
- foreach (DetailStorage part in _storage.GetAllParts())
- {
- Console.WriteLine($"- {part.Detail.Name}: {part.Amount} шт. (Цена: {part.Detail.Price})");
- }
- }
- private void RemoveCarQueue(Car car)
- {
- Queue<Car> newQueue = new Queue<Car>(_cars.Where(i => i != car));
- _cars.Clear();
- foreach (Car i in newQueue)
- {
- _cars.Enqueue(i);
- }
- }
- }
- class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandom(int maxValue)
- {
- return s_random.Next(0, maxValue + 1);
- }
- public static int GenerateRandom(int minValue, int maxValue)
- {
- return s_random.Next(minValue, maxValue + 1);
- }
- public static int BaseRandomGenerator(int maxValue)
- {
- return s_random.Next(0, maxValue);
- }
- public static int BaseRandomGenerator(int minValue, int maxValue)
- {
- return s_random.Next(minValue, maxValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement