Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _03._SchoolLibrary
- {
- class SchoolLibrary
- {
- static void Main()
- {
- List<string> books = Console.ReadLine()
- .Split('&')
- .ToList();
- string command = Console.ReadLine();
- while (command != "Done")
- {
- string[] currentCommand = command.Split(" | ");
- string instruction = currentCommand[0];
- if (instruction == "Add Book")
- {
- string bookName = currentCommand[1];
- if (!books.Contains(bookName))
- {
- books.Insert(0, bookName);
- }
- }
- else if (instruction == "Take Book")
- {
- string bookName = currentCommand[1];
- if (books.Contains(bookName))
- {
- books.Remove(bookName);
- }
- }
- else if (instruction == "Swap Books")
- {
- string firstBook = currentCommand[1];
- string secondBook = currentCommand[2];
- if (books.Contains(firstBook) && books.Contains(secondBook))
- {
- int firstIndex = books.IndexOf(firstBook);
- int secondIndex = books.IndexOf(secondBook);
- books.Remove(secondBook);
- books.Insert(firstIndex, secondBook);
- books.Remove(firstBook);
- books.Insert(secondIndex, firstBook);
- }
- }
- else if (instruction == "Insert Book")
- {
- string bookName = currentCommand[1];
- books.Add(bookName);
- }
- else if (instruction == "Check Book")
- {
- int index = int.Parse(currentCommand[1]);
- if (0 <= index && index < books.Count)
- {
- Console.WriteLine(books[index]);
- }
- }
- command = Console.ReadLine();
- }
- Console.WriteLine(string.Join(", ", books));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment