Advertisement
ranee

Магазин

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