Teo_Yanchev

C# Fundamentals - 03. SchoolLibrary - MidExam 10.12.2020

Oct 20th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03._SchoolLibrary
  6. {
  7. class SchoolLibrary
  8. {
  9. static void Main()
  10. {
  11. List<string> books = Console.ReadLine()
  12. .Split('&')
  13. .ToList();
  14.  
  15. string command = Console.ReadLine();
  16. while (command != "Done")
  17. {
  18. string[] currentCommand = command.Split(" | ");
  19. string instruction = currentCommand[0];
  20.  
  21. if (instruction == "Add Book")
  22. {
  23. string bookName = currentCommand[1];
  24. if (!books.Contains(bookName))
  25. {
  26. books.Insert(0, bookName);
  27. }
  28. }
  29.  
  30. else if (instruction == "Take Book")
  31. {
  32. string bookName = currentCommand[1];
  33.  
  34. if (books.Contains(bookName))
  35. {
  36. books.Remove(bookName);
  37. }
  38. }
  39.  
  40. else if (instruction == "Swap Books")
  41. {
  42. string firstBook = currentCommand[1];
  43. string secondBook = currentCommand[2];
  44.  
  45. if (books.Contains(firstBook) && books.Contains(secondBook))
  46. {
  47. int firstIndex = books.IndexOf(firstBook);
  48. int secondIndex = books.IndexOf(secondBook);
  49.  
  50. books.Remove(secondBook);
  51. books.Insert(firstIndex, secondBook);
  52. books.Remove(firstBook);
  53. books.Insert(secondIndex, firstBook);
  54. }
  55. }
  56.  
  57. else if (instruction == "Insert Book")
  58. {
  59. string bookName = currentCommand[1];
  60.  
  61. books.Add(bookName);
  62. }
  63.  
  64. else if (instruction == "Check Book")
  65. {
  66. int index = int.Parse(currentCommand[1]);
  67.  
  68. if (0 <= index && index < books.Count)
  69. {
  70. Console.WriteLine(books[index]);
  71. }
  72. }
  73.  
  74. command = Console.ReadLine();
  75. }
  76.  
  77. Console.WriteLine(string.Join(", ", books));
  78. }
  79. }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment