Advertisement
dmitryEfremov

Untitled

Aug 15th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Design;
  4. using System.Data;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Reflection.Metadata;
  8.  
  9. namespace ConsoleApp3
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Buyer buyer = new Buyer();
  16. Seller seller = new Seller();
  17. while (true)
  18. {
  19. Console.ForegroundColor = ConsoleColor.Green;
  20. Console.SetCursorPosition(45, 0);
  21. Console.WriteLine(buyer.ShowCash());
  22. Console.SetCursorPosition(0, 0);
  23. Console.ForegroundColor = ConsoleColor.White;
  24. Console.WriteLine("<<<Добро пожаловать в магазин>>>");
  25. Console.WriteLine("Нажмите 1 что бы посмотреть список доступных для покупки товаров");
  26. Console.WriteLine("Нажмите 2 что бы купить товар");
  27. Console.WriteLine("Нажмите 3 что бы посмотреть инвентарь");
  28. Console.WriteLine("Нажмите 4 что бы узнать кассу в магазине\n");
  29. Console.Write("Выберите функцию: ");
  30. switch (Convert.ToInt32(Console.ReadLine()))
  31. {
  32. case 1:
  33. seller.ShowProducts();
  34. break;
  35. case 2:
  36. Console.Write("Напишите название товара: ");
  37. string value = Console.ReadLine();
  38. if(buyer.CheckSolvency(seller, value))
  39. {
  40. buyer.InventoryAdds(seller.SellProduct(value, buyer.Pay()));
  41. }
  42. else
  43. {
  44. Console.WriteLine("Недостаточно денег");
  45. }
  46. break;
  47. case 3:
  48. buyer.ShowInventory();
  49. break;
  50. case 4:
  51. seller.ShowCash();
  52. break;
  53. default:
  54. Console.WriteLine("Вы ввели неверную функцию, повторите попытку...");
  55. break;
  56. }
  57. Console.ReadLine();
  58. Console.Clear();
  59. }
  60. }
  61. }
  62.  
  63. class Buyer
  64. {
  65. Seller seller;
  66. private List<Product> _inventory = new List<Product>();
  67. private int _money;
  68. private int _moneyToPay;
  69.  
  70. public Buyer()
  71. {
  72. Random rnd = new Random();
  73. _money = rnd.Next(1000, 1500);
  74. }
  75.  
  76. public int ShowCash()
  77. {
  78. return _money;
  79. }
  80.  
  81. public bool CheckSolvency(Seller seller, string product)
  82. {
  83. _moneyToPay = seller.ShowPrice(product);
  84. if (_money >= _moneyToPay)
  85. {
  86. return true;
  87. }
  88. else
  89. {
  90. _moneyToPay = 0;
  91. return false;
  92. }
  93. }
  94.  
  95. public int Pay()
  96. {
  97. _money -= _moneyToPay;
  98. return _moneyToPay;
  99. }
  100.  
  101. public void InventoryAdds(Product item)
  102. {
  103. _inventory.Add(item);
  104. }
  105.  
  106. public void ShowInventory()
  107. {
  108. foreach (var item in _inventory)
  109. {
  110. Console.WriteLine(item._product);
  111. }
  112. }
  113. }
  114.  
  115. class Seller
  116. {
  117. private Dictionary<string, Product> _products = new Dictionary<string, Product>
  118. {
  119. { "Молоко", new Product("Молоко", 50) },
  120. { "Сигареты", new Product("Сигареты", 50) },
  121. { "Сахар", new Product("Сахар", 50) },
  122. { "Соль", new Product("Соль", 50) }
  123. };
  124. private int _money;
  125.  
  126. public void ShowCash()
  127. {
  128. Console.WriteLine("Касса продуктового магазина - " + _money);
  129. }
  130.  
  131. public int ShowPrice(string product)
  132. {
  133. return _products[product]._priсe;
  134. }
  135.  
  136. public void ShowProducts()
  137. {
  138. foreach (var product in _products)
  139. {
  140. Console.WriteLine($"{product.Value._priсe} - {product.Value._product}");
  141. }
  142. }
  143.  
  144. public Product SellProduct(string product, int moneyToPay)
  145. {
  146. _money += moneyToPay;
  147. var value = _products[product];
  148. _products.Remove(product);
  149. return value;
  150. }
  151. }
  152.  
  153. class Product
  154. {
  155. public string _product { get; private set; }
  156. public int _priсe { get; private set; }
  157.  
  158. public Product(string product, int priсe)
  159. {
  160. _product = product;
  161. _priсe = priсe;
  162. }
  163. }
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement