Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Program
- {
- static void Main(string[] args)
- {
- CarService carService = new CarService();
- bool isWorking = true;
- while (isWorking)
- {
- carService.Work();
- }
- }
- }
- class CarService
- {
- private DetailsStorage _detailsStorage;
- private Car _car;
- private int _breakageIndex;
- private int _moneyBalance = 0;
- public CarService()
- {
- _detailsStorage = new DetailsStorage();
- _car = new Car();
- }
- public void Work()
- {
- _car = Clone();
- int sumFine = 10;
- Detail breakDetail = TakeBreakDetail();
- int priceForTheWork = breakDetail.Price + breakDetail.PriceForRepairs;
- _detailsStorage.ShowDetails();
- Console.SetCursorPosition(0, 14);
- Console.WriteLine($"Банк:{_moneyBalance}\nВ автосервис заехал автомобиль.\n\n|Сломалась деталь {breakDetail.Name}\n|Цена за работу:{priceForTheWork} монет\n\nВведите название детали для замены или если нужной детали нет, введите noDetails и заплатите штраф.");
- string userInput = Console.ReadLine();
- if (userInput == "noDetails")
- {
- _moneyBalance -= sumFine;
- }
- else
- {
- if (breakDetail.Name == userInput)
- {
- Detail newDetail = TakeDetail(userInput);
- _moneyBalance += priceForTheWork;
- Console.WriteLine($"Починка прошла успешно! Автосервис заработал {priceForTheWork} монет.");
- ReturnNewDetail(newDetail);
- }
- else
- {
- Detail temporaryDetail = TakeDetail(userInput);
- _moneyBalance -= breakDetail.Price;
- Console.WriteLine($"Вы заменили не ту деталь. Вам придется возместить клиенту {breakDetail.Price} монет.");
- }
- }
- Console.ReadKey();
- Console.Clear();
- }
- public Detail TakeDetail(string detailName)
- {
- return _detailsStorage.GiveDetail(detailName);
- }
- public Detail TakeBreakDetail()
- {
- return _car.GiveBreakDetail();
- }
- public void ReturnNewDetail(Detail detail)
- {
- _car.TakeNewDetail(detail);
- }
- public Car Clone()
- {
- return new Car();
- }
- }
- class DetailsStorage
- {
- private static Random _random = new Random();
- private List<Detail> _details;
- public DetailsStorage()
- {
- _details = new List<Detail>();
- AddToDetails(new Wheel());
- AddToDetails(new Conditioner());
- AddToDetails(new Transmission());
- }
- public void AddToDetails(Detail detail)
- {
- int minIndex = 1;
- int maxIndex = 5;
- int maxDetails = _random.Next(minIndex, maxIndex);
- for (int i = 0; i < maxDetails; i++)
- {
- _details.Add(detail.Clone());
- }
- }
- public Detail GiveDetail(string detailName)
- {
- Detail temporaryDetail = null;
- foreach (var detail in _details)
- {
- if (detail.Name == detailName)
- {
- temporaryDetail = detail;
- break;
- }
- }
- _details.Remove(temporaryDetail);
- return temporaryDetail;
- }
- public void ShowDetails()
- {
- Console.WriteLine("Кол-во деталей на складе:");
- foreach (var detail in _details)
- {
- detail.ShowInfo();
- }
- }
- }
- class Detail
- {
- public Detail(string name, int price,int priceForRepairs, bool condition)
- {
- Name = name;
- Price = price;
- PriceForRepairs = priceForRepairs;
- Condition = condition;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- public int PriceForRepairs { get; private set; }
- public bool Condition { get; private set; }
- public virtual Detail Clone()
- {
- return new Detail(Name, Price, PriceForRepairs, Condition);
- }
- public void ShowInfo()
- {
- Console.WriteLine($"{Name}|Цена:{Price}");
- }
- public void BreakDetail()
- {
- Condition = false;
- }
- }
- class Wheel : Detail
- {
- public override Detail Clone()
- {
- return new Wheel();
- }
- public Wheel() : base ("Колесо", 10, 20, true) { }
- }
- class Conditioner : Detail
- {
- public override Detail Clone()
- {
- return new Conditioner();
- }
- public Conditioner() : base("Кондиционер", 15, 30, true) { }
- }
- class Transmission : Detail
- {
- public override Detail Clone()
- {
- return new Transmission();
- }
- public Transmission() : base("Коробка передач", 30, 60, true) { }
- }
- class Car
- {
- private List<Detail> _details;
- public Car()
- {
- _details = new List<Detail>() { new Wheel(), new Conditioner(), new Transmission() };
- BreakCar();
- }
- public void BreakCar()
- {
- int randomIndex = Utils.GetRandomNumber(_details.Count - 1);
- _details[randomIndex].BreakDetail();
- }
- public void TakeNewDetail(Detail detail)
- {
- _details.Add(detail);
- }
- public Detail GiveBreakDetail()
- {
- Detail temporaryDetail = null;
- foreach (Detail detail in _details)
- {
- if(detail.Condition == false)
- {
- temporaryDetail = detail;
- break;
- }
- }
- _details.Remove(temporaryDetail);
- return temporaryDetail;
- }
- }
- class Utils
- {
- private static Random _random = new Random();
- public static int GetRandomNumber(int max)
- {
- return _random.Next(max + 1);
- }
- public static int ConvertToInt()
- {
- int templateNumber;
- string userInput = string.Empty;
- while (int.TryParse(userInput, out templateNumber) == false)
- {
- userInput = Console.ReadLine();
- }
- return templateNumber;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement