Advertisement
SIRAKOV4444

Untitled

Feb 27th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. package com.company;
  2. import java.text.DecimalFormat;
  3. import java.util.*;
  4. import java.util.stream.Stream;
  5.  
  6. public class Ibiza {
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9.  
  10. String[] input = sc.nextLine().split("&");
  11. List<String> shelfWithBooks = new ArrayList<>(Arrays.asList(input));
  12.  
  13. String command;
  14. while (!"Done".equals(command = sc.nextLine())) {
  15. String[] tokens = command.split(" \\| ");
  16. if ("Add Book".equals(tokens[0])) {
  17. String bookName = tokens[1];
  18.  
  19. if (shelfWithBooks.contains(bookName)) {
  20. continue;
  21. } else {
  22. shelfWithBooks.add(0, bookName);
  23. }
  24. }
  25. if ("Take Book".equals(tokens[0])) {
  26. String bookName = tokens[1];
  27.  
  28. if (!shelfWithBooks.contains(bookName)) {
  29. continue;
  30. } else {
  31. shelfWithBooks.remove(bookName);
  32. }
  33. }
  34. if ("Swap Books".equals(tokens[0])) {
  35. String firstBook = tokens[1];
  36. String secondBook = tokens[2];
  37. int indexOfTheFirstBook = shelfWithBooks.indexOf(firstBook);
  38. int indexOfTheSecondBook = shelfWithBooks.indexOf(secondBook);
  39.  
  40. if (shelfWithBooks.contains(firstBook) && shelfWithBooks.contains(secondBook)) {
  41. Collections.swap(shelfWithBooks, indexOfTheFirstBook, indexOfTheSecondBook);
  42. }
  43. }
  44. if ("Insert Book".equals(tokens[0])) {
  45. String bookName = tokens[1];
  46. shelfWithBooks.add(bookName);
  47. }
  48. if ("Check Book".equals(tokens[0])) {
  49. int index = Integer.parseInt(tokens[1]);
  50.  
  51. if (index < 0 || index > shelfWithBooks.size()) {
  52. continue;
  53. } else {
  54. String bookName = shelfWithBooks.get(index);
  55. System.out.println(bookName);
  56. }
  57. }
  58. }
  59. String completedBookshelf = String.join(", ", shelfWithBooks);
  60. System.out.println(completedBookshelf);
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement