Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Classes
- {
- internal class Program
- {
- static void Main()
- {
- Supermarket supermarket = new Supermarket();
- supermarket.Work();
- }
- }
- class Supermarket
- {
- private List<Product> _products = new List<Product>();
- private Queue<Client> _clients = new Queue<Client>();
- private int _revenue;
- private void CreateProducts()
- {
- int productsCount = 100;
- for (int i = 0; i < productsCount; i++)
- {
- _products.Add(new Product(i + 1));
- }
- }
- private void CreateClients()
- {
- int clientsCount = 10;
- for (int i = 0; i < clientsCount; i++)
- {
- _clients.Enqueue(new Client(_products));
- }
- }
- public void Work()
- {
- CreateProducts();
- CreateClients();
- int clientNumber = 1;
- bool isWorking = true;
- while (isWorking)
- {
- _clients.Peek().ShowCard();
- _revenue += _clients.Peek().Buy();
- _clients.Peek().ShowBag();
- _clients.Dequeue();
- ShowRevenue();
- isWorking = _clients.Count > 0;
- if (isWorking)
- {
- Console.WriteLine($"\nNext client {++clientNumber}\nPress something");
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- private void ShowRevenue() => Console.WriteLine("\nRevenue: " + _revenue);
- }
- class Product
- {
- public Product(int index)
- {
- Initialize(index);
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- private void Initialize(int index)
- {
- Name = "product " + index;
- int minValue = 50;
- int maxValue = 300;
- Price = UserUtils.GenerateRandomNumber(minValue, maxValue);
- }
- }
- class Client
- {
- private int _wallet;
- private List<Product> _cart = new List<Product>();
- private List<Product> _bag = new List<Product>();
- public Client(List<Product> products)
- {
- Initialize(products);
- }
- public void ShowCard()
- {
- UserUtils.ShowProduct(_cart);
- }
- public int Buy()
- {
- bool canBuy = false;
- int purchasePrice = 0;
- while (canBuy == false)
- {
- purchasePrice = 0;
- foreach (Product product in _cart)
- {
- purchasePrice += product.Price;
- }
- Console.WriteLine($"\npurchase price: {purchasePrice}");
- if (purchasePrice > _wallet && _cart.Count != 0)
- {
- ThrowOutPurchase(_cart);
- }
- else if (_cart.Count == 0)
- {
- Console.WriteLine("\nThere are no purchases in the cart");
- canBuy = true;
- }
- else
- {
- Console.WriteLine("\nThere is enough money to buy");
- canBuy = true;
- ReplaceInBag(_cart);
- }
- }
- _wallet -= purchasePrice;
- return purchasePrice;
- }
- public void ShowBag()
- {
- Console.WriteLine("Products in the bag:");
- UserUtils.ShowProduct(_bag);
- }
- private void Initialize(List<Product> products)
- {
- int minWalletValue = 500;
- int maxWalletValue = 1000;
- _wallet = UserUtils.GenerateRandomNumber(minWalletValue, maxWalletValue);
- int minProductsCount = 2;
- int maxProductsCount = 10;
- int productsInCart = UserUtils.GenerateRandomNumber(minProductsCount, maxProductsCount);
- for (int i = 0; i < productsInCart; i++)
- {
- int productIndex = UserUtils.GenerateRandomNumber(0, products.Count);
- _cart.Add(products[productIndex]);
- }
- }
- private void ThrowOutPurchase(List<Product> _cart)
- {
- int randomProduct = UserUtils.GenerateRandomNumber(_cart.Count);
- Console.WriteLine($"\nNot enough money to buy\nA random product is thrown out of the cart:{_cart[randomProduct].Name} - price:{_cart[randomProduct].Price}");
- _cart.RemoveAt(randomProduct);
- Console.WriteLine("press something");
- Console.ReadKey();
- }
- private void ReplaceInBag(List<Product> _cart)
- {
- int cartCount = _cart.Count;
- for (int i = 0; i < cartCount; i++)
- {
- _bag.Add(_cart[0]);
- _cart.RemoveAt(0);
- }
- }
- }
- class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int min, int max) => s_random.Next(min, max);
- public static int GenerateRandomNumber(int max) => s_random.Next(max);
- public static void ShowProduct(List<Product> products)
- {
- int index = 1;
- foreach (Product product in products)
- {
- Console.WriteLine(index++ + " - " + product.Name + " - price:" + product.Price);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement