Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Clight_36_OOP_Shoping
- {
- class Program
- {
- static void Main(string[] args)
- {
- Product tomato = new Product("Томаты", 150);
- Product cucumber = new Product("Огурцы", 113);
- Product chiken = new Product("Курица", 470);
- Product chips = new Product("Чипсы", 110);
- Product juice = new Product("Сок", 100);
- Product potato = new Product("Картошка", 40);
- Product iceCream = new Product("Мороженое", 65);
- Product apple = new Product("Яблоки", 100);
- Product orange = new Product("Апельсины", 200);
- Product bubbleGum = new Product("Жевательная резинка", 50);
- Product chocolate = new Product("Шоколад", 150);
- Product stillWater = new Product("Не газированная вода", 50);
- Product soda = new Product("Газированная вода", 50);
- Product fish = new Product("Рыба", 230);
- List<Product> allProducts = new List<Product> { tomato, cucumber, chiken, juice, potato, iceCream, apple, orange, bubbleGum, chocolate, stillWater, soda, fish };
- List<string> names = new List<string> { "Иван", "Владимир", "Оксана", "Генадий", "Марина", "Игорь", "Евгений", "Мария", "Надежда", "Руслан", "Сергей", "Ирина" };
- Shop shop = new Shop("Продукты у Дома", 0, allProducts);
- Queue<Client> shopClients = shop.CollectClients(10, names);
- bool isOpen = true;
- while (isOpen)
- {
- Client currentClient = shopClients.Dequeue();
- Console.Clear();
- Console.WriteLine($"Магазин \"{shop.Name}\". Касса: {shop.Cash} руб.");
- Console.ForegroundColor = ConsoleColor.DarkGray;
- Console.WriteLine("Нажмите любую клавишу, чтобы пробить товары.\n");
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine($"На кассе {currentClient.Name}, имея в кошельке {currentClient.Wallet} руб.\n");
- Console.WriteLine("В корзине:");
- currentClient.ShowBasket();
- Console.ReadKey();
- bool isRemoveProducts = false;
- if (!currentClient.ReadyToBuy)
- {
- currentClient.RemoveProductsFromBasket();
- isRemoveProducts = true;
- }
- if (isRemoveProducts && currentClient.IsBasketEmpty())
- {
- Console.WriteLine("\nУ клиента оказалось мало денег и он решил ничего не покупать.");
- Console.ReadKey();
- }
- else if(isRemoveProducts)
- {
- Console.WriteLine($"\nНе на все товары хватило денег. {currentClient.Name} выбрал(а): \n");
- currentClient.ShowBasket();
- shop.MakePurchase(currentClient);
- Console.ReadKey();
- }
- else if (currentClient.ReadyToBuy && !isRemoveProducts)
- {
- shop.MakePurchase(currentClient);
- Console.WriteLine("\nПокупка прошла успешно.");
- Console.ReadKey();
- }
- else
- {
- Console.WriteLine("\nПокупка не удалась.");
- Console.ReadKey();
- }
- if (shopClients.Count == 0)
- {
- Console.Clear();
- Console.WriteLine($"\nОчередь подошла к концу и магазин закрывается. Касса: {shop.Cash} руб.");
- Console.ReadKey();
- isOpen = false;
- }
- }
- }
- }
- class Shop
- {
- public string Name { get; private set; }
- public int Cash { get; private set; }
- private List<Product> _shopProducts;
- public Shop(string name, int cash, List<Product> products)
- {
- Name = name;
- Cash = cash;
- _shopProducts = products;
- }
- public Queue<Client> CollectClients(int numberClients, List<string> names)
- {
- if (numberClients <= 0)
- numberClients = 1;
- Queue<Client> currentClients = new Queue<Client>();
- for (int i = 0; i < numberClients; i++)
- {
- Client tempClient = new Client(names, _shopProducts);
- currentClients.Enqueue(tempClient);
- }
- return currentClients;
- }
- public void MakePurchase(Client client)
- {
- Cash += client.GetSumPriceProducts();
- client.MakePurchase();
- }
- }
- class Client
- {
- public string Name { get; private set; }
- public int Wallet { get; private set; }
- private Basket _basket;
- private Random _random = new Random();
- public bool ReadyToBuy { get { return _basket.GetSumPriceProducts() < Wallet; }}
- public Client(List<string> names, List<Product> products)
- {
- Name = names[_random.Next(0, (names.Count - 1))];
- Wallet = _random.Next(150, 2001);
- int numberProducts = _random.Next(1, (products.Count + 1));
- List<Product> tempProducts = new List<Product>(numberProducts);
- for (int i = 0; i < numberProducts; i++)
- {
- int tempNum = _random.Next(0, products.Count);
- tempProducts.Add(products[tempNum]);
- }
- _basket = new Basket(tempProducts);
- }
- public void RemoveProductsFromBasket()
- {
- while (_basket.GetSumPriceProducts() > Wallet && !_basket.IsEmpty())
- {
- int tempNumberProduct = _random.Next(0, _basket.GetNumberProductsOfBasket());
- _basket.RemoveProduct(tempNumberProduct);
- }
- }
- public void ShowBasket()
- {
- _basket.ShowProducts();
- }
- public int GetSumPriceProducts()
- {
- return _basket.GetSumPriceProducts();
- }
- public void MakePurchase()
- {
- Wallet -= _basket.GetSumPriceProducts();
- _basket.ClearBasket();
- }
- public bool IsBasketEmpty()
- {
- return _basket.IsEmpty();
- }
- }
- class Basket
- {
- private List<Product> _products;
- public Basket(List<Product> products)
- {
- _products = products;
- }
- public void ShowProducts()
- {
- for (int i = 0; i < _products.Count; i++)
- {
- if ((i + 1) < 10)
- {
- Console.Write("0" + (i + 1) + ". ");
- _products[i].ShowProduct();
- }
- else
- {
- Console.Write((i + 1) + ". ");
- _products[i].ShowProduct();
- }
- }
- }
- public int GetSumPriceProducts()
- {
- int sum = 0;
- foreach (var product in _products)
- {
- sum += product.Price;
- }
- return sum;
- }
- public bool IsEmpty()
- {
- return _products.Count == 0;
- }
- public int GetNumberProductsOfBasket()
- {
- return _products.Count;
- }
- public void RemoveProduct(int numberProduct)
- {
- _products.RemoveAt(numberProduct);
- }
- public void ClearBasket()
- {
- _products.Clear();
- }
- }
- class Product
- {
- public string Name { get; private set; }
- public int Price { get; private set; }
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public void ShowProduct()
- {
- Console.WriteLine(Name + ", " + Price + " руб.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement