Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Numerics;
- namespace Shop
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string CommandShowSellerGoods = "1";
- const string CommandSellGoods = "2";
- const string CommandShowPlayerInventory = "3";
- const string CommandExit = "4";
- bool isWork = true;
- Seller seller = new Seller();
- Player player = new Player();
- Menu menu = new Menu(seller.GetProducts());
- while (isWork)
- {
- Console.Clear();
- Console.WriteLine($"{CommandShowSellerGoods}. Показать список товаров." +
- $"\n{CommandSellGoods}. Продать товар." +
- $"\n{CommandShowPlayerInventory}. Показать сумку покупателя." +
- $"\n{CommandExit}. Выйти");
- switch (Console.ReadLine())
- {
- case CommandShowSellerGoods:
- seller.ShowAllItems();
- break;
- case CommandSellGoods:
- menu.TryRun(seller, player);
- break;
- case CommandShowPlayerInventory:
- player.ShowAllItems();
- break;
- case CommandExit:
- isWork = false;
- break;
- default:
- Console.WriteLine("Ошибка, такого пункта меню нет");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- class Trader
- {
- protected List<Product> Products = new List<Product>();
- protected int Money = 0;
- virtual public void ShowAllItems()
- {
- if (Products.Count > 0)
- {
- foreach (Product item in Products)
- {
- item.ShowInfo();
- }
- }
- else
- {
- Console.WriteLine($"Инвентарь пуст.");
- }
- }
- }
- class Seller : Trader
- {
- public Seller()
- {
- Products.Add(new Product("Выход", 0));
- Products.Add(new Product("Хлеб", 12));
- Products.Add(new Product("Овес", 4));
- Products.Add(new Product("Сыр", 118));
- Products.Add(new Product("Яйца", 32));
- Products.Add(new Product("Чай", 23));
- }
- public List<Product> GetProducts()
- {
- return Products;
- }
- public void TakeMoney(int payment)
- {
- Money += payment;
- }
- public void Sell(int index)
- {
- Product product = Products[index];
- Products.Remove(product);
- Money += product.Price;
- }
- public override void ShowAllItems()
- {
- if (Products.Count > 1)
- {
- foreach (Product item in Products)
- {
- if(item.Name == "Выход")
- {
- continue;
- }
- else
- {
- item.ShowInfo();
- }
- }
- }
- else
- {
- Console.WriteLine($"Инвентарь пуст.");
- }
- }
- }
- class Player : Trader
- {
- private int _moneyToPay;
- public int Money { get; private set; } = 5000;
- public void AddGoods(Product product)
- {
- Products.Add(product);
- }
- public bool CheckSolvency(Product product)
- {
- _moneyToPay = product.Price;
- if (Money >= _moneyToPay)
- {
- return true;
- }
- else
- {
- _moneyToPay = 0;
- return false;
- }
- }
- public int Pay()
- {
- Money -= _moneyToPay;
- return _moneyToPay;
- }
- }
- class Product
- {
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"Название товара: {Name}, цена: {Price}.");
- }
- }
- class Menu
- {
- private List<Product> _items;
- private int _selectedItemIndex;
- public Menu(List<Product> items)
- {
- _items = items;
- _selectedItemIndex = 0;
- }
- private void Display()
- {
- Console.Clear();
- Console.WriteLine("Управление ▲, ▼, Enter, Esc.");
- for (int i = 0; i < _items.Count; i++)
- {
- int isequenceNumber = i;
- var currentItem = _items[i];
- if (i == _selectedItemIndex)
- {
- Console.ForegroundColor = ConsoleColor.Black;
- Console.BackgroundColor = ConsoleColor.Gray;
- if(i == 0)
- {
- Console.WriteLine($"{isequenceNumber}. {currentItem.Name}.");
- }
- else
- {
- Console.WriteLine($"{isequenceNumber}. {currentItem.Name}, Цена: {currentItem.Price}");
- }
- }
- else
- {
- if (i == 0)
- {
- Console.WriteLine($"{isequenceNumber}. {currentItem.Name}.");
- }
- else
- {
- Console.WriteLine($"{isequenceNumber}. {currentItem.Name}, Цена: {currentItem.Price}");
- }
- }
- Console.ResetColor();
- }
- }
- public void MoveUp()
- {
- _selectedItemIndex--;
- if (_selectedItemIndex < 0)
- {
- _selectedItemIndex = _items.Count - 1;
- }
- }
- public void MoveDown()
- {
- _selectedItemIndex++;
- if (_selectedItemIndex >= _items.Count)
- {
- _selectedItemIndex = 0;
- }
- }
- public void СhooseGoods(Seller seller, Player player, ref bool isExit)
- {
- Product product = _items[_selectedItemIndex];
- if(_selectedItemIndex == 0)
- {
- isExit = false;
- }
- else
- {
- if (player.CheckSolvency(product))
- {
- seller.Sell(_selectedItemIndex);
- player.AddGoods(product);
- seller.TakeMoney(player.Pay());
- MoveUp();
- Console.WriteLine($"Осталось денег: {player.Money}.");
- Console.ReadKey();
- }
- else
- {
- Console.WriteLine("Не хватает денег.");
- Console.ReadKey();
- }
- }
- }
- public void TryRun(Seller seller, Player player)
- {
- if(_items.Count> 1)
- {
- Run(seller, player);
- }
- else
- {
- Console.WriteLine("В магазине нет товаров.");
- }
- }
- public void Run(Seller seller, Player player)
- {
- const ConsoleKey KeyMoveUp = ConsoleKey.UpArrow;
- const ConsoleKey KeyMoveDown = ConsoleKey.DownArrow;
- const ConsoleKey KeyPressingEnter = ConsoleKey.Enter;
- const ConsoleKey KeyPressingEscape = ConsoleKey.Escape;
- bool isWork = true;
- while (isWork)
- {
- Display();
- ConsoleKeyInfo keyInfo;
- keyInfo = Console.ReadKey(true);
- switch (keyInfo.Key)
- {
- case KeyMoveUp:
- MoveUp();
- break;
- case KeyMoveDown:
- MoveDown();
- break;
- case KeyPressingEnter:
- СhooseGoods(seller, player, ref isWork);
- break;
- case KeyPressingEscape:
- isWork = false;
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement