Skip to content

Commit

Permalink
Merge pull request #188 from Joshuahoky/branch-testing
Browse files Browse the repository at this point in the history
Fix minor bugs and add testing for remove and display commands
  • Loading branch information
Joshuahoky authored Apr 13, 2024
2 parents 61a1891 + b9e8c4c commit 107ecf5
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 64 deletions.
44 changes: 0 additions & 44 deletions BookBuddy.log.1

This file was deleted.

34 changes: 18 additions & 16 deletions src/main/java/seedu/bookbuddy/bookdetailsmodifier/BookDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ public class BookDisplay {
* @throws IndexOutOfBoundsException if the index is out of range.
*/
public static void displayDetails(int index, BookList books) throws IndexOutOfBoundsException {
if (index < 0 || index > books.getSize()) {
if (books.getBooks().isEmpty()) {
System.out.println("Unable to display details as the list is empty.");
} else if (index <= 0 || index > books.getSize()) {
throw new IndexOutOfBoundsException("Invalid book index. Please enter a valid index.");
} else {
String label = Label.getLabel(books.getBook(index));
String genre = Genre.getGenre(books.getBook(index));
String author = Author.getAuthor(books.getBook(index));
int rating = Rating.getRating(books.getBook(index));
String summary = Summary.getSummary(books.getBook(index));
System.out.println("Here are the details of your book:");
System.out.println("Title: " + Title.getTitle(books.getBook(index)));
System.out.println("Status: " + (Read.getRead(books.getBook(index)) ? "Read on " +
Read.getDateTimeRead(books.getBook(index)) : "Unread"));
System.out.println("Author: " + (author.isEmpty() ? "No author provided" : author));
System.out.println("Label: " + (label.isEmpty() ? "No label provided" : label));
System.out.println("Genre: " + (genre.isEmpty() ? "No genre provided" : genre));
System.out.println("Rating: " + ((rating == 0) ? "No rating provided" : rating));
System.out.println("Summary: " + (summary.isEmpty() ? "No summary provided" : summary));
}

String label = Label.getLabel(books.getBook(index));
String genre = Genre.getGenre(books.getBook(index));
String author = Author.getAuthor(books.getBook(index));
int rating = Rating.getRating(books.getBook(index));
String summary = Summary.getSummary(books.getBook(index));
System.out.println("Here are the details of your book:");
System.out.println("Title: " + Title.getTitle(books.getBook(index)));
System.out.println("Status: " + (Read.getRead(books.getBook(index)) ? "Read on " +
Read.getDateTimeRead(books.getBook(index)) : "Unread"));
System.out.println("Author: " + (author.isEmpty() ? "No author provided" : author));
System.out.println("Label: " + (label.isEmpty() ? "No label provided" : label));
System.out.println("Genre: " + (genre.isEmpty() ? "No genre provided" : genre));
System.out.println("Rating: " + ((rating == 0) ? "No rating provided" : rating));
System.out.println("Summary: " + (summary.isEmpty() ? "No summary provided" : summary));
}

//@@author yeozongyao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void parseAuthor(BookList books, String[] inputArray) {
"to have an author name");
index = Integer.parseInt(authorMessageParts[0].trim());
assert index >= 0 : "Index should be non-negative";
String author = authorMessageParts[1];
String author = authorMessageParts[1].trim();
String title = Title.getTitle(books.getBooks().get(index - 1));
if (author.equals(Author.getAuthor(books.getBooks().get(index - 1)))) {
Ui.printAuthorAlreadySet(title, author);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class ParserDisplay {
//@@author joshuahoky
static void parseDisplay(BookList books, String[] inputArray) {
int index;
assert inputArray.length >= 2 : "Command requires additional arguments";
assert inputArray.length == 2 : "Command requires additional arguments";
Exceptions.validateCommandArguments(inputArray,2 , "The display " +
"Command requires a book index");
index = Integer.parseInt(inputArray[1]);
index = Integer.parseInt(inputArray[1].trim());
BookDisplay.displayDetails(index, books);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class ParserRemove {
static void parseRemove(BookList books, String[] inputArray) {
int index;
assert inputArray.length >= 2 : "Command requires additional arguments";
assert inputArray.length == 2 : "Command requires additional arguments";
Exceptions.validateCommandArguments(inputArray, 2, "The remove " +
"Command requires a book index");
index = Integer.parseInt(inputArray[1].trim());
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/seedu/bookbuddy/ParserMainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,52 @@ void testParser() {
assertEquals("[R] Gulliver's Travels", books.getBook(1).toString());
}

@Test
void parseInvalidRemoveCommandThrowsException() {
BookList books = new BookList();
String input = "remove 1";
ParserMain.parseCommand(input, books); // Execute the command that should trigger the error message

String expectedOutput = "Unable to remove book as the list is empty.";
assertTrue(outContent.toString().contains(expectedOutput),
"Expected output message not found in the console output.");
}

@Test
void parseInvalidDisplayCommandThrowsException() {
BookList books = new BookList();
String input = "display 1";
ParserMain.parseCommand(input, books); // Execute the command that should trigger the error message

String expectedOutput = "Unable to display details as the list is empty.";
assertTrue(outContent.toString().contains(expectedOutput),
"Expected output message not found in the console output.");
}

@Test
void parseInvalidRemoveIndexThrowsException() {
BookList books = new BookList();
BookListModifier.addBook(books, "Don Quixote");
String input = "remove 2";
ParserMain.parseCommand(input, books); // Execute the command that should trigger the error message

String expectedOutput = "Invalid book index. Please enter a valid index";
assertTrue(outContent.toString().contains(expectedOutput),
"Expected output message not found in the console output.");
}

@Test
void parseInvalidDisplayIndexThrowsException() {
BookList books = new BookList();
BookListModifier.addBook(books, "Don Quixote");
String input = "display -1";
ParserMain.parseCommand(input, books); // Execute the command that should trigger the error message

String expectedOutput = "Invalid book index. Please enter a valid index";
assertTrue(outContent.toString().contains(expectedOutput),
"Expected output message not found in the console output.");
}

@Test
void parseAddCommand() {
BookList testBookList = new BookList();
Expand Down

0 comments on commit 107ecf5

Please sign in to comment.