Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Clight_25_OOP_Shop
- {
- class Program
- {
- static void Main(string[] args)
- {
- int numProductsInRow = 3;
- int step = 25;
- int ySecondRow = 12;
- int xSecondRow = 30;
- int yCheckMenu = 5;
- int yShowProducts = 8;
- int buyerWallet = 10000;
- Product tomato = new Product("Помидоры", 15, 250);
- Product cucumber = new Product("Огурцы", 15, 200);
- Product potato = new Product("Картофель", 20, 100);
- Product orange = new Product("Апельсин", 7, 350);
- Product aplle = new Product("Яблоки", 13, 150);
- Product kiwi = new Product("Киви", 5, 300);
- Shop vegetableShop = new Shop("Овощной", new Product[] { tomato, cucumber, potato, orange, kiwi, aplle }, 0);
- string name = "Покупатель";
- Buyer buyer = new Buyer(name, new Product[0], buyerWallet);
- while (true)
- {
- Console.CursorVisible = false;
- Console.WriteLine($"Добро пожаловать в магазин \"{vegetableShop.GetName()}\"\n\n");
- Console.WriteLine("Выберете действие:\n");
- Console.WriteLine("F1 - Зайти в магазин");
- Console.WriteLine("F2 - Посмотреть свои покупки");
- Console.WriteLine("F5 - Выйти");
- ConsoleKeyInfo key = Console.ReadKey();
- switch (key.Key)
- {
- case ConsoleKey.F1:
- bool inShop = true;
- while (inShop)
- {
- Console.Clear();
- Console.WriteLine("Выберете действие:\n");
- Console.WriteLine("F1 - Совершить покупку");
- Console.WriteLine("F5 - Назад");
- Console.SetCursorPosition(50, 0);
- Console.WriteLine($"Ваш кошелек: {buyer.GetWallet()}");
- Console.SetCursorPosition(0, ySecondRow - 2);
- Console.WriteLine("Прилавок магазина:\n");
- vegetableShop.ShowProducts(0, ySecondRow, numProductsInRow, step);
- Console.SetCursorPosition(2 * xSecondRow, ySecondRow - 2);
- Console.WriteLine("Ваши покупки:\n");
- buyer.ShowProducts(2 * xSecondRow, ySecondRow, numProductsInRow, step);
- ConsoleKeyInfo keyShop = Console.ReadKey();
- switch (keyShop.Key)
- {
- case ConsoleKey.F1:
- int currentCost;
- int userProduct = CheckUserInput(vegetableShop.GetProducts(), yCheckMenu);
- int userQuantity = CheckUserInput(vegetableShop.GetProducts(), yCheckMenu, userProduct, out currentCost);
- bool checkUserBuyPossible = CheckUserInput(vegetableShop.GetProducts(), yCheckMenu, userProduct, userQuantity, currentCost);
- if (checkUserBuyPossible)
- {
- bool goodShopingBuyer = false;
- bool goodShopingShop = false;
- Product[] currentPurchase = new Product[0];
- buyer.DoShoping(vegetableShop.GetProducts(), userProduct, userQuantity);
- vegetableShop.DoShoping(vegetableShop.GetProducts(), userProduct, userQuantity);
- if (goodShopingBuyer && goodShopingShop)
- {
- Console.WriteLine("Покупка успешно совершена.");
- }
- else
- {
- Console.WriteLine("Покупка не прошла");
- }
- }
- else
- {
- Console.SetCursorPosition(0, yCheckMenu + 1);
- Console.WriteLine($"У вас недостаточно средств для этой покупки. " +
- $"Не хватает {(vegetableShop.GetPrice(userProduct) * userQuantity) - buyer.GetWallet()} руб.");
- Console.ReadKey();
- Console.SetCursorPosition(0, yCheckMenu);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- break;
- case ConsoleKey.F5:
- inShop = false;
- break;
- }
- }
- break;
- case ConsoleKey.F2:
- Console.SetCursorPosition(0, ySecondRow - 2);
- Console.WriteLine("Ваши покупки:\n");
- buyer.ShowProducts(0, ySecondRow, numProductsInRow, step);
- break;
- case ConsoleKey.F5:
- Console.WriteLine("\n\nПриходите еще!");
- Console.ReadKey();
- Environment.Exit(0);
- break;
- }
- Console.Clear();
- }
- }
- static int CheckUserInput(Product[] products, int yCheckMenu)
- {
- bool checkUserInput;
- bool checkInput = true;
- int input = 0;
- while (checkInput)
- {
- Console.SetCursorPosition(0, yCheckMenu);
- Console.Write($"Выберите номер продукта (1 - {products.Length}):");
- string userInput = Console.ReadLine();
- checkUserInput = Int32.TryParse(userInput, out int value);
- if (checkUserInput && value <= products.Length && value > 0)
- {
- input = value;
- checkInput = false;
- }
- else
- {
- Console.SetCursorPosition(0, yCheckMenu + 1);
- Console.WriteLine("Введите корректное число.");
- Console.ReadKey();
- Console.SetCursorPosition(0, yCheckMenu);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- return input;
- }
- static int CheckUserInput(Product[] products, int yCheckMenu, int num, out int currentCost)
- {
- bool checkUserInput;
- bool checkInput = true;
- int input = 0;
- while (checkInput)
- {
- Console.SetCursorPosition(0, yCheckMenu);
- Console.Write($"Выберите количество товара (1 - {products[num - 1]._quantity}):");
- string userInput = Console.ReadLine();
- checkUserInput = Int32.TryParse(userInput, out int value);
- if (checkUserInput && value <= products[num - 1]._quantity && value > 0)
- {
- input = value;
- checkInput = false;
- }
- else
- {
- Console.SetCursorPosition(0, yCheckMenu + 1);
- Console.WriteLine("Введите корректное число.");
- Console.ReadKey();
- Console.SetCursorPosition(0, yCheckMenu);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- currentCost = products[num - 1]._price * input;
- return input;
- }
- static bool CheckUserInput(Product[] products, int yCheckMenu, int num, int quantity, int currentWallet)
- {
- bool checkUserInput;
- if (currentWallet >= quantity * products[num - 1]._price)
- {
- currentWallet = quantity * products[num - 1]._price;
- checkUserInput = true;
- return checkUserInput;
- }
- else
- {
- checkUserInput = false;
- return checkUserInput;
- }
- }
- }
- interface IShoping
- {
- bool DoShoping(Product[] products, int num, int quantity);
- }
- class Dealer
- {
- protected string _name;
- protected Product[] _products;
- public Dealer(string name, Product[] products)
- {
- _name = name;
- _products = products;
- }
- public void ShowProducts(int x, int y, int numProductsInRow, int step)
- {
- int j = 0;
- int z = 0;
- for (int i = 0; i < _products.Length; i++)
- {
- if (i > 0)
- {
- j++;
- if (j == numProductsInRow)
- {
- j = 0;
- z++;
- }
- }
- Console.SetCursorPosition(x + (z * step), y + (j * 4));
- Console.Write((i + 1) + " ");
- _products[i].ShowProduct(2 + x + (z * step), y + (j * 4));
- }
- }
- public string GetName()
- {
- return _name;
- }
- public int GetQuantity()
- {
- return _products.Length;
- }
- public Product[] GetProducts()
- {
- return _products;
- }
- public int GetPrice(int num)
- {
- int currentPrice = _products[num - 1]._price;
- return currentPrice;
- }
- }
- class Shop : Dealer, IShoping
- {
- protected int _income;
- public Shop(string name, Product[] products, int income) : base(name, products)
- {
- _income = income;
- }
- public bool DoShoping(Product[] products, int num, int quantity)
- {
- bool goodShoping;
- if (_products[num - 1]._quantity > quantity)
- {
- _income += _products[num - 1]._price * quantity;
- _products[num - 1].SubstractQuantity(quantity);
- goodShoping = true;
- return goodShoping;
- }
- else if (_products[num - 1]._quantity == quantity)
- {
- _income += _products[num - 1]._price * quantity;
- Product[] currentProducts = new Product[_products.Length - 1];
- for (int i = 0; i < num - 1; i++)
- {
- currentProducts[i] = _products[i];
- }
- for (int i = num - 1; i < currentProducts.Length; i++)
- {
- currentProducts[i] = _products[i + 1];
- }
- _products = currentProducts;
- goodShoping = true;
- return goodShoping;
- }
- else
- {
- goodShoping = false;
- return goodShoping;
- }
- }
- }
- class Buyer : Dealer, IShoping
- {
- protected int _wallet;
- public Buyer(string name, Product[] products, int wallet) : base(name, products)
- {
- _wallet = wallet;
- }
- public bool DoShoping(Product[] products, int num, int quantity)
- {
- if (_products.Length == 0)
- {
- }
- bool goodShoping = false;
- int j = 0;
- int currentNum = 0;
- for (int i = 0; i < _products.Length; i++)
- {
- if (products[num - 1]._name == _products[i]._name)
- {
- j++;
- currentNum = i;
- }
- }
- if (j > 0)
- {
- _products[currentNum].AddQuantity(quantity);
- _wallet = products[num - 1]._price * quantity;
- goodShoping = true;
- }
- else if (_products.Length == 0)
- {
- Product buyProduct = products[num - 1];
- Product[] currentProducts = new Product[] { products[num - 1] };
- _products = currentProducts;
- _wallet -= buyProduct._price * quantity;
- goodShoping = true;
- }
- else
- {
- Product buyProduct = products[num - 1];
- Product[] currentProducts = new Product[_products.Length + 1];
- for (int i = 0; i < _products.Length; i++)
- {
- currentProducts[i] = _products[i];
- }
- buyProduct.SetQuantity(quantity);
- currentProducts[currentProducts.Length - 1] = buyProduct;
- _products = currentProducts;
- _wallet -= buyProduct._price * quantity;
- goodShoping = true;
- }
- return goodShoping;
- }
- public int GetWallet()
- {
- return _wallet;
- }
- }
- class Product
- {
- public string _name { get; protected set; }
- public int _price { get; protected set; }
- public int _quantity { get; protected set; }
- public Product(string name, int quantity, int price)
- {
- _name = name;
- _price = price;
- _quantity = quantity;
- }
- public void SetQuantity(int quantity)
- {
- _quantity = quantity;
- }
- public void AddQuantity(int quantity)
- {
- _quantity += quantity;
- }
- public void SubstractQuantity(int quantity)
- {
- _quantity -= quantity;
- }
- public void ShowProduct(int x, int y)
- {
- Console.SetCursorPosition(x, y);
- Console.Write($"{_name}");
- Console.SetCursorPosition(x, y + 1);
- Console.Write($"Кол-во: {_quantity} кг");
- Console.SetCursorPosition(x, y + 2);
- Console.Write($"Цена: {_price} руб.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement