Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- Shop shop = new Shop(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:
- shop.PurchaseItems(seller, player);
- break;
- case CommandShowPlayerInventory:
- player.ShowAllItems();
- break;
- case CommandExit:
- isWork = false;
- break;
- default:
- Console.WriteLine("Ошибка, такого пункта меню нет");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- class Trader
- {
- virtual public void ShowAllItems()
- {
- if (Products.Count > 0)
- {
- foreach (Product item in Products)
- {
- item.ShowInfo();
- }
- }
- else
- {
- Console.WriteLine($"Инвентарь пуст.");
- }
- }
- protected List<Product> Products = new List<Product>();
- protected int Money = 0;
- }
- class Seller : Trader
- {
- 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()
- {
- List<Product> productsCopy = new List<Product>(Products);
- return productsCopy;
- }
- public void TakeMoney(int payment)
- {
- Money += payment;
- }
- public void Sell(int index)
- {
- Product product = Products[index];
- Products.Remove(product);
- Money += product.Price;
- }
- }
- class Player : Trader
- {
- public new int Money { get; private set; } = 5000;
- public void AddGoods(Product product)
- {
- Products.Add(product);
- }
- public bool IsSolvency(Product product)
- {
- return Money >= product.Price;
- }
- public int Pay(int paymant)
- {
- Money -= paymant;
- return Money;
- }
- }
- 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 Shop
- {
- private List<Product> _items;
- private int _selectedItemIndex;
- public Shop(List<Product> items)
- {
- _items = items;
- _selectedItemIndex = 0;
- }
- public void СhooseGoods(Seller seller, Player player)
- {
- Product product = _items[_selectedItemIndex];
- if (player.IsSolvency(product))
- {
- seller.Sell(_selectedItemIndex);
- player.AddGoods(product);
- seller.TakeMoney(player.Pay(product.Price));
- _items.Remove(product);
- if (_selectedItemIndex == _items.Count)
- {
- MoveUp();
- }
- Console.WriteLine($"Осталось денег: {player.Money}.");
- Console.ReadKey();
- }
- else
- {
- Console.WriteLine("Не хватает денег.");
- Console.ReadKey();
- }
- }
- public void PurchaseItems(Seller seller, Player player)
- {
- if (_items.Count > 0)
- {
- 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;
- const ConsoleKey KeyPressingE = ConsoleKey.E;
- bool isWork = true;
- while (isWork)
- {
- if (_items.Count == 0)
- {
- isWork = false;
- Display();
- }
- else
- {
- Display();
- ConsoleKeyInfo keyInfo;
- keyInfo = Console.ReadKey(true);
- switch (keyInfo.Key)
- {
- case KeyMoveUp:
- MoveUp();
- break;
- case KeyMoveDown:
- MoveDown();
- break;
- case KeyPressingEnter:
- СhooseGoods(seller, player);
- break;
- case KeyPressingE:
- ChechExit(ref isWork);
- break;
- case KeyPressingEscape:
- isWork = false;
- break;
- }
- }
- }
- }
- private void ChechExit(ref bool isWork)
- {
- string exit = "xit";
- Console.Write("E");
- string input = Console.ReadLine();
- if (input.ToLower() == exit)
- {
- isWork = false;
- }
- }
- private void Display()
- {
- if(_items.Count > 0)
- {
- Console.Clear();
- Console.WriteLine("Управление ▲, ▼, Enter, Esc, Exit.");
- for (int i = 0; i < _items.Count; i++)
- {
- int isequenceNumber = i + 1;
- Product currentItem = _items[i];
- Console.ForegroundColor = i == _selectedItemIndex ? ConsoleColor.Black : ConsoleColor.Gray;
- Console.BackgroundColor = i == _selectedItemIndex ? ConsoleColor.Gray : ConsoleColor.Black;
- Console.WriteLine($"{isequenceNumber}. {currentItem.Name}, Цена: {currentItem.Price}");
- Console.ResetColor();
- }
- }
- else
- {
- Console.WriteLine("Товары закончились");
- }
- }
- private void MoveUp()
- {
- _selectedItemIndex--;
- if (_selectedItemIndex < 0)
- {
- _selectedItemIndex = _items.Count - 1;
- }
- }
- private void MoveDown()
- {
- _selectedItemIndex++;
- if (_selectedItemIndex >= _items.Count)
- {
- _selectedItemIndex = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement