Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2324S2#14 from Joshuahoky/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshuahoky authored Mar 19, 2024
2 parents e6bc1b8 + 0dac213 commit 6b07f6f
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 35 deletions.
26 changes: 14 additions & 12 deletions src/main/java/seedu/bookbuddy/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -39,17 +39,19 @@ public boolean isRead() {
*/
public void markBookAsRead() {
this.isRead = true;
System.out.println("Successfully marked " + this.getTitle() + " as read.");
}

/**
* Marks the book as unread.
*/
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;
}
}
33 changes: 20 additions & 13 deletions src/main/java/seedu/bookbuddy/BookBuddy.java
Original file line number Diff line number Diff line change
@@ -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!");
}

}
19 changes: 11 additions & 8 deletions src/main/java/seedu/bookbuddy/BookList.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,37 @@ 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.");
}

/**
* Deletes a book from the list by its index.
* @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.");
}

/**
* Marks a book as read by its 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();
}

/**
* Marks a book as unread by its 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();
}

/**
Expand All @@ -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.");
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/seedu/bookbuddy/Parser.java
Original file line number Diff line number Diff line change
@@ -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");
}
}

}
2 changes: 1 addition & 1 deletion src/test/java/seedu/bookbuddy/BookListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
23 changes: 23 additions & 0 deletions src/test/java/seedu/bookbuddy/ParserTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
4 changes: 3 additions & 1 deletion text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6b07f6f

Please sign in to comment.