Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Service
- {
- class Program
- {
- static void Main(string[] args)
- {
- int clientNumber = 3;
- Service service = new Service();
- for (int i = 0; i < clientNumber; i++)
- service.Serve(new Client());
- }
- }
- class UserUtils
- {
- public static int GetNumberFromUser()
- {
- string userInput;
- int userNumber;
- bool isNumber = false;
- do
- {
- userInput = Console.ReadLine();
- isNumber = int.TryParse(userInput, out userNumber);
- }
- while (isNumber == false);
- return userNumber;
- }
- }
- interface IMoneyOwner
- {
- void Pay(int money);
- void ReceiveMoney(int money);
- }
- class Client : IMoneyOwner
- {
- private int _creditCard = 1000;
- public Client()
- {
- Car = new Car();
- }
- public string Name { get; private set; }
- public Car Car { get; private set; }
- public void Pay(int money)
- {
- _creditCard -= money;
- Console.WriteLine($"Клиент передал {money} денег, у него осталось {_creditCard}");
- }
- public void ReceiveMoney(int money)
- {
- _creditCard += money;
- Console.WriteLine($"Клиент получил {money} денег, теперь у него {_creditCard}");
- }
- }
- class Fabric
- {
- public Component CreateComponent(Component.Kind componentKind)
- {
- switch (componentKind)
- {
- case Component.Kind.Hood:
- return new Hood();
- case Component.Kind.DoorLock:
- return new DoorLock();
- case Component.Kind.HandBreak:
- return new HandBreake();
- case Component.Kind.Battery:
- return new Battery();
- case Component.Kind.AirFilter:
- return new AirFilter();
- case Component.Kind.Radiator:
- return new Radiator();
- default:
- return null;
- }
- }
- }
- class Component
- {
- public Component()
- {
- IsExploitable = true;
- }
- public enum Kind
- {
- Hood,
- DoorLock,
- HandBreak,
- Battery,
- AirFilter,
- Radiator
- }
- public Kind FixtureKind { get; protected set; }
- public int Price { get; protected set; }
- public string Name { get; protected set; }
- public bool IsExploitable { get; private set; }
- public void Break()
- {
- IsExploitable = false;
- }
- }
- class Hood : Component
- {
- public Hood() : base()
- {
- Price = 120;
- FixtureKind = Kind.Hood;
- Name = "капот";
- }
- }
- class DoorLock : Component
- {
- public DoorLock() : base()
- {
- Price = 20;
- FixtureKind = Kind.DoorLock;
- Name = "замок";
- }
- }
- class HandBreake : Component
- {
- public HandBreake() : base()
- {
- Price = 100;
- FixtureKind = Kind.HandBreak;
- Name = "тормоз";
- }
- }
- class Battery : Component
- {
- public Battery() : base()
- {
- Price = 80;
- FixtureKind = Kind.Battery;
- Name = "аккумулятор";
- }
- }
- class AirFilter : Component
- {
- public AirFilter() : base()
- {
- Price = 30;
- FixtureKind = Kind.AirFilter;
- Name = "фильтр";
- }
- }
- class Radiator : Component
- {
- public Radiator() : base()
- {
- Price = 200;
- FixtureKind = Kind.Radiator;
- Name = "радиатор";
- }
- }
- class Car
- {
- private static Random _random;
- private Dictionary<Component.Kind, Component> _components = new Dictionary<Component.Kind, Component>();
- private Component _brokenComponent;
- static Car()
- {
- _random = new Random();
- }
- public Car()
- {
- Component component;
- Fabric fabric = new Fabric();
- var componentKinds = Enum.GetValues(typeof(Component.Kind));
- foreach (Component.Kind componentKind in componentKinds)
- {
- component = fabric.CreateComponent(componentKind);
- _components.Add(component.FixtureKind, component);
- }
- Break();
- }
- public bool IsExpoitable => _brokenComponent == null;
- public void SetupComponent(Component component)
- {
- if (_components.ContainsKey(component.FixtureKind))
- _components[component.FixtureKind] = component;
- RefreshBreakingState();
- }
- public Component GetBreaking()
- {
- return _brokenComponent;
- }
- public void RefreshBreakingState()
- {
- _brokenComponent = null;
- foreach (var name in _components.Keys)
- if (_components[name].IsExploitable == false)
- _brokenComponent = _components[name];
- }
- private void Break()
- {
- int brokenComponentNumber = _random.Next(_components.Count);
- Component.Kind brokenComponentKind = _components.ElementAt(brokenComponentNumber).Key;
- _components[brokenComponentKind].Break();
- RefreshBreakingState();
- }
- }
- class Service : IMoneyOwner
- {
- private Dictionary<Component.Kind, int> _workPrice;
- private Car _servingCar;
- private Client _servingClient;
- private Storage _storage;
- private int _balance = 10000;
- private int _penalty = 200;
- public Service()
- {
- _workPrice = new Dictionary<Component.Kind, int>()
- {
- { Component.Kind.AirFilter, 100 },
- { Component.Kind.Battery, 100 },
- { Component.Kind.DoorLock, 200 },
- { Component.Kind.HandBreak, 500 },
- { Component.Kind.Hood, 500 },
- { Component.Kind.Radiator, 700 },
- };
- _storage = new Storage();
- }
- public void Pay(int money)
- {
- _balance -= money;
- Console.WriteLine($"Автосервис передал {money} денег, у него осталось {_balance}");
- }
- public void ReceiveMoney(int money)
- {
- _balance += money;
- Console.WriteLine($"Автосервис получил {money} денег, теперь у него {_balance}");
- }
- public void Serve(Client client)
- {
- int userChoise;
- _servingClient = client;
- _servingCar = _servingClient.Car;
- Component brokenComponent = _servingCar.GetBreaking();
- Console.WriteLine($"Неисправность в машине клиента: {brokenComponent.Name}");
- int repairCost = GetFullPrice(brokenComponent);
- Console.WriteLine($"Стоимость ремонта: {repairCost}");
- Console.WriteLine("Какую деталь принести со склада на замену?");
- foreach (Component.Kind componentKind in Enum.GetValues(typeof(Component.Kind)))
- Console.WriteLine($"{(int)componentKind + 1} {_storage.ComponentNames[(int)componentKind]}");
- do
- userChoise = UserUtils.GetNumberFromUser() - 1;
- while ((userChoise >= Enum.GetValues(typeof(Component.Kind)).Length) || (userChoise < 0));
- Component.Kind newComponentKind = (Component.Kind)userChoise;
- bool isComponentAvailable = _storage.VerifyAvailability(newComponentKind);
- if (isComponentAvailable)
- {
- Component newComponent = _storage.EjectComponent(newComponentKind);
- _servingCar.SetupComponent(newComponent);
- if (_servingCar.IsExpoitable == true)
- {
- Console.WriteLine($"Клиент оплачивает {repairCost}");
- client.Pay(repairCost);
- ReceiveMoney(repairCost);
- }
- else
- {
- Console.WriteLine($"Поломка не устранена, платим клиенту штраф в размере стоимости ремонта");
- Pay(repairCost);
- client.ReceiveMoney(repairCost);
- }
- }
- else
- {
- Console.WriteLine($"Детали нет, платим клиенту неустойку");
- Pay(_penalty);
- client.ReceiveMoney(_penalty);
- }
- Console.WriteLine();
- }
- private int GetFullPrice(Component component)
- {
- return component.Price + _workPrice[component.FixtureKind];
- }
- }
- class Rack
- {
- private Stack<Component> _components = new Stack<Component>();
- public int Count => _components.Count;
- public void PutComponent(Component component)
- {
- _components.Push(component);
- }
- public Component EjectComponent()
- {
- if (_components.Count != 0)
- return _components.Pop();
- else
- return null;
- }
- }
- class Storage
- {
- private static Random _random;
- private Dictionary<Component.Kind, Rack> _racks = new Dictionary<Component.Kind, Rack>();
- private List<string> _componentNames = new List<string>();
- static Storage()
- {
- _random = new Random();
- }
- public Storage()
- {
- Fabric fabric = new Fabric();
- var componentKinds = Enum.GetValues(typeof(Component.Kind));
- foreach (Component.Kind componentKind in componentKinds)
- {
- int maxComponentNumber = 5;
- Rack rack = new Rack();
- int componentNumber = _random.Next(maxComponentNumber);
- Component component = fabric.CreateComponent(componentKind);
- _componentNames.Add(component.Name);
- for (int i = 0; i < componentNumber; i++)
- {
- component = fabric.CreateComponent(componentKind);
- rack.PutComponent(component);
- }
- _racks.Add(componentKind, rack);
- }
- }
- public IReadOnlyList<string> ComponentNames => _componentNames;
- public bool VerifyAvailability(Component.Kind componentKind)
- {
- if (_racks.ContainsKey(componentKind) == false)
- return false;
- return _racks[componentKind].Count > 0;
- }
- public Component EjectComponent(Component.Kind name)
- {
- return _racks[name].EjectComponent();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement