Anonim_999

Shop upgrade

Jul 12th, 2021 (edited)
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string userInput;
  10.             Seller seller = new Seller();
  11.             seller.Addroduct();
  12.             Player player = new Player();
  13.             bool isContinue = true;
  14.  
  15.             while (isContinue)
  16.             {
  17.                 Console.ReadKey();
  18.                 Console.Clear();
  19.                 Console.WriteLine("Меню: \n1.Посмотреть товар\n2.Купить товар\n3.Открыть инвентарь\n4.Выход");
  20.                 userInput = Console.ReadLine();
  21.  
  22.                 switch (userInput)
  23.                 {
  24.                     case "1":
  25.                         seller.ShowProducts();
  26.                         break;
  27.                     case "2":
  28.                         Console.Write("\nВведите название товара: ");
  29.                         userInput = Console.ReadLine();
  30.                         seller.SellProduct(userInput, player);
  31.                         break;
  32.                     case "3":
  33.                         player.ShowInventory();
  34.                         break;
  35.                     case "4":
  36.                         isContinue = false;
  37.                         break;
  38.                     default:
  39.                         Console.WriteLine("Нет такой команды");
  40.                         break;
  41.                 }
  42.             }
  43.         }
  44.  
  45.         class Player
  46.         {
  47.             private List<Product> _inventory = new List<Product>();
  48.  
  49.             public void AddProduct(string item)
  50.             {
  51.                 _inventory.Add(new Product(item));
  52.             }
  53.  
  54.             public void ShowInventory()
  55.             {
  56.                 Console.WriteLine("Инвентарь:");
  57.  
  58.                 for (int i = 0; i < _inventory.Count; i++)
  59.                 {
  60.                     _inventory[i].ShowProduct();
  61.                 }
  62.             }
  63.         }
  64.  
  65.         class Seller
  66.         {
  67.             private List<string> _products = new List<string>();
  68.  
  69.             public void Addroduct()
  70.             {
  71.                 _products.AddRange(new string[] { "Руда","Альманах", "Руна телепортации", "Глаз Инноса"});
  72.             }
  73.  
  74.             public void ShowProducts()
  75.             {
  76.                 if (_products.Count >= 1)
  77.                 {
  78.                     Console.WriteLine("Товары:");
  79.  
  80.                     for (int i = 0; i < _products.Count; i++)
  81.                     {
  82.                         Console.WriteLine($"{i + 1}.{_products[i]}");
  83.                     }
  84.                 }
  85.                 else
  86.                 {
  87.                     Console.WriteLine("Товаров нет в наличии");
  88.                 }
  89.                
  90.             }
  91.  
  92.             public void SellProduct(string userInput, Player player)
  93.             {
  94.                 bool isFind = false;
  95.  
  96.                 foreach (string product in _products)
  97.                 {
  98.                     if (product == userInput)
  99.                     {
  100.                         player.AddProduct(product);
  101.                         _products.Remove(product);
  102.                         Console.WriteLine($"Товар {product} - куплен");
  103.                         isFind = true;
  104.                         break;
  105.                     }
  106.                 }
  107.  
  108.                 if (isFind == false)
  109.                 {
  110.                     Console.WriteLine("Продукт в списке не найден");
  111.                 }
  112.             }
  113.         }
  114.  
  115.         class Product
  116.         {
  117.             private string _name;
  118.  
  119.             public Product(string name)
  120.             {
  121.                 _name = name;
  122.             }
  123.  
  124.             public void ShowProduct()
  125.             {
  126.                 Console.WriteLine(_name);
  127.             }
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment