Advertisement
Comrade_Sharikov

Shop

Mar 31st, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             bool isOpen = true;
  10.             Product product;
  11.             Seller seller = new Seller();
  12.  
  13.             Console.WriteLine("Добрый день. Сколько у вас монет?");
  14.             int playerMoney = Convert.ToInt32(Console.ReadLine());
  15.  
  16.             Player player = new Player(playerMoney);
  17.  
  18.             while (isOpen)
  19.             {
  20.                 Console.WriteLine("Выберите команду");
  21.                 Console.WriteLine("1 - Список товаров у продавца\n" + "2 - Купить товар\n" +
  22.                     "3 - Список товаров у игрока\n" + "0 - Выход из программы");
  23.                 int userInput = Convert.ToInt32(Console.ReadLine());
  24.  
  25.                 Console.WriteLine();
  26.                 Console.Clear();
  27.                 switch (userInput)
  28.                 {
  29.                     case 1:
  30.                         seller.ShowSellerInventory();
  31.                         break;
  32.                     case 2:
  33.                         seller.ShowSellerInventory();
  34.                         Console.WriteLine("Выберите номер товара, который хотите купить\n");
  35.                         int userNumber = Convert.ToInt32(Console.ReadLine()) - 1;
  36.  
  37.                         product = seller.GetProduct(userNumber);
  38.  
  39.                         if (player.IsEnoughMoney(product) == true)
  40.                         {
  41.                             seller.SellProduct(userNumber);
  42.                         }
  43.                         break;
  44.                     case 3:
  45.                         player.ShowPlayerProducts();
  46.                         break;
  47.                     case 0:
  48.                         isOpen = false;
  49.                         break;
  50.                     default:
  51.                         Console.WriteLine("Недопустимое значение");
  52.                         break;
  53.                 }
  54.             }
  55.         }
  56.     }
  57.  
  58.     class Inventory
  59.     {
  60.         private Product[] _products;
  61.  
  62.         public Product[] Products
  63.         {
  64.             get
  65.             {
  66.                 return _products;
  67.             }
  68.         }
  69.  
  70.         public Inventory()
  71.         {
  72.             _products = new Product[] {new Product("Печенье", 70),
  73.                 new Product("Молоко", 50), new Product("Яблоки", 150),
  74.                 new Product("Сыр", 200), new Product("Киви", 120) };
  75.         }
  76.     }
  77.  
  78.     class Product
  79.     {
  80.         public string Name { get; private set; }
  81.         public int Price { get; private set; }
  82.  
  83.         public Product(string name, int price)
  84.         {
  85.             Name = name;
  86.             Price = price;
  87.         }
  88.  
  89.         public void ShowInfo()
  90.         {
  91.             Console.WriteLine(Name + ", цена " + Price + "у.е.");
  92.         }
  93.     }
  94.  
  95.     class Player
  96.     {
  97.         private Product[] _products = new Product[0];
  98.         private int _money;
  99.  
  100.         public Player(int money)
  101.         {
  102.             _money = money;
  103.         }
  104.  
  105.         public bool IsEnoughMoney(Product product)
  106.         {
  107.             bool isEnoughMoney = false;
  108.  
  109.             if (_money >= product.Price)
  110.             {
  111.                 _money -= product.Price;
  112.                 AddProduct(product);
  113.                 Console.WriteLine("Товар куплен");
  114.                 Console.WriteLine("\nУ игрока осталось " + _money + " монет\n");
  115.  
  116.                 isEnoughMoney = true;
  117.             }
  118.             else
  119.             {
  120.                 Console.WriteLine("У игрока недостаточно денег");
  121.             }
  122.  
  123.             return isEnoughMoney;
  124.         }
  125.  
  126.         public void AddProduct(Product product)
  127.         {
  128.             Product[] tempArray = new Product[_products.Length + 1];
  129.  
  130.             for (int i = 0; i < _products.Length; i++)
  131.             {
  132.                 tempArray[i] = _products[i];
  133.             }
  134.  
  135.             tempArray[tempArray.Length - 1] = product;
  136.             _products = tempArray;
  137.         }
  138.  
  139.         public void ShowPlayerProducts()
  140.         {
  141.             for (int i = 0; i < _products.Length; i++)
  142.             {
  143.                 Console.Write(i + 1 + ") ");
  144.                 _products[i].ShowInfo();
  145.             }
  146.         }
  147.     }
  148.  
  149.     class Seller
  150.     {
  151.         private Inventory _inventory = new Inventory();
  152.         private Product[] _products;
  153.  
  154.         public Seller()
  155.         {
  156.             _products = _inventory.Products;
  157.         }
  158.  
  159.         public Product GetProduct(int index)
  160.         {
  161.             return _products[index];
  162.         }
  163.  
  164.         public void SellProduct(int index)
  165.         {
  166.             Product[] tempArray = new Product[_products.Length - 1];
  167.  
  168.             for (int i = 0; i < index; i++)
  169.             {
  170.                 tempArray[i] = _products[i];
  171.             }
  172.  
  173.             for (int i = index; i < tempArray.Length; i++)
  174.             {
  175.                 tempArray[i] = _products[i + 1];
  176.             }
  177.             _products = tempArray;
  178.         }
  179.  
  180.         public void ShowSellerInventory()
  181.         {
  182.             for (int i = 0; i < _products.Length; i++)
  183.             {
  184.                 Console.Write(i + 1 + ") ");
  185.                 _products[i].ShowInfo();
  186.             }
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement