Advertisement
lifesaver800

School Library

Feb 26th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class SchoolLibrary {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] strings = scanner.nextLine().split("&");
  10.         List<String> books = new ArrayList<>();
  11.  
  12.         for (String string : strings) {
  13.             books.add(string);
  14.         }
  15.  
  16.         String command = scanner.nextLine();
  17.         while (!"Done".equals(command)) {
  18.             String[] tokens = command.split("\\s+\\| ");
  19.             String input = tokens[0];
  20.  
  21.             switch (input) {
  22.                 case "Add Book":
  23.  
  24.                     if (!books.contains(tokens[1])) {
  25.                         books.add(0, tokens[1]);
  26.                     }
  27.                     break;
  28.                 case "Take Book":
  29.  
  30.                     if (books.contains(tokens[1])) {
  31.                         books.remove(tokens[1]);
  32.                     }
  33.                     break;
  34.                 case "Swap Books":
  35.                     String book1 = tokens[1];
  36.                     String book2 = tokens[2];
  37.                     int book1Index = books.indexOf(book1);
  38.                     int book2Index = books.indexOf(book2);
  39.                     books.set(book1Index, book2);
  40.                     books.set(book2Index, book1);
  41.                     break;
  42.                 case "Insert Book":
  43.                     books.add(tokens[1]);
  44.                     break;
  45.                 case "Check Book":
  46.                     int index = Integer.parseInt(tokens[1]);
  47.                     if (index >= 0 && index <= books.size()) {
  48.                         System.out.println(books.get(index));
  49.                     }
  50.                     break;
  51.             }
  52.  
  53.             command = scanner.nextLine();
  54.         }
  55.  
  56.         System.out.println(String.join(", ", books));
  57.  
  58.  
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement