Advertisement
gospod1978

MidExam/Group2/Tanks Collector

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