Last active
June 8, 2025 09:09
-
-
Save Ariveder4i/c1ad92d7211cb4588f4770d73259b991 to your computer and use it in GitHub Desktop.
ДЗ: Автосервис
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace CarService | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
CarService carService = new CarService(5000, 300, 50); | |
carService.Work(); | |
} | |
} | |
class CarService | |
{ | |
private CarFactory _carFactory; | |
private Storage _storage; | |
private Queue <CarOwner> _owners; | |
public CarService(int cashRegister, int fine, int priceRepair) | |
{ | |
_carFactory = new CarFactory(); | |
_storage = new Storage(); | |
_owners = new Queue<CarOwner>(); | |
FillOwners(); | |
CashRegister = cashRegister; | |
Fine = fine; | |
PriceRepair = priceRepair; | |
} | |
public int CashRegister { get; private set; } | |
public int Fine { get; private set; } | |
public int PriceRepair { get; private set; } | |
public void Work() | |
{ | |
const string FixCarCommand = "1"; | |
const string RefuseRepairCommand = "2"; | |
while (_owners.Count > 0) | |
{ | |
CarOwner carOwner = _owners.Dequeue(); | |
Console.WriteLine("*** Автосервис <<РУКАСТЫЙ ДЖОН>> ***\n"); | |
Console.WriteLine($"Денег в кассе: {CashRegister}"); | |
Console.WriteLine($"Для начала ремонта нажмите {FixCarCommand}"); | |
Console.WriteLine($"Для отказа в ремонте нажмите {RefuseRepairCommand}"); | |
Console.WriteLine($"Штраф за отказ ремонта составит {Fine}"); | |
Console.WriteLine($"Цена за замену детали составит {PriceRepair}"); | |
Console.WriteLine("\nCейчас обслуживается: "); | |
carOwner.ShowInfo(); | |
string command = Console.ReadLine(); | |
switch (command) | |
{ | |
case FixCarCommand: | |
FixCar(carOwner); | |
break; | |
case RefuseRepairCommand: | |
TransferMoneyForClient(carOwner); | |
break; | |
default: | |
Console.WriteLine("нет такой команды"); | |
break; | |
} | |
} | |
Console.Clear(); | |
} | |
private void FixCar(CarOwner carOwner) | |
{ | |
Console.Clear(); | |
while (carOwner.GetNumberWholeDetails() != carOwner.GetNumberDetails()) | |
{ | |
Console.WriteLine($"Касса автосервиса: {CashRegister}\n"); | |
_storage.ShowInfo(); | |
carOwner.ShowInfo(); | |
int refuseRepair = 0; | |
Console.Write($"\nВведите номер детали, чтобы ее заменить на деталь со склада " + | |
$"\nили введите <<{refuseRepair}>> для отказа, тогда вы заплатите " + | |
$"владельцу штраф авто <<{Fine}>> за каждую непочиненную деталь: "); | |
if (int.TryParse(Console.ReadLine(), out int index)) | |
{ | |
if (index > 0 && index <= carOwner.GetNumberDetails()) | |
{ | |
string nameDetail = carOwner.GetDetail(index - 1).Name; | |
Detail detail = _storage.GetDetail(nameDetail); | |
Detail detailOwner = carOwner.GetDetail(index - 1); | |
ReplaceDetail(detail, detailOwner, carOwner); | |
} | |
else if (index == refuseRepair) | |
{ | |
TransferFineForNotRepairDetail(carOwner); | |
return; | |
} | |
else | |
{ | |
Console.WriteLine("такой детали тут нет"); | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("введено не число"); | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
Console.WriteLine($"Владелец {carOwner.Name} уехал, ему заменили все сломанные детали"); | |
Console.ReadKey(); | |
} | |
private void ReplaceDetail(Detail detail, Detail detailOwner, CarOwner carOwner) | |
{ | |
if (_storage.Contains(detail)) | |
{ | |
if (detailOwner.IsBroken) | |
{ | |
carOwner.ReplaceDetail(detail); | |
_storage.RemoveDetail(detail); | |
carOwner.TransferMoney(detail); | |
AddMoney(detail); | |
} | |
else | |
{ | |
Console.WriteLine("деталь не сломана!"); | |
Console.ReadKey(); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("На складе нет такой детали"); | |
Console.ReadKey(); | |
} | |
Console.Clear(); | |
} | |
private void AddMoney(Detail detail) | |
{ | |
CashRegister += detail.Price + PriceRepair; | |
} | |
private void TransferFineForNotRepairDetail(CarOwner carOwner) | |
{ | |
int detailCount = carOwner.GetNumberDetails(); | |
int numberNotBrokenDetail = carOwner.GetNumberWholeDetails(); | |
int numberNotRepairDetail = detailCount - numberNotBrokenDetail; | |
if (numberNotRepairDetail > 0) | |
{ | |
int fineforNotRepairDetail = Fine * numberNotRepairDetail; | |
CashRegister -= fineforNotRepairDetail; | |
carOwner.AddMoney(fineforNotRepairDetail); | |
_owners.Dequeue(); | |
Console.Clear(); | |
} | |
} | |
private void FillOwners() | |
{ | |
_owners.Enqueue(new CarOwner(10000, "Иван", _carFactory.Create())); | |
_owners.Enqueue(new CarOwner(10000, "Петр", _carFactory.Create())); | |
_owners.Enqueue(new CarOwner(10000, "Аркадий", _carFactory.Create())); | |
_owners.Enqueue(new CarOwner(10000, "Геннадий", _carFactory.Create())); | |
_owners.Enqueue(new CarOwner(10000, "Екатерина", _carFactory.Create())); | |
} | |
private void TransferMoneyForClient(CarOwner carOwner) | |
{ | |
CashRegister -= Fine; | |
carOwner.AddMoney(Fine); | |
Console.Clear(); | |
} | |
} | |
class Storage | |
{ | |
private Dictionary<Detail, int> _details; | |
public Storage() | |
{ | |
FillDetails(); | |
} | |
public Detail GetDetail(string name) | |
{ | |
foreach (var detail in _details) | |
{ | |
if (detail.Key.Name == name) | |
{ | |
return detail.Key; | |
} | |
} | |
return null; | |
} | |
public bool Contains(Detail detail) | |
{ | |
return _details[detail] > 0; | |
} | |
public void ShowInfo() | |
{ | |
Console.WriteLine("* * * Прайс-лист автосервиса * * *\n"); | |
foreach (var detail in _details) | |
{ | |
detail.Key.ShowInfoPrice(); | |
Console.WriteLine($", колличество деталей: {detail.Value}"); | |
} | |
Console.WriteLine(); | |
} | |
public void RemoveDetail(Detail detail) | |
{ | |
_details[detail]--; | |
} | |
private int GetRandomNumber() | |
{ | |
int minRandomNumber = 2; | |
int maxRandomNumber = 5; | |
return UserUtils.Generate(minRandomNumber, maxRandomNumber + 1); | |
} | |
private void FillDetails() | |
{ | |
DetailCatalog detailcatalog = new DetailCatalog(); | |
List<Detail> details = detailcatalog.CreateCatalogDetails(); | |
_details = new Dictionary<Detail, int>(); | |
foreach (Detail detail in details) | |
{ | |
_details.Add(detail.Clone(), GetRandomNumber()); | |
} | |
} | |
} | |
class CarOwner | |
{ | |
private Car _car; | |
public CarOwner(int money, string name, Car car) | |
{ | |
_car = car; | |
Money = money; | |
Name = name; | |
} | |
public int Money { get; private set; } | |
public string Name { get; private set; } | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"Владелец машины: {Name}, денег: {Money}"); | |
_car.ShowInfo(); | |
} | |
public int GetNumberWholeDetails() | |
{ | |
return _car.GetNumberWholeDetails(); | |
} | |
public int GetNumberDetails() | |
{ | |
return _car.DetailCount; | |
} | |
public Detail GetDetail(int index) | |
{ | |
return _car.GetDetail(index); | |
} | |
public void ReplaceDetail(Detail detail) | |
{ | |
_car.ReplaceDetail(detail); | |
} | |
public int AddMoney(int money) | |
{ | |
Money += money; | |
return Money; | |
} | |
public int TransferMoney(Detail detail) | |
{ | |
Money -= detail.Price; | |
return Money; | |
} | |
} | |
class Car | |
{ | |
private List<Detail> _catalog; | |
public Car(List<Detail> catalog) | |
{ | |
_catalog = catalog; | |
} | |
public int DetailCount => _catalog.Count; | |
public void ShowInfo() | |
{ | |
for (int i = 0; i < _catalog.Count; i++) | |
{ | |
_catalog[i].ShowInfoNoPrice(i + 1); | |
} | |
Console.WriteLine(); | |
} | |
public int GetNumberWholeDetails() | |
{ | |
int numberWholeDetails = 0; | |
foreach (Detail detail in _catalog) | |
{ | |
if (detail.IsBroken == false) | |
{ | |
numberWholeDetails++; | |
} | |
} | |
return numberWholeDetails; | |
} | |
public void ReplaceDetail(Detail detail) | |
{ | |
Detail foundDetail = null; | |
foreach (var item in _catalog) | |
{ | |
if (detail.Name == item.Name) | |
{ | |
foundDetail = item; | |
break; | |
} | |
} | |
_catalog.Remove(foundDetail); | |
_catalog.Add(detail); | |
} | |
public Detail GetDetail(int index) | |
{ | |
return _catalog[index]; | |
} | |
} | |
class CarFactory | |
{ | |
public Car Create() | |
{ | |
DetailCatalog detailCatalog = new DetailCatalog(); | |
List<Detail> details = detailCatalog.CreateCatalogDetails(); | |
RollChance(details); | |
return new Car(details); | |
} | |
private void RollChance(List<Detail> details) | |
{ | |
int percent = 30; | |
foreach (Detail detail in details) | |
{ | |
if (UserUtils.RollChance(percent)) | |
{ | |
detail.GetBroken(); | |
} | |
} | |
foreach (Detail detail in details) | |
{ | |
if (!detail.IsBroken) | |
{ | |
detail.GetBroken(); | |
break; | |
} | |
} | |
} | |
} | |
class DetailCatalog | |
{ | |
public List <Detail> CreateCatalogDetails() | |
{ | |
List < Detail > catalog = new List<Detail> () | |
{ | |
new Detail ("Двигатель", 1200 ), | |
new Detail ("Трансмиссия", 550), | |
new Detail ("Колесо", 80), | |
new Detail ("Аккумулятор", 250), | |
new Detail ("Шаровая", 180), | |
new Detail ("Лобовое стекло", 300) | |
}; | |
return catalog; | |
} | |
} | |
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 GetBroken() | |
{ | |
IsBroken = true; | |
} | |
public Detail Clone() | |
{ | |
return new Detail(Name, Price); | |
} | |
public void ShowInfoNoPrice(int index) | |
{ | |
string status = IsBroken ? "сломано" : "не сломано"; | |
Console.WriteLine($"{index}) {Name}, {status}"); | |
} | |
public void ShowInfoPrice() | |
{ | |
Console.Write($"заменить {Name}, цена: {Price}"); | |
} | |
} | |
static class UserUtils | |
{ | |
private static Random s_random = new Random(); | |
public static int Generate(int minValue, int maxValue) | |
{ | |
return s_random.Next(minValue, maxValue); | |
} | |
public static bool RollChance(int percent) | |
{ | |
int maxPercent = 100; | |
return Generate(0, maxPercent + 1) <= percent; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment