eternalmeg

test

Oct 23rd, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class problem03 {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. List<String> books = Arrays.stream(scanner.nextLine().split("\\&")).collect(Collectors.toList());
  12.  
  13.  
  14. String input = scanner.nextLine();
  15.  
  16. while (!input.equals("Done")){
  17. String[] commands = input.split(" \\|\\ ");
  18. String mainCommand = commands[0];
  19.  
  20.  
  21. switch (mainCommand) {
  22. case "Add Book":
  23. String book = commands[1];
  24. if (books.contains(book)) {
  25. input = scanner.nextLine();
  26. continue;
  27. }else {
  28. books.add(0, book);
  29. }
  30. break;
  31. case "Take Book":
  32. book = commands[1];
  33. if(books.contains(book)) {
  34. books.remove(book);
  35. }else {
  36. input= scanner.nextLine();
  37. continue;
  38. }
  39. break;
  40. case "Swap Books":
  41. book = commands[1];
  42. String book2 = commands[2];
  43. if(books.contains(book)&&books.contains(book2)){
  44. int indexFirstBook = books.indexOf(book);
  45. int indexSecondBook = books.indexOf(book2);
  46. Collections.swap(books, indexFirstBook,indexSecondBook);
  47. }else {
  48. input = scanner.nextLine();
  49. continue;
  50. }
  51.  
  52.  
  53. break;
  54. case "Insert Book":
  55. book = commands[1];
  56. if(!books.contains(book)) {
  57. int lastIndex = books.size();
  58. books.add(lastIndex, book);
  59. }else {
  60. input= scanner.nextLine();
  61. continue;
  62. }
  63. break;
  64. case "Check Book":
  65. int index = Integer.parseInt(commands[1]);
  66. if(books.size()-1 >=index){
  67. System.out.println(books.get(index));
  68. }else {
  69. input = scanner.nextLine();
  70. continue;
  71. }
  72. break;
  73. }
  74.  
  75. input=scanner.nextLine();
  76. }
  77.  
  78. System.out.println(String.join(", ",books));
  79. }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment