Advertisement
ranee

Untitled

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