Skip to content

Instantly share code, notes, and snippets.

@ShamilAitov
Last active August 13, 2023 21:10
Show Gist options
  • Save ShamilAitov/cc539dc8d52cc09289deb1dd0c5b2303 to your computer and use it in GitHub Desktop.
Save ShamilAitov/cc539dc8d52cc09289deb1dd0c5b2303 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ООП13Задание
{
internal class Program
{
static void Main(string[] args)
{
CarService carService = new CarService();
carService.Play();
}
}
class CarService
{
private Warehouse _warehouse = new Warehouse();
private Queue<Client> _clients = new Queue<Client>();
private string _name = "АвтоСпорт";
private int _pricePerJob = 4000;
private int _money = 10000;
private int _priceFine = 1000;
public CarService()
{
AddQueueCustomers();
}
public void Play()
{
const int TakeNewClient = 1;
const int ExitProgram = 2;
int minWallet = 0;
int userInput = 0;
bool isWorking = true;
while (isWorking == true || _money <= minWallet)
{
Console.WriteLine($"Автосервис - {_name}");
Console.Write($"{TakeNewClient} - взять в работу нового клиента\n{ExitProgram} - закрыть автосервис\nВведите команду: ");
userInput = GetNumber();
switch (userInput)
{
case TakeNewClient:
ServeCustomer();
break;
case ExitProgram:
isWorking = false;
break;
default:
Console.Write("Попробуйте еще раз: ");
break;
}
Console.ReadKey();
Console.Clear();
}
}
private void ServeCustomer()
{
if (_clients.Count > 0)
{
Client client = _clients.Dequeue();
int pricePerJob = client.Detail.Price + _pricePerJob;
int priceFine = 0;
Console.Clear();
Console.WriteLine($"У клиента сломалась машина, под маркой автомобиля: {client.Car} - требуется замена детали: {client.Detail.Name}");
Console.WriteLine($"Итоговая цена: {pricePerJob}\n");
if (client.CanPay(pricePerJob))
{
_warehouse.ShowInfoSlots();
Console.Write($"Введите под каким номером использовать деталь: ");
int detailIndex = _warehouse.ReadIndex();
Slot selectedSlot = _warehouse.GetSlot(detailIndex);
if (_warehouse.TryDecreaseSlot(detailIndex))
{
if (client.Detail.Name == selectedSlot.Detail.Name)
{
client.DecreaseWallet(pricePerJob);
_money += pricePerJob;
Console.Write("Деталь заменена успешно: ");
ShowInfoWallet();
}
else
{
Console.WriteLine("Вы заменили не ту деталь!");
priceFine = DecreaseWallet();
client.IncreaseWallet(priceFine);
ShowPriceFine(priceFine);
ShowInfoWallet();
}
}
else
{
Console.WriteLine($"У вас закончились детали под наименование: {selectedSlot.Detail.Name}");
priceFine = DecreaseWallet();
client.IncreaseWallet(priceFine);
ShowPriceFine(priceFine);
ShowInfoWallet();
}
}
else
{
Console.Write("У клиента недостаточно средств, приедет в следующий раз!\nНажмите на любую кнопку, чтобы продолжить: ");
}
}
else
{
Console.WriteLine("Клиентов больше не осталось, пора заканчивать смену!");
}
}
private int DecreaseWallet()
{
int priceFine = _priceFine;
int minWallet = 0;
if (_money < _priceFine)
{
priceFine = _money;
_money = minWallet;
}
else
{
_money -= priceFine;
}
return priceFine;
}
private void ShowPriceFine(int priceFine)
{
if (priceFine == _priceFine)
{
Console.WriteLine($"Совершена выплата штрафа {priceFine}");
}
else
{
Console.WriteLine($"Совершена выплата штрафа: отдали последние {priceFine}");
}
}
private int GetNumber()
{
int index;
while (int.TryParse(Console.ReadLine(), out index) == false)
{
Console.Write("Нужно ввести именно число, попробуйте еще раз: ");
}
return index;
}
private void ShowInfoWallet()
{
Console.WriteLine($"Баланс на счету {_money}");
}
private void AddQueueCustomers()
{
Dictionary<string, int> details = new Dictionary<string, int>
{ { "Подшипник", 1600 }, { "Ступица", 2200 }, { "Маховик", 2900 },
{ "Сальник", 1700 }, { "Бензанасос", 3200 }, { "Патрубок", 1900 } };
string[] nameCar = { "Мерседес", "Ауди", "БВМ", "Лексус", "Ламборгини", "Тесла" };
int randomNameCar = 0;
int randomNameDetail = 0;
int randomWallet = 0;
int customerQueue = 10;
int minWallet = 4000;
int maxWallet = 10000;
for (int i = 0; i < customerQueue; i++)
{
randomWallet = UserUtils.GetRandomNumber(minWallet, maxWallet);
randomNameCar = UserUtils.GetRandomNumber(nameCar.Length);
randomNameDetail = UserUtils.GetRandomNumber(details.Count);
Detail detail = new Detail(details.ElementAt(randomNameDetail).Key, details.ElementAt(randomNameDetail).Value);
_clients.Enqueue(new Client(nameCar[randomNameCar], randomWallet, detail));
}
}
}
class Warehouse
{
private List<Detail> _details = new List<Detail>();
private List<Slot> _slots = new List<Slot>();
public Warehouse()
{
FillSlot();
}
public void FillSlot()
{
int minNumberAutoParts = 3;
int maxNumberAutoParts = 10;
int numberAutoParts;
FillDetails();
for (int i = 0; i < _details.Count; i++)
{
numberAutoParts = UserUtils.GetRandomNumber(minNumberAutoParts, maxNumberAutoParts);
_slots.Add(new Slot(_details[i], numberAutoParts));
}
}
public Slot GetSlot(int index)
{
return _slots[index];
}
public bool TryDecreaseSlot(int index)
{
if (_slots[index].CanDecrease)
{
_slots[index].DecreaseCount();
return true;
}
else
{
return false;
}
}
public int ReadIndex()
{
int index = GetNumber();
while (0 > index || index >= _slots.Count)
{
Console.Write("Такой детали нет, попробуйте еще раз: ");
index = GetNumber();
}
return index;
}
private int GetNumber()
{
int index;
while (int.TryParse(Console.ReadLine(), out index) == false)
{
Console.Write("Нужно ввести именно число, попробуйте еще раз: ");
}
return index;
}
public void FillDetails()
{
Dictionary<string, int> details = new Dictionary<string, int>
{ { "Подшипник", 1600 }, { "Ступица", 2200 }, { "Маховик", 2900 },
{ "Сальник", 1700 }, { "Бензанасос", 3200 }, { "Патрубок", 1900 } };
foreach (var detail in details)
{
_details.Add(new Detail(detail.Key, detail.Value));
}
}
public void ShowInfoSlots()
{
Console.WriteLine("На складе следующее наличие деталь: ");
for (int i = 0; i < _slots.Count; i++)
{
Console.Write($"Под №{i} - ");
_slots[i].ShowInfoDetails();
}
}
}
class Slot
{
public Slot(Detail detail, int count)
{
Detail = detail;
Count = count;
}
public Detail Detail { get; private set; }
public int Count { get; private set; }
public bool CanDecrease => Count > 0;
public void ShowInfoDetails()
{
Detail.ShowInfo();
Console.WriteLine($"({Count} шт.)");
}
public void DecreaseCount()
{
if (CanDecrease)
{
Count--;
}
}
}
class Detail
{
public Detail(string name, int price)
{
Name = name;
Price = price;
}
public string Name { get; private set; }
public int Price { get; private set; }
public void ShowInfo()
{
Console.Write($"Деталь: {Name} - {Price} руб.");
}
}
class Client
{
public Client(string car, int money, Detail detail)
{
Car = car;
Money = money;
Detail = detail;
}
public int Money { get; private set; }
public string Car { get; private set; }
public Detail Detail { get; private set; }
public void DecreaseWallet(int purchasePrice)
{
Money -= purchasePrice;
}
public bool IncreaseWallet(int purchasePrice)
{
int minPurchasePrice = 0;
if (purchasePrice < minPurchasePrice)
{
return false;
}
Money += purchasePrice;
return true;
}
public bool CanPay(int purchasePrice)
{
return Money > purchasePrice;
}
}
class UserUtils
{
private static Random _random = new Random();
public static int GetRandomNumber(int min, int max)
{
return _random.Next(min, max);
}
public static int GetRandomNumber(int max)
{
return _random.Next(max);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment