Advertisement
Torgach

magazine

Apr 1st, 2021
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace magazine
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             bool isWork = true;
  14.  
  15.             Trader trader = new Trader();
  16.  
  17.             Player player = new Player(1000);
  18.  
  19.             while (isWork)
  20.             {
  21.                 player.ShowBag();
  22.  
  23.                 Console.WriteLine("\n[1] - Показать список товаров;\n" +
  24.                 "[2] - Купить товар;\n" +
  25.                 "[3] - Выйти из магазина");
  26.                 Console.Write("Ввод: ");
  27.  
  28.                 switch (Console.ReadLine())
  29.                 {
  30.                     case "1":
  31.                         trader.ShowGoods();
  32.                         break;
  33.                     case "2":
  34.                         trader.Trade(player);
  35.                         break;
  36.                     case "3":
  37.                         isWork = false;
  38.                         break;
  39.                 }
  40.             }
  41.         }
  42.     }
  43.  
  44.     class Trader
  45.     {
  46.         private int _cashbox;
  47.         private List<Goods> _goods = new List<Goods>();
  48.  
  49.         public Trader()
  50.         {
  51.             Random rand = new Random();
  52.  
  53.             for (int i = 0; i < 15; i++)
  54.             {
  55.                 _goods.Add(new Goods(rand.Next(0, 1000), rand.Next(0, 1000)));
  56.             }
  57.         }
  58.  
  59.         public void Trade(Player player)
  60.         {
  61.             Console.Write("Выберите товар из списка: ");
  62.             if (int.TryParse(Console.ReadLine(), out int goodsNumber)
  63.                 && (goodsNumber >= 0 && goodsNumber < _goods.Count))
  64.             {
  65.                 --goodsNumber;
  66.  
  67.                 if (player.CheckMoney(_goods[goodsNumber]))
  68.                 {
  69.                     Console.WriteLine("Оплачиваем товар...");
  70.                     _cashbox += player.ToPay(_goods[goodsNumber]);
  71.                     _goods.RemoveAt(goodsNumber);
  72.                 }
  73.                 else
  74.                 {
  75.                     Console.WriteLine("У вас нет денег на товар!");
  76.                     return;
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 Console.WriteLine("Товара нет в списке!");
  82.                 return;
  83.             }
  84.         }
  85.  
  86.         public void ShowGoods()
  87.         {
  88.             Console.WriteLine("\nСписок товаров: ");
  89.             for (int i = 0; i < _goods.Count; i++)
  90.             {
  91.                 Console.WriteLine($"[№{i + 1}]: #{_goods[i].ProductCode} - ${_goods[i].Price}");
  92.             }
  93.         }
  94.     }
  95.  
  96.     class Goods
  97.     {
  98.         public int ProductCode { get; private set; }
  99.         public int Price { get; private set; }
  100.  
  101.         public Goods(int productCode, int price)
  102.         {
  103.             ProductCode = productCode;
  104.             Price = price;
  105.         }
  106.     }
  107.  
  108.     class Player
  109.     {
  110.         private int _money;
  111.  
  112.         private List<Goods> _shopingBag = new List<Goods>();
  113.  
  114.         public Player(int money)
  115.         {
  116.             _money = money;
  117.         }
  118.  
  119.         public bool CheckMoney(Goods goods)
  120.         {
  121.             return _money >= goods.Price;
  122.         }
  123.  
  124.         public int ToPay(Goods goods)
  125.         {
  126.             _money -= goods.Price;
  127.             _shopingBag.Add(goods);
  128.             return goods.Price;
  129.         }
  130.  
  131.         public void ShowBag()
  132.         {
  133.             Console.WriteLine($"\nКошелек: ${_money}");
  134.  
  135.             if (_shopingBag.Count != 0)
  136.             {
  137.                 Console.WriteLine("Корзина: ");
  138.                 foreach (var goods in _shopingBag)
  139.                 {
  140.                     Console.WriteLine($"Товар с номером: {goods.ProductCode}");
  141.                 }
  142.             }
  143.             else
  144.             {
  145.                 Console.WriteLine("Корзина пуста!");
  146.             }
  147.         }
  148.     }
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement