Skip to content

Instantly share code, notes, and snippets.

@lordero64
Last active September 13, 2023 08:27
Show Gist options
  • Save lordero64/a17be45c0b9ac751c257457c1a077ddc to your computer and use it in GitHub Desktop.
Save lordero64/a17be45c0b9ac751c257457c1a077ddc to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace Car_Repair
{
internal class Program
{
static void Main(string[] args)
{
CarService carService = new CarService();
carService.Work();
}
}
class CarService
{
private static Random _random = new Random();
private Storage _storage = new Storage();
private Queue<Car> _clients = new Queue<Car>();
private int _money = 320;
public CarService()
{
FillQueue();
}
public void Work()
{
Console.WriteLine("Добро пожаловать в автосервис!");
bool isWorking = true;
while (isWorking)
{
Console.WriteLine($"В очереди {_clients.Count} клиентов");
Console.WriteLine($"Ваш Баланс - {_money}\n");
ServeClient(_clients.Dequeue());
if (_money <= 0)
{
Console.WriteLine("Вы банкрот");
isWorking = false;
}
if (_clients.Count == 0)
{
Console.WriteLine($"\nРабочий день закончен! Ваш баланс = {_money}");
isWorking = false;
}
Console.ReadKey();
}
}
private void FillQueue()
{
int clientsAmount = 10;
for (int i = 0; i < clientsAmount; i++)
{
_clients.Enqueue(new Car());
}
}
private void ServeClient(Car car)
{
Console.WriteLine("Новый клиент.Выполяется поиск поврежденых деталей");
Detail brokenDetail = car.Diagnose();
if (brokenDetail == null)
{
Console.WriteLine("Машина в хорошем состоянии");
int diagnosticPrice = 15;
_money += diagnosticPrice;
Console.WriteLine("Плата за диагностику = {diagnosticPrice}\n");
return;
}
else
{
Console.WriteLine($"Была найдена поврежденная деталь - {brokenDetail.Name}");
}
Console.WriteLine("Поиск запчасти на складе для замены");
bool isFound = _storage.TryToGetDetail(brokenDetail);
if (isFound)
{
Console.WriteLine($"Деталь {brokenDetail.Name} найдена на складе!");
_storage.RemoveDetail(brokenDetail);
car.ChangeDetail(brokenDetail);
int repairPrice = 35;
Console.WriteLine($"За замену детали на новую Вы заработали - {repairPrice + brokenDetail.Price}\n");
_money += repairPrice + brokenDetail.Price;
}
else
{
Console.WriteLine("Нужной детали нет на складе!");
_storage.AddDetail(brokenDetail);
int fine = 200;
Console.WriteLine($"Штраф - {fine}, закупка недостающей детали - {brokenDetail.Price}\n");
_money -= brokenDetail.Price + fine;
}
}
}
class Car
{
private static Random _random = new Random();
private List<Detail> _carDetails;
public Car()
{
_carDetails = new List<Detail>()
{
new Detail( "Двигатель", 120),
new Detail( "Фильтр", 20),
new Detail( "Коробка передач", 80),
new Detail( "Колодки", 40),
new Detail( "Подвеска", 60)
};
}
public Detail Diagnose()
{
Detail brokenDetal;
brokenDetal = _carDetails[_random.Next(_carDetails.Count)];
brokenDetal.BreakDetail();
return brokenDetal;
}
public void ChangeDetail(Detail detail)
{
for (int i = 0; i < _carDetails.Count; i++)
{
if (_carDetails[i].Name == detail.Name)
{
_carDetails.Remove(_carDetails[i]);
_carDetails.Add(detail);
}
}
}
}
class Storage
{
private List<Detail> _storageDetails = new List<Detail>();
public Storage()
{
_storageDetails.Add(new Detail("Двигатель", 120));
_storageDetails.Add(new Detail("Фильтр", 20));
_storageDetails.Add(new Detail("Фильтр", 20));
_storageDetails.Add(new Detail("Коробка передач", 80));
_storageDetails.Add(new Detail("Коробка передач", 80));
_storageDetails.Add(new Detail("Колодки", 40));
_storageDetails.Add(new Detail("Колодки", 40));
_storageDetails.Add(new Detail("Подвеска", 60));
_storageDetails.Add(new Detail("Подвеска", 60));
_storageDetails.Add(new Detail("Подвеска", 60));
}
public void AddDetail(Detail detail)
{
_storageDetails.Add(detail);
}
public void RemoveDetail(Detail detail)
{
_storageDetails.Remove(detail);
}
public bool TryToGetDetail(Detail detail)
{
for (int i = 0; i < _storageDetails.Count; i++)
{
if (_storageDetails[i].Name == detail.Name)
{
return true;
}
}
return false;
}
}
class Detail
{
public Detail(string name, int price)
{
Name = name;
Price = price;
isBroken = false;
}
public string Name { get; private set; }
public int Price { get; private set; }
public bool isBroken { get; private set; }
public void BreakDetail()
{
isBroken = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment