Advertisement
2629881

Untitled

Aug 12th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.15 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. using System.Threading;
  7.  
  8. namespace inventory
  9. {    
  10.     public class Inventory
  11.     {        
  12.         public string[] arrayItems = new string[6];
  13.         int itemsCount = 0;
  14.         public void AddItem(string item) // dodaje item
  15.         {
  16.             //ewentualnie
  17.             //if (items.Contains(null))
  18.             //{
  19.             //    items[Array.IndexOf(items, null)] = newItem;
  20.             //}
  21.             if (itemsCount == 6)
  22.             {
  23.                 Console.WriteLine("Inventory full");
  24.                 Thread.Sleep(350);
  25.             }
  26.             else
  27.             {
  28.                 arrayItems[Array.IndexOf(arrayItems, null)] = item;
  29.                 itemsCount++;
  30.                 Console.WriteLine("{0} has been added to your inventory. Number of items:{1}", item, itemsCount);
  31.                 Thread.Sleep(350);
  32.             }
  33.         }
  34.  
  35.         public void SwitchItem(int number, string item)
  36.         {
  37.             arrayItems[number - 1] = item;
  38.         }
  39.         public void DisplayItemInBorder(int a, int b) // wyświetla itemy w ramce
  40.         {
  41.             //item0
  42.             Console.SetCursorPosition(2, 5);
  43.             Console.Write(arrayItems[0]);
  44.             //item1
  45.             Console.SetCursorPosition(5+a, 5);
  46.             Console.Write(arrayItems[1]);
  47.             //item2
  48.             Console.SetCursorPosition(8+a+b, 5);
  49.             Console.Write(arrayItems[2]);
  50.             //item3
  51.             Console.SetCursorPosition(2, 9);
  52.             Console.Write(arrayItems[3]);
  53.             //item4
  54.             Console.SetCursorPosition(5+a, 9);
  55.             Console.Write(arrayItems[4]);
  56.             //item5
  57.             Console.SetCursorPosition(8+a+b, 9);
  58.             Console.Write(arrayItems[5]);
  59.         }
  60.         public void DisplayItems() //to było zanim zrobiłem ramkę w ramach ćwiczeń
  61.         {
  62.             Console.SetCursorPosition(0, 1);
  63.             Console.WriteLine("Your items are:");
  64.             Console.SetCursorPosition(0, 2);
  65.             Console.Write(new string(' ', Console.WindowWidth));
  66.             Console.SetCursorPosition(0, 2);
  67.             foreach (string item in arrayItems)
  68.             {
  69.                 if (item == null)
  70.                 {
  71.                     Console.Write("Empty;");
  72.                 }
  73.                 else
  74.                 {
  75.                     Console.Write(item + ";");
  76.                 }
  77.             }
  78.         }
  79.  
  80.         public int FindLongerName(int a, int b) // to znajduje dłuższą nazwę z pary żeby skalować ramkę
  81.         {                                       // moze być zbędny kod
  82.             int numberOfChars;                  
  83.             if (arrayItems[a] == null)
  84.             {
  85.                 numberOfChars = 1;
  86.                 return numberOfChars;
  87.             }
  88.             else
  89.             {
  90.                 if (arrayItems[b] == null)
  91.                 {
  92.                     numberOfChars = arrayItems[a].Length;
  93.                     return numberOfChars;
  94.                 }
  95.                 else
  96.                 {
  97.                     if (arrayItems[a].Length > arrayItems[b].Length)
  98.                     {
  99.                         numberOfChars = arrayItems[a].Length;
  100.                         return numberOfChars;
  101.                     }
  102.                     else
  103.                     {
  104.                         numberOfChars = arrayItems[b].Length;
  105.                         return numberOfChars;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.     }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.     public class Border
  117.     {
  118.         Inventory inventory = new Inventory();
  119.        
  120.         public int row = Console.CursorTop;
  121.         public int col = Console.CursorLeft;
  122.  
  123.         public void WriteFrame(string s, int x, int y) //pozwala całą ramkę przesunąć na raz
  124.         {
  125.             Console.SetCursorPosition(row + x, col + y);
  126.             Console.Write(s);
  127.         }
  128.        
  129.         public void DisplayFrame(int a, int b, int c)
  130.         {
  131.             Console.SetCursorPosition(0, 3);                            
  132.             Console.Write(new string(' ', Console.WindowWidth));
  133.             Console.Write(new string(' ', Console.WindowWidth));
  134.             Console.Write(new string(' ', Console.WindowWidth));
  135.             Console.Write(new string(' ', Console.WindowWidth));
  136.             Console.Write(new string(' ', Console.WindowWidth));        //czyści konsole w obszarze ramki
  137.             Console.Write(new string(' ', Console.WindowWidth));
  138.             Console.Write(new string(' ', Console.WindowWidth));
  139.             Console.Write(new string(' ', Console.WindowWidth));
  140.             Console.Write(new string(' ', Console.WindowWidth));
  141.            
  142.             //top1                                                    //kod ramki poniżej
  143.             WriteFrame("-", 1, 3);
  144.             WriteFrame("-", 3, 3);
  145.             for (int i = 0; i <= a; i++)
  146.             {
  147.                 WriteFrame("-", 2 + i, 3);
  148.             }
  149.             //mid1
  150.             WriteFrame("-", 1, 7);
  151.             WriteFrame("-", 3, 7);
  152.             for (int i = 0; i <= a; i++)
  153.             {
  154.                 WriteFrame("-", 2 + i, 7);
  155.             }
  156.             //bot1
  157.             WriteFrame("-", 1, 11);
  158.             WriteFrame("-", 3, 11);
  159.             for (int i = 0; i <= a; i++)
  160.             {
  161.                 WriteFrame("-", 2 + i, 11);
  162.             }
  163.             //top2
  164.             for (int i = 0; i <= a + b; i++)
  165.             {
  166.                 WriteFrame("-", 5 + i, 3);
  167.             }
  168.             //mid2
  169.             for (int i = 0; i <= a + b; i++)
  170.             {
  171.                 WriteFrame("-", 5 + i, 7);
  172.             }
  173.             //bot2
  174.             for (int i = 0; i <= a + b; i++)
  175.             {
  176.                 WriteFrame("-", 5 + i, 11);
  177.             }
  178.             //top3
  179.             for (int i = 0; i <= a + b + c; i++)
  180.             {
  181.                 WriteFrame("-", 8 + i, 3);
  182.             }
  183.             //mid3
  184.             for (int i = 0; i <= a + b + c; i++)
  185.             {
  186.                 WriteFrame("-", 8 + i, 7);
  187.             }
  188.             //bot3
  189.             for (int i = 0; i <= a + b + c; i++)
  190.             {
  191.                 WriteFrame("-", 8 + i, 11);
  192.             }
  193.             //vertical 1
  194.             WriteFrame("1", 0, 3);
  195.             WriteFrame("|", 0, 4);
  196.             WriteFrame("|", 0, 5);
  197.             WriteFrame("|", 0, 6);
  198.             WriteFrame("4", 0, 7);
  199.             WriteFrame("|", 0, 8);
  200.             WriteFrame("|", 0, 9);
  201.             WriteFrame("|", 0, 10);
  202.             WriteFrame("+", 0, 11);
  203.             //vertical 2
  204.             WriteFrame("2", 3 + a, 3);
  205.             WriteFrame("|", 3 + a, 4);
  206.             WriteFrame("|", 3 + a, 5);
  207.             WriteFrame("|", 3 + a, 6);
  208.             WriteFrame("5", 3 + a, 7);
  209.             WriteFrame("|", 3 + a, 8);
  210.             WriteFrame("|", 3 + a, 9);
  211.             WriteFrame("|", 3 + a, 10);
  212.             WriteFrame("+", 3 + a, 11);
  213.             //vertical 3
  214.             WriteFrame("3", 6 + a + b, 3);
  215.             WriteFrame("|", 6 + a + b, 4);
  216.             WriteFrame("|", 6 + a + b, 5);
  217.             WriteFrame("|", 6 + a + b, 6);
  218.             WriteFrame("6", 6 + a + b, 7);
  219.             WriteFrame("|", 6 + a + b, 8);
  220.             WriteFrame("|", 6 + a + b, 9);
  221.             WriteFrame("|", 6 + a + b, 10);
  222.             WriteFrame("+", 6 + a + b, 11);
  223.             //vertical 4
  224.             WriteFrame("+", 9 + a + b + c, 3);
  225.             WriteFrame("|", 9 + a + b + c, 4);
  226.             WriteFrame("|", 9 + a + b + c, 5);
  227.             WriteFrame("|", 9 + a + b + c, 6);
  228.             WriteFrame("+", 9 + a + b + c, 7);
  229.             WriteFrame("|", 9 + a + b + c, 8);
  230.             WriteFrame("|", 9 + a + b + c, 9);
  231.             WriteFrame("|", 9 + a + b + c, 10);
  232.             WriteFrame("+", 9 + a + b + c, 11);
  233.         }
  234.     }
  235.  
  236.  
  237.  
  238.  
  239. class Program
  240.     {
  241.         static void Main(string[] args)
  242.         {
  243.             Inventory inventory = new Inventory();
  244.             Border border = new Border();
  245.             Console.WriteLine("Wpisz komende /add /swap /display /x.");
  246.          
  247.  
  248.             while (true)
  249.             {
  250.                 border.DisplayFrame(inventory.FindLongerName(0, 3), inventory.FindLongerName(1, 4), inventory.FindLongerName(2, 5));
  251.                 inventory.DisplayItemInBorder(inventory.FindLongerName(0, 3), inventory.FindLongerName(1, 4));
  252.                 Console.SetCursorPosition(0,12);
  253.                 Console.Write(new string(' ', Console.WindowWidth));
  254.                 Console.Write(new string(' ', Console.WindowWidth));
  255.                 inventory.DisplayItems();
  256.                 Console.SetCursorPosition(0, 12);
  257.                 string input = Console.ReadLine();
  258.                 string command = string.Empty;
  259.                 command = input.Split(' ')[0];
  260.  
  261.                 switch (command)
  262.                 {
  263.                     case "/display":  //to juz nic nie robi
  264.                         inventory.DisplayItems();
  265.                         break;
  266.                     case "/add":
  267.                         string item = input.Split(' ')[1]; //tutaj potrzebuję ulepszyć, żeby móc dodawać itemy z 2-3 członowymi nazwami np. Rod of Ages wywali program
  268.                         inventory.AddItem(item);
  269.                         inventory.DisplayItems();
  270.                         break;
  271.                     case "/swap":           //zamienia item
  272.                         int numer = Convert.ToInt32(input.Split(' ')[1]);
  273.                         string newItem = input.Split(' ')[2];
  274.                         inventory.SwitchItem(numer, newItem);
  275.                         break;
  276.                     case "/x": //to było do testowania
  277.                         //int itemIndex = Int32.Parse(input.Split(' ')[1]);
  278.                         //inventory.DisplaySelectedItem(itemIndex);
  279.                         //Console.Write(inventory.FindLongerName());
  280.                         //Console.Write(inventory.FindLongerName(0, 3));
  281.                         Thread.Sleep(1000);
  282.                         break;
  283.                 }
  284.             }
  285.             //Console.ReadKey();
  286.         }
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement