Advertisement
Rodunskiy

Untitled

Aug 10th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Policy;
  4.  
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         Seller seller = new Seller();
  10.         Player player = new Player();
  11.         Shop shop = new Shop(player, seller);
  12.  
  13.         shop.Work();
  14.     }
  15. }
  16.  
  17. class Shop
  18. {
  19.     private Player _player = null;
  20.     private Seller _seller = null;
  21.  
  22.     public Shop(Player player, Seller seller)
  23.     {
  24.         _player = player;
  25.         _seller = seller;
  26.     }
  27.  
  28.     public void Work()
  29.     {
  30.         const string ShowInventorySellerCommand = "1";
  31.         const string BuyProductCommand = "2";
  32.         const string ShowInventoryPlayerCommand = "3";
  33.         const string IsExitCommand = "4";
  34.  
  35.         string userInput;
  36.         bool isWorking = true;
  37.  
  38.         while (isWorking)
  39.         {
  40.             Console.Clear();
  41.             Console.WriteLine($"Игрок зашёл в лавку торговца. У него {_player.ShowMoney()} монет.\n{ShowInventorySellerCommand})Посмотреть товар.\n{BuyProductCommand})Купить товар.\n" +
  42.                 $"{ShowInventoryPlayerCommand})Посмотреть свой инвентарь.\n{IsExitCommand})Покинуть лавку.");
  43.             userInput = Console.ReadLine();
  44.             Console.Clear();
  45.  
  46.             switch (userInput)
  47.             {
  48.                 case ShowInventorySellerCommand:
  49.                     _seller.ShowInventory();
  50.                     break;
  51.  
  52.                 case BuyProductCommand:
  53.                     SaleProduct();
  54.                     break;
  55.  
  56.                 case ShowInventoryPlayerCommand:
  57.                     _player.ShowInventory();
  58.                     break;
  59.  
  60.                 case IsExitCommand:
  61.                     isWorking = false;
  62.                     break;
  63.             }
  64.         }
  65.     }
  66.  
  67.     public void SaleProduct()
  68.     {
  69.         Console.WriteLine("Что бы вы хотели купить?");
  70.         string userInput = Console.ReadLine();
  71.  
  72.         if(_seller.TrySearch(userInput, out Product selectedProduct))
  73.         {
  74.             if (_player.IsEnoughMoney(selectedProduct))
  75.             {
  76.                 _seller.GiveProduct(selectedProduct);
  77.                 _player.TakeProduct(selectedProduct);
  78.             }
  79.         }
  80.     }
  81. }
  82.  
  83. class Human
  84. {
  85.     protected List<Product> Products = new List<Product>();
  86.  
  87.     protected int Money = 0;
  88.  
  89.     public void ShowInventory()
  90.     {
  91.         foreach (Product product in Products)
  92.         {
  93.             Console.WriteLine($"Название:{product.Name}|Цена:{product.Price}|Урон:{product.Damage}|Прочность:{product.Endurance}");
  94.         }
  95.  
  96.         Console.ReadKey();
  97.     }
  98. }
  99.  
  100. class Seller : Human
  101. {
  102.     public Seller()
  103.     {
  104.         FillProduct();
  105.     }
  106.  
  107.     public void FillProduct()
  108.     {
  109.         Products.Add(new Product("WoodKnife", 5, 25, 100));
  110.         Products.Add(new Product("IronKnife", 15, 50, 150));
  111.         Products.Add(new Product("GoldKnife", 30, 100, 25));
  112.     }
  113.  
  114.     public void GiveProduct(Product product)
  115.     {
  116.         Products.Remove(product);
  117.         Money += product.Price;
  118.     }
  119.  
  120.     public bool TrySearch(string productName, out Product selectedProduct)
  121.     {
  122.         foreach (var product in Products)
  123.         {
  124.             if (product.Name == productName)
  125.             {
  126.                 selectedProduct = product;
  127.  
  128.                 Console.WriteLine("Такой товар есть в наличии!");
  129.                 Console.ReadKey();
  130.                
  131.                 return true;
  132.             }
  133.         }
  134.  
  135.         selectedProduct = null;
  136.  
  137.         Console.WriteLine("Такого товара нет в наличии!");
  138.         Console.ReadKey();
  139.  
  140.         return false;
  141.     }
  142. }
  143.  
  144. class Player : Human
  145. {
  146.     private Random _random = new Random();
  147.  
  148.     public Player()
  149.     {
  150.         Money = CreateMoney();
  151.     }
  152.  
  153.     private int CreateMoney()
  154.     {
  155.         int minMoney = 5;
  156.         int maxMoney = 30;
  157.  
  158.         int temporaryMoney = _random.Next(minMoney, maxMoney);
  159.  
  160.         return temporaryMoney;
  161.     }
  162.  
  163.     public bool IsEnoughMoney(Product product)
  164.     {
  165.         if (Money - product.Price < 0)
  166.         {
  167.             Console.WriteLine("У игрока недостаточно денег. Пускай покупает что-то другое или сваливает.");
  168.             Console.ReadKey();
  169.  
  170.             return false;
  171.         }
  172.  
  173.         return true;
  174.     }
  175.  
  176.     public void TakeProduct(Product product)
  177.     {
  178.         Products.Add(product);
  179.         Money -= product.Price;
  180.     }
  181.  
  182.     public int ShowMoney()
  183.     {
  184.         return Money;
  185.     }
  186. }
  187.  
  188.  
  189. class Product
  190. {
  191.     public Product (string name,int price, int damage, int endurance)
  192.     {
  193.         Name = name;
  194.         Price = price;
  195.         Damage = damage;
  196.         Endurance = endurance;
  197.     }
  198.  
  199.     public string Name { get; private set; }
  200.     public int Price { get; private set; }
  201.     public int Damage { get; private set; }
  202.     public int Endurance { get; private set; }
  203. }
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement