Advertisement
SnowPhoenix347

5.4

Nov 8th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.05 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace FifthProject
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Character character = new Character();
  11.             Player player = new Player();
  12.             Trader trader = new Trader();
  13.  
  14.             int id, price = 0, scale = 0;
  15.             string name = null;
  16.             bool usability = false;
  17.  
  18.             while (true)
  19.             {
  20.                 ShowMenu();
  21.                
  22.                 int menu = ConvertToInt();
  23.                 switch (menu)
  24.                 {
  25.                     case 1:
  26.                         AddInfo(ref price, ref scale, ref name,ref usability);
  27.                         trader.AddNewItem(price, scale, name, usability, ref player.playerItems);
  28.                         break;
  29.                     case 2:
  30.                         AddInfo(ref price, ref scale, ref name, ref usability);
  31.                         player.AddNewItem(price, scale, name, usability, ref trader.traderItems);
  32.                         break;
  33.                     case 3:
  34.                         player.ShowItems(player.playerItems);
  35.                         id = ConvertToInt();
  36.                         character.TradeItem(ref player.playerItems, ref trader.traderItems, id);
  37.                         break;
  38.                     case 4:
  39.                         trader.ShowItems(trader.traderItems);
  40.                         id = ConvertToInt();
  41.                         character.TradeItem(ref trader.traderItems, ref player.playerItems, id);
  42.                         break;
  43.                     case 5:
  44.                         player.ShowItems(player.playerItems);
  45.                         Console.ReadKey();
  46.                         break;
  47.                     case 6:
  48.                         trader.ShowItems(trader.traderItems);
  49.                         Console.ReadKey();
  50.                         break;
  51.                 }
  52.                 Console.Clear();
  53.             }
  54.         }
  55.  
  56.         static void ShowMenu()
  57.         {
  58.             Console.WriteLine("\tMenu\n" +
  59.                               "1. Добавить новый предмет в инвентарь\n" +
  60.                               "2. Добавить новый предмет торговцу\n" +
  61.                               "3. Продать предмет\n" +
  62.                               "4. Купить предмет\n" +
  63.                               "5. Показать инвентарь\n" +
  64.                               "6. Показать инвентарь продавца\n");
  65.         }
  66.  
  67.         static void AddInfo(ref int price, ref int scale, ref string name, ref bool usability)
  68.         {
  69.             Console.WriteLine("Введите цену");
  70.             price = ConvertToInt();
  71.             Console.WriteLine("Введите размер");
  72.             scale = ConvertToInt();
  73.             Console.WriteLine("Введите имя");
  74.             name = Console.ReadLine();
  75.             Console.WriteLine("Можно ли использовать этот предмет?\n" +
  76.                               "1. Да\n" +
  77.                               "2. Нет\n");
  78.             int menu = ConvertToInt();
  79.             if (menu == 1)
  80.             {
  81.                 usability = true;
  82.             }
  83.             else if (menu == 2)
  84.             {
  85.                 usability = false;
  86.             }
  87.         }
  88.        
  89.         static int ConvertToInt()
  90.         {
  91.             int output;
  92.             bool enterIsCorrect = false;
  93.             do
  94.             {
  95.                 string input = Console.ReadLine();
  96.                 enterIsCorrect = int.TryParse(input, out output);
  97.                 if (!enterIsCorrect)
  98.                 {
  99.                     Console.WriteLine("Error. Try again");
  100.                 }
  101.             } while (!enterIsCorrect);
  102.  
  103.             return output;
  104.         }
  105.     }
  106.  
  107.     class Character
  108.     {
  109.         public void AddNewItem(int price, int scale, string name, bool usability, ref Item[] items)
  110.         {
  111.             Item[] tempItems = new Item[items.Length + 1];
  112.  
  113.             for (int i = 0; i < tempItems.Length - 1; i++)
  114.             {
  115.                 tempItems[i] = items[i];
  116.             }
  117.  
  118.             tempItems[tempItems.Length - 1] = new Item(price, scale, name, usability);
  119.             items = tempItems;
  120.         }
  121.        
  122.         public void RemoveItem(int id, ref Item[] items)
  123.         {
  124.             items[id] = null;
  125.             Item[] tempItems = new Item[items.Length - 1];
  126.             for (int i = 0; i < items.Length - 1; i++)
  127.             {
  128.                 if (items[i] != null)
  129.                 {
  130.                     tempItems[i] = items[i];
  131.                 }
  132.                 else
  133.                 {
  134.                     tempItems[i] = items[i + 1];
  135.                 }
  136.             }
  137.  
  138.             items = tempItems;
  139.         }
  140.  
  141.         public void TradeItem(ref Item[] items,ref Item[] itemsSecond, int id)
  142.         {
  143.             AddNewItem(0,0,null,false, ref itemsSecond);
  144.             itemsSecond[itemsSecond.Length - 1] = items[id];
  145.             RemoveItem(id, ref items);
  146.         }
  147.  
  148.         public void ShowItems(Item[] items)
  149.         {
  150.             for (int i = 0; i < items.Length; i++)
  151.             {
  152.                 Console.WriteLine($"ID: {i} Название: {items[i].Name} Цена: {items[i].Price} " +
  153.                                   $"Размер: {items[i].Scale} Юзабльность: {items[i].Usability}");
  154.             }
  155.         }
  156.     }
  157.    
  158.     class Trader : Character
  159.     {
  160.         public Item[] traderItems = new Item[0];
  161.     }
  162.    
  163.     class Player : Character
  164.     {
  165.         public Item[] playerItems = new Item[0];
  166.     }
  167.  
  168.     class Item
  169.     {
  170.         public int Price;
  171.         public int Scale;
  172.         public string Name;
  173.         public bool Usability;
  174.        
  175.         public Item(int price, int scale, string name, bool usability)
  176.         {
  177.             Price = price;
  178.             Scale = scale;
  179.             Name = name;
  180.             Usability = usability;
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement