Skip to content

Instantly share code, notes, and snippets.

@Dead-in-side
Last active April 27, 2024 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dead-in-side/3ce4512511aef969c81af1cbd1f27910 to your computer and use it in GitHub Desktop.
Save Dead-in-side/3ce4512511aef969c81af1cbd1f27910 to your computer and use it in GitHub Desktop.
namespace IJunior
{
public class Program
{
private static void Main(string[] args)
{
AutoserviceWorker worker = new AutoserviceWorker();
worker.Work();
}
}
public class AutoserviceWorker
{
private AutoService _autoService = new AutoService();
public void Work()
{
bool isWork = true;
while (isWork)
{
_autoService.ShowInfo();
Console.WriteLine();
Console.WriteLine("Введите команду: ");
Console.WriteLine((int)ConsoleCommand.InspectNextCar + " Принять следующего клиента");
Console.WriteLine((int)ConsoleCommand.ShowDetails + ". Показать детали на складе");
Console.WriteLine((int)ConsoleCommand.Exit + " Выход.");
if (int.TryParse(Console.ReadLine(), out int userInput))
{
switch (userInput)
{
case (int)ConsoleCommand.ShowDetails:
_autoService.ShowDetails();
break;
case (int)ConsoleCommand.InspectNextCar:
_autoService.InspectCar();
break;
case (int)ConsoleCommand.Exit:
isWork = false;
break;
default:
Console.WriteLine("некорректный ввод");
break;
}
}
Console.ReadLine();
Console.Clear();
}
}
}
public class AutoService
{
private List<Detail> _details = new List<Detail>();
private Queue<Car> _cars = new Queue<Car>();
private DetailFactory _detailFactory = new DetailFactory();
Dictionary<string, int> _logBook = new Dictionary<string, int>();
private int _numberOfDetail = 50;
private int _numberOFCars = 10;
private int _money = 0;
private int _defaultPrice = 30;
public AutoService()
{
AddDetails();
AddCars();
}
public void ShowInfo()
{
Console.WriteLine($"Автосервис. Монеты: {_money}.\nМашин в очереди:{_cars.Count}\nДеталей осталось: {_details.Count}");
}
public void ShowDetails()
{
foreach (string key in _logBook.Keys)
{
Console.WriteLine($"{key}. кол-во - {_logBook[key]}");
}
}
public void InspectCar()
{
if (_cars.Count == 0 == false)
{
Car car = _cars.Dequeue();
bool isWork = true;
while (isWork)
{
car.ShowInfo();
Console.WriteLine("Начать процесс починки?");
Console.WriteLine((int)ConsoleCommand.Yes + ". Да");
Console.WriteLine((int)ConsoleCommand.No + ". Нет");
if (int.TryParse(Console.ReadLine(), out int userInput))
{
if (userInput == (int)ConsoleCommand.Yes)
{
Console.Clear();
ChangeDetail(car);
isWork = false;
}
else if (userInput == (int)ConsoleCommand.No)
{
Console.WriteLine("Вы отказали клиенту в обслуживании. Он уехал");
isWork = false;
}
else
{
Console.WriteLine("Некорректный ввод");
}
}
Console.ReadLine();
Console.Clear();
}
}
else
{
Console.WriteLine("Клиенты закончились");
}
}
private void ChangeDetail(Car car)
{
bool isWork = true;
int changedDetails = 0;
string exitWord = "Exit";
string userInput;
while (isWork)
{
car.ShowInfo();
Console.WriteLine($"Номер детали для замены? Введите {exitWord}, если хотите прервать замену деталей\nВы заплатите неустойку");
userInput = Console.ReadLine();
if (int.TryParse(userInput, out int index) && index > 0 && index <= car.DetailNumber)
{
index--;
Detail detail = _details.Find(delegate (Detail detail) { return detail.Name == car.GetDetailAtIndex(index).Name; });
if (_details.Remove(detail) == false || car.GetDetailAtIndex(index).IsFine == true)
{
Console.WriteLine("Вы пытаетесь поменять некорректную деталь. Вы платите компенсацию");
_money -= _defaultPrice;
}
else
{
car.RemoveDetail(car.GetDetailAtIndex(index));
car.SetNewDetail(detail);
_logBook[detail.Name]--;
_money += _defaultPrice + detail.ChangePrice;
changedDetails++;
}
}
else if (userInput == exitWord)
{
if (changedDetails == 0)
{
_money -= _defaultPrice;
}
else
{
_money -= _defaultPrice * (car.BrokenDetails - changedDetails);
}
break;
}
if (TryFindBrokenDetail(car) == false)
{
Console.WriteLine("Все сломанные детали заменены");
isWork = false;
}
Console.Clear();
}
}
private bool TryFindBrokenDetail(Car car)
{
for (int i = 0; i < car.DetailNumber; i++)
{
if (car.GetDetailAtIndex(i).IsFine == false)
{
return true;
}
}
return false;
}
private void AddDetails()
{
foreach (string names in _detailFactory.PriceListNames)
{
_logBook.Add(names, 0);
}
for (int i = 0; i < _numberOfDetail; i++)
{
Detail detail = _detailFactory.Create();
_logBook[detail.Name]++;
_details.Add(detail);
}
}
private void AddCars()
{
for (int i = 0; i < _numberOFCars; i++)
{
_cars.Enqueue(new Car());
}
}
}
public class Car
{
private List<Detail> _details = new List<Detail>();
private DetailFactory _detailFactory = new DetailFactory();
private int _minDetailBroken = 1;
public Car()
{
AddDetails();
}
public int DetailNumber { get; } = 10;
public int BrokenDetails { get; private set; }
public Detail GetDetailAtIndex(int index) => _details[index];
public void SetNewDetail(Detail detail) => _details.Add(detail);
public void RemoveDetail(Detail detail) => _details.Remove(detail);
public void ShowInfo()
{
for (int i = 0; i < _details.Count; i++)
{
int number = i + 1;
Console.WriteLine(number + ". " + _details[i].ShowInfo());
}
}
private void AddDetails()
{
BrokenDetails = 0;
bool isFirst = true;
for (int i = 0; i < DetailNumber; i++)
{
Detail detail = _detailFactory.Create();
if (BrokenDetails < UsingTools.GetRandomNumber(_minDetailBroken, DetailNumber) && isFirst)
{
detail.BreakDown();
BrokenDetails++;
}
_details.Add(detail);
isFirst = UsingTools.GetRandomBoolen();
}
}
}
public class Detail
{
public Detail(string name, int changePrice)
{
ChangePrice = changePrice;
Name = name;
}
public int ChangePrice { get; private set; }
public bool IsFine { get; private set; } = true;
public string Name { get; private set; }
public void BreakDown() => IsFine = false;
public string ShowInfo()
{
string message;
if (IsFine)
{
message = $"{Name}";
}
else
{
message = $"{Name} -- сломано, нужна замена.";
}
return message;
}
}
public class DetailFactory
{
private List<string> _names = new List<string> { "Деталь Тормозной системы", "Деталь Подвески", "Деталь Двигателя", "Деталь КПП", "Кусок Аккумулятора", "часть Топливного насоса" };
private Dictionary<string, int> _priceList = new Dictionary<string, int>();
private int _minPrice = 20;
private int _maxPrice = 50;
public DetailFactory()
{
FillPriceList();
}
public List<string> PriceListNames => _priceList.Keys.ToList();
public Detail Create()
{
string name = _names[UsingTools.GetRandomNumber(0, _names.Count)];
int price = _priceList.GetValueOrDefault(name);
return new Detail(name, price);
}
private void FillPriceList()
{
foreach (string name in _names)
{
_priceList.Add(name, UsingTools.GetRandomNumber(_minPrice, _maxPrice));
}
}
}
public class UsingTools
{
private static Random s_random = new Random();
public static int GetRandomNumber(int min, int max) => s_random.Next(min, max);
public static bool GetRandomBoolen()
{
int numberForBool = 10;
return s_random.Next(numberForBool) % 2 == 0;
}
}
public enum ConsoleCommand
{
Yes = 1,
No,
InspectNextCar = 1,
ShowDetails,
Exit
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment