Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Ijunior
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Game game = new Game();
- game.Work();
- }
- }
- class Game
- {
- private Service _service = new Service();
- public void Work()
- {
- const string CommandServiceCar = "1";
- const string CommandShowStorage = "2";
- const string CommandExit = "3";
- bool isWork = true;
- while (isWork)
- {
- Console.WriteLine("Машин в очереди: " + _service.CarsCount);
- Console.Write($"\n{CommandServiceCar} - обслужить машину" +
- $"\n{CommandShowStorage} - посмотреть детали на складе" +
- $"\n{CommandExit} - выйти" +
- $"\nВведите номер: ");
- switch (Console.ReadLine())
- {
- case CommandServiceCar:
- _service.FixCar();
- break;
- case CommandShowStorage:
- _service.ShowStorageInfo();
- break;
- case CommandExit:
- isWork = false;
- break;
- default:
- Console.WriteLine("Некорректный ввод");
- break;
- }
- if (_service.CarsCount == 0)
- {
- Console.WriteLine("Вы починили все машины");
- isWork = false;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- class Service
- {
- private FactoryDetails _factoryDetails;
- private Storage _storage;
- private Queue<Car> _cars;
- private int _money;
- private int _priceRepair;
- private int _peneltyFix;
- private int _penaltyDetail;
- public Service()
- {
- _factoryDetails = new FactoryDetails();
- _storage = new Storage(_factoryDetails);
- _cars = new Queue<Car>();
- CreateCars();
- _money = 1000;
- _priceRepair = 100;
- _peneltyFix = 100;
- _penaltyDetail = 50;
- }
- public int CarsCount => _cars.Count;
- public void FixCar()
- {
- const string CommandCancel = "exit";
- bool isWork = true;
- bool isPenalty = false;
- Car car = _cars.Dequeue();
- Console.Clear();
- Console.Write($"Для начала ремонта нажмите любую клавишу, для отмены введите '{CommandCancel}' : ");
- if (Console.ReadLine().ToLower() == CommandCancel)
- {
- int countFixPenalty = 1;
- PayPenalty(_peneltyFix, countFixPenalty);
- }
- else
- {
- while (isWork)
- {
- if (car.HasBrokenDetail() > 0)
- {
- Console.Clear();
- Console.WriteLine($"Деньги мастерской: {_money}\n\n");
- car.ShowInfo();
- ReplaceDetail(car, ref isPenalty);
- if (isPenalty)
- {
- PayPenalty(_penaltyDetail, car.HasBrokenDetail());
- isWork = false;
- }
- Console.ReadKey();
- }
- else
- {
- isWork = false;
- }
- }
- }
- }
- public void ReplaceDetail(Car car, ref bool isPenalty)
- {
- const string CommandCancel = "exit";
- Console.Write($"Введите номер детали для замены или '{CommandCancel}' для окончания ремонта: ");
- string userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int index))
- {
- if (index > 0 && index <= car.DetailsCount)
- {
- index--;
- Detail detail = car.GetDetail(index);
- bool isBroken = detail.IsBroken;
- if (_storage.TryGetDetail(ref detail))
- {
- car.ReplaceDetail(index, detail);
- Console.WriteLine($"Вы заменили деталь {detail.Name}");
- if (isBroken)
- EarnMoney(detail.Price);
- else
- Console.WriteLine("Вы не получаете денег, деталь была исправна");
- }
- else
- {
- Console.WriteLine("На складе отсутствует эта деталь");
- }
- }
- else
- {
- Console.WriteLine("Такого номера нет");
- }
- }
- else if (userInput.ToLower() == CommandCancel)
- {
- isPenalty = true;
- }
- else
- {
- Console.WriteLine("Некорректный ввод");
- }
- }
- public void ShowStorageInfo()
- {
- _storage.ShowInfo();
- }
- private void EarnMoney(int detailPrice)
- {
- _money += detailPrice + _priceRepair;
- Console.WriteLine("Вы заработали " + (detailPrice + _priceRepair));
- }
- private void PayPenalty(int penalty, int count)
- {
- _money -= penalty * count;
- Console.WriteLine("Вы заплатили неустойку в размере: " + (penalty * count));
- }
- private void CreateCars()
- {
- int count = 10;
- for (int i = 0; i < count; i++)
- {
- _cars.Enqueue(new Car(_factoryDetails.CopyList()));
- }
- }
- }
- class Storage
- {
- private FactoryDetails _factoryDetails;
- private List<Box> _boxes;
- public Storage(FactoryDetails factoryDetails)
- {
- _factoryDetails = factoryDetails;
- _boxes = new List<Box>();
- CreateBoxes();
- }
- public void CreateBoxes()
- {
- int minDetails = 1;
- int maxDetails = 10;
- for (int i = 0; i < _factoryDetails.DetailsCount; i++)
- {
- _boxes.Add(new Box(_factoryDetails.CopyDetail(i), Utils.GetRandomNumber(minDetails, maxDetails)));
- }
- }
- public bool TryGetDetail(ref Detail detail)
- {
- for (int i = _boxes.Count - 1; i >= 0; i--)
- {
- if (_boxes[i].Detail.Name == detail.Name)
- {
- _boxes[i].ReduceDetail();
- detail = _boxes[i].Detail;
- if (_boxes[i].Count == 0)
- {
- _boxes.RemoveAt(i);
- }
- return true;
- }
- }
- return false;
- }
- public void ShowInfo()
- {
- foreach (Box box in _boxes)
- {
- box.ShowInfo();
- }
- }
- }
- class Box
- {
- public Box(Detail detail, int count)
- {
- Detail = detail;
- Count = count;
- }
- public Detail Detail { get; }
- public int Count { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"{Detail.Name} - {Count} штук");
- }
- public void ReduceDetail()
- {
- Count--;
- }
- }
- class FactoryDetails
- {
- private List<Detail> _details = new List<Detail>();
- public FactoryDetails()
- {
- Create();
- }
- public int DetailsCount => _details.Count;
- public List<Detail> CopyList()
- {
- List<Detail> copiedDetails = new List<Detail>();
- foreach (Detail detail in _details)
- {
- copiedDetails.Add(new Detail(detail.Name, detail.Price));
- }
- return copiedDetails;
- }
- public Detail CopyDetail(int index)
- {
- return _details[index];
- }
- private void Create()
- {
- int minPrice = 100;
- int maxPrice = 400;
- string[] names = new string[]
- {
- "генератор", "бензонасос", "патрубки",
- "термостат", "распредвал", "коленвал",
- "сцепление", "маховик", "глушитель"
- };
- for (int i = 0; i < names.Length; i++)
- {
- int price = Utils.GetRandomNumber(minPrice, maxPrice);
- _details.Add(new Detail(names[i], price));
- }
- }
- }
- class Car
- {
- private List<Detail> _details;
- public Car(List<Detail> details)
- {
- _details = details;
- BrokeRandomDetails();
- }
- public int DetailsCount => _details.Count;
- public void ShowInfo()
- {
- for (int i = 0; i < _details.Count; i++)
- {
- Console.Write(i + 1 + " ");
- _details[i].ShowInfo();
- }
- }
- public int HasBrokenDetail()
- {
- int count = 0;
- foreach (Detail detail in _details)
- {
- if (detail.IsBroken)
- count++;
- }
- return count;
- }
- public Detail GetDetail(int index)
- {
- return _details.ElementAt(index);
- }
- public void ReplaceDetail(int index, Detail detail)
- {
- _details.RemoveAt(index);
- _details.Add(detail);
- }
- private void BrokeRandomDetails()
- {
- int minPercent = 0;
- int maxPercent = 4;
- int countBrokenDetails = 0;
- foreach (Detail detail in _details)
- {
- if (Utils.GetRandomNumber(minPercent, maxPercent) == 0)
- {
- detail.MakeBroken();
- countBrokenDetails++;
- }
- }
- if (countBrokenDetails == 0)
- _details[Utils.GetRandomNumber(0, _details.Count - 1)].MakeBroken();
- }
- }
- class Detail
- {
- public Detail(string name, int price)
- {
- Name = name;
- Price = price;
- IsBroken = false;
- }
- public string Name { get; }
- public int Price { get; }
- public bool IsBroken { get; private set; }
- public void MakeBroken()
- {
- IsBroken = true;
- }
- public void ShowInfo()
- {
- if (IsBroken)
- Console.WriteLine($"{Name} - деталь сломана, стоимость замены - {Price} монет");
- else
- Console.WriteLine($"{Name} - деталь исправна");
- }
- }
- class Utils
- {
- private static Random s_random = new Random();
- public static int GetRandomNumber(int minValue, int maxValue)
- {
- return s_random.Next(minValue, maxValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment