Advertisement
ranee

Магазин

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