Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace OOPTask9Supermarket
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Supermarket supermarket = new Supermarket();
- supermarket.Work();
- }
- }
- public class Supermarket
- {
- private List<Product> _products = new List<Product>();
- private Queue<Client> _clients = new Queue<Client>();
- private int _money = 0;
- public Supermarket()
- {
- CreateProducts();
- CreateClients();
- }
- public void Work()
- {
- while (_clients.Count > 0)
- {
- Console.WriteLine($"Баланс супермаркета {_money} и в очереди {_clients.Count} покупателей");
- Client client = _clients.Dequeue();
- Console.WriteLine("\nСледующий клиент подошел к кассе\n");
- if (client.IsEmptyCart)
- {
- Console.WriteLine("Он ничего не хочет купить, а просто протискивается мимо кассы");
- }
- else
- {
- Console.WriteLine("Он хочет купить:");
- client.ShowCart();
- Console.WriteLine();
- client.ValidateCart();
- Console.WriteLine();
- if (client.IsEmptyCart)
- {
- Console.WriteLine("У покупателя совсем ни на что не хватило денег");
- }
- else
- {
- int takenMoney = client.GiveMoneyForProducts();
- Console.WriteLine($"За {takenMoney} он покупает:");
- client.ShowCart();
- _money += takenMoney;
- client.TakeProducts();
- }
- }
- Console.WriteLine("\nПокупатель уходит домой");
- Console.WriteLine("\nДля продолжения нажмите любую клавишу");
- Console.ReadKey();
- Console.Clear();
- }
- Console.WriteLine($"Все клиенты обслужены, баланс супермаркета: {_money} рублей");
- Console.ReadKey();
- }
- public void CreateClients()
- {
- const int CountClients = 20;
- const int MinimumDesirePercent = 0;
- const int MaximumDesirePercent = 100;
- const int MinimumMoney = 100;
- const int MaximumMoney = 5000;
- for (int i = 0; i < CountClients; i++)
- {
- Client client = new Client(UserUtils.GenerateRandomNumber(MinimumMoney, MaximumMoney),
- UserUtils.GenerateRandomNumber(MinimumDesirePercent, MaximumDesirePercent));
- OfferProducts(client);
- _clients.Enqueue(client);
- }
- }
- private void OfferProducts(Client client)
- {
- foreach (Product product in _products)
- {
- client.DecideProduct(product);
- }
- }
- private void CreateProducts()
- {
- _products.Add(new Product("йогурт", 50));
- _products.Add(new Product("банан", 150));
- _products.Add(new Product("помидор", 200));
- _products.Add(new Product("морковь", 30));
- _products.Add(new Product("хлеб", 80));
- _products.Add(new Product("молоко", 100));
- _products.Add(new Product("печенье", 50));
- _products.Add(new Product("мыло", 50));
- _products.Add(new Product("веревка", 50));
- }
- }
- public class Client
- {
- private const int MinimumProductCount = 1;
- private const int MaximumProductCount = 20;
- private List<Product> _cart = new List<Product>();
- private List<Product> _bag = new List<Product>();
- private int _money;
- private int _desirePercent;
- public Client(int money, int desirePercent)
- {
- _money = money;
- _desirePercent = desirePercent;
- }
- public bool IsEmptyCart => _cart.Count == 0;
- public void DecideProduct(Product product)
- {
- if(UserUtils.IsProbability(_desirePercent))
- {
- _cart.Add(product.Clone(UserUtils.GenerateRandomNumber(MinimumProductCount, MaximumProductCount)));
- }
- }
- private int CalcCartCost()
- {
- int cartCost = 0;
- foreach(Product product in _cart)
- {
- cartCost += product.Cost;
- }
- return cartCost;
- }
- public void ValidateCart()
- {
- while (CalcCartCost() > _money)
- {
- RemoveRandomProduct();
- }
- }
- public int GiveMoneyForProducts()
- {
- int cartCost = CalcCartCost();
- _money -= cartCost;
- return cartCost;
- }
- public void TakeProducts()
- {
- foreach(Product product in _cart)
- {
- _bag.Add(product);
- }
- _cart.Clear();
- }
- private void RemoveRandomProduct()
- {
- Product randomProduct = _cart[UserUtils.GenerateRandomNumber(0, _cart.Count)];
- Console.Write("Не хватает денег и выкидывает: ");
- randomProduct.ShowInfo();
- _cart.Remove(randomProduct);
- }
- public void ShowCart()
- {
- foreach(Product product in _cart)
- {
- product.ShowInfo();
- }
- }
- }
- public class Product
- {
- private string _name;
- private int _price;
- private int _count;
- public Product(string name, int price, int count = 0)
- {
- _name = name;
- _price = price;
- _count = count;
- }
- public int Cost => _price * _count;
- public void ShowShortInfo()
- {
- Console.WriteLine($"\"{_name}\" : {_price} рублей");
- }
- public void ShowInfo()
- {
- Console.WriteLine($"\"{_name}\" : {_price} x {_count} = {Cost} рублей");
- }
- public Product Clone(int count)
- {
- return new Product( _name, _price, count);
- }
- }
- public class UserUtils
- {
- private const int MinimumPercent = 0;
- private const int MaximumPercent = 100;
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int minimumNumber, int maximumNumber)
- {
- return s_random.Next(minimumNumber, maximumNumber);
- }
- public static bool IsProbability(int percent)
- {
- return s_random.Next(MinimumPercent, MaximumPercent) < percent;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement