Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class SchoolLibrary {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split("&");
- List<String> shelfWithBooks = new ArrayList<>(Arrays.asList(input));
- String command;
- while (!"Done".equals(command = scanner.nextLine())) {
- String[] tokens = command.split(" \\| ");
- if ("Add Book".equals(tokens[0])) {
- String bookName = tokens[1];
- if (shelfWithBooks.contains(bookName)) {
- continue;
- } else {
- shelfWithBooks.add(0, bookName);
- }
- }
- if ("Take Book".equals(tokens[0])) {
- String bookName = tokens[1];
- if (!shelfWithBooks.contains(bookName)) {
- continue;
- } else {
- shelfWithBooks.remove(bookName);
- }
- }
- if ("Swap Books".equals(tokens[0])) {
- String firstBook = tokens[1];
- String secondBook = tokens[2];
- int indexOfTheFirstBook = shelfWithBooks.indexOf(tokens[1]);
- int indexOfTheSecondBook = shelfWithBooks.indexOf(tokens[2]);
- if (shelfWithBooks.contains(firstBook) && shelfWithBooks.contains(secondBook)) {
- Collections.swap(shelfWithBooks, indexOfTheFirstBook, indexOfTheSecondBook);
- }
- }
- if ("Insert Book".equals(tokens[0])) {
- String bookName = tokens[1];
- shelfWithBooks.add(bookName);
- }
- if ("Check Book".equals(tokens[0])) {
- int index = Integer.parseInt(tokens[1]);
- if (index < 0 || index > shelfWithBooks.size()) {
- continue;
- } else {
- String bookName = shelfWithBooks.get(index);
- System.out.println(bookName);
- }
- }
- }
- String completedBookshelf = String.join(", ", shelfWithBooks);
- System.out.println(completedBookshelf);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement