mattnguyen

My Retake Mid Exam 17/08/2021 Problem 3

Aug 17th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Numerics;
  4. using System.Text;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Reflection.Metadata.Ecma335;
  8. using System.Runtime.ExceptionServices;
  9. using System.Threading;
  10. using System.Text.RegularExpressions;
  11.  
  12.  
  13. namespace ConsoleApp24
  14. {
  15. class Program
  16. {
  17. static void Main(string[] args)
  18. {
  19. List<string> products = Console.ReadLine().Split("|").ToList();
  20. string input = Console.ReadLine();
  21.  
  22. while (input != "Shop!")
  23. {
  24. string[] commands = input.Split("%");
  25.  
  26. switch (commands[0])
  27. {
  28. case "Important":
  29. if (products.Contains(commands[1]))
  30. {
  31. products.Remove(commands[1]);
  32. products.Insert(0, commands[1]);
  33. }
  34. else
  35. {
  36. products.Insert(0, commands[1]);
  37. }
  38. break;
  39. case "Add":
  40. if (!products.Contains(commands[1]))
  41. {
  42. products.Add(commands[1]);
  43. }
  44. else
  45. {
  46. Console.WriteLine("The product is already in the list");
  47. }
  48. break;
  49. case "Swap":
  50.  
  51. if (!products.Contains(commands[1]))
  52. {
  53. Console.WriteLine($"Product {commands[1]} missing!");
  54. }
  55. else if (!products.Contains(commands[2]))
  56. {
  57. Console.WriteLine($"Product {commands[2]} missing!");
  58. }
  59. else
  60. {
  61. int index1 = products.IndexOf(commands[1]);
  62. int index2 = products.IndexOf(commands[2]);
  63. string temp = products[index1];
  64. products[index1] = products[index2];
  65. products[index2] = temp;
  66. }
  67.  
  68. break;
  69. case "Remove":
  70. if (products.Contains(commands[1]))
  71. {
  72. products.Remove(commands[1]);
  73. }
  74. else
  75. {
  76. Console.WriteLine($"Product {commands[1]} isn't in the list.");
  77. }
  78. break;
  79. }
  80.  
  81. input = Console.ReadLine();
  82. }
  83.  
  84. for (int i = 0; i < products.Count; i++)
  85. {
  86. Console.WriteLine($"{i + 1}. {products[i]}");
  87. }
  88.  
  89. }
  90. }
  91. }
  92.  
  93.  
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment