Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. List<String> books = new ArrayList<>();
  12.  
  13. books = Arrays.stream(scanner.nextLine().split("&")).collect(Collectors.toList());
  14.  
  15. while (true){
  16.  
  17. String[] cmd = scanner.nextLine().split(" \\| ");
  18.  
  19. if (cmd[0].equals("Done")){
  20. break;
  21. }
  22.  
  23. if (cmd[0].equals("Add Book")){
  24.  
  25. if (!books.contains(cmd[1])) {
  26. books.add(0,cmd[1]);
  27. }
  28. }
  29. else if (cmd[0].equals("Take Book")){
  30.  
  31. if (books.contains(cmd[1])) {
  32. books.remove(cmd[1]);
  33. }
  34. }
  35. else if (cmd[0].equals("Swap Books")){
  36.  
  37. String book1 = cmd[1];
  38. String book2 = cmd[2];
  39.  
  40. if (books.contains(book1) && books.contains(book2)){
  41.  
  42. int indexOfBook1 = books.indexOf(book1);
  43. int indexOfBook2 = books.indexOf(book2);
  44.  
  45. books.set(indexOfBook1,book2);
  46. books.set(indexOfBook2,book1);
  47. }
  48. }
  49. else if (cmd[0].equals("Insert Book")){
  50.  
  51. if (!books.contains(cmd[1])) {
  52. books.add(cmd[1]);
  53. }
  54. }
  55. else if (cmd[0].equals("Check Book")){
  56.  
  57. int index = Integer.parseInt(cmd[1]);
  58.  
  59. if (index< books.size()) {
  60. System.out.println(books.get(index));
  61. }
  62. }
  63. }
  64.  
  65. for (String book : books) {
  66. System.out.print(book +" ");
  67. }
  68. System.out.println();
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement