SaNik74

Shop2

Nov 18th, 2023 (edited)
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.20 KB | None | 0 0
  1. namespace BuyerAndSeller;
  2.  
  3. class Program
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         const string ShowProductCommand = "1";
  8.         const string SellCommand = "2";
  9.         const string ShowBagCommand = "3";
  10.         const string ExitCommand = "4";
  11.  
  12.         bool isActive = true;
  13.  
  14.         Buyer buyer = new Buyer();
  15.         Seller seller = new Seller();
  16.  
  17.         while (isActive)
  18.         {
  19.             buyer.ShowMoney();
  20.  
  21.             Console.WriteLine("Магазин\n");
  22.             Console.WriteLine($"{ShowProductCommand} - Показать весь ассортимент товаров");
  23.             Console.WriteLine($"{SellCommand} - Купить товар");
  24.             Console.WriteLine($"{ShowBagCommand} - Посмотреть список купленных товаров");
  25.             Console.WriteLine($"{ExitCommand} - Выход\n");
  26.  
  27.             string userInput = Console.ReadLine();
  28.  
  29.             switch (userInput)
  30.             {
  31.                 case ShowProductCommand:
  32.                     seller.ShowProducts();
  33.                     break;
  34.                 case SellCommand:
  35.                     seller.Sell(buyer);
  36.                     break;
  37.                 case ShowBagCommand:
  38.                     buyer.ShowBag();
  39.                     break;
  40.                 case ExitCommand:
  41.                     isActive = false;
  42.                     Console.WriteLine("\nВыход из магазина.\n");
  43.                     break;
  44.                 default:
  45.                     Console.WriteLine("Такой команды нет.\n");
  46.                     break;
  47.             }
  48.  
  49.             Console.ReadKey(true);
  50.             Console.Clear();
  51.         }
  52.     }
  53. }
  54.  
  55. class Product
  56. {
  57.     public Product(string name, int price)
  58.     {
  59.         Name = name;
  60.         Price = price;
  61.     }
  62.  
  63.     public string Name { get; private set; }
  64.     public int Price { get; private set; }
  65. }
  66.  
  67. class Cell
  68. {
  69.     public Cell(Product product, int amount)
  70.     {
  71.         Product = product;
  72.         Amount = amount;
  73.     }
  74.  
  75.     public Product Product { get; private set; }
  76.     public int Amount { get; private set; }
  77.  
  78.     public void DecreaseAmount(int amount)
  79.     {
  80.         Amount -= amount;
  81.     }
  82. }
  83.  
  84. class Goods
  85. {
  86.     public List<Cell> cell = new List<Cell>();
  87.     public Dictionary<string, int> bag = new Dictionary<string, int>();
  88.     public int money = 4000;
  89.  
  90.     public void ShowProducts()
  91.     {
  92.         foreach (var item in cell)
  93.         {
  94.             Console.WriteLine($"{item.Product.Name} - {item.Amount}шт.\t{item.Product.Price}");
  95.         }
  96.     }
  97.  
  98.     public void ShowBag()
  99.     {
  100.         foreach (var item in bag)
  101.         {
  102.             Console.WriteLine($"{item.Key}  -  {item.Value} шт.");
  103.         }
  104.     }
  105. }
  106.  
  107. class Seller : Goods
  108. {
  109.     public Seller()
  110.     {
  111.         cell.Add(new Cell(new Product("Мана", 12), 67));
  112.         cell.Add(new Cell(new Product("Хилка", 18), 89));
  113.         cell.Add(new Cell(new Product("Берсерк", 60), 5));
  114.         cell.Add(new Cell(new Product("Меч", 1000), 2));
  115.     }
  116.  
  117.     public void Sell(Buyer buyer)
  118.     {
  119.         string productName;
  120.         int amount;
  121.  
  122.         ShowProducts();
  123.         Console.Write("Введите название товара, который хотите приобрести: ");
  124.         productName = Console.ReadLine();
  125.  
  126.         if (TryToFindProduct(productName, out Cell product))
  127.         {
  128.             Console.Write("Введите желаемое количество товара: ");
  129.             amount = ReadInt();
  130.  
  131.             if (amount > product.Amount)
  132.             {
  133.                 Console.WriteLine("В магазине недостаточно данного товара.");
  134.                 Console.WriteLine("Вы можете ввести другое количество или выбрать другой товар.");
  135.             }
  136.             else
  137.             {
  138.                 if (buyer.Buy(product.Product, amount) == true)
  139.                 {
  140.                     product.DecreaseAmount(amount);
  141.                 }
  142.             }
  143.         }
  144.     }
  145.  
  146.     private int ReadInt()
  147.     {
  148.         int result;
  149.  
  150.         while (int.TryParse(Console.ReadLine(), out result) == false)
  151.         {
  152.             Console.WriteLine("Необходимо ввести целое число.\n");
  153.             Console.Write("Введи целое число: ");
  154.         }
  155.  
  156.         return result;
  157.     }
  158.  
  159.     private bool TryToFindProduct(string title, out Cell findItem)
  160.     {
  161.         findItem = null;
  162.  
  163.         foreach (var item in cell)
  164.         {
  165.             if (item.Product.Name.ToLower() == title.ToLower())
  166.             {
  167.                 findItem = item;
  168.                 return true;
  169.             }
  170.         }
  171.  
  172.         Console.WriteLine("Такого товара нет. Повторите поиск.");
  173.         return false;
  174.     }
  175. }
  176.  
  177. class Buyer : Goods
  178. {
  179.     public bool Buy(Product product, int amount)
  180.     {
  181.         int moneyNeedForBuy = product.Price * amount;
  182.  
  183.         if (money == 0)
  184.         {
  185.             Console.WriteLine("У вас закончились деньги");
  186.             return false;
  187.         }
  188.         else if (moneyNeedForBuy > money)
  189.         {
  190.             Console.WriteLine("Недостаточно денег для покупки.");
  191.             return false;
  192.         }
  193.         else
  194.         {
  195.             money -= moneyNeedForBuy;
  196.             AddToBag(product, amount);
  197.             Console.WriteLine("Товар добавлен в сумку.");
  198.             return true;
  199.         }
  200.     }
  201.  
  202.     public void AddToBag(Product product, int amount)
  203.     {
  204.         if (bag.ContainsKey(product.Name) == false || bag.Count == 0)
  205.         {
  206.             bag.Add(product.Name, amount);
  207.         }
  208.         else
  209.         {
  210.             foreach (var item in bag)
  211.             {
  212.                 if (item.Key == product.Name)
  213.                 {
  214.                     bag[product.Name] += amount;
  215.                 }
  216.             }
  217.         }
  218.     }
  219.  
  220.     public void ShowMoney()
  221.     {
  222.         Console.SetCursorPosition(0, 30);
  223.         Console.WriteLine($"У вас есть - {money} монет.");
  224.         Console.SetCursorPosition(0, 0);
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment