Advertisement
svephoto

School Library [Java]

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