Advertisement
Rodunskiy

Untitled

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