Anonim_999

Сooler Shop

Sep 19th, 2021 (edited)
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Queue<Client> clients = new Queue<Client>();
  10.  
  11.             Console.Write("Number of clients in the queue: ");
  12.             string userInput = Console.ReadLine();
  13.             int number;
  14.             int clientStack = 1;
  15.  
  16.             if (int.TryParse(userInput, out number))
  17.             {
  18.                 clientStack = Convert.ToInt32(userInput);
  19.  
  20.                 for (int i = 0; i < clientStack; i++)
  21.                 {
  22.                     clients.Enqueue(new Client());
  23.                 }
  24.                 int indexClient = 1;
  25.  
  26.                 while (clients.Count > 0)
  27.                 {
  28.                     CheckPurchase(clients.Dequeue(), indexClient);
  29.                     indexClient++;
  30.                 }
  31.             }
  32.             else
  33.             {
  34.                 Console.WriteLine("Input error");  
  35.             }      
  36.         }
  37.        
  38.         public static void CheckPurchase(Client client, int indexClient)
  39.         {
  40.             Console.WriteLine($"Client: {indexClient}");
  41.             client.ShowStats();
  42.             Console.WriteLine($"Purchase amount: {client.GetPurchaseAmount()}");
  43.  
  44.             while (client.GetPurchaseAmount() > client.Money)
  45.             {
  46.                 Console.Clear();
  47.                 Console.WriteLine($"Client: {indexClient}");
  48.                 client.ShowStats();
  49.                 Console.WriteLine($"Purchase amount: {client.GetPurchaseAmount()}");
  50.                 Console.ReadKey();
  51.  
  52.                 if (client.GetPurchaseAmount() > client.Money)
  53.                 {
  54.                     Console.WriteLine("Not enough money");
  55.                     client.RemoveRandomProduct();
  56.                     Console.ReadKey();
  57.                 }
  58.             }
  59.             Console.Clear();
  60.             Console.WriteLine($"Client: {indexClient}");
  61.             client.ShowStats();
  62.             Console.WriteLine($"Purchase amount: {client.GetPurchaseAmount()}");
  63.             Console.ReadKey();
  64.         }
  65.     }
  66. }
  67.  
  68. class Client
  69. {
  70.     private Basket _basket;
  71.     private Random _random;
  72.  
  73.     public int Money { get; }
  74.  
  75.     public Client()
  76.     {
  77.         _basket = new Basket();
  78.         _random = new Random();
  79.         Money = _random.Next(100, 400);
  80.     }
  81.  
  82.     public void ShowStats()
  83.     {
  84.         Console.WriteLine($"Money: {Money}");
  85.         _basket.ShowProducts();
  86.     }
  87.    
  88.     public int GetPurchaseAmount()
  89.     {
  90.         return _basket.CountPurchaseAmount();
  91.     }
  92.  
  93.     public void RemoveRandomProduct()
  94.     {
  95.         _basket.RemoveRandomProduct();
  96.     }
  97. }
  98.  
  99. class Product
  100. {
  101.     private Random _random;
  102.     public int Price { get; }
  103.     public string Name { get; }
  104.  
  105.     public Product(string name)
  106.     {
  107.         _random = new Random();
  108.         Name = name;
  109.         Price = _random.Next(50, 150);
  110.     }
  111.  
  112.     public void ShowProductInfo()
  113.     {
  114.         Console.WriteLine($"name: {Name} || price: {Price}");
  115.     }
  116. }
  117.  
  118. class Basket
  119. {
  120.     private Random _random;
  121.     private List<Product> _products;
  122.  
  123.     public Basket()
  124.     {
  125.         _products = new List<Product>();
  126.         _random = new Random();
  127.         SetProducts();
  128.     }
  129.  
  130.     public void RemoveRandomProduct()
  131.     {
  132.         int indexProduct = _random.Next(0, _products.Count);
  133.         Console.WriteLine($"Product: {_products[indexProduct].Name} was removed");
  134.         _products.RemoveAt(indexProduct);
  135.     }
  136.  
  137.     public void ShowProducts()
  138.     {
  139.         for (int i = 0; i < _products.Count; i++)
  140.         {
  141.             _products[i].ShowProductInfo();
  142.         }
  143.     }
  144.    
  145.     public int CountPurchaseAmount()
  146.     {
  147.         int purchaseAmount = 0;
  148.  
  149.         for (int i = 0; i < _products.Count; i++)
  150.         {
  151.             purchaseAmount += _products[i].Price;
  152.         }
  153.         return purchaseAmount;
  154.     }
  155.     private void SetProducts()
  156.     {
  157.         List<string> products = new List<string> { "Milk", "Beer", "Bread", "Egg" };
  158.  
  159.         for (int i = 0; i < products.Count; i++)
  160.         {
  161.             _products.Add(new Product(products[i]));
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment