Advertisement
Rodunskiy

Untitled

Aug 10th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.22 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.         _seller.TrySearch(userInput, out Product selectedProduct);
  73.  
  74.         if(selectedProduct != null)
  75.         {
  76.             if (_player.IsEnoughMoney(selectedProduct))
  77.             {
  78.                 _seller.GiveProduct(selectedProduct);
  79.                 _player.TakeProduct(selectedProduct);
  80.             }
  81.         }
  82.     }
  83. }
  84.  
  85. class Human
  86. {
  87.     protected List<Product> Products = new List<Product>();
  88.  
  89.     protected int Money = 0;
  90.  
  91.     public void ShowInventory()
  92.     {
  93.         foreach (Product product in Products)
  94.         {
  95.             Console.WriteLine($"Название:{product.Name}|Цена:{product.Price}|Урон:{product.Damage}|Прочность:{product.Endurance}");
  96.         }
  97.  
  98.         Console.ReadKey();
  99.     }
  100. }
  101.  
  102. class Seller : Human
  103. {
  104.     public Seller()
  105.     {
  106.         FillProduct();
  107.     }
  108.  
  109.     public void FillProduct()
  110.     {
  111.         Products.Add(new Product("WoodKnife", 5, 25, 100));
  112.         Products.Add(new Product("IronKnife", 15, 50, 150));
  113.         Products.Add(new Product("GoldKnife", 30, 100, 25));
  114.     }
  115.  
  116.     public void GiveProduct(Product product)
  117.     {
  118.         Products.Remove(product);
  119.         Money += product.Price;
  120.     }
  121.  
  122.     public void TrySearch(string productName, out Product selectedProduct)
  123.     {
  124.         Product temporaryProduct = null;
  125.  
  126.         foreach (var product in Products)
  127.         {
  128.             if (product.Name == productName)
  129.             {
  130.                 temporaryProduct = product;
  131.  
  132.                 Console.WriteLine("Такой товар есть в наличии!");
  133.                 Console.ReadKey();
  134.                 break;
  135.             }
  136.             else
  137.             {
  138.                 selectedProduct = null;
  139.  
  140.                 Console.WriteLine("Такого товара нет в наличии!");
  141.                 Console.ReadKey();
  142.                 break;
  143.             }
  144.         }
  145.  
  146.         selectedProduct = temporaryProduct;
  147.     }
  148. }
  149.  
  150. class Player : Human
  151. {
  152.     private Random _random = new Random();
  153.  
  154.     public Player()
  155.     {
  156.         Money = CreateMoney();
  157.     }
  158.  
  159.     private int CreateMoney()
  160.     {
  161.         int minMoney = 5;
  162.         int maxMoney = 30;
  163.  
  164.         int temporaryMoney = _random.Next(minMoney, maxMoney);
  165.  
  166.         return temporaryMoney;
  167.     }
  168.  
  169.     public bool IsEnoughMoney(Product product)
  170.     {
  171.         if (Money - product.Price < 0)
  172.         {
  173.             Console.WriteLine("У игрока недостаточно денег. Пускай покупает что-то другое или сваливает.");
  174.             Console.ReadKey();
  175.  
  176.             return false;
  177.         }
  178.  
  179.         return true;
  180.     }
  181.  
  182.     public void TakeProduct(Product product)
  183.     {
  184.         Products.Add(product);
  185.         Money -= product.Price;
  186.     }
  187.  
  188.     public int ShowMoney()
  189.     {
  190.         return Money;
  191.     }
  192. }
  193.  
  194.  
  195. class Product
  196. {
  197.     public Product (string name,int price, int damage, int endurance)
  198.     {
  199.         Name = name;
  200.         Price = price;
  201.         Damage = damage;
  202.         Endurance = endurance;
  203.     }
  204.  
  205.     public string Name { get; private set; }
  206.     public int Price { get; private set; }
  207.     public int Damage { get; private set; }
  208.     public int Endurance { get; private set; }
  209. }
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement