Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class SchoolLibrary {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] strings = scanner.nextLine().split("&");
- List<String> books = new ArrayList<>();
- for (String string : strings) {
- books.add(string);
- }
- String command = scanner.nextLine();
- while (!"Done".equals(command)) {
- String[] tokens = command.split("\\s+\\| ");
- String input = tokens[0];
- switch (input) {
- case "Add Book":
- if (!books.contains(tokens[1])) {
- books.add(0, tokens[1]);
- }
- break;
- case "Take Book":
- if (books.contains(tokens[1])) {
- books.remove(tokens[1]);
- }
- break;
- case "Swap Books":
- String book1 = tokens[1];
- String book2 = tokens[2];
- int book1Index = books.indexOf(book1);
- int book2Index = books.indexOf(book2);
- books.set(book1Index, book2);
- books.set(book2Index, book1);
- break;
- case "Insert Book":
- books.add(tokens[1]);
- break;
- case "Check Book":
- int index = Integer.parseInt(tokens[1]);
- if (index >= 0 && index <= books.size()) {
- System.out.println(books.get(index));
- }
- break;
- }
- command = scanner.nextLine();
- }
- System.out.println(String.join(", ", books));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement