Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Classes
- {
- internal class Program
- {
- static void Main()
- {
- CarService carService = new CarService();
- carService.Work();
- }
- }
- public class CarService
- {
- private Storage _storage;
- private Queue<Car> _cars;
- private int _carQueueCount;
- private Wallet _wallet;
- public CarService()
- {
- _storage = new Storage();
- _cars = new Queue<Car>();
- _carQueueCount = 10;
- _wallet = new Wallet();
- CreateQueue();
- }
- public void Work()
- {
- int carNumber = 1;
- while (_cars.Count != 0)
- {
- Car car = _cars.Dequeue();
- bool havePart = false;
- List<SparePart> brokenParts = car.GetBrokenPart().ToList();
- foreach (SparePart part in brokenParts)
- {
- if (_storage.TryGetParts(part))
- {
- havePart = true;
- }
- }
- if (havePart == false)
- {
- Console.WriteLine($"\nCar number {carNumber}");
- car.Show();
- _wallet.ReceiveFine(havePart);
- }
- while (car.CheckBreakdowns() == false && havePart == true)
- {
- Console.WriteLine($"Car number {carNumber}");
- car.Show();
- havePart = TryRepair(car, brokenParts);
- Console.WriteLine("\nPress something...");
- Console.ReadKey(true);
- Console.WriteLine();
- }
- carNumber++;
- }
- }
- private void CreateQueue()
- {
- for (int i = 0; i < _carQueueCount; i++)
- {
- Car car = new Car();
- if (car.CheckBreakdowns())
- {
- i--;
- }
- else
- {
- _cars.Enqueue(car);
- }
- }
- }
- private bool TryRepair(Car car, List<SparePart> brokenParts)
- {
- foreach (SparePart part in brokenParts)
- {
- if (_storage.TryGetParts(part))
- {
- car.Repair(part);
- _storage.UseUp(part);
- brokenParts.Remove(part);
- Console.WriteLine($"\nWas repaired {part.Name}");
- _wallet.ReceiveIncome(part);
- return true;
- }
- else
- {
- brokenParts.Remove(part);
- Console.WriteLine($"Can't fix {part.Name}");
- _wallet.ReceiveFine();
- return true;
- }
- }
- return false;
- }
- }
- public class Wallet
- {
- private int _amount;
- private int _penalty;
- private int _fullPenalty;
- private PartProvider _partProvider;
- private List<PartCharacteristic> _price;
- public Wallet()
- {
- _amount = 0;
- _penalty = 200;
- _fullPenalty = 1000;
- _partProvider = new PartProvider();
- _price = new List<PartCharacteristic>();
- FillPrice();
- }
- public void ReceiveIncome(SparePart part)
- {
- PartCharacteristic brokenSparePart = _price.Where(sparePart => sparePart.Name == part.Name).Single();
- _amount += brokenSparePart.CostOfSparePart;
- _amount += brokenSparePart.CostOfRepair;
- Console.WriteLine($"\ncar service receives {brokenSparePart.CostOfSparePart} + {brokenSparePart.CostOfRepair} ");
- Show();
- }
- public void ReceiveFine(bool havePart = true)
- {
- int penalty;
- if (havePart)
- {
- penalty = _penalty;
- Console.WriteLine($"\ncar service receives a fine of -{penalty} ");
- }
- else
- {
- penalty = _fullPenalty;
- Console.WriteLine($"\nThere are no suitable spare parts for this car\nCar service receives a fine of -{penalty} ");
- }
- _amount -= penalty;
- Show();
- }
- private void Show()
- {
- Console.WriteLine($"\nThe car service center has {_amount} on its balance sheet ");
- }
- private void FillPrice()
- {
- for (int i = 0; i < _partProvider.GetCount(); i++)
- {
- _price.Add(_partProvider.GetCharacteristic(i));
- }
- }
- }
- public class Storage
- {
- private List<SparePart> _carParts;
- private PartProvider _provider;
- private int _partMaxCount;
- public Storage()
- {
- _carParts = new List<SparePart>();
- _provider = new PartProvider();
- _partMaxCount = 5;
- Fill();
- }
- public bool TryGetParts(SparePart part)
- {
- return _carParts.Any(sparePart => sparePart.Name == part.Name);
- }
- public void UseUp(SparePart part)
- {
- _carParts.Remove(_carParts.First(sparePart => sparePart.Name == part.Name));
- }
- private void Fill()
- {
- for (int i = 0; i < _provider.GetCount(); i++)
- {
- for (int j = 0; j < UserUtil.GetRandom(_partMaxCount + 1); j++)
- {
- _carParts.Add(new SparePart(_provider.GetCharacteristic(i).Name, _provider.GetCharacteristic(i).IsIntact));
- }
- }
- }
- }
- public class Car
- {
- private List<SparePart> _car;
- private PartProvider _provider;
- public Car()
- {
- _provider = new PartProvider();
- _car = new List<SparePart>();
- Create();
- }
- public bool CheckBreakdowns()
- {
- foreach (SparePart sparePart in _car)
- {
- if (sparePart.IsIntact == false)
- {
- return false;
- }
- }
- return true;
- }
- public List<SparePart> GetBrokenPart()
- {
- List<SparePart> BrokenSparePart = _car.Where(part => part.IsIntact == false).ToList();
- return BrokenSparePart;
- }
- public void Repair(SparePart part)
- {
- foreach (SparePart sparePart in _car)
- {
- if (sparePart.Name == part.Name)
- {
- sparePart.Fix();
- }
- }
- }
- public void Show()
- {
- foreach (SparePart sparePart in _car)
- {
- if (sparePart.IsIntact)
- {
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.DarkRed;
- }
- Console.WriteLine(sparePart.Name);
- Console.ResetColor();
- }
- }
- private void Create()
- {
- int chanceIntact = 100;
- int chanceBreak = 40;
- for (int i = 0; i < _provider.GetCount(); i++)
- {
- bool isIntact = chanceBreak >= UserUtil.GetRandom(chanceIntact + 1) ? false : true;
- _car.Add(new SparePart(_provider.GetCharacteristic(i).Name, isIntact));
- }
- }
- }
- public class PartProvider
- {
- private List<PartCharacteristic> _parts;
- public PartProvider()
- {
- _parts = new List<PartCharacteristic>()
- {
- new PartCharacteristic("spare part 1", true, 100, 100) ,
- new PartCharacteristic("spare part 2", true, 200, 200) ,
- new PartCharacteristic("spare part 3", true, 300, 300) ,
- new PartCharacteristic("spare part 4", true, 400, 400) ,
- new PartCharacteristic("spare part 5", true, 500, 500) ,
- new PartCharacteristic("spare part 6", true, 600, 600) ,
- new PartCharacteristic("spare part 7", true, 700, 700)
- };
- }
- public int GetCount()
- {
- return _parts.Count;
- }
- public PartCharacteristic GetCharacteristic(int index)
- {
- return _parts[index];
- }
- }
- public class PartCharacteristic
- {
- public PartCharacteristic(string name, bool isIntact, int costOfSparePart, int costOfRepair)
- {
- Name = name;
- IsIntact = isIntact;
- CostOfSparePart = costOfSparePart;
- CostOfRepair = costOfRepair;
- }
- public string Name { get; private set; }
- public bool IsIntact { get; private set; }
- public int CostOfSparePart { get; private set; }
- public int CostOfRepair { get; private set; }
- }
- public class SparePart
- {
- public SparePart(string name, bool isIntact)
- {
- Name = name;
- IsIntact = isIntact;
- }
- public string Name { get; private set; }
- public bool IsIntact { get; private set; }
- public void Fix()
- {
- IsIntact = true;
- }
- }
- public static class UserUtil
- {
- private static Random s_random = new Random();
- public static int GetRandom(int max, int min = 0)
- {
- return s_random.Next(min, max);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement