Advertisement
RedFlys

Home Work 5.4

Nov 27th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Lesson5._4_v3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string userValue;
  14.             int userNumber;
  15.             int userQuality;
  16.  
  17.             Render render = new Render();
  18.             DataBaseItems dataBaseItems = new DataBaseItems();
  19.             Item[] item = dataBaseItems.Items();
  20.  
  21.             while (true)
  22.             {
  23.                 Console.WriteLine("Вы стоите перед торговцем. \n1. Торговать. \n2. Уйти.");
  24.                 userValue = Console.ReadLine();
  25.                
  26.                 Console.Clear();
  27.                 if (TryNumber1or2(userValue))
  28.                 {
  29.                     userNumber = Convert.ToInt32(userValue);
  30.  
  31.                     if (userNumber == 1)
  32.                     {
  33.                         while (true)
  34.                         {
  35.                             render.RenderTradeList(item);
  36.                             userValue = Console.ReadLine();
  37.  
  38.                             if (TryNumber1or2or3(userValue))
  39.                             {
  40.                                 userNumber = Convert.ToInt32(userValue);
  41.  
  42.                                 if (userNumber == 1)
  43.                                 {
  44.                                     Console.WriteLine("Укажите номер предмета, который хотите купить: ");
  45.                                     userNumber = Convert.ToInt32(Console.ReadLine());
  46.                                     Console.WriteLine("Укажите количество, которое хотите купить: ");
  47.                                     userQuality = Convert.ToInt32(Console.ReadLine());
  48.  
  49.                                     if (dataBaseItems.TryBuy(item, userNumber, userQuality))
  50.                                     {
  51.                                         item = dataBaseItems.BuyItem(item, userNumber, userQuality);
  52.                                     }
  53.                                     else
  54.                                     {
  55.                                         render.Error();
  56.                                     }
  57.                                     Console.Clear();
  58.                                 }
  59.                                 else if (userNumber == 2)
  60.                                 {
  61.                                     Console.WriteLine("Укажите номер предмета, который хотите продать: ");
  62.                                     userNumber = Convert.ToInt32(Console.ReadLine());
  63.                                     Console.WriteLine("Укажите количество, которое хотите продать: ");
  64.                                     userQuality = Convert.ToInt32(Console.ReadLine());
  65.  
  66.                                     if (dataBaseItems.TrySell(item, userNumber, userQuality))
  67.                                     {
  68.                                         item = dataBaseItems.SellItem(item, userNumber, userQuality);
  69.                                     }
  70.                                     else
  71.                                     {
  72.                                         render.Error();
  73.                                     }
  74.                                     Console.Clear();
  75.                                 }
  76.                                 else if (userNumber == 3)
  77.                                 {
  78.                                     Console.Clear();
  79.                                     break;
  80.                                 }
  81.                             }
  82.                         }
  83.                     }
  84.                     else if (userNumber == 2)
  85.                     {
  86.                         break;
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.  
  92.         static bool TryNumber1or2 (string userNumber)
  93.         {
  94.             if (userNumber == "1" || userNumber == "2")
  95.                 return true;
  96.             else
  97.                 return false;
  98.         }
  99.  
  100.         static bool TryNumber1or2or3(string userNumber)
  101.         {
  102.             if (userNumber == "1" || userNumber == "2" || userNumber == "3")
  103.                 return true;
  104.             else
  105.                 return false;
  106.         }
  107.     }
  108.  
  109.     class Item
  110.     {
  111.         public string Name;
  112.         public int Cost;
  113.         public int UserQuanity;
  114.         public int TraderQuanity;
  115.  
  116.         public Item(string name = "none", int cost = 1, int userQuanity = 0, int traderQuanity = 0)
  117.         {
  118.             Name = name;
  119.             Cost = cost;
  120.             UserQuanity = userQuanity;
  121.             TraderQuanity = traderQuanity;
  122.         }
  123.     }
  124.  
  125.     class DataBaseItems
  126.     {
  127.         public Item[] Items()
  128.         {
  129.             Item[] items =
  130.             {
  131.                 new Item("Золото", 1, 5000, 99999),
  132.                 new Item("Бита", 1000, 1, 1),
  133.                 new Item("Мамино кольцо", 10000, 1, 0),
  134.                 new Item("Авакадо", 30, 0, 10),
  135.                 new Item("Банан", 10, 0, 40),
  136.                 new Item("Яблоко", 4, 0, 200),
  137.                 new Item("Дыня", 30, 0, 2)
  138.             };
  139.  
  140.             return items;
  141.         }
  142.  
  143.         public Item[] BuyItem(Item[] item,int userNumber, int userQuanity)
  144.         {
  145.             int i = 0;
  146.             int y = 0;
  147.  
  148.             while (y != userNumber)
  149.             {
  150.                 i++;
  151.  
  152.                 if (i == 0)
  153.                     continue;
  154.  
  155.                 if (item[i].UserQuanity == 0)
  156.                     continue;
  157.  
  158.                 y++;
  159.             }
  160.  
  161.             item[i].UserQuanity += userQuanity;
  162.             item[i].TraderQuanity -= userQuanity;
  163.  
  164.             item[0].UserQuanity -= (userQuanity * item[i].Cost);
  165.             item[0].TraderQuanity += (userQuanity * item[i].Cost);
  166.  
  167.             return item;
  168.         }        
  169.  
  170.         public Item[] SellItem(Item[] item, int userNumber, int userQuanity)
  171.         {
  172.             int i = 0;
  173.             int y = 0;
  174.  
  175.             while (y != userNumber)
  176.             {
  177.                 i++;
  178.  
  179.                 if (i == 0)
  180.                     continue;
  181.  
  182.                 if (item[i].UserQuanity == 0)
  183.                     continue;
  184.  
  185.                 y++;
  186.             }
  187.  
  188.             item[i].UserQuanity -= userQuanity;
  189.             item[i].TraderQuanity += userQuanity;
  190.  
  191.             item[0].UserQuanity += (userQuanity * item[i].Cost);
  192.             item[0].TraderQuanity -= (userQuanity * item[i].Cost);
  193.            
  194.             return item;
  195.         }
  196.  
  197.         public bool TryBuy(Item[] item, int userNumber, int userQuanity)
  198.         {
  199.             int i = 0;
  200.             int y = 0;
  201.  
  202.             while (y != userNumber)
  203.             {
  204.                 if (i < item.Length)
  205.                     i++;
  206.  
  207.                 if (i == item.Length)
  208.                     break;
  209.  
  210.                 if (i == 0)
  211.                     continue;
  212.  
  213.                 if (item[i].UserQuanity == 0)
  214.                     continue;
  215.                
  216.                 y++;
  217.             }
  218.  
  219.             if (item[i].TraderQuanity - userQuanity >= 0 && item[0].UserQuanity - (userQuanity * item[i].Cost) >= 0)
  220.                 return true;
  221.             else
  222.                 return false;
  223.         }
  224.  
  225.         public bool TrySell(Item[] item, int userNumber, int userQuanity)
  226.         {
  227.             int i = 0;
  228.             int y = 0;
  229.  
  230.             while (y != userNumber)
  231.             {
  232.                 if (i < item.Length)
  233.                     i++;
  234.                
  235.                 if (i == item.Length)
  236.                     break;
  237.  
  238.                 if (i == 0)
  239.                     continue;
  240.  
  241.                 if (item[i].UserQuanity == 0)                
  242.                     continue;
  243.                
  244.                 y++;
  245.             }
  246.  
  247.             if (i < item.Length && item[i].UserQuanity - userQuanity >= 0 && item[0].TraderQuanity - (userQuanity * item[i].Cost) >= 0)
  248.                 return true;
  249.             else
  250.                 return false;
  251.         }
  252.     }
  253.  
  254.     class Render
  255.     {
  256.         public void RenderTradeList(Item[] item)
  257.         {
  258.             RenderNameTradeLIst();
  259.             RenderTrade(item);
  260.             RenderCommandTrade();
  261.         }
  262.  
  263.         public void RenderNameTradeLIst()
  264.         {
  265.             Console.SetCursorPosition(0, 0);
  266.             Console.WriteLine("Ваш инвентарь:");
  267.  
  268.             Console.SetCursorPosition(60, 0);
  269.             Console.WriteLine("Предметы торговца:");
  270.         }
  271.  
  272.         public void RenderTrade(Item[] item)
  273.         {
  274.             RenderTradeName();
  275.             RenderInventer(item);
  276.             RenderGold(item);
  277.         }
  278.  
  279.         public void RenderTradeName()
  280.         {
  281.             Console.SetCursorPosition(0, 2);
  282.             Console.WriteLine("Название            | Кол-во | Стоимость |");
  283.             Console.SetCursorPosition(60, 2);
  284.             Console.WriteLine("Название            | Кол-во | Стоимость |");
  285.         }
  286.  
  287.         public void RenderInventer(Item[] item)
  288.         {
  289.             int y = 4;
  290.             int j = 1;
  291.             RenderUserInventer(item, y, j);
  292.             RenderTraderInveter(item, y, j);
  293.         }
  294.  
  295.         public void RenderUserInventer(Item[] item, int y, int j)
  296.         {
  297.             string name;
  298.             string quanity;
  299.             string cost;
  300.  
  301.             for (int i = 0; i < item.Length; i++)
  302.             {
  303.                 if (i == 0)
  304.                     continue;
  305.  
  306.                 if (item[i].UserQuanity == 0)
  307.                     continue;
  308.  
  309.                 Console.SetCursorPosition(0, y);
  310.  
  311.                 name = Name(item[i].Name);
  312.                 quanity = Quanity(item[i].UserQuanity);
  313.                 cost = Cost(item[i].Cost);
  314.  
  315.                 Console.WriteLine(j + ". " + name + quanity + cost);
  316.  
  317.                 y++;
  318.                 j++;
  319.             }
  320.         }
  321.  
  322.         public void RenderTraderInveter(Item[] item, int y, int j)
  323.         {
  324.             string name;
  325.             string quanity;
  326.             string cost;
  327.  
  328.             for (int i = 0; i < item.Length; i++)
  329.             {
  330.                 if (i == 0)
  331.                     continue;
  332.  
  333.                 if (item[i].TraderQuanity == 0)
  334.                     continue;
  335.  
  336.                 Console.SetCursorPosition(60, y);
  337.  
  338.                 name = Name(item[i].Name);
  339.                 quanity = Quanity(item[i].TraderQuanity);
  340.                 cost = Cost(item[i].Cost);
  341.  
  342.                 Console.WriteLine(j + ". " + name + quanity + cost);
  343.  
  344.                 y++;
  345.                 j++;
  346.             }
  347.         }
  348.  
  349.         public void RenderGold(Item[] item)
  350.         {
  351.             Console.SetCursorPosition(0, 10);
  352.             Console.WriteLine("Ваше золото: " + item[0].UserQuanity);
  353.             Console.SetCursorPosition(60, 10);
  354.             Console.WriteLine("Золото торговца: " + item[0].TraderQuanity);
  355.         }
  356.                
  357.         public string Name(string name)
  358.         {
  359.             while (name.Length < 16)
  360.             {
  361.                 name += " ";
  362.             }
  363.             name += " | ";
  364.  
  365.             return name;
  366.         }
  367.  
  368.         public string Quanity(int quanity)
  369.         {
  370.             string quanityItem = " " + quanity;
  371.  
  372.             while (quanityItem.Length < 6)
  373.             {
  374.                 quanityItem = " " + quanityItem;
  375.             }
  376.             quanityItem += " | ";
  377.  
  378.             return quanityItem;
  379.         }
  380.  
  381.         public string Cost(int cost)
  382.         {
  383.             string costItem = " " + cost;
  384.  
  385.             while (costItem.Length < 9)
  386.             {
  387.                 costItem = " " + costItem;
  388.             }
  389.             costItem += " |";
  390.  
  391.             return costItem;
  392.         }
  393.  
  394.         public void RenderCommandTrade()
  395.         {
  396.             Console.SetCursorPosition(0, 14);
  397.  
  398.             Console.WriteLine("1. Купить. \n2. Продать. \n3. Уйти.");
  399.         }
  400.  
  401.         public void Error()
  402.         {
  403.             Console.WriteLine("Нужно построить зиккурат... Или правильно ввести данные.");
  404.             Console.ReadKey();
  405.         }
  406.     }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement