Advertisement
holllowknight

Автосервис

May 4th, 2023
884
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.92 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) || (userNumber <= 0));
  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 static Component MakeComponent(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 int Price { get; protected set; }
  117.         public string Name { get; protected set; }
  118.         public bool IsExploitable { get; private set; }
  119.  
  120.         public static string ConvertKindToName(Kind kind)
  121.         {
  122.             switch (kind)
  123.             {
  124.                 case Component.Kind.hood:
  125.                     return "капот";
  126.  
  127.                 case Component.Kind.doorLock:
  128.                     return "замок";
  129.  
  130.                 case Component.Kind.handBreak:
  131.                     return "тормоз";
  132.  
  133.                 case Component.Kind.battery:
  134.                     return "аккумулятор";
  135.  
  136.                 case Component.Kind.airFilter:
  137.                     return "фильтр";
  138.  
  139.                 case Component.Kind.radiator:
  140.                     return "радиатор";
  141.  
  142.                 default:
  143.                     return null;
  144.             }
  145.         }
  146.  
  147.         public void Break()
  148.         {
  149.             IsExploitable = false;
  150.         }
  151.     }
  152.  
  153.     class Hood : Component
  154.     {
  155.         public Hood() : base()
  156.         {
  157.             Price = 120;
  158.             Name = ConvertKindToName(Kind.hood);
  159.         }
  160.     }
  161.  
  162.     class DoorLock : Component
  163.     {
  164.         public DoorLock() : base()
  165.         {
  166.             Price = 20;
  167.             Name = ConvertKindToName(Kind.doorLock);
  168.         }
  169.     }
  170.  
  171.     class HandBreake : Component
  172.     {
  173.         public HandBreake() : base()
  174.         {
  175.             Price = 100;
  176.             Name = ConvertKindToName(Kind.handBreak);
  177.         }
  178.     }
  179.  
  180.     class Battery : Component
  181.     {
  182.         public Battery() : base()
  183.         {
  184.             Price = 80;
  185.             Name = ConvertKindToName(Kind.battery);
  186.         }
  187.     }
  188.  
  189.     class AirFilter : Component
  190.     {
  191.         public AirFilter() : base()
  192.         {
  193.             Price = 30;
  194.             Name = ConvertKindToName(Kind.airFilter);
  195.         }
  196.     }
  197.  
  198.     class Radiator : Component
  199.     {
  200.         public Radiator() : base()
  201.         {
  202.             Price = 200;
  203.             Name = ConvertKindToName(Kind.radiator);
  204.         }
  205.     }
  206.  
  207.     class Car
  208.     {
  209.         private static Random _random;
  210.         private Dictionary<string, Component> _components = new Dictionary<string, Component>();
  211.         private Component _brokenComponent;
  212.  
  213.         static Car()
  214.         {
  215.             _random = new Random();
  216.         }
  217.  
  218.         public Car()
  219.         {
  220.             Component component;
  221.             var componentKinds = Enum.GetValues(typeof(Component.Kind));
  222.  
  223.             foreach (Component.Kind componentKind in componentKinds)
  224.             {
  225.                 component = Fabric.MakeComponent(componentKind);
  226.                 _components.Add(component.Name, component);
  227.             }
  228.  
  229.             Break();
  230.         }
  231.  
  232.         public bool IsExpoitable => _brokenComponent == null;
  233.  
  234.         public void SetupComponent(Component component)
  235.         {
  236.             if (_components.ContainsKey(component.Name))
  237.                 _components[component.Name] = component;
  238.  
  239.             RefreshBreakingState();
  240.         }
  241.  
  242.         public Component GetBreaking()
  243.         {
  244.             return _brokenComponent;
  245.         }
  246.  
  247.         private void Break()
  248.         {
  249.             int brokenComponentNumber = _random.Next(_components.Count);
  250.             string brokenComponentName = _components.ElementAt(brokenComponentNumber).Key;
  251.             _components[brokenComponentName].Break();
  252.             RefreshBreakingState();
  253.         }
  254.  
  255.         public void RefreshBreakingState()
  256.         {
  257.             _brokenComponent = null;
  258.  
  259.             foreach (var name in _components.Keys)
  260.                 if (_components[name].IsExploitable == false)
  261.                     _brokenComponent = _components[name];
  262.         }
  263.     }
  264.  
  265.     class Service : IMoneyOwner
  266.     {
  267.         private Dictionary<string, int> _workPrice;
  268.         private Car _servingCar;
  269.         private Client _servingClient;
  270.         private Storage _storage;
  271.         private int _balance = 10000;
  272.         private int _penalty = 200;
  273.  
  274.         public Service()
  275.         {
  276.             _workPrice = new Dictionary<string, int>()
  277.             {
  278.                 { Component.ConvertKindToName(Component.Kind.airFilter), 100 },
  279.                 { Component.ConvertKindToName(Component.Kind.battery), 100 },
  280.                 { Component.ConvertKindToName(Component.Kind.doorLock), 200 },
  281.                 { Component.ConvertKindToName(Component.Kind.handBreak), 500 },
  282.                 { Component.ConvertKindToName(Component.Kind.hood), 500 },
  283.                 { Component.ConvertKindToName(Component.Kind.radiator), 700 },
  284.             };
  285.             _storage = new Storage();
  286.         }
  287.  
  288.         public void Serve(Client client)
  289.         {
  290.             _servingClient = client;
  291.             _servingCar = _servingClient.car;
  292.             Component brokenComponent = _servingCar.GetBreaking();
  293.             Console.WriteLine($"Неисправность в машине клиента: {brokenComponent.Name}");
  294.             int repairCost = GetFullPrice(brokenComponent);
  295.             Console.WriteLine($"Стоимость ремонта: {repairCost}");
  296.             Console.WriteLine("Какую деталь принести со склада на замену?");
  297.  
  298.             foreach (Component.Kind componentKind in Enum.GetValues(typeof(Component.Kind)))
  299.                 Console.WriteLine($"{(int)componentKind + 1} {Component.ConvertKindToName(componentKind)}");
  300.  
  301.             int userChoise;
  302.             do
  303.             {
  304.                 userChoise = UserUtils.GetNumberFromUser() - 1;
  305.             }
  306.             while (userChoise >= Enum.GetValues(typeof(Component.Kind)).Length);
  307.  
  308.             string newComponentName = Component.ConvertKindToName((Component.Kind)userChoise);
  309.             bool existComponentxist = _storage.CheckAvailability(newComponentName);
  310.  
  311.             if (existComponentxist)
  312.             {
  313.                 Component newComponent = _storage.EjectComponent(newComponentName);
  314.                 _servingCar.SetupComponent(newComponent);
  315.  
  316.                 if (_servingCar.IsExpoitable == true)
  317.                 {
  318.                     Console.WriteLine($"Клиент оплачивает {repairCost}");
  319.                     client.Pay(repairCost);
  320.                     ReceiveMoney(repairCost);
  321.                 }
  322.                 else
  323.                 {
  324.                     Console.WriteLine($"Поломка не устранена, платим клиенту штраф в размере стоимости ремонта");
  325.                     Pay(repairCost);
  326.                     client.ReceiveMoney(repairCost);
  327.                 }
  328.             }
  329.             else
  330.             {
  331.                 Console.WriteLine($"Детали нет, платим клиенту неустойку");
  332.                 Pay(_penalty);
  333.                 client.ReceiveMoney(_penalty);
  334.             }
  335.  
  336.             Console.WriteLine();
  337.         }
  338.  
  339.         private int GetFullPrice(Component component)
  340.         {
  341.             return component.Price + _workPrice[component.Name];
  342.         }
  343.  
  344.         public void Pay(int money)
  345.         {
  346.             _balance -= money;
  347.             Console.WriteLine($"Автосервис передал {money} денег, у него осталось {_balance}");
  348.         }
  349.  
  350.         public void ReceiveMoney(int money)
  351.         {
  352.             _balance += money;
  353.             Console.WriteLine($"Автосервис получил {money} денег, теперь у него {_balance}");
  354.         }
  355.     }
  356.  
  357.     class Rack
  358.     {
  359.         private Stack<Component> _components = new Stack<Component>();
  360.  
  361.         public int Count => _components.Count;
  362.  
  363.         public void PutComponent(Component component)
  364.         {
  365.             _components.Push(component);
  366.         }
  367.  
  368.         public Component EjectComponent()
  369.         {
  370.             if (_components.Count != 0)
  371.                 return _components.Pop();
  372.             else
  373.                 return null;
  374.         }
  375.     }
  376.  
  377.     class Storage
  378.     {
  379.         private static Random _random;
  380.         private Dictionary<string, Rack> _racks = new Dictionary<string, Rack>();
  381.         private int _maxComponentNumber = 5;
  382.  
  383.         static Storage()
  384.         {
  385.             _random = new Random();
  386.         }
  387.  
  388.         public Storage()
  389.         {
  390.             var componentKinds = Enum.GetValues(typeof(Component.Kind));
  391.  
  392.             foreach (Component.Kind componentKind in componentKinds)
  393.             {
  394.                 Rack rack = new Rack();
  395.                 int componentNumber = _random.Next(_maxComponentNumber + 1);
  396.  
  397.                 for (int i = 0; i < componentNumber; i++)
  398.                     rack.PutComponent(Fabric.MakeComponent(componentKind));
  399.  
  400.                 _racks.Add(Component.ConvertKindToName(componentKind), rack);
  401.             }
  402.         }
  403.  
  404.         public bool CheckAvailability(string componentName)
  405.         {
  406.             if (_racks.ContainsKey(componentName) == false)
  407.                 return false;
  408.  
  409.             if (_racks[componentName].Count > 0)
  410.                 return true;
  411.             else
  412.                 return false;
  413.         }
  414.  
  415.         public Component EjectComponent(string name)
  416.         {
  417.             return _racks[name].EjectComponent();
  418.         }
  419.     }
  420. }
  421.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement