Advertisement
AngelVasilev

SoftUni Fundamentals - School Library

Feb 25th, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace School_Library
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> books = Console.ReadLine().Split("&").ToList();
  12.             string command = Console.ReadLine();
  13.  
  14.             while (command != "Done")
  15.             {
  16.                 string[] splittedCommand = command.Split(" | ").ToArray();
  17.  
  18.                 switch (splittedCommand[0])
  19.                 {
  20.                     case "Add Book":
  21.                         if (!books.Contains(splittedCommand[1]))
  22.                         {
  23.                             books.Insert(0, splittedCommand[1]);
  24.                         }
  25.                         break;
  26.                     case "Take Book":
  27.                         if (books.Contains(splittedCommand[1]))
  28.                         {
  29.                             books.Remove(splittedCommand[1]);
  30.                         }
  31.                         break;
  32.                     case "Swap Books":
  33.                         if (books.Contains(splittedCommand[1]) && books.Contains(splittedCommand[2]))
  34.                         {
  35.                             int indexOfFirstBook = books.IndexOf(splittedCommand[1]);
  36.                             int indexOfSecondBook = books.IndexOf(splittedCommand[2]);
  37.                             string temp = books[indexOfFirstBook];
  38.  
  39.                             books[indexOfFirstBook] = splittedCommand[2];
  40.                             books[indexOfSecondBook] = temp;
  41.                         }
  42.                         break;
  43.                     case "Insert Book":
  44.                         books.Add(splittedCommand[1]);
  45.                         break;
  46.                     case "Check Book":
  47.                         if (books.ElementAtOrDefault(int.Parse(splittedCommand[1])) != null)
  48.                         {
  49.                             string searchedBook = books.ElementAt(int.Parse(splittedCommand[1]));
  50.                             Console.WriteLine(searchedBook);
  51.                         }
  52.                         break;
  53.                 }
  54.                 command = Console.ReadLine();
  55.             }
  56.  
  57.             Console.WriteLine(string.Join(", ", books));
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement