diff --git a/src/main/java/seedu/bookbuddy/Book.java b/src/main/java/seedu/bookbuddy/Book.java index 29cd47b025..df1f1c46e7 100644 --- a/src/main/java/seedu/bookbuddy/Book.java +++ b/src/main/java/seedu/bookbuddy/Book.java @@ -2,27 +2,27 @@ public class Book { - public String description; + public String title; protected boolean isRead; /** - * Creates a new Task with the specified description. + * Creates a new Book with the specified title. * - * @param description The description of the task. + * @param title The description of the book. */ - public Book(String description) { - this.description = description; // Description of the task - this.isRead = false; //Completion status of the task(True: Read, False: Unread) + public Book(String title) { + this.title = title; // Description of the book + this.isRead = false; //Completion status of the book (True: Read, False: Unread) } /** - * Returns the description of the book. + * Returns the title of the book. * - * @return The description of the book. + * @return The title of the book. */ - public String getDescription() { - return this.description; + public String getTitle() { + return this.title; } /** @@ -39,6 +39,7 @@ public boolean isRead() { */ public void markBookAsRead() { this.isRead = true; + System.out.println("Successfully marked " + this.getTitle() + " as read."); } /** @@ -46,10 +47,11 @@ public void markBookAsRead() { */ public void markBookAsUnread() { this.isRead = false; + System.out.println("Successfully marked " + this.getTitle() + " as unread."); } public String toString() { - String statusMark = this.isRead() ? "X" : " "; // Mark with 'x' if completed - return this.description + " [" + statusMark + "]" ; + String statusMark = this.isRead() ? "R" : "U"; // Mark with 'R' if read and 'U' if unread + return "[" + statusMark + "] " + this.title; } } diff --git a/src/main/java/seedu/bookbuddy/BookBuddy.java b/src/main/java/seedu/bookbuddy/BookBuddy.java index fcbd38ee7a..c031fce6f4 100644 --- a/src/main/java/seedu/bookbuddy/BookBuddy.java +++ b/src/main/java/seedu/bookbuddy/BookBuddy.java @@ -1,24 +1,31 @@ package seedu.bookbuddy; -import java.util.Objects; import java.util.Scanner; public class BookBuddy { - private static BookList bookList = new BookList(); + private static BookList books = new BookList(); public static void main(String[] args) { + printWelcomeMessage(); + getUserInput(books); + } + + public static void printWelcomeMessage() { + System.out.println("Hello! We are BookBuddy!"); + System.out.println("How can I help you today?"); + } - System.out.println("Hello! We are bookbuddy!"); - Scanner scanner = new Scanner(System.in); - while(true) { - String input = scanner.nextLine(); - String command = input.split(" ", 2)[0]; - if (Objects.equals(command, "addBook")) { - String actualDescription = input.split(" ", 2)[1]; - bookList.addBook(actualDescription); - } else if (Objects.equals(command, "list")) { - bookList.printAllBooks(); - } + public static void getUserInput(BookList books) { + Scanner input = new Scanner(System.in); + + //noinspection InfiniteLoopStatement + while (true) { + String userInput = input.nextLine(); + Parser.parseCommand(userInput, books); } } + public static void printExitMessage() { + System.out.println("Thank you for using BookBuddy! Hope to see you again!"); + } + } diff --git a/src/main/java/seedu/bookbuddy/BookList.java b/src/main/java/seedu/bookbuddy/BookList.java index 199de1a605..96091dbe17 100644 --- a/src/main/java/seedu/bookbuddy/BookList.java +++ b/src/main/java/seedu/bookbuddy/BookList.java @@ -35,10 +35,11 @@ public Book getBook(int index){ /** * Adds a new Book to the list. - * @param taskDescription The description of the book. + * @param title The title of the book. */ - public void addBook(String taskDescription) { - books.add(new Book(taskDescription)); + public void addBook(String title) { + books.add(new Book(title)); + System.out.println("Successfully added " + title + " to the list."); } /** @@ -46,7 +47,9 @@ public void addBook(String taskDescription) { * @param index The index of the book to delete. */ public void deleteBook(int index) { - books.remove(index-1); + Book book = books.get(index - 1); + books.remove(index - 1); + System.out.println("Successfully removed " + book.getTitle() + " from the list."); } /** @@ -54,7 +57,7 @@ public void deleteBook(int index) { * @param index The index of the book to mark as read. */ public void markDoneByIndex(int index) { - books.get(index-1).markBookAsRead(); + books.get(index - 1).markBookAsRead(); } /** @@ -62,7 +65,7 @@ public void markDoneByIndex(int index) { * @param index The index of the book to mark as unread. */ public void markUndoneByIndex(int index) { - books.get(index-1).markBookAsUnread(); + books.get(index - 1).markBookAsUnread(); } /** @@ -73,8 +76,8 @@ public void printAllBooks() { System.out.println("All books:"); for (int i = 0; i < books.size(); i++) { Book currentBook = books.get(i); - System.out.print((i + 1) + "."); - System.out.println(currentBook); + System.out.print((i + 1) + ". "); + System.out.println(currentBook.toString()); } } else { System.out.println("The list is empty."); diff --git a/src/main/java/seedu/bookbuddy/Parser.java b/src/main/java/seedu/bookbuddy/Parser.java new file mode 100644 index 0000000000..afdbb6bbda --- /dev/null +++ b/src/main/java/seedu/bookbuddy/Parser.java @@ -0,0 +1,54 @@ +package seedu.bookbuddy; + +/** + * Parses inputs from the user in order to execute the correct commands. + */ + +public class Parser { + public static final String ADD_COMMAND = "add"; + public static final String REMOVE_COMMAND = "remove"; + public static final String LIST_COMMAND = "list"; + public static final String MARK_COMMAND = "mark"; + public static final String UNMARK_COMMAND = "unmark"; + public static final String EXIT_COMMAND = "bye"; + + /** + * Scans the user input for valid commands and handles them accordingly. + * @param input input from the user + * @param books ArrayList of books + */ + + public static void parseCommand( String input, BookList books) { + String[] inputArray = input.split(" ", 2); + String command = inputArray[0].toLowerCase(); + int index; + + switch (command) { + case ADD_COMMAND: + books.addBook(inputArray[1]); + break; + case REMOVE_COMMAND: + index = Integer.parseInt(inputArray[1]); + books.deleteBook(index); + break; + case LIST_COMMAND: + books.printAllBooks(); + break; + case MARK_COMMAND: + index = Integer.parseInt(inputArray[1]); + books.markDoneByIndex(index); + break; + case UNMARK_COMMAND: + index = Integer.parseInt(inputArray[1]); + books.markUndoneByIndex(index); + break; + case EXIT_COMMAND: + BookBuddy.printExitMessage(); + System.exit(0); + break; + default: + System.out.println("Sorry but that is not a valid command. Please try again"); + } + } + +} diff --git a/src/test/java/seedu/bookbuddy/BookListTest.java b/src/test/java/seedu/bookbuddy/BookListTest.java index ba72a86909..7960e929a1 100644 --- a/src/test/java/seedu/bookbuddy/BookListTest.java +++ b/src/test/java/seedu/bookbuddy/BookListTest.java @@ -18,7 +18,7 @@ void addBook() { BookList bookList = new BookList(); bookList.addBook("Harry Potter"); assertEquals(1, bookList.getSize()); - assertEquals("Harry Potter [ ]", bookList.getBook(0).toString()); + assertEquals("[U] Harry Potter", bookList.getBook(0).toString()); } } diff --git a/src/test/java/seedu/bookbuddy/ParserTest.java b/src/test/java/seedu/bookbuddy/ParserTest.java new file mode 100644 index 0000000000..9c5833dbf8 --- /dev/null +++ b/src/test/java/seedu/bookbuddy/ParserTest.java @@ -0,0 +1,23 @@ +package seedu.bookbuddy; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ParserTest { + @Test + void testParser() { + BookList books = new BookList(); + books.addBook("Don Quixote"); + books.addBook("Gulliver's Travels"); + assertEquals(2, books.getSize()); + books.markDoneByIndex(1); + assertEquals("[R] Don Quixote", books.getBook(0).toString()); + assertEquals("[U] Gulliver's Travels", books.getBook(1).toString()); + books.deleteBook(1); + books.markDoneByIndex(1); + assertTrue(books.getBook(0).isRead); + assertEquals("[R] Gulliver's Travels", books.getBook(0).toString()); + } +} diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index f49ba41f61..83e2d80f5a 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1 +1,3 @@ -Hello! We are bookbuddy! +Hello! We are BookBuddy! +How can I help you today? +Sorry but that is not a valid command. Please try again