ranee

Магазин

Mar 26th, 2023 (edited)
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.16 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.         virtual public void ShowAllItems()
  60.         {
  61.             if (Products.Count > 0)
  62.             {
  63.                 foreach (Product item in Products)
  64.                 {
  65.                     item.ShowInfo();
  66.                 }
  67.             }
  68.             else
  69.             {
  70.                 Console.WriteLine($"Инвентарь пуст.");
  71.             }
  72.         }
  73.  
  74.         protected List<Product> Products = new List<Product>();
  75.         protected int Money = 0;
  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.             List<Product> productsCopy = new List<Product>(Products);
  93.             return productsCopy;
  94.         }
  95.  
  96.         public void TakeMoney(int payment)
  97.         {
  98.             Money += payment;
  99.         }
  100.  
  101.         public void Sell(int index)
  102.         {
  103.             Product product = Products[index];
  104.             Products.Remove(product);
  105.             Money += product.Price;
  106.         }
  107.  
  108.         public override void ShowAllItems()
  109.         {
  110.             if (Products.Count > 1)
  111.             {
  112.                 foreach (Product item in Products)
  113.                 {
  114.                     if (item.Name == "Выход")
  115.                     {
  116.                         continue;
  117.                     }
  118.                     else
  119.                     {
  120.                         item.ShowInfo();
  121.                     }
  122.                 }
  123.             }
  124.             else
  125.             {
  126.                 Console.WriteLine($"Инвентарь пуст.");
  127.             }
  128.         }
  129.     }
  130.  
  131.     class Player : Trader
  132.     {
  133.         public new int Money { get; private set; } = 5000;
  134.  
  135.         public void AddGoods(Product product)
  136.         {
  137.             Products.Add(product);
  138.         }
  139.  
  140.         public bool CheckSolvency(Product product)
  141.         {
  142.             if (Money >= product.Price)
  143.             {
  144.                 return true;
  145.             }
  146.             else
  147.             {
  148.                 return false;
  149.             }
  150.         }
  151.  
  152.         public int Pay(int paymant)
  153.         {
  154.             Money -= paymant;
  155.             return Money;
  156.         }
  157.     }
  158.  
  159.     class Product
  160.     {
  161.         public Product(string name, int price)
  162.         {
  163.             Name = name;
  164.             Price = price;
  165.         }
  166.  
  167.         public string Name { get; private set; }
  168.         public int Price { get; private set; }
  169.  
  170.         public void ShowInfo()
  171.         {
  172.             Console.WriteLine($"Название товара: {Name}, цена: {Price}.");
  173.         }
  174.     }
  175.  
  176.     class Menu
  177.     {
  178.         private List<Product> _items;
  179.         private int _selectedItemIndex;
  180.  
  181.         public Menu(List<Product> items)
  182.         {
  183.             _items = items;
  184.             _selectedItemIndex = 0;
  185.         }
  186.  
  187.         private void Display()
  188.         {
  189.             Console.Clear();
  190.             Console.WriteLine("Управление ▲, ▼, Enter, Esc.");
  191.  
  192.             for (int i = 0; i < _items.Count; i++)
  193.             {
  194.                 int isequenceNumber = i;
  195.                 Product currentItem = _items[i];
  196.                 if (i == _selectedItemIndex)
  197.                 {
  198.                     Console.ForegroundColor = ConsoleColor.Black;
  199.                     Console.BackgroundColor = ConsoleColor.Gray;
  200.  
  201.                     if (i == 0)
  202.                     {
  203.                         Console.WriteLine($"{isequenceNumber}. {currentItem.Name}.");
  204.                     }
  205.                     else
  206.                     {
  207.                         Console.WriteLine($"{isequenceNumber}. {currentItem.Name}, Цена: {currentItem.Price}");
  208.                     }
  209.                 }
  210.                 else
  211.                 {
  212.                     if (i == 0)
  213.                     {
  214.                         Console.WriteLine($"{isequenceNumber}. {currentItem.Name}.");
  215.                     }
  216.                     else
  217.                     {
  218.                         Console.WriteLine($"{isequenceNumber}. {currentItem.Name}, Цена: {currentItem.Price}");
  219.                     }
  220.                 }
  221.  
  222.                 Console.ResetColor();
  223.             }
  224.         }
  225.  
  226.         public void MoveUp()
  227.         {
  228.             _selectedItemIndex--;
  229.  
  230.             if (_selectedItemIndex < 0)
  231.             {
  232.                 _selectedItemIndex = _items.Count - 1;
  233.             }
  234.         }
  235.  
  236.         public void MoveDown()
  237.         {
  238.             _selectedItemIndex++;
  239.  
  240.             if (_selectedItemIndex >= _items.Count)
  241.             {
  242.                 _selectedItemIndex = 0;
  243.             }
  244.         }
  245.  
  246.         public void СhooseGoods(Seller seller, Player player, ref bool isExit)
  247.         {
  248.             Product product = _items[_selectedItemIndex];
  249.            
  250.             if (_selectedItemIndex == 0)
  251.             {
  252.                 isExit = false;
  253.             }
  254.             else
  255.             {
  256.                 if (player.CheckSolvency(product))
  257.                 {
  258.                     seller.Sell(_selectedItemIndex);
  259.                     player.AddGoods(product);
  260.                     seller.TakeMoney(player.Pay(product.Price));
  261.                     _items.Remove(product);
  262.                    
  263.                     if(_selectedItemIndex == _items.Count)
  264.                     {
  265.                         MoveUp();
  266.                     }
  267.  
  268.                     Console.WriteLine($"Осталось денег: {player.Money}.");
  269.                     Console.ReadKey();
  270.                 }
  271.                 else
  272.                 {
  273.                     Console.WriteLine("Не хватает денег.");
  274.                     Console.ReadKey();
  275.                 }
  276.             }
  277.         }
  278.  
  279.         public void TryRun(Seller seller, Player player)
  280.         {
  281.             if (_items.Count > 1)
  282.             {
  283.                 Run(seller, player);
  284.             }
  285.             else
  286.             {
  287.                 Console.WriteLine("В магазине нет товаров.");
  288.             }
  289.         }
  290.  
  291.         public void Run(Seller seller, Player player)
  292.         {
  293.             const ConsoleKey KeyMoveUp = ConsoleKey.UpArrow;
  294.             const ConsoleKey KeyMoveDown = ConsoleKey.DownArrow;
  295.             const ConsoleKey KeyPressingEnter = ConsoleKey.Enter;
  296.             const ConsoleKey KeyPressingEscape = ConsoleKey.Escape;
  297.  
  298.             bool isWork = true;
  299.  
  300.             while (isWork)
  301.             {
  302.                 Display();
  303.                 ConsoleKeyInfo keyInfo;
  304.                 keyInfo = Console.ReadKey(true);
  305.  
  306.                 switch (keyInfo.Key)
  307.                 {
  308.                     case KeyMoveUp:
  309.                         MoveUp();
  310.                         break;
  311.  
  312.                     case KeyMoveDown:
  313.                         MoveDown();
  314.                         break;
  315.  
  316.                     case KeyPressingEnter:
  317.                         СhooseGoods(seller, player, ref isWork);
  318.                         break;
  319.  
  320.                     case KeyPressingEscape:
  321.                         isWork = false;
  322.                         break;
  323.                 }
  324.             }
  325.         }
  326.     }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment