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 buyerWallet = 5000;
- 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(0, ySecondRow - 3);
- Console.WriteLine($"Выручка магазина: {vegetableShop.GetMoney()} руб.");
- Console.SetCursorPosition(0, ySecondRow - 2);
- Console.WriteLine("Прилавок магазина:\n");
- vegetableShop.ShowProducts(0, ySecondRow, numProductsInRow, step);
- Console.SetCursorPosition(2 * xSecondRow, ySecondRow - 3);
- Console.WriteLine($"Ваш кошелек: {buyer.GetMoney()} руб.");
- Console.SetCursorPosition(2 * xSecondRow, ySecondRow - 2);
- Console.WriteLine("Ваши покупки:\n");
- buyer.ShowProducts(2 * xSecondRow, ySecondRow, numProductsInRow, step, buyer.GetCurrentProducts());
- ConsoleKeyInfo keyShop = Console.ReadKey();
- switch (keyShop.Key)
- {
- case ConsoleKey.F1:
- int userNumProduct = CheckUserInput(vegetableShop.GetProducts(), yCheckMenu);
- int userQuantity = CheckUserInput(vegetableShop.GetProducts(), yCheckMenu, userNumProduct);
- bool checkUserBuyPossible = CheckUserInput(vegetableShop.GetProducts(), yCheckMenu, userNumProduct, userQuantity, buyer.GetMoney());
- if (checkUserBuyPossible)
- {
- bool goodShopingBuyer = false;
- bool goodShopingShop = false;
- goodShopingBuyer = buyer.DoShoping(vegetableShop.GetProducts(), userNumProduct, userQuantity);
- goodShopingShop = vegetableShop.DoShoping(vegetableShop.GetProducts(), userNumProduct, userQuantity);
- if (goodShopingBuyer != true && goodShopingShop != true)
- {
- Console.SetCursorPosition(0, ySecondRow - 5);
- Console.WriteLine("Покупка не прошла");
- Console.ReadKey();
- }
- }
- else
- {
- Console.SetCursorPosition(0, yCheckMenu + 1);
- Console.WriteLine($"У вас недостаточно средств для этой покупки. " +
- $"Не хватает {(vegetableShop.GetPrice(userNumProduct - 1) * userQuantity) - buyer.GetMoney()} руб.");
- Console.ReadKey();
- Console.SetCursorPosition(0, yCheckMenu);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- break;
- case ConsoleKey.F5:
- buyer.MergeProducts();
- inShop = false;
- break;
- }
- }
- break;
- case ConsoleKey.F2:
- Console.SetCursorPosition(0, ySecondRow - 3);
- Console.WriteLine($"Ваш кошелек: {buyer.GetMoney()} руб.");
- Console.SetCursorPosition(0, ySecondRow - 2);
- Console.WriteLine("Ваши покупки:\n");
- buyer.ShowProducts(0, ySecondRow, numProductsInRow, step, buyer.GetProducts());
- Console.ReadKey();
- 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)
- {
- 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(" ");
- }
- }
- return input;
- }
- static bool CheckUserInput(Product[] products, int yCheckMenu, int num, int quantity, int currentWallet)
- {
- bool checkUserInput;
- int currentCost = quantity * products[num - 1]._price;
- if (currentCost <= currentWallet)
- {
- checkUserInput = true;
- return checkUserInput;
- }
- else
- {
- checkUserInput = false;
- return checkUserInput;
- }
- }
- }
- interface IShoping
- {
- bool DoShoping(Product[] products, int num, int quantity);
- }
- interface IGetMoney
- {
- int GetMoney();
- }
- 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)
- {
- if (_products.Length > 0)
- {
- 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));
- }
- }
- else
- {
- Console.SetCursorPosition(0, 12);
- Console.WriteLine("<пусто>");
- }
- }
- public void ShowProducts(int x, int y, int numProductsInRow, int step, Product[] products)
- {
- if (products.Length > 0)
- {
- 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].ShowProductNoPrice(2 + x + (z * step), y + (j * 4));
- }
- }
- else
- {
- Console.SetCursorPosition(x, 12);
- Console.WriteLine("<пусто>");
- }
- }
- public string GetName()
- {
- return _name;
- }
- public Product[] GetProducts()
- {
- return _products;
- }
- public int GetPrice(int num)
- {
- int currentPrice = _products[num]._price;
- return currentPrice;
- }
- }
- class Shop : Dealer, IShoping, IGetMoney
- {
- 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;
- }
- }
- public int GetMoney()
- {
- return _income;
- }
- }
- class Buyer : Dealer, IShoping, IGetMoney
- {
- protected int _wallet;
- protected Product[] _currentProducts;
- public Buyer(string name, Product[] products, int wallet) : base(name, products)
- {
- _wallet = wallet;
- _currentProducts = new Product[0];
- }
- public bool DoShoping(Product[] products, int num, int quantity)
- {
- bool goodShoping = false;
- bool equalName = false;
- for (int i = 0; i < _currentProducts.Length; i++)
- {
- if (products[num - 1]._name == _currentProducts[i]._name)
- {
- _currentProducts[i].AddQuantity(quantity);
- _wallet -= products[num - 1]._price * quantity;
- goodShoping = true;
- equalName = true;
- }
- }
- if (equalName != true)
- {
- Product[] tempProducts = new Product[_currentProducts.Length + 1];
- Product tempProduct = new Product(products[num - 1]._name, quantity, products[num - 1]._price);
- for (int j = 0; j < _currentProducts.Length; j++)
- {
- tempProducts[j] = _currentProducts[j];
- }
- tempProducts[tempProducts.Length - 1] = tempProduct;
- _wallet -= products[num - 1]._price * quantity;
- _currentProducts = tempProducts;
- goodShoping = true;
- }
- return goodShoping;
- }
- public Product[] GetCurrentProducts()
- {
- return _currentProducts;
- }
- public int GetMoney()
- {
- return _wallet;
- }
- public void MergeProducts()
- {
- Product[] mergeProducts = new Product[_products.Length];
- for (int i = 0; i < mergeProducts.Length; i++)
- {
- mergeProducts[i] = _products[i];
- }
- for (int i = 0; i < _currentProducts.Length; i++)
- {
- int z = 0;
- for (int j = 0; j < mergeProducts.Length; j++)
- {
- if (_currentProducts[i]._name == mergeProducts[j]._name)
- {
- mergeProducts[j].AddQuantity(_currentProducts[i]._quantity);
- z++;
- }
- }
- if (z == 0)
- {
- Product[] tempProducts = new Product[mergeProducts.Length + 1];
- for (int y = 0; y < mergeProducts.Length; y++)
- {
- tempProducts[y] = mergeProducts[y];
- }
- Product tempProduct = new Product(_currentProducts[i]._name, _currentProducts[i]._quantity, _currentProducts[i]._price);
- tempProducts[tempProducts.Length - 1] = _currentProducts[i];
- mergeProducts = tempProducts;
- }
- }
- _currentProducts = new Product[0];
- _products = mergeProducts;
- }
- }
- 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 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} руб.");
- }
- public void ShowProductNoPrice(int x, int y)
- {
- Console.SetCursorPosition(x, y);
- Console.Write($"{_name}");
- Console.SetCursorPosition(x, y + 1);
- Console.Write($"Кол-во: {_quantity} кг");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement