Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class problem03 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> books = Arrays.stream(scanner.nextLine().split("\\&")).collect(Collectors.toList());
- String input = scanner.nextLine();
- while (!input.equals("Done")){
- String[] commands = input.split(" \\|\\ ");
- String mainCommand = commands[0];
- switch (mainCommand) {
- case "Add Book":
- String book = commands[1];
- if (books.contains(book)) {
- input = scanner.nextLine();
- continue;
- }else {
- books.add(0, book);
- }
- break;
- case "Take Book":
- book = commands[1];
- if(books.contains(book)) {
- books.remove(book);
- }else {
- input= scanner.nextLine();
- continue;
- }
- break;
- case "Swap Books":
- book = commands[1];
- String book2 = commands[2];
- if(books.contains(book)&&books.contains(book2)){
- int indexFirstBook = books.indexOf(book);
- int indexSecondBook = books.indexOf(book2);
- Collections.swap(books, indexFirstBook,indexSecondBook);
- }else {
- input = scanner.nextLine();
- continue;
- }
- break;
- case "Insert Book":
- book = commands[1];
- if(!books.contains(book)) {
- int lastIndex = books.size();
- books.add(lastIndex, book);
- }else {
- input= scanner.nextLine();
- continue;
- }
- break;
- case "Check Book":
- int index = Integer.parseInt(commands[1]);
- if(books.size()-1 >=index){
- System.out.println(books.get(index));
- }else {
- input = scanner.nextLine();
- continue;
- }
- break;
- }
- input=scanner.nextLine();
- }
- System.out.println(String.join(", ",books));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment