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;
- public void Work()
- {
- CreateProducts();
- CreateClients();
- int clientNumber = 1;
- bool isWorking = true;
- while (isWorking)
- {
- Client currentClient = _clients.Dequeue();
- currentClient.ShowCard();
- _revenue += currentClient.Buy();
- currentClient.ShowBag();
- ShowRevenue();
- isWorking = _clients.Count > 0;
- if (isWorking)
- {
- Console.WriteLine($"\nNext client {++clientNumber}\nPress something");
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- 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;
- List<Client> clients = new List<Client>(clientsCount);
- for (int i = 0; i < clientsCount; i++)
- {
- clients.Add(new Client(_products));
- }
- AddClients(clients);
- }
- private void AddClients(List<Client> clients)
- {
- foreach (Client client in clients)
- {
- _clients.Enqueue(client);
- }
- }
- 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 + 1);
- }
- }
- class Client
- {
- private List<Product> _cart = new List<Product>();
- private List<Product> _bag = new List<Product>();
- private int _wallet;
- public Client(List<Product> products)
- {
- Initialize(products);
- }
- public void ShowCard()
- {
- ShowProducts(_cart);
- }
- public int Buy()
- {
- int purchasePrice = 0;
- foreach (Product product in _cart)
- {
- purchasePrice += product.Price;
- }
- bool canBuy = false;
- while (canBuy == false)
- {
- Console.WriteLine($"\npurchase price: {purchasePrice}");
- if (purchasePrice > _wallet && _cart.Count != 0)
- {
- purchasePrice -= ThrowOutPurchase(_cart);
- }
- else
- {
- canBuy = true;
- }
- }
- if (_cart.Count == 0)
- {
- Console.WriteLine("\nThere are no purchases in the cart");
- }
- else
- {
- Console.WriteLine("\nThere is enough money to buy");
- ReplaceInBag(_cart);
- }
- _wallet -= purchasePrice;
- return purchasePrice;
- }
- public void ShowBag()
- {
- Console.WriteLine("Products in the bag:");
- ShowProducts(_bag);
- }
- private void Initialize(List<Product> products)
- {
- int minWalletValue = 500;
- int maxWalletValue = 1000;
- _wallet = UserUtils.GenerateRandomNumber(minWalletValue, maxWalletValue + 1);
- int minProductsCount = 2;
- int maxProductsCount = 10;
- int productsInCart = UserUtils.GenerateRandomNumber(minProductsCount, maxProductsCount + 1);
- for (int i = 0; i < productsInCart; i++)
- {
- int productIndex = UserUtils.GenerateRandomNumber(products.Count);
- _cart.Add(products[productIndex]);
- }
- }
- private int ThrowOutPurchase(List<Product> cart)
- {
- int randomProductIndex = UserUtils.GenerateRandomNumber(cart.Count);
- int throwOutProductPrice = cart[randomProductIndex].Price;
- Console.WriteLine($"\nNot enough money to buy\nA random product is thrown out of the cart:{cart[randomProductIndex].Name} - price:{cart[randomProductIndex].Price}");
- cart.RemoveAt(randomProductIndex);
- Console.WriteLine("press something");
- Console.ReadKey();
- return throwOutProductPrice;
- }
- private void ReplaceInBag(List<Product> _cart)
- {
- int cartCount = _cart.Count;
- for (int i = 0; i < cartCount; i++)
- {
- _bag.Add(_cart[0]);
- _cart.RemoveAt(0);
- }
- }
- private void ShowProducts(List<Product> products)
- {
- int index = 1;
- foreach (Product product in products)
- {
- Console.WriteLine(index++ + " - " + product.Name + " - price:" + product.Price);
- }
- }
- }
- class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int min, int max)
- {
- return s_random.Next(min, max);
- }
- public static int GenerateRandomNumber(int max)
- {
- return s_random.Next(max);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement