/Task 50: CarService Secret
Created
October 6, 2023 18:28
-
-
Save adamkruver/fc60860e97c6ea8b01d0827fd982e9fb to your computer and use it in GitHub Desktop.
Task 50: CarService
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
namespace CarService | |
{ | |
public static class Program | |
{ | |
public static void Main() | |
{ | |
PartFactory partFactory = new PartFactory(); | |
CarFactory carFactory = new CarFactory(partFactory); | |
Queue<Car> cars = carFactory.CreateQueue(); | |
CarService carService = new CarService(partFactory); | |
carService.Work(cars); | |
} | |
} | |
public static class UserUtils | |
{ | |
private static readonly Random s_random = new(); | |
public static int GetRandomNumber(int maxRange) | |
{ | |
return s_random.Next(maxRange); | |
} | |
public static int GetRandomNumber(int minRange, int maxRange) | |
{ | |
return s_random.Next(minRange, maxRange); | |
} | |
} | |
public class PartFactory | |
{ | |
private readonly List<Part> _parts = new(); | |
public PartFactory() | |
=> Init(); | |
public List<Part> GetParts() | |
{ | |
List<Part> parts = new(); | |
foreach (Part part in _parts) | |
{ | |
parts.Add(new Part(part)); | |
} | |
return parts; | |
} | |
private void Init() | |
{ | |
_parts.AddRange(new List<Part>() | |
{ | |
new Part("Steering rack", 25000), | |
new Part("Brake pad", 11250), | |
new Part("Air filter", 2250), | |
new Part("Oil filter", 750), | |
new Part("Generator", 20000), | |
new Part("Carburetor", 15000), | |
new Part("Shock absorber", 12500), | |
new Part("Carputer", 10000), | |
}); | |
} | |
} | |
public class Part | |
{ | |
public Part(string name, int price) | |
{ | |
Name = name; | |
Price = price; | |
IsBroken = false; | |
} | |
public Part(Part part) : this(part.Name, part.Price) | |
{ | |
} | |
public string Name { get; private set; } | |
public int Price { get; private set; } | |
public bool IsBroken { get; private set; } | |
public Part Clone() | |
=> new(Name, Price); | |
public void Break() | |
=> IsBroken = true; | |
} | |
public class CarFactory | |
{ | |
private readonly Queue<Car> _cars = new(); | |
private readonly PartFactory _partFactory; | |
public CarFactory(PartFactory partFactory) => | |
_partFactory = partFactory; | |
public Queue<Car> CreateQueue() | |
{ | |
int minCarRange = 15; | |
int maxCarRange = 25; | |
int amountCars = UserUtils.GetRandomNumber(minCarRange, maxCarRange); | |
for (int i = 0; i < amountCars; i++) | |
{ | |
_cars.Enqueue(new Car(_partFactory.GetParts())); | |
} | |
return _cars; | |
} | |
} | |
public class Car | |
{ | |
private readonly List<Part> _parts; | |
public Car(List<Part> parts) | |
{ | |
_parts = parts; | |
BreakPart(); | |
} | |
public bool TryGetBrokenPart(out Part brokenPart) | |
{ | |
foreach (Part part in _parts) | |
{ | |
if (part.IsBroken) | |
{ | |
brokenPart = part; | |
return true; | |
} | |
} | |
brokenPart = default; | |
return false; | |
} | |
private void BreakPart() | |
{ | |
int index = UserUtils.GetRandomNumber(_parts.Count); | |
_parts[index].Break(); | |
} | |
} | |
public class PartsStorage | |
{ | |
private readonly List<Parts> _parts = new(); | |
public PartsStorage(List<Part> parts) => | |
Init(parts); | |
public void ShowAmount() | |
{ | |
Console.WriteLine("\n[Storage]"); | |
Console.WriteLine("Part: \t\t\tPrice: \t\tAmount:"); | |
foreach (Parts storagePart in _parts) | |
{ | |
storagePart.Show(); | |
} | |
Console.WriteLine(); | |
} | |
public Part GetRandomPart() | |
=> _parts[UserUtils.GetRandomNumber(_parts.Count)].GetPart(); | |
public bool HaveAmount(Part part) | |
=> Search(part).Amount > 0; | |
public void DecreaseAmount(Part part) | |
=> Search(part).DecreaseAmount(); | |
private Parts Search(Part part) | |
{ | |
foreach (Parts storagePart in _parts) | |
if (storagePart.GetPart().Name == part.Name) | |
return storagePart; | |
return null; | |
} | |
private void Init(List<Part> parts) | |
{ | |
int minAmountPart = 3; | |
int maxAmountPart = 10; | |
foreach (Part part in parts) | |
{ | |
int amount = UserUtils.GetRandomNumber(minAmountPart, maxAmountPart); | |
_parts.Add(new Parts(part, amount)); | |
} | |
} | |
} | |
public class Parts | |
{ | |
private readonly Part _part; | |
private int _amount; | |
public Parts(Part part, int amount) | |
{ | |
_part = part; | |
_amount = amount; | |
} | |
public int Amount => _amount; | |
public Part GetPart() => | |
_part.Clone(); | |
public void DecreaseAmount() => | |
_amount--; | |
public void Show() => | |
Console.WriteLine($"{_part.Name}\t\t{_part.Price}\t\t{_amount}"); | |
} | |
public class CarService | |
{ | |
private readonly PartsStorage _storageParts; | |
private int _money = 20000; | |
public CarService(PartFactory partFactory) | |
=> _storageParts = new PartsStorage(partFactory.GetParts()); | |
public void Work(Queue<Car> cars) | |
{ | |
while (cars.Count > 0) | |
{ | |
Console.Clear(); | |
ShowBalance(); | |
Car car = cars.Dequeue(); | |
ShowCarsAmount(cars.Count); | |
if (car.TryGetBrokenPart(out Part brokenPart) == false) | |
{ | |
Console.WriteLine("Машина не сломана"); | |
continue; | |
} | |
ShowBrokenPart(brokenPart); | |
int totalPrice = CalculatePrice(brokenPart); | |
ShowTotalPrice(totalPrice); | |
_storageParts.ShowAmount(); | |
RequestAcceptForRepair(); | |
if (_storageParts.TryGetPart(brokenPart, out Part part) == false) | |
{ | |
Console.WriteLine("Запчасти нет на складе"); | |
continue; | |
} | |
if (TryRepairCar(car, part) == false) | |
{ | |
Console.WriteLine("Штраф"); | |
continue; | |
} | |
Console.ReadKey(); | |
} | |
} | |
private void ShowBrokenPart(Part part) | |
{ | |
Console.WriteLine($"Broken part: {part.Name}"); | |
} | |
private void RequestAcceptForRepair() | |
{ | |
Console.Write("For repair press any key..."); | |
Console.ReadKey(); | |
} | |
private void Pay() | |
{ | |
Console.WriteLine($"The repair was completed successfully. \n" + | |
$"CarService is working {_totalPrice}\n"); | |
_money += _totalPrice; | |
_storageParts.DecreaseAmount(_cars.Peek().GetBrokenPart()); | |
} | |
private void ShowBalance() | |
=> Console.WriteLine($"[CarService 'Second Life']\n" + | |
$"Balance: {_money}\n"); | |
private void ShowCarsAmount(int amount) | |
{ | |
Console.Write($"[Queue: {amount} cars]\n" + | |
$"First car in queue with "); | |
} | |
private int CalculatePrice(Part part) | |
{ | |
float normalizedPercent = 100; | |
int percent = 20; | |
int partPrice = part.Price; | |
int workPrice = (int)(partPrice / normalizedPercent) * percent; | |
int totalPrice = partPrice + workPrice; | |
return totalPrice; | |
} | |
private void ShowTotalPrice(int totalPrice) | |
{ | |
Console.WriteLine($"Total price = {totalPrice} (price for work = {workPrice})"); | |
} | |
private bool TryGetPart(Part partReference, out Part part) | |
{ | |
Part part = _cars.Peek().GetBrokenPart(); | |
return _storageParts.HaveAmount(part); | |
} | |
private void PayFine() | |
{ | |
Console.WriteLine($"\nNecessary part is missing in the storage. \n" + | |
$"Paid a fine in the amount of {_workPrice}"); | |
_money -= _workPrice; | |
} | |
private bool TryRepairCar(Car car) | |
{ | |
string nameBrokenPart = _cars.Peek().GetBrokenPart().Name; | |
string repearPart = _storageParts.GetRandomPart().Name; | |
return nameBrokenPart != repearPart; | |
} | |
private void CompensatesForDamage() | |
{ | |
Console.WriteLine($"The part could not be repaired. \n" + | |
$"Compensation in the amount of {_workPrice}\n"); | |
_money -= _workPrice; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment