Advertisement
ranee

Магазин

Mar 25th, 2023
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.30 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Shop
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             const string CommandShowSellerGoods = "1";
  10.             const string CommandSellGoods = "2";
  11.             const string CommandShowPlayerInventory = "3";
  12.             const string CommandExit = "4";
  13.  
  14.             bool isWork = true;
  15.             Seller seller = new Seller();
  16.             Player player = new Player();
  17.             Menu menu = new Menu(seller.GetProducts());
  18.  
  19.             while (isWork)
  20.             {
  21.                 Console.Clear();
  22.                 Console.WriteLine($"{CommandShowSellerGoods}. Показать список товаров." +
  23.                     $"\n{CommandSellGoods}. Продать товар." +
  24.                     $"\n{CommandShowPlayerInventory}. Показать сумку покупателя." +
  25.                     $"\n{CommandExit}. Выйти");
  26.  
  27.                 switch (Console.ReadLine())
  28.                 {
  29.                     case CommandShowSellerGoods:
  30.                         seller.ShowAllItems();
  31.                         break;
  32.  
  33.                     case CommandSellGoods:
  34.                         menu.Run(seller, player);
  35.                         break;
  36.  
  37.                     case CommandShowPlayerInventory:
  38.                         player.ShowAllItems();
  39.                         break;
  40.  
  41.                     case CommandExit:
  42.                         isWork = false;
  43.                         break;
  44.  
  45.                     default:
  46.                         Console.WriteLine("Ошибка, такого пункта меню нет");
  47.                         break;
  48.                 }
  49.  
  50.                 Console.ReadKey();
  51.                 Console.Clear();
  52.             }
  53.         }
  54.     }
  55.  
  56.     class Trader
  57.     {
  58.         protected List<Product> _products = new List<Product>();
  59.         protected int _money = 0;
  60.  
  61.         public void ShowAllItems()
  62.         {
  63.             if (_products.Count > 0)
  64.             {
  65.                 foreach (Product item in _products)
  66.                 {
  67.                     item.ShowInfo();
  68.                 }
  69.             }
  70.             else
  71.             {
  72.                 Console.WriteLine($"Инвентарь пуст.");
  73.             }
  74.         }
  75.     }
  76.  
  77.     class Seller : Trader
  78.     {
  79.         public Seller()
  80.         {
  81.             _products.Add(new Product("Хлеб", 12));
  82.             _products.Add(new Product("Овес", 4));
  83.             _products.Add(new Product("Сыр", 118));
  84.             _products.Add(new Product("Яйца", 32));
  85.             _products.Add(new Product("Чай", 23));
  86.         }
  87.  
  88.         public List<Product> GetProducts()
  89.         {
  90.             return _products;
  91.         }
  92.  
  93.         public void TakeMoney(int payment)
  94.         {
  95.             _money += payment;
  96.         }
  97.  
  98.         public void Sell(int index)
  99.         {
  100.             Product product = _products[index];
  101.             _products.Remove(product);
  102.             _money += product.Price;
  103.         }
  104.     }
  105.  
  106.     class Player : Trader
  107.     {
  108.         private int _moneyToPay;
  109.  
  110.         public int Money { get; private set; } = 50;
  111.  
  112.         public void AddGoods(Product product)
  113.         {
  114.             _products.Add(product);
  115.         }
  116.  
  117.         public bool CheckSolvency(Product product)
  118.         {
  119.             _moneyToPay = product.Price;
  120.  
  121.             if (Money >= _moneyToPay)
  122.             {
  123.                 return true;
  124.             }
  125.             else
  126.             {
  127.                 _moneyToPay = 0;
  128.                 return false;
  129.             }
  130.         }
  131.  
  132.         public int Pay()
  133.         {
  134.             Money -= _moneyToPay;
  135.             return _moneyToPay;
  136.         }
  137.     }
  138.  
  139.     class Product
  140.     {
  141.         public Product(string name, int price)
  142.         {
  143.             Name = name;
  144.             Price = price;
  145.         }
  146.  
  147.         public string Name { get; private set; }
  148.         public int Price { get; private set; }
  149.  
  150.         public void ShowInfo()
  151.         {
  152.             Console.WriteLine($"Название товара: {Name}, цена: {Price}.");
  153.         }
  154.     }
  155.  
  156.     class Menu
  157.     {
  158.         private List<Product> _items;
  159.         private int _selectedItemIndex;
  160.  
  161.         public Menu(List<Product> items)
  162.         {
  163.             _items = items;
  164.             _selectedItemIndex = 0;
  165.         }
  166.  
  167.         private void Display()
  168.         {
  169.             Console.Clear();
  170.             Console.WriteLine("Управление ▲, ▼, Enter, Esc.");
  171.  
  172.             for (int i = 0; i < _items.Count; i++)
  173.             {
  174.                 int isequenceNumber = i + 1;
  175.                 var currentItem = _items[i];
  176.                 if (i == _selectedItemIndex)
  177.                 {
  178.                     Console.ForegroundColor = ConsoleColor.Black;
  179.                     Console.BackgroundColor = ConsoleColor.Gray;
  180.                     Console.WriteLine($"{isequenceNumber}. {currentItem.Name}, Цена: {currentItem.Price}");
  181.                 }
  182.                 else
  183.                 {
  184.                     Console.WriteLine($"{isequenceNumber}. {currentItem.Name}, Цена: {currentItem.Price}");
  185.                 }
  186.  
  187.                 Console.ResetColor();
  188.             }
  189.         }
  190.  
  191.         public void MoveUp()
  192.         {
  193.             _selectedItemIndex--;
  194.  
  195.             if (_selectedItemIndex < 0)
  196.             {
  197.                 _selectedItemIndex = _items.Count - 1;
  198.             }
  199.         }
  200.  
  201.         public void MoveDown()
  202.         {
  203.             _selectedItemIndex++;
  204.  
  205.             if (_selectedItemIndex >= _items.Count)
  206.             {
  207.                 _selectedItemIndex = 0;
  208.             }
  209.         }
  210.  
  211.         public void СhooseGoods(Seller seller, Player player)
  212.         {
  213.             Product product = _items[_selectedItemIndex];
  214.  
  215.             if (player.CheckSolvency(product))
  216.             {
  217.                 seller.Sell(_selectedItemIndex);
  218.                 player.AddGoods(product);
  219.                 seller.TakeMoney(player.Pay());
  220.  
  221.                 Console.WriteLine($"Осталось денег: {player.Money}.");
  222.                 Console.ReadKey();
  223.             }
  224.             else
  225.             {
  226.                 Console.WriteLine("Не хватает денег.");
  227.                 Console.ReadKey();
  228.             }
  229.         }
  230.  
  231.         public void Run(Seller seller, Player player)
  232.         {
  233.             const ConsoleKey KeyMoveUp = ConsoleKey.UpArrow;
  234.             const ConsoleKey KeyMoveDown = ConsoleKey.DownArrow;
  235.             const ConsoleKey KeyPressingEnter = ConsoleKey.Enter;
  236.             const ConsoleKey KeyPressingEscape = ConsoleKey.Escape;
  237.  
  238.             bool isWork = true;
  239.  
  240.             while (isWork)
  241.             {
  242.                 Display();
  243.                 ConsoleKeyInfo keyInfo;
  244.                 keyInfo = Console.ReadKey(true);
  245.  
  246.                 switch (keyInfo.Key)
  247.                 {
  248.                     case KeyMoveUp:
  249.                         MoveUp();
  250.                         break;
  251.  
  252.                     case KeyMoveDown:
  253.                         MoveDown();
  254.                         break;
  255.  
  256.                     case KeyPressingEnter:
  257.                         СhooseGoods(seller, player);
  258.                         break;
  259.  
  260.                     case KeyPressingEscape:
  261.                         isWork = false;
  262.                         break;
  263.                 }
  264.             }
  265.         }
  266.     }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement