petyaminkova91

Untitled

Nov 26th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _03_TanksCollector
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<string> tanks = Console.ReadLine().Split(", ").ToList();
  12. int commandsCount = int.Parse(Console.ReadLine());
  13.  
  14.  
  15. for (int i = 0; i < commandsCount; i++)
  16. {
  17. string[] commandParts = Console.ReadLine().Split(", ");
  18. string tankName = String.Empty;
  19. int index = 0;
  20.  
  21. string command = commandParts[0];
  22.  
  23. if (command == "Add")
  24. {
  25. tankName = commandParts[1];
  26.  
  27. if (tanks.Contains(tankName))
  28. {
  29. Console.WriteLine("Tank is already bought");
  30. }
  31. else
  32. {
  33. Console.WriteLine("Tank successfully bought");
  34. tanks.Add(tankName);
  35. }
  36. }
  37. else if (command == "Remove")
  38. {
  39. tankName = commandParts[1];
  40.  
  41. if (tanks.Contains(tankName))
  42. {
  43. Console.WriteLine("Tank successfully sold");
  44. tanks.Remove(tankName);
  45. }
  46. else
  47. {
  48. Console.WriteLine("Tank not found");
  49. }
  50. }
  51. else if (command == "Remove At")
  52. {
  53. index = int.Parse(commandParts[1]);
  54.  
  55. if (0 <= index && index < tanks.Count)
  56. {
  57. tanks.RemoveAt(index);
  58. Console.WriteLine("Tank successfully sold");
  59. }
  60. else
  61. {
  62. Console.WriteLine("Index out of range");
  63. }
  64. }
  65. else if (command == "Insert")
  66. {
  67. index = int.Parse(commandParts[1]);
  68. tankName = commandParts[2];
  69. bool isIndexValid = 0 <= index && index <= tanks.Count;
  70.  
  71. if (isIndexValid && tanks.Contains(tankName))
  72. {
  73. Console.WriteLine("Tank is already bought");
  74. }
  75. else if (isIndexValid && !tanks.Contains(tankName))
  76. {
  77. Console.WriteLine("Tank successfully bought");
  78. tanks.Insert(index, tankName);
  79. }
  80. else
  81. {
  82. Console.WriteLine("Index out of range");
  83. }
  84. }
  85. }
  86.  
  87. Console.WriteLine(String.Join(", ", tanks));
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment