Advertisement
Rodunskiy

Untitled

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