Advertisement
Rodunskiy

Untitled

Aug 2nd, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. class Program
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         const string ShowInventorySellerCommand = "1";
  8.         const string BuyProductCommand = "2";
  9.         const string ShowInventoryPlayerCommand = "3";
  10.         const string IsExitCommand = "4";
  11.  
  12.         string userInput;
  13.         bool isWorking = true;
  14.  
  15.         Seller seller = new Seller();
  16.         Player player = new Player();
  17.  
  18.         while(isWorking)
  19.         {
  20.             Console.Clear();
  21.             Console.WriteLine("Вы зашли в лавку торговца.");
  22.             Console.WriteLine($"{ShowInventorySellerCommand})Посмотреть товар.\n{BuyProductCommand})Купить товар.\n" +
  23.                 $"{ShowInventoryPlayerCommand})Посмотреть свой инвентарь.\n{IsExitCommand})Покинуть лавку.");
  24.             userInput = Console.ReadLine();
  25.             Console.Clear();
  26.  
  27.             switch (userInput)
  28.             {
  29.                 case ShowInventorySellerCommand:
  30.                     seller.ShowInventory();
  31.                     break;
  32.  
  33.                 case BuyProductCommand:
  34.                     player.BuyProduct(seller.SellProduct());
  35.                     break;
  36.  
  37.                 case ShowInventoryPlayerCommand:
  38.                     player.ShowInventory();
  39.                     break;
  40.  
  41.                 case IsExitCommand:
  42.                     isWorking = false;
  43.                     break;
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. class Human
  50. {
  51.     protected List<Product> Products = new List<Product>();
  52.  
  53.     public void ShowInventory()
  54.     {
  55.         foreach (Product product in Products)
  56.         {
  57.             Console.WriteLine($"Название:{product.Name}|Цена:{product.Price}|Урон:{product.Damage}|Прочность:{product.Endurance}");
  58.         }
  59.  
  60.         Console.ReadKey();
  61.     }
  62. }
  63.  
  64. class Seller : Human
  65. {
  66.     private Product _product;
  67.  
  68.     public Seller()
  69.     {
  70.         Products.AddRange(new List<Product>() { new Product("WoodKnife", 5, 25, 100), new Product("IronKnife", 15, 50, 150), new Product("GoldKnife", 30, 100, 25) });
  71.     }
  72.     public Product SellProduct()
  73.     {
  74.         Product product = null;
  75.  
  76.         if (Products.Count <= 0)
  77.         {
  78.             Console.WriteLine("У торговца закончился товар.");
  79.             Console.ReadKey();
  80.         }
  81.         else
  82.         {
  83.             Console.WriteLine("Что бы вы хотели купить?");
  84.             string userInput = Console.ReadLine();
  85.  
  86.             if (IsAvailable(userInput))
  87.             {
  88.                 foreach (var item in Products)
  89.                 {
  90.                     if (item.Name == userInput)
  91.                     {
  92.                         product = item;
  93.                         break;
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.  
  99.         Products.Remove(product);
  100.  
  101.         return product;
  102.     }
  103.  
  104.     public bool IsAvailable(string productName)
  105.     {
  106.         foreach (var product in Products)
  107.         {
  108.             if (product.Name == productName)
  109.             {
  110.                 return true;
  111.                 break;
  112.             }
  113.         }
  114.  
  115.         Console.WriteLine("Такого товара нет в наличии!");
  116.         Console.ReadKey();
  117.  
  118.         return false;
  119.     }
  120. }
  121.  
  122. class Player : Human
  123. {
  124.     public void BuyProduct(Product product)
  125.     {
  126.         if (product != null)
  127.         {
  128.             Products.Add(product);
  129.         }
  130.     }
  131. }
  132.  
  133.  
  134. class Product
  135. {
  136.     public Product (string name,int price, int damage, int endurance)
  137.     {
  138.         Name = name;
  139.         Price = price;
  140.         Damage = damage;
  141.         Endurance = endurance;
  142.     }
  143.  
  144.     public string Name { get; private set; }
  145.     public int Price { get; private set; }
  146.     public int Damage { get; private set; }
  147.     public int Endurance { get; private set; }
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement