Askor

Hw31

Nov 13th, 2021 (edited)
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Trade
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string userInput;
  14.             Seller seller = new Seller();
  15.             Player player = new Player(1000);
  16.  
  17.             seller.FillInventory();
  18.  
  19.             do
  20.             {
  21.                 Console.WriteLine(" 1 - Посмотреть товары\n 2 - Купить товар\n 3 - Посмотреть свой инвентарь");
  22.                 userInput = Console.ReadLine();
  23.                 Console.Clear();
  24.  
  25.                 switch (userInput)
  26.                 {
  27.                     case "1":
  28.                         Console.WriteLine("Имеющиеся товары:");
  29.                         seller.ShowInventory();
  30.                         break;
  31.                     case "2":
  32.                         Item item;
  33.                         Console.WriteLine("Введите название товара для покупки:");
  34.                         userInput = Console.ReadLine();
  35.                         item = seller.GetItem(userInput);
  36.  
  37.                         if (item != null && player.IsEnenoughMoney(item))
  38.                         {
  39.                             player.BuyItem(item);
  40.                             seller.SellItem(item);
  41.                         }
  42.                         break;
  43.                     case "3":
  44.                         Console.WriteLine("Ваш инвентарь:");
  45.                         player.ShowInventory();
  46.                         break;
  47.                     default:
  48.                         Console.WriteLine("Такого действия нет");
  49.                         break;
  50.                 }
  51.  
  52.                 Console.WriteLine("Нажмите любую кнопку для продолжения | Для выхода нажмите ESC");
  53.             }
  54.             while (Console.ReadKey().Key != ConsoleKey.Escape);
  55.         }
  56.     }
  57.  
  58.     class Person
  59.     {
  60.         protected Inventory Inventory;
  61.  
  62.         public Person()
  63.         {
  64.             Inventory = new Inventory();
  65.         }
  66.  
  67.         public virtual void ShowInventory()
  68.         {
  69.             Inventory.ShowItems();
  70.         }
  71.     }
  72.  
  73.     class Player : Person
  74.     {
  75.         private int _money;
  76.  
  77.         public Player(int money)
  78.         {
  79.             _money = money;
  80.         }
  81.  
  82.         public bool IsEnenoughMoney(Item item)
  83.         {
  84.             if(item.Price <= _money)
  85.             {
  86.                 return true;
  87.             }
  88.  
  89.             Console.WriteLine("У вас недостаточно денег");
  90.             return false;
  91.         }
  92.         public void BuyItem(Item item)
  93.         {
  94.             if (item != null)
  95.             {
  96.                 _money -= item.Price;
  97.                 Inventory.AddItem(item);
  98.             }
  99.         }
  100.  
  101.         public override void ShowInventory()
  102.         {
  103.             Inventory.ShowItems();
  104.             Console.WriteLine($"Ваш Баланс: {_money}");
  105.         }
  106.     }
  107.  
  108.     class Seller : Person
  109.     {
  110.  
  111.         public Item GetItem(string name)
  112.         {
  113.             if(Inventory.TryGetItem(name, out Item item) == false)
  114.             {
  115.                 Console.WriteLine("Такого предмета нет");
  116.             }
  117.  
  118.             return item;
  119.         }
  120.         public void SellItem(Item item)
  121.         {
  122.             Inventory.RemoveItem(item);
  123.         }
  124.  
  125.         public void FillInventory()
  126.         {
  127.             Inventory.AddItem("1", 100);
  128.             Inventory.AddItem("2", 200);
  129.             Inventory.AddItem("3", 300);
  130.             Inventory.AddItem("4", 400);
  131.             Inventory.AddItem("5", 500);
  132.         }
  133.     }
  134.  
  135.     class Inventory
  136.     {
  137.         private List<Item>_items;
  138.  
  139.         public Inventory()
  140.         {
  141.             _items = new List<Item>();
  142.         }
  143.  
  144.         public void ShowItems()
  145.         {
  146.             foreach (Item item in _items)
  147.             {
  148.                 item.ShowInfo();
  149.             }
  150.         }
  151.  
  152.         public void AddItem(string name, int price)
  153.         {
  154.             Item item = new Item(name, price);
  155.             _items.Add(item);
  156.         }
  157.  
  158.         public void AddItem(Item item)
  159.         {
  160.             _items.Add(item);
  161.         }
  162.  
  163.         public void RemoveItem(Item item)
  164.         {
  165.             _items.Remove(item);
  166.         }
  167.  
  168.         public bool TryGetItem(string name, out Item item)
  169.         {
  170.             for (int i = 0; i < _items.Count; i++)
  171.             {
  172.                 if (_items[i].Name.Equals(name))
  173.                 {
  174.                     item = _items[i];
  175.                     return true;
  176.                 }
  177.             }
  178.  
  179.             item = null;
  180.             return false;
  181.         }
  182.  
  183.     }
  184.  
  185.     class Item
  186.     {
  187.         public string Name { get; private set; }
  188.         public int Price { get; private set; }
  189.  
  190.         public Item(string name, int price)
  191.         {
  192.             Name = name;
  193.             Price = price;
  194.         }
  195.  
  196.         public void ShowInfo()
  197.         {
  198.             Console.WriteLine($"{Name} | Цена - {Price}");
  199.         }
  200.     }
  201. }
  202.  
Advertisement
Add Comment
Please, Sign In to add comment