Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Trade
- {
- class Program
- {
- static void Main(string[] args)
- {
- string userInput;
- Seller seller = new Seller();
- Player player = new Player(1000);
- seller.FillInventory();
- do
- {
- Console.WriteLine(" 1 - Посмотреть товары\n 2 - Купить товар\n 3 - Посмотреть свой инвентарь");
- userInput = Console.ReadLine();
- Console.Clear();
- switch (userInput)
- {
- case "1":
- Console.WriteLine("Имеющиеся товары:");
- seller.ShowInventory();
- break;
- case "2":
- Item item;
- Console.WriteLine("Введите название товара для покупки:");
- userInput = Console.ReadLine();
- item = seller.GetItem(userInput);
- if (item != null && player.IsEnenoughMoney(item))
- {
- player.BuyItem(item);
- seller.SellItem(item);
- }
- break;
- case "3":
- Console.WriteLine("Ваш инвентарь:");
- player.ShowInventory();
- break;
- default:
- Console.WriteLine("Такого действия нет");
- break;
- }
- Console.WriteLine("Нажмите любую кнопку для продолжения | Для выхода нажмите ESC");
- }
- while (Console.ReadKey().Key != ConsoleKey.Escape);
- }
- }
- class Person
- {
- protected Inventory Inventory;
- public Person()
- {
- Inventory = new Inventory();
- }
- public virtual void ShowInventory()
- {
- Inventory.ShowItems();
- }
- }
- class Player : Person
- {
- private int _money;
- public Player(int money)
- {
- _money = money;
- }
- public bool IsEnenoughMoney(Item item)
- {
- if(item.Price <= _money)
- {
- return true;
- }
- Console.WriteLine("У вас недостаточно денег");
- return false;
- }
- public void BuyItem(Item item)
- {
- if (item != null)
- {
- _money -= item.Price;
- Inventory.AddItem(item);
- }
- }
- public override void ShowInventory()
- {
- Inventory.ShowItems();
- Console.WriteLine($"Ваш Баланс: {_money}");
- }
- }
- class Seller : Person
- {
- public Item GetItem(string name)
- {
- if(Inventory.TryGetItem(name, out Item item) == false)
- {
- Console.WriteLine("Такого предмета нет");
- }
- return item;
- }
- public void SellItem(Item item)
- {
- Inventory.RemoveItem(item);
- }
- public void FillInventory()
- {
- Inventory.AddItem("1", 100);
- Inventory.AddItem("2", 200);
- Inventory.AddItem("3", 300);
- Inventory.AddItem("4", 400);
- Inventory.AddItem("5", 500);
- }
- }
- class Inventory
- {
- private List<Item>_items;
- public Inventory()
- {
- _items = new List<Item>();
- }
- public void ShowItems()
- {
- foreach (Item item in _items)
- {
- item.ShowInfo();
- }
- }
- public void AddItem(string name, int price)
- {
- Item item = new Item(name, price);
- _items.Add(item);
- }
- public void AddItem(Item item)
- {
- _items.Add(item);
- }
- public void RemoveItem(Item item)
- {
- _items.Remove(item);
- }
- public bool TryGetItem(string name, out Item item)
- {
- for (int i = 0; i < _items.Count; i++)
- {
- if (_items[i].Name.Equals(name))
- {
- item = _items[i];
- return true;
- }
- }
- item = null;
- return false;
- }
- }
- class Item
- {
- public string Name { get; private set; }
- public int Price { get; private set; }
- public Item(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public void ShowInfo()
- {
- Console.WriteLine($"{Name} | Цена - {Price}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment