Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace OOPTask13AutoService
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- CarService carService = new CarService();
- carService.Work();
- }
- }
- public class CarService
- {
- private const int WorkPrice = 5000;
- private const int PenaltyRejectionBeforeRepairStarts = 3000;
- private const int PenaltyRejectionAfterRepairStarts = 1000;
- private Queue<Car> _cars = new Queue<Car>();
- private Storage _storage = new Storage();
- private int _money = 0;
- public CarService()
- {
- CreateCars();
- }
- public void Work()
- {
- const string CommandRepairNextCar = "1";
- const string CommandShowStorage = "2";
- const string CommandExit = "3";
- bool isWorking = true;
- string userInput;
- while (isWorking)
- {
- Console.WriteLine($"У сервиса в кассе {_money} рублей и в очереди на ремонт {_cars.Count} машин\n");
- Console.WriteLine($"{CommandRepairNextCar} - ремонтировать следующую машину");
- Console.WriteLine($"{CommandShowStorage} - смотреть склад компонентов");
- Console.WriteLine($"{CommandExit} - выход");
- Console.Write("\nВведите команду:");
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandRepairNextCar:
- RepairNextCar();
- break;
- case CommandShowStorage:
- _storage.ShowInfo();
- break;
- case CommandExit:
- isWorking = false;
- break;
- default:
- Console.WriteLine("\nНеизвестнеая команда");
- break;
- }
- Console.WriteLine("\nДля продолжения нажмите любую клавишу...");
- Console.ReadKey();
- Console.Clear();
- }
- Console.WriteLine("\nДо свидания!");
- }
- private void RepairNextCar()
- {
- if (_cars.Count == 0)
- {
- Console.WriteLine("\nАвтомобили в очереди закончились");
- return;
- }
- Car car = _cars.Dequeue();
- bool wasRepair = false;
- int notAviableComponentsCount = 0;
- for (int i = 0; i < car.ComponentsCount; i ++)
- {
- Component component = car.GetComponent(i);
- if (component.IsBroken)
- {
- Component newComponent;
- if (_storage.TryGetComponent(component.Name, out newComponent))
- {
- int repairCost = newComponent.Price + newComponent.WorkTime * WorkPrice;
- car.RewriteComponent(newComponent, i);
- Console.Write($"\nПроизведен ремонт за {repairCost} рублей: ");
- _money += repairCost;
- wasRepair = true;
- }
- else
- {
- Console.Write($"\nНа складе нет детали: ");
- notAviableComponentsCount++;
- }
- component.ShowInfo();
- }
- }
- ProcessAfterRepair(wasRepair, notAviableComponentsCount);
- }
- private void ProcessAfterRepair(bool wasRepair, int notAviableComponentsCount)
- {
- if (wasRepair == false)
- {
- Console.WriteLine($"\nК сожалению на складе нет ниодной из поломанных деталей\n" +
- $"Cервис платит клиенту штраф: {PenaltyRejectionBeforeRepairStarts} рублей\n" +
- $"Клиент уезжает в другой сервис\n");
- _money -= PenaltyRejectionBeforeRepairStarts;
- }
- else if (notAviableComponentsCount > 0)
- {
- int penaltyForRejection = PenaltyRejectionAfterRepairStarts * notAviableComponentsCount;
- Console.WriteLine($"\nК сожалению на складе не было {notAviableComponentsCount} деталей\n" +
- $"Cервис платит клиенту штраф: {penaltyForRejection} рублей\n" +
- $"Клиент уезжает в другой сервис\n");
- _money -= penaltyForRejection;
- }
- else
- {
- Console.WriteLine("\nВсе отремонтировано!\n" +
- "Клиент остался недоволен только стоимостью )\n");
- }
- }
- private void CreateCars()
- {
- const int MinimumBrokenComponentsCount = 1;
- const int MaximumBrokenComponentsCount = 7;
- const int MinimumCarsCount = 10;
- const int MaximumCarsCount = 30;
- int carsCount = UserUtils.GenerateRandomNumber(MinimumCarsCount, MaximumCarsCount);
- for (int i = 0; i < carsCount; i++)
- {
- int brokenComponentsCount = UserUtils.GenerateRandomNumber(MinimumBrokenComponentsCount, MaximumBrokenComponentsCount);
- Car car = new Car(_storage.GetAllComponentsForCar(), brokenComponentsCount);
- _cars.Enqueue(car);
- }
- }
- }
- public class Storage
- {
- private Dictionary<string, Shelf> _shelfes = new Dictionary<string, Shelf>();
- public Storage()
- {
- CreateComponents();
- }
- private void CreateComponents()
- {
- const int MinimumComponentCount = 1;
- const int MaximumComponentCount = 10;
- const int MinimumComponentPrice = 20000;
- const int MaximumComponentPrice = 200000;
- const int MinimumWorkTime = 1;
- const int MaximumWorkTime = 15;
- string[] names =
- {
- "рычаг",
- "тормозной диск",
- "колесо",
- "двигатель",
- "руль",
- "радиатор",
- "генератор",
- "ремень ГРМ",
- "аккумулятор",
- "лобовое стекло",
- "стартер",
- "амортизаторы",
- "прокладка между сиденьем и рулем"
- };
- foreach(string name in names)
- {
- _shelfes.Add(name, new Shelf(name,
- UserUtils.GenerateRandomNumber(MinimumComponentPrice, MaximumComponentPrice),
- UserUtils.GenerateRandomNumber(MinimumWorkTime, MaximumWorkTime),
- UserUtils.GenerateRandomNumber(MinimumComponentCount, MaximumComponentCount)
- ));
- }
- }
- public List<Component> GetAllComponentsForCar()
- {
- List<Component> components = new List<Component>();
- foreach(Shelf shelf in _shelfes.Values)
- {
- components.Add(shelf.GetComponentClone());
- }
- return components;
- }
- public void ShowInfo()
- {
- Console.WriteLine("На складе деталей:\n");
- foreach (String name in _shelfes.Keys)
- {
- Console.WriteLine($"{name} - {_shelfes[name].Count} штук");
- }
- }
- public bool TryGetComponent(string name, out Component component)
- {
- if (_shelfes.ContainsKey(name))
- {
- if(_shelfes[name].Count > 0)
- {
- component = _shelfes[name].TakeOneComponent();
- return true;
- }
- }
- component = null;
- return false;
- }
- }
- public class Shelf
- {
- private Component _component;
- public Shelf(string name, int price, int workTime, int count)
- {
- _component = new Component(name, price, workTime);
- Count = count;
- }
- public int Count { get; private set; }
- public Component TakeOneComponent()
- {
- Count--;
- return _component.Clone();
- }
- public Component GetComponentClone()
- {
- return _component.Clone();
- }
- }
- public class Component
- {
- public Component(string name, int price, int workTime)
- {
- Name = name;
- Price = price;
- WorkTime = workTime;
- IsBroken = false;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- public int WorkTime { get; private set; }
- public bool IsBroken { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"{Name}, цена детали: {Price}, время ремонта: {WorkTime} часа");
- }
- public void MarkAsBroken()
- {
- IsBroken = true;
- }
- public Component Clone()
- {
- return new Component(Name, Price, WorkTime);
- }
- }
- public class Car
- {
- private List<Component> _components = new List<Component>();
- public Car(List<Component> components, int brokenComponentsCount)
- {
- _components = components;
- MarkComponentsAsBroken(brokenComponentsCount);
- }
- private void MarkComponentsAsBroken( int brokenComponentsCount)
- {
- int markedComponentsCount = 0;
- while (markedComponentsCount < brokenComponentsCount)
- {
- Component component = GetRandomComponent();
- if (component.IsBroken == false)
- {
- component.MarkAsBroken();
- markedComponentsCount++;
- }
- }
- }
- public int ComponentsCount => _components.Count;
- public Component GetRandomComponent()
- {
- return _components[UserUtils.GenerateRandomNumber(0, _components.Count)];
- }
- public Component GetComponent(int index)
- {
- return _components[index];
- }
- public void RewriteComponent(Component component, int index)
- {
- _components[index] = component;
- }
- }
- public class UserUtils
- {
- private const int MinimumPercent = 0;
- private const int MaximumPercent = 100;
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int minimumNumber, int maximumNumber)
- {
- return s_random.Next(minimumNumber, maximumNumber);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement