Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CarService
- {
- class Program
- {
- static void Main(string[] args)
- {
- CarService carService = new CarService();
- carService.Work();
- }
- }
- class CarService
- {
- private Storage _storage = new Storage();
- private Queue<Car> _cars = new Queue<Car>();
- public int Money { get; private set; }
- public CarService()
- {
- int carCount = 100;
- Money = 0;
- CreateCarsQueue(carCount);
- }
- public void Work()
- {
- bool _isWorking = true;
- ConsoleKeyInfo consoleKey;
- Console.WriteLine("Press any button to start");
- consoleKey = Console.ReadKey();
- Console.Clear();
- while (_isWorking)
- {
- if (_cars.Count > 0 && consoleKey.Key != ConsoleKey.Escape)
- {
- _storage.ShowInfo();
- Console.WriteLine($"Balance - {Money}$\n");
- ReciveTheOrder();
- }
- else
- {
- _isWorking = false;
- }
- Console.WriteLine("\nPress any button to continue");
- consoleKey = Console.ReadKey();
- Console.Clear();
- }
- }
- private void ReciveTheOrder()
- {
- Car car = _cars.Peek();
- bool isServiced = true;
- Console.WriteLine("Need to repair:");
- car.ShowInfo();
- Console.WriteLine();
- Console.WriteLine("1 - Accept repair | 2 - Reject repair");
- Console.Write("Input: ");
- switch (Console.ReadLine())
- {
- case "1":
- Fix(car);
- break;
- case "2":
- Console.WriteLine("You rejected the repair and received a fine");
- TakeFine();
- break;
- default:
- Console.WriteLine("Wrong action");
- isServiced = false;
- break;
- }
- if(isServiced == true)
- {
- _cars.Dequeue();
- }
- }
- private void Fix(Car car)
- {
- Detail detail;
- Console.Write("Choose detail: ");
- detail = _storage.ChooseElement(GetNumber() - 1);
- if(detail != null && detail.Name == car.BrokenElement.Name)
- {
- _storage.RemoveElement(detail);
- TakePaid(detail);
- Console.WriteLine("Fixed");
- }
- else
- {
- TakeFine();
- Console.WriteLine("Wrong detail");
- }
- }
- private void TakeFine()
- {
- int _priceForFine = 300;
- Money -= _priceForFine;
- }
- private void TakePaid(Detail detail)
- {
- int _priceForRepair = 50;
- Money += detail.Price + _priceForRepair;
- }
- private void CreateCarsQueue(int size)
- {
- Random random = new Random();
- int randomCount;
- for (int i = 0; i < size; i++)
- {
- randomCount = random.Next();
- _cars.Enqueue(new Car(randomCount));
- }
- }
- private int GetNumber()
- {
- int number;
- string userInput;
- do
- {
- userInput = Console.ReadLine();
- }
- while (int.TryParse(userInput, out number) == false);
- return number;
- }
- }
- class Storage
- {
- private DatabaseOfDetails _dataBase;
- private List<Detail> _details;
- public Storage()
- {
- _dataBase = new DatabaseOfDetails();
- _details = new List<Detail>();
- Fill();
- }
- public void ShowInfo()
- {
- int countOfDetails;
- int index = 1;
- Console.WriteLine("\n------------------------Storage--------------------------");
- foreach (Detail detail in _details.Distinct())
- {
- countOfDetails = _details.Where(x => x == detail).Count();
- detail.ShowInfo(countOfDetails, index);
- index++;
- }
- Console.WriteLine("------------------------Storage--------------------------\n");
- }
- public Detail ChooseElement(int index)
- {
- if (index >= 0 && index < _details.Distinct().Count())
- {
- Detail detail = _details.Distinct().ElementAt(index);
- Console.WriteLine($"You choose - {detail.Name}");
- return detail;
- }
- else
- {
- return null;
- }
- }
- public void RemoveElement(Detail detail)
- {
- if(_details.Contains(detail))
- {
- _details.Remove(detail);
- }
- else
- {
- Console.WriteLine("Error |remove element|");
- }
- }
- private void Fill()
- {
- int countOfDetails = 5;
- foreach (Detail detail in _dataBase.GetElementsList())
- {
- for (int i = 0; i < countOfDetails; i++)
- {
- _details.Add(detail);
- }
- }
- }
- }
- class Car
- {
- private DatabaseOfDetails _dataBase;
- public Detail BrokenElement { get; private set; }
- public Car(int randomCount)
- {
- _dataBase = new DatabaseOfDetails();
- BrokenElement = _dataBase.GetRandomDetail(randomCount);
- }
- public void ShowInfo()
- {
- BrokenElement.ShowInfo();
- }
- }
- class Detail
- {
- public string Name { get; private set; }
- public int Price { get; private set; }
- public Detail(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public void ShowInfo()
- {
- Console.WriteLine($"Element - {Name} | Price - {Price}$");
- }
- public void ShowInfo(int count, int index)
- {
- Console.WriteLine($"{index}. Element - {Name} | Price - {Price}$ | Count - {count}");
- }
- }
- class DatabaseOfDetails
- {
- private Dictionary<string, int> _details = new Dictionary<string, int>();
- public DatabaseOfDetails()
- {
- Create();
- }
- public Detail GetRandomDetail(int randomCount)
- {
- Random random = new Random(randomCount);
- Detail detail;
- string name;
- int price;
- string[] detailsName = GetDetailsName();
- int detailIndex = random.Next(0, detailsName.Length);
- name = detailsName[detailIndex];
- price = _details[name];
- detail = new Detail(name, price);
- return detail;
- }
- public List<Detail> GetElementsList()
- {
- List<Detail> details = new List<Detail>();
- foreach (KeyValuePair<string, int> keyValue in _details)
- {
- details.Add(new Detail(keyValue.Key, keyValue.Value));
- }
- return details;
- }
- private void Create()
- {
- _details.Add("battery", 5);
- _details.Add("brakes", 7);
- _details.Add("clutch", 13);
- _details.Add("engine", 30);
- _details.Add("gear box", 23);
- _details.Add("radiator", 9);
- }
- private string[] GetDetailsName()
- {
- Dictionary<string, int>.KeyCollection keyCollection = _details.Keys;
- string[] detailsName = new string[_details.Count];
- int detailIndex = 0;
- foreach (string name in keyCollection)
- {
- detailsName[detailIndex] = name;
- detailIndex++;
- }
- return detailsName;
- }
- }
- }
Add Comment
Please, Sign In to add comment