Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Shop
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string ShowSellerGoodsCommand = "1";
- const string SellGoodsCommand = "2";
- const string ShowPlayerInventoryCommand = "3";
- const string ExitCommand = "4";
- bool isWork = true;
- Seller seller = new Seller();
- Player player = new Player();
- Menu menu = new Menu(seller.GetProducts());
- while (isWork)
- {
- Console.Clear();
- Console.WriteLine($"{ShowSellerGoodsCommand}. Показать список товаров." +
- $"\n{SellGoodsCommand}. Продать товар." +
- $"\n{ShowPlayerInventoryCommand}. Показать сумку покупателя." +
- $"\n{ExitCommand}. Выйти");
- switch (Console.ReadLine())
- {
- case ShowSellerGoodsCommand:
- seller.ShowAllItems();
- break;
- case SellGoodsCommand:
- menu.Run(seller, player);
- break;
- case ShowPlayerInventoryCommand:
- player.ShowAllItems();
- break;
- case ExitCommand:
- isWork = false;
- break;
- default:
- Console.WriteLine("Ошибка, такого пункта меню нет");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- class Seller
- {
- private List<Product> _products = new List<Product>();
- private int _money = 0;
- public Seller()
- {
- _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 void ShowAllItems()
- {
- if (_products.Count > 0)
- {
- foreach (Product _product in _products)
- {
- _product.ShowInfo();
- }
- }
- else
- {
- Console.WriteLine($"Инвентарь продавца пуст.");
- }
- }
- }
- class Player
- {
- private List<Product> _bag;
- private int _moneyToPay;
- public Player()
- {
- _bag = new List<Product>();
- }
- public int Money { get; private set; } = 50;
- public void AddGoods(Product product)
- {
- _bag.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;
- }
- public void ShowAllItems()
- {
- if (_bag.Count > 0)
- {
- foreach (Product item in _bag)
- {
- item.ShowInfo();
- }
- }
- else
- {
- Console.WriteLine($"Инвентарь покупателя пуст.");
- }
- }
- }
- 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++)
- {
- if (i == _selectedItemIndex)
- {
- Console.ForegroundColor = ConsoleColor.Black;
- Console.BackgroundColor = ConsoleColor.Gray;
- Console.WriteLine($"{i + 1}. {_items[i].Name}, Цена: {_items[i].Price}");
- }
- else
- {
- Console.WriteLine($"{i + 1}. {_items[i].Name}, Цена: {_items[i].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 СhoiceOfGoods(Seller seller, Player player)
- {
- Product product = _items[_selectedItemIndex];
- if (player.CheckSolvency(product))
- {
- seller.Sell(_selectedItemIndex);
- player.AddGoods(product);
- seller.TakeMoney(player.Pay());
- Console.WriteLine($"Осталось денег: {player.Money}.");
- Console.ReadKey();
- }
- else
- {
- Console.WriteLine("Не хватает денег.");
- Console.ReadKey();
- }
- }
- public void Run(Seller seller, Player player)
- {
- bool isWork = true;
- while (isWork)
- {
- Display();
- ConsoleKeyInfo keyInfo;
- keyInfo = Console.ReadKey(true);
- switch (keyInfo.Key)
- {
- case ConsoleKey.UpArrow:
- MoveUp();
- break;
- case ConsoleKey.DownArrow:
- MoveDown();
- break;
- case ConsoleKey.Enter:
- СhoiceOfGoods(seller, player);
- break;
- case ConsoleKey.Escape:
- isWork = false;
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement