lovelyvook

Example - Computer club

Jul 11th, 2024 (edited)
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Ijunior
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             ComputerClub computerClub = new ComputerClub(8);
  11.             computerClub.Work();
  12.         }
  13.     }
  14.  
  15.     class ComputerClub
  16.     {
  17.         private int _money;
  18.         private List<Computer> _computers = new List<Computer>();
  19.         private Queue<Client> _clients = new Queue<Client>();
  20.  
  21.         public ComputerClub(int computerCount)
  22.         {
  23.             Random random = new Random();
  24.  
  25.             for (int i = 0; i < computerCount; i++)
  26.             {
  27.                 _computers.Add(new Computer(random.Next(5, 15)));
  28.             }
  29.  
  30.             CreateNewClients(25, random);
  31.         }
  32.  
  33.         public void CreateNewClients(int count, Random random)
  34.         {
  35.             for (int i = 0; i < count; i++)
  36.             {
  37.                 _clients.Enqueue(new Client(random.Next(200, 300), random));
  38.             }
  39.         }
  40.  
  41.         public void Work()
  42.         {
  43.             while (_clients.Count > 0)
  44.             {
  45.                 Client newClient = _clients.Dequeue();
  46.                 Console.WriteLine($"Баланс клуба {_money}");
  47.                 Console.WriteLine($"Клиент хочет купить {newClient.DesiredMinutes} минут");
  48.                 ShowAllComputersState();
  49.  
  50.                 Console.Write("\nЗабронировать компьютер номер: ");
  51.                 string userInput = Console.ReadLine();
  52.  
  53.                 if (int.TryParse(userInput, out int computerNumber))
  54.                 {
  55.                     computerNumber -= 1;
  56.  
  57.                     if (computerNumber >= 0 && computerNumber < _computers.Count)
  58.                     {
  59.                         if (_computers[computerNumber].IsTaken)
  60.                         {
  61.                             Console.WriteLine("Компьютер занят");
  62.                         }
  63.                         else
  64.                         {
  65.                             if (newClient.CheckSolvency(_computers[computerNumber]))
  66.                             {
  67.                                 Console.WriteLine("Клиент сел за компьютер " + (computerNumber + 1));
  68.                                 _money += newClient.Pay();
  69.                                 _computers[computerNumber].BecomeTaken(newClient);
  70.                             }
  71.                             else
  72.                             {
  73.                                 Console.WriteLine("Клиент не смог оплатить");
  74.                             }
  75.                         }
  76.                     }
  77.                     else
  78.                     {
  79.                         Console.WriteLine("Такого номера нет");
  80.                     }
  81.                 }
  82.                 else
  83.                 {
  84.                     Console.WriteLine("Неверный ввод");
  85.                 }
  86.  
  87.                 Console.Write("Нажмите любую клавишу для продолжения работы");
  88.                 Console.ReadKey();
  89.                 Console.Clear();
  90.                 SpendOneMinute();
  91.             }
  92.         }
  93.  
  94.         private void ShowAllComputersState()
  95.         {
  96.             Console.WriteLine("Список компьюетров: ");
  97.  
  98.             for (int i = 0; i < _computers.Count; i++)
  99.             {
  100.                 Console.Write(i + 1 + " - ");
  101.                 _computers[i].ShowState();
  102.             }
  103.         }
  104.  
  105.         private void SpendOneMinute()
  106.         {
  107.             foreach (var computer in _computers)
  108.             {
  109.                 computer.SpendOneMinute();
  110.             }
  111.         }
  112.     }
  113.  
  114.     class Computer
  115.     {
  116.         private Client _client;
  117.         private int _minitesRemaining;
  118.  
  119.         public Computer(int pricePerMinute)
  120.         {
  121.             PricePerMinute = pricePerMinute;
  122.         }
  123.  
  124.         public int PricePerMinute { get; private set; }
  125.         public bool IsTaken
  126.         {
  127.             get
  128.             {
  129.                 return _minitesRemaining > 0;
  130.             }
  131.         }
  132.  
  133.         public void BecomeTaken(Client client)
  134.         {
  135.             _client = client;
  136.             _minitesRemaining = _client.DesiredMinutes;
  137.         }
  138.  
  139.         public void BecomeEmpty()
  140.         {
  141.             _client = null;
  142.         }
  143.  
  144.         public void SpendOneMinute()
  145.         {
  146.             _minitesRemaining--;
  147.         }
  148.  
  149.         public void ShowState()
  150.         {
  151.             if (IsTaken)
  152.                 Console.WriteLine($"Компьютер занят, осталось минут: {_minitesRemaining} ");
  153.             else
  154.                 Console.WriteLine($"Компьютер свободен, цена за минуту: {PricePerMinute}");
  155.         }
  156.     }
  157.  
  158.     class Client
  159.     {
  160.         private int _money;
  161.         private int _moneyToPay;
  162.  
  163.         public int DesiredMinutes { get; private set; }
  164.  
  165.         public Client(int money, Random random)
  166.         {
  167.             _money = money;
  168.             DesiredMinutes = random.Next(10, 30);
  169.         }
  170.  
  171.         public bool CheckSolvency(Computer computer)
  172.         {
  173.             _moneyToPay = DesiredMinutes * computer.PricePerMinute;
  174.  
  175.             if (_money >= _moneyToPay)
  176.             {
  177.                 return true;
  178.             }
  179.             else
  180.             {
  181.                 _moneyToPay = 0;
  182.                 return false;
  183.             }
  184.         }
  185.  
  186.         public int Pay()
  187.         {
  188.             _money -= _moneyToPay;
  189.             return _moneyToPay;
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment