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) || (userNumber <= 0));
- 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 static Component MakeComponent(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 int Price { get; protected set; }
- public string Name { get; protected set; }
- public bool IsExploitable { get; private set; }
- public static string ConvertKindToName(Kind kind)
- {
- switch (kind)
- {
- case Component.Kind.hood:
- return "капот";
- case Component.Kind.doorLock:
- return "замок";
- case Component.Kind.handBreak:
- return "тормоз";
- case Component.Kind.battery:
- return "аккумулятор";
- case Component.Kind.airFilter:
- return "фильтр";
- case Component.Kind.radiator:
- return "радиатор";
- default:
- return null;
- }
- }
- public void Break()
- {
- IsExploitable = false;
- }
- }
- class Hood : Component
- {
- public Hood() : base()
- {
- Price = 120;
- Name = ConvertKindToName(Kind.hood);
- }
- }
- class DoorLock : Component
- {
- public DoorLock() : base()
- {
- Price = 20;
- Name = ConvertKindToName(Kind.doorLock);
- }
- }
- class HandBreake : Component
- {
- public HandBreake() : base()
- {
- Price = 100;
- Name = ConvertKindToName(Kind.handBreak);
- }
- }
- class Battery : Component
- {
- public Battery() : base()
- {
- Price = 80;
- Name = ConvertKindToName(Kind.battery);
- }
- }
- class AirFilter : Component
- {
- public AirFilter() : base()
- {
- Price = 30;
- Name = ConvertKindToName(Kind.airFilter);
- }
- }
- class Radiator : Component
- {
- public Radiator() : base()
- {
- Price = 200;
- Name = ConvertKindToName(Kind.radiator);
- }
- }
- class Car
- {
- private static Random _random;
- private Dictionary<string, Component> _components = new Dictionary<string, Component>();
- private Component _brokenComponent;
- static Car()
- {
- _random = new Random();
- }
- public Car()
- {
- Component component;
- var componentKinds = Enum.GetValues(typeof(Component.Kind));
- foreach (Component.Kind componentKind in componentKinds)
- {
- component = Fabric.MakeComponent(componentKind);
- _components.Add(component.Name, component);
- }
- Break();
- }
- public bool IsExpoitable => _brokenComponent == null;
- public void SetupComponent(Component component)
- {
- if (_components.ContainsKey(component.Name))
- _components[component.Name] = component;
- RefreshBreakingState();
- }
- public Component GetBreaking()
- {
- return _brokenComponent;
- }
- private void Break()
- {
- int brokenComponentNumber = _random.Next(_components.Count);
- string brokenComponentName = _components.ElementAt(brokenComponentNumber).Key;
- _components[brokenComponentName].Break();
- RefreshBreakingState();
- }
- public void RefreshBreakingState()
- {
- _brokenComponent = null;
- foreach (var name in _components.Keys)
- if (_components[name].IsExploitable == false)
- _brokenComponent = _components[name];
- }
- }
- class Service : IMoneyOwner
- {
- private Dictionary<string, int> _workPrice;
- private Car _servingCar;
- private Client _servingClient;
- private Storage _storage;
- private int _balance = 10000;
- private int _penalty = 200;
- public Service()
- {
- _workPrice = new Dictionary<string, int>()
- {
- { Component.ConvertKindToName(Component.Kind.airFilter), 100 },
- { Component.ConvertKindToName(Component.Kind.battery), 100 },
- { Component.ConvertKindToName(Component.Kind.doorLock), 200 },
- { Component.ConvertKindToName(Component.Kind.handBreak), 500 },
- { Component.ConvertKindToName(Component.Kind.hood), 500 },
- { Component.ConvertKindToName(Component.Kind.radiator), 700 },
- };
- _storage = new Storage();
- }
- public void Serve(Client client)
- {
- _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} {Component.ConvertKindToName(componentKind)}");
- int userChoise;
- do
- {
- userChoise = UserUtils.GetNumberFromUser() - 1;
- }
- while (userChoise >= Enum.GetValues(typeof(Component.Kind)).Length);
- string newComponentName = Component.ConvertKindToName((Component.Kind)userChoise);
- bool existComponentxist = _storage.CheckAvailability(newComponentName);
- if (existComponentxist)
- {
- Component newComponent = _storage.EjectComponent(newComponentName);
- _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.Name];
- }
- public void Pay(int money)
- {
- _balance -= money;
- Console.WriteLine($"Автосервис передал {money} денег, у него осталось {_balance}");
- }
- public void ReceiveMoney(int money)
- {
- _balance += money;
- Console.WriteLine($"Автосервис получил {money} денег, теперь у него {_balance}");
- }
- }
- 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<string, Rack> _racks = new Dictionary<string, Rack>();
- private int _maxComponentNumber = 5;
- static Storage()
- {
- _random = new Random();
- }
- public Storage()
- {
- var componentKinds = Enum.GetValues(typeof(Component.Kind));
- foreach (Component.Kind componentKind in componentKinds)
- {
- Rack rack = new Rack();
- int componentNumber = _random.Next(_maxComponentNumber + 1);
- for (int i = 0; i < componentNumber; i++)
- rack.PutComponent(Fabric.MakeComponent(componentKind));
- _racks.Add(Component.ConvertKindToName(componentKind), rack);
- }
- }
- public bool CheckAvailability(string componentName)
- {
- if (_racks.ContainsKey(componentName) == false)
- return false;
- if (_racks[componentName].Count > 0)
- return true;
- else
- return false;
- }
- public Component EjectComponent(string name)
- {
- return _racks[name].EjectComponent();
- }
- }
- }
Advertisement
Advertisement