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.Basket.IsEmpty)
- {
- 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> _products;
- public Shop(string name, int cash, List<Product> products)
- {
- Name = name;
- Cash = cash;
- _products = products;
- }
- public Queue<Client> CollectClients(int countClients, List<string> names)
- {
- if (countClients <= 0)
- countClients = 1;
- Queue<Client> clients = new Queue<Client>();
- for (int i = 0; i < countClients; i++)
- {
- Client tempClient = new Client(names, _products);
- clients.Enqueue(tempClient);
- }
- return clients;
- }
- public void MakePurchase(Client client)
- {
- Cash += client.MakePurchase();
- }
- }
- class Client
- {
- private Random _random = new Random();
- public bool ReadyToBuy { get { return Basket.GetSumPriceProducts() < Wallet; } }
- public string Name { get; private set; }
- public int Wallet { get; private set; }
- public Basket Basket { get; private set; }
- public Client(List<string> names, List<Product> products)
- {
- Name = names[_random.Next(0, (names.Count - 1))];
- Wallet = _random.Next(150, 2001);
- int countProductsInBasket = _random.Next(1, (products.Count + 1));
- for (int i = 0; i < countProductsInBasket; i++)
- {
- int tempProduct = _random.Next(0, products.Count);
- Basket.AddProduct(products[tempProduct]);
- }
- }
- public void RemoveProductsFromBasket()
- {
- while (Basket.GetSumPriceProducts() > Wallet && !Basket.IsEmpty)
- {
- int tempNumberProduct = _random.Next(0, Basket.GetCountProductsInBasket());
- Basket.RemoveProduct(tempNumberProduct);
- }
- }
- public void ShowBasket()
- {
- Basket.ShowProducts();
- }
- public int MakePurchase()
- {
- int SumPrice = Basket.GetSumPriceProducts();
- Wallet -= SumPrice;
- Basket.ClearBasket();
- return SumPrice;
- }
- }
- class Basket
- {
- public bool IsEmpty { get { return _products.Count == 0; } private set { } }
- private List<Product> _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 void AddProduct(Product product)
- {
- _products.Add(product);
- }
- public int GetSumPriceProducts()
- {
- int sum = 0;
- foreach (var product in _products)
- {
- sum += product.Price;
- }
- return sum;
- }
- public int GetCountProductsInBasket()
- {
- 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