Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Queue<Client> clients = new Queue<Client>();
- Console.Write("Number of clients in the queue: ");
- string userInput = Console.ReadLine();
- int number;
- int clientStack = 1;
- if (int.TryParse(userInput, out number))
- {
- clientStack = Convert.ToInt32(userInput);
- for (int i = 0; i < clientStack; i++)
- {
- clients.Enqueue(new Client());
- }
- int indexClient = 1;
- while (clients.Count > 0)
- {
- CheckPurchase(clients.Dequeue(), indexClient);
- indexClient++;
- }
- }
- else
- {
- Console.WriteLine("Input error");
- }
- }
- public static void CheckPurchase(Client client, int indexClient)
- {
- Console.WriteLine($"Client: {indexClient}");
- client.ShowStats();
- Console.WriteLine($"Purchase amount: {client.GetPurchaseAmount()}");
- while (client.GetPurchaseAmount() > client.Money)
- {
- Console.Clear();
- Console.WriteLine($"Client: {indexClient}");
- client.ShowStats();
- Console.WriteLine($"Purchase amount: {client.GetPurchaseAmount()}");
- Console.ReadKey();
- if (client.GetPurchaseAmount() > client.Money)
- {
- Console.WriteLine("Not enough money");
- client.RemoveRandomProduct();
- Console.ReadKey();
- }
- }
- Console.Clear();
- Console.WriteLine($"Client: {indexClient}");
- client.ShowStats();
- Console.WriteLine($"Purchase amount: {client.GetPurchaseAmount()}");
- Console.ReadKey();
- }
- }
- }
- class Client
- {
- private Basket _basket;
- private Random _random;
- public int Money { get; }
- public Client()
- {
- _basket = new Basket();
- _random = new Random();
- Money = _random.Next(100, 400);
- }
- public void ShowStats()
- {
- Console.WriteLine($"Money: {Money}");
- _basket.ShowProducts();
- }
- public int GetPurchaseAmount()
- {
- return _basket.CountPurchaseAmount();
- }
- public void RemoveRandomProduct()
- {
- _basket.RemoveRandomProduct();
- }
- }
- class Product
- {
- private Random _random;
- public int Price { get; }
- public string Name { get; }
- public Product(string name)
- {
- _random = new Random();
- Name = name;
- Price = _random.Next(50, 150);
- }
- public void ShowProductInfo()
- {
- Console.WriteLine($"name: {Name} || price: {Price}");
- }
- }
- class Basket
- {
- private Random _random;
- private List<Product> _products;
- public Basket()
- {
- _products = new List<Product>();
- _random = new Random();
- SetProducts();
- }
- public void RemoveRandomProduct()
- {
- int indexProduct = _random.Next(0, _products.Count);
- Console.WriteLine($"Product: {_products[indexProduct].Name} was removed");
- _products.RemoveAt(indexProduct);
- }
- public void ShowProducts()
- {
- for (int i = 0; i < _products.Count; i++)
- {
- _products[i].ShowProductInfo();
- }
- }
- public int CountPurchaseAmount()
- {
- int purchaseAmount = 0;
- for (int i = 0; i < _products.Count; i++)
- {
- purchaseAmount += _products[i].Price;
- }
- return purchaseAmount;
- }
- private void SetProducts()
- {
- List<string> products = new List<string> { "Milk", "Beer", "Bread", "Egg" };
- for (int i = 0; i < products.Count; i++)
- {
- _products.Add(new Product(products[i]));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment