Vapio

task18

Apr 6th, 2021 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. public class Program
  5. {
  6.     public static void Main()
  7.     {
  8.         string[] names = new string[0];
  9.         string[] positions = new string[0];
  10.  
  11.         string choose;
  12.         bool isRun = true;
  13.         while (isRun)
  14.         {
  15.             Console.WriteLine("\nInput number of action : ");
  16.             Console.WriteLine("1. Add case.\n" +
  17.                               "2. Output all cases.\n" +
  18.                               "3. Remove case.\n" +
  19.                               "4. Search case on name.\n" +
  20.                               "5. Exit.");
  21.  
  22.             choose = Console.ReadLine();
  23.             switch (choose)
  24.             {
  25.                 case "1":
  26.                     AddCase(ref names, ref positions);
  27.                     break;
  28.                 case "2":
  29.                     OutputCases(names, positions);
  30.                     break;
  31.                 case "3":
  32.                     RemoveCase(ref names, ref positions);
  33.                     break;
  34.                 case "4":
  35.                     SearchCase(names, positions);
  36.                     break;
  37.                 case "5":
  38.                     isRun = false;
  39.                     break;
  40.                 default:
  41.                     Console.WriteLine("You write wrong number of command.");
  42.                     break;
  43.             }
  44.         }
  45.     }
  46.  
  47.     public static void AddCase(ref string[] names, ref string[] positions)
  48.     {
  49.         string name;
  50.         string position;
  51.         Console.WriteLine("Input name of person : ");
  52.         name = Console.ReadLine();
  53.         AddElement(ref names, name);
  54.         Console.WriteLine("Input position of person : ");
  55.         position = Console.ReadLine();
  56.         AddElement(ref positions, position);
  57.     }
  58.  
  59.     public static void AddElement(ref string[] array, string element)
  60.     {
  61.         string[] arrayNew = new string[array.Length + 1];
  62.  
  63.         for (int i = 0; i < array.Length; ++i)
  64.             arrayNew[i] = array[i];
  65.         arrayNew[array.Length] = element;
  66.  
  67.         array = arrayNew;
  68.     }
  69.  
  70.     public static void SearchCase(string[] names, string[] positions)
  71.     {
  72.         bool isFind = true;
  73.         bool isFound = false;
  74.         string input;
  75.  
  76.         Console.WriteLine("Please input name of person : ");
  77.         input = Console.ReadLine();
  78.         for (int i = 0; isFind && i < names.Length; ++i)
  79.         {
  80.             if (names[i].Equals(input))
  81.             {
  82.                 isFound = true;
  83.                 isFind = false;
  84.                 Console.WriteLine($"{i}. {names[i]} - {positions[i]}");
  85.             }
  86.         }
  87.  
  88.         if (isFound == false)
  89.         {
  90.             Console.WriteLine("Cannot find person");
  91.         }
  92.     }
  93.  
  94.     public static void OutputCases(string[] names, string[] positions)
  95.     {
  96.         for (int i = 0; i < names.GetLength(0); ++i)
  97.             Console.Write($"{i}. {names[i]} - {positions[i]} ");
  98.         Console.WriteLine();
  99.     }
  100.  
  101.     public static void RemoveCase(ref string[] names, ref string[] positions)
  102.     {
  103.         if (names.Length != 0)
  104.         {
  105.             int caseIndex = 0;
  106.             bool isInput = true;
  107.             while (isInput)
  108.             {
  109.                 Console.WriteLine($"Amount of cases : {names.Length}");
  110.                 Console.WriteLine("Please type a number of case : ");
  111.                 caseIndex = Convert.ToInt32(Console.ReadLine());
  112.                 if (caseIndex >= 0 && caseIndex < names.Length)
  113.                     isInput = false;
  114.             }
  115.             RemoveElement(ref names, caseIndex);
  116.             RemoveElement(ref positions, caseIndex);
  117.         }
  118.     }
  119.  
  120.     public static void RemoveElement(ref string[] array, int index)
  121.     {
  122.         string[] arrayNew = new string[array.Length - 1];
  123.  
  124.         for (int i = 0; i < index; ++i)
  125.             arrayNew[i] = array[i];
  126.  
  127.         for (int i = index, j = index + 1; j < array.Length; ++i, ++j)
  128.             arrayNew[i] = array[j];
  129.  
  130.         array = arrayNew;
  131.     }
  132. }
Add Comment
Please, Sign In to add comment