Advertisement
holllowknight

Автосервис2

May 6th, 2023 (edited)
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Service
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int clientNumber = 3;
  12.             Service service = new Service();
  13.  
  14.             for (int i = 0; i < clientNumber; i++)
  15.                 service.Serve(new Client());
  16.         }
  17.     }
  18.  
  19.     class UserUtils
  20.     {
  21.         public static int GetNumberFromUser()
  22.         {
  23.             string userInput;
  24.             int userNumber;
  25.             bool isNumber = false;
  26.  
  27.             do
  28.             {
  29.                 userInput = Console.ReadLine();
  30.                 isNumber = int.TryParse(userInput, out userNumber);
  31.             }
  32.             while (isNumber == false);
  33.  
  34.             return userNumber;
  35.         }
  36.     }
  37.  
  38.     interface IMoneyOwner
  39.     {
  40.         void Pay(int money);
  41.         void ReceiveMoney(int money);
  42.     }
  43.  
  44.     class Client : IMoneyOwner
  45.     {
  46.         private int _creditCard = 1000;
  47.  
  48.         public Client()
  49.         {
  50.             Car = new Car();
  51.         }
  52.  
  53.         public string Name { get; private set; }
  54.         public Car Car { get; private set; }
  55.  
  56.         public void Pay(int money)
  57.         {
  58.             _creditCard -= money;
  59.             Console.WriteLine($"Клиент передал {money} денег, у него осталось {_creditCard}");
  60.         }
  61.  
  62.         public void ReceiveMoney(int money)
  63.         {
  64.             _creditCard += money;
  65.             Console.WriteLine($"Клиент получил {money} денег, теперь у него {_creditCard}");
  66.         }
  67.     }
  68.  
  69.     class Fabric
  70.     {
  71.         public Component CreateComponent(Component.Kind componentKind)
  72.         {
  73.             switch (componentKind)
  74.             {
  75.                 case Component.Kind.Hood:
  76.                     return new Hood();
  77.  
  78.                 case Component.Kind.DoorLock:
  79.                     return new DoorLock();
  80.  
  81.                 case Component.Kind.HandBreak:
  82.                     return new HandBreake();
  83.  
  84.                 case Component.Kind.Battery:
  85.                     return new Battery();
  86.  
  87.                 case Component.Kind.AirFilter:
  88.                     return new AirFilter();
  89.  
  90.                 case Component.Kind.Radiator:
  91.                     return new Radiator();
  92.  
  93.                 default:
  94.                     return null;
  95.             }
  96.         }
  97.     }
  98.  
  99.     class Component
  100.     {
  101.         public Component()
  102.         {
  103.             IsExploitable = true;
  104.         }
  105.  
  106.         public enum Kind
  107.         {
  108.             Hood,
  109.             DoorLock,
  110.             HandBreak,
  111.             Battery,
  112.             AirFilter,
  113.             Radiator
  114.         }
  115.  
  116.         public Kind FixtureKind { get; protected set; }
  117.         public int Price { get; protected set; }
  118.         public string Name { get; protected set; }
  119.         public bool IsExploitable { get; private set; }
  120.  
  121.         public void Break()
  122.         {
  123.             IsExploitable = false;
  124.         }
  125.     }
  126.  
  127.     class Hood : Component
  128.     {
  129.         public Hood() : base()
  130.         {
  131.             Price = 120;
  132.             FixtureKind = Kind.Hood;
  133.             Name = "капот";
  134.         }
  135.     }
  136.  
  137.     class DoorLock : Component
  138.     {
  139.         public DoorLock() : base()
  140.         {
  141.             Price = 20;
  142.             FixtureKind = Kind.DoorLock;
  143.             Name = "замок";
  144.         }
  145.     }
  146.  
  147.     class HandBreake : Component
  148.     {
  149.         public HandBreake() : base()
  150.         {
  151.             Price = 100;
  152.             FixtureKind = Kind.HandBreak;
  153.             Name = "тормоз";
  154.         }
  155.     }
  156.  
  157.     class Battery : Component
  158.     {
  159.         public Battery() : base()
  160.         {
  161.             Price = 80;
  162.             FixtureKind = Kind.Battery;
  163.             Name = "аккумулятор";
  164.         }
  165.     }
  166.  
  167.     class AirFilter : Component
  168.     {
  169.         public AirFilter() : base()
  170.         {
  171.             Price = 30;
  172.             FixtureKind = Kind.AirFilter;
  173.             Name = "фильтр";
  174.         }
  175.     }
  176.  
  177.     class Radiator : Component
  178.     {
  179.         public Radiator() : base()
  180.         {
  181.             Price = 200;
  182.             FixtureKind = Kind.Radiator;
  183.             Name = "радиатор";
  184.         }
  185.     }
  186.  
  187.     class Car
  188.     {
  189.         private static Random _random;
  190.         private Dictionary<Component.Kind, Component> _components = new Dictionary<Component.Kind, Component>();
  191.         private Component _brokenComponent;
  192.  
  193.         static Car()
  194.         {
  195.             _random = new Random();
  196.         }
  197.  
  198.         public Car()
  199.         {
  200.             Component component;
  201.             Fabric fabric = new Fabric();
  202.             var componentKinds = Enum.GetValues(typeof(Component.Kind));
  203.  
  204.             foreach (Component.Kind componentKind in componentKinds)
  205.             {
  206.                 component = fabric.CreateComponent(componentKind);
  207.                 _components.Add(component.FixtureKind, component);
  208.             }
  209.  
  210.             Break();
  211.         }
  212.  
  213.         public bool IsExpoitable => _brokenComponent == null;
  214.  
  215.         public void SetupComponent(Component component)
  216.         {
  217.             if (_components.ContainsKey(component.FixtureKind))
  218.                 _components[component.FixtureKind] = component;
  219.  
  220.             RefreshBreakingState();
  221.         }
  222.  
  223.         public Component GetBreaking()
  224.         {
  225.             return _brokenComponent;
  226.         }
  227.  
  228.         public void RefreshBreakingState()
  229.         {
  230.             _brokenComponent = null;
  231.  
  232.             foreach (var name in _components.Keys)
  233.                 if (_components[name].IsExploitable == false)
  234.                     _brokenComponent = _components[name];
  235.         }
  236.  
  237.         private void Break()
  238.         {
  239.             int brokenComponentNumber = _random.Next(_components.Count);
  240.             Component.Kind brokenComponentKind = _components.ElementAt(brokenComponentNumber).Key;
  241.             _components[brokenComponentKind].Break();
  242.             RefreshBreakingState();
  243.         }
  244.     }
  245.  
  246.     class Service : IMoneyOwner
  247.     {
  248.         private Dictionary<Component.Kind, int> _workPrice;
  249.         private Car _servingCar;
  250.         private Client _servingClient;
  251.         private Storage _storage;
  252.         private int _balance = 10000;
  253.         private int _penalty = 200;
  254.  
  255.         public Service()
  256.         {
  257.             _workPrice = new Dictionary<Component.Kind, int>()
  258.             {
  259.                 { Component.Kind.AirFilter, 100 },
  260.                 { Component.Kind.Battery, 100 },
  261.                 { Component.Kind.DoorLock, 200 },
  262.                 { Component.Kind.HandBreak, 500 },
  263.                 { Component.Kind.Hood, 500 },
  264.                 { Component.Kind.Radiator, 700 },
  265.             };
  266.             _storage = new Storage();
  267.         }
  268.  
  269.         public void Pay(int money)
  270.         {
  271.             _balance -= money;
  272.             Console.WriteLine($"Автосервис передал {money} денег, у него осталось {_balance}");
  273.         }
  274.  
  275.         public void ReceiveMoney(int money)
  276.         {
  277.             _balance += money;
  278.             Console.WriteLine($"Автосервис получил {money} денег, теперь у него {_balance}");
  279.         }
  280.  
  281.         public void Serve(Client client)
  282.         {
  283.             int userChoise;
  284.             _servingClient = client;
  285.             _servingCar = _servingClient.Car;
  286.             Component brokenComponent = _servingCar.GetBreaking();
  287.             Console.WriteLine($"Неисправность в машине клиента: {brokenComponent.Name}");
  288.             int repairCost = GetFullPrice(brokenComponent);
  289.             Console.WriteLine($"Стоимость ремонта: {repairCost}");
  290.             Console.WriteLine("Какую деталь принести со склада на замену?");
  291.  
  292.             foreach (Component.Kind componentKind in Enum.GetValues(typeof(Component.Kind)))
  293.                 Console.WriteLine($"{(int)componentKind + 1} {_storage.ComponentNames[(int)componentKind]}");
  294.  
  295.             do
  296.                 userChoise = UserUtils.GetNumberFromUser() - 1;
  297.             while ((userChoise >= Enum.GetValues(typeof(Component.Kind)).Length) || (userChoise < 0));
  298.  
  299.             Component.Kind newComponentKind = (Component.Kind)userChoise;
  300.             bool isComponentAvailable = _storage.VerifyAvailability(newComponentKind);
  301.  
  302.             if (isComponentAvailable)
  303.             {
  304.                 Component newComponent = _storage.EjectComponent(newComponentKind);
  305.                 _servingCar.SetupComponent(newComponent);
  306.  
  307.                 if (_servingCar.IsExpoitable == true)
  308.                 {
  309.                     Console.WriteLine($"Клиент оплачивает {repairCost}");
  310.                     client.Pay(repairCost);
  311.                     ReceiveMoney(repairCost);
  312.                 }
  313.                 else
  314.                 {
  315.                     Console.WriteLine($"Поломка не устранена, платим клиенту штраф в размере стоимости ремонта");
  316.                     Pay(repairCost);
  317.                     client.ReceiveMoney(repairCost);
  318.                 }
  319.             }
  320.             else
  321.             {
  322.                 Console.WriteLine($"Детали нет, платим клиенту неустойку");
  323.                 Pay(_penalty);
  324.                 client.ReceiveMoney(_penalty);
  325.             }
  326.  
  327.             Console.WriteLine();
  328.         }
  329.  
  330.         private int GetFullPrice(Component component)
  331.         {
  332.             return component.Price + _workPrice[component.FixtureKind];
  333.         }
  334.     }
  335.  
  336.     class Rack
  337.     {
  338.         private Stack<Component> _components = new Stack<Component>();
  339.  
  340.         public int Count => _components.Count;
  341.  
  342.         public void PutComponent(Component component)
  343.         {
  344.             _components.Push(component);
  345.         }
  346.  
  347.         public Component EjectComponent()
  348.         {
  349.             if (_components.Count != 0)
  350.                 return _components.Pop();
  351.             else
  352.                 return null;
  353.         }
  354.     }
  355.  
  356.     class Storage
  357.     {
  358.         private static Random _random;
  359.         private Dictionary<Component.Kind, Rack> _racks = new Dictionary<Component.Kind, Rack>();
  360.         private List<string> _componentNames = new List<string>();
  361.  
  362.         static Storage()
  363.         {
  364.             _random = new Random();
  365.         }
  366.  
  367.         public Storage()
  368.         {
  369.             Fabric fabric = new Fabric();
  370.             var componentKinds = Enum.GetValues(typeof(Component.Kind));
  371.  
  372.             foreach (Component.Kind componentKind in componentKinds)
  373.             {
  374.                 int maxComponentNumber = 5;
  375.                 Rack rack = new Rack();
  376.                 int componentNumber = _random.Next(maxComponentNumber);
  377.                 Component component = fabric.CreateComponent(componentKind);
  378.                 _componentNames.Add(component.Name);
  379.  
  380.                 for (int i = 0; i < componentNumber; i++)
  381.                 {
  382.                     component = fabric.CreateComponent(componentKind);
  383.                     rack.PutComponent(component);
  384.                 }
  385.  
  386.                 _racks.Add(componentKind, rack);
  387.             }
  388.         }
  389.  
  390.         public IReadOnlyList<string> ComponentNames => _componentNames;
  391.  
  392.         public bool VerifyAvailability(Component.Kind componentKind)
  393.         {
  394.             if (_racks.ContainsKey(componentKind) == false)
  395.                 return false;
  396.  
  397.             return _racks[componentKind].Count > 0;
  398.         }
  399.  
  400.         public Component EjectComponent(Component.Kind name)
  401.         {
  402.             return _racks[name].EjectComponent();
  403.         }
  404.     }
  405. }
  406.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement