Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2324S2#37 from yeozongyao/branch-zong…
Browse files Browse the repository at this point in the history
…yao-defensiveCoding

Branch zongyao defensive coding
  • Loading branch information
yeozongyao authored Mar 21, 2024
2 parents 90fbea3 + 744cc10 commit 2414f3a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test {

testLogging {
events "passed", "skipped", "failed"
jvmArgs '-da'

showExceptions true
exceptionFormat "full"
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/bookbuddy/BookBuddy.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public static void getUserInput(BookList books) {
System.out.println(e.getMessage());
} catch (InvalidCommandArgumentException e) {
System.out.println(e.getMessage());
} catch (Exception e) { // Generic catch block for any other exceptions
LOGGER.log(Level.SEVERE, "An unexpected error occurred: {0}", e.getMessage());
System.out.println("An unexpected error occurred. Please contact support.");
}
}
}
Expand Down
31 changes: 26 additions & 5 deletions src/main/java/seedu/bookbuddy/BookList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import exceptions.BookNotFoundException;

import java.util.ArrayList;
import java.util.logging.Level;

import static seedu.bookbuddy.BookBuddy.LOGGER;

/**
* Manages a list of books, allowing for operations such as adding, deleting,
Expand Down Expand Up @@ -36,18 +39,24 @@ public Book getBook(int index) throws BookNotFoundException{
if (index < 0 || index > books.size()) {
throw new BookNotFoundException("Book index out of range.");
}
assert books.get(index) != null : "Retrieved book should not be null";
return books.get(index);
assert books.get(index - 1) != null : "Retrieved book should not be null";
assert books.get(index - 1) instanceof Book : "Object at index should be an instance of Book";
return books.get(index - 1);
}

/**
* Adds a new Book to the list.
* @param title The title of the book.
*/
public void addBook(String title) {
books.add(new Book(title));
Ui.addBookMessage(title);
assert !books.isEmpty() : "Book list should not be empty after adding a book";
try {
books.add(new Book(title));
Ui.addBookMessage(title);
assert !books.isEmpty() : "Book list should not be empty after adding a book";
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "An unexpected error occurred: {0}", e.getMessage());
throw e; // Rethrow or handle as needed
}
}

/**
Expand All @@ -61,6 +70,9 @@ public void deleteBook(int index) throws IndexOutOfBoundsException {
assert books.size() >= 0 : "Book list size should not be negative after deletion";
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid book index. Please enter a valid index");
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "An unexpected error occurred: {0}", e.getMessage());
throw e; // Rethrow or handle as needed
}
}

Expand All @@ -72,8 +84,12 @@ public void markDoneByIndex(int index) throws IndexOutOfBoundsException{
try {
assert index > 0 && index <= books.size() : "Index out of valid range";
books.get(index - 1).markBookAsRead();
assert books.get(index - 1).isRead() : "Book should be marked as read";
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid book index. Please enter a valid index");
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "An unexpected error occurred: {0}", e.getMessage());
throw e; // Rethrow or handle as needed
}
}

Expand All @@ -85,19 +101,24 @@ public void markUndoneByIndex(int index) throws IndexOutOfBoundsException{
try {
assert index > 0 && index <= books.size() : "Index out of valid range";
books.get(index - 1).markBookAsUnread();
assert !books.get(index - 1).isRead() : "Book should be marked as unread";
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid book index. Please enter a valid index");
} catch (Exception e) { // Generic catch block for any other exceptions
System.out.println("An unexpected error occurred. Please contact support.");
}
}

/**
* Prints all books currently in the list.
*/
public static void printAllBooks() {
assert BookList.books != null : "Books list should not be null since it has been initialised.";
if (!BookList.books.isEmpty()) {
System.out.println("All books:");
for (int i = 0; i < BookList.books.size(); i++) {
Book currentBook = BookList.books.get(i);
assert currentBook != null : "Book in list should not be null";
System.out.print((i + 1) + ". ");
System.out.println(currentBook.toString());
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/bookbuddy/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ public static void parseCommand(String input, BookList books) {
try {
switch (command) {
case ADD_COMMAND:
assert inputArray.length >= 2 : "Command requires additional arguments";
if (inputArray.length < 2) {
LOGGER.log(Level.WARNING, "The add Command requires a book title", inputArray);
System.out.println("throwing invalidcommand");
throw new InvalidCommandArgumentException("The add command requires a book title.");
}
books.addBook(inputArray[1]);
break;
case REMOVE_COMMAND:
assert inputArray.length >= 2 : "Command requires additional arguments";
try {
index = Integer.parseInt(inputArray[1]);
books.deleteBook(index);
Expand All @@ -56,8 +59,10 @@ public static void parseCommand(String input, BookList books) {
books.printAllBooks();
break;
case MARK_COMMAND:
assert inputArray.length >= 2 : "Command requires additional arguments";
try {
index = Integer.parseInt(inputArray[1]);
assert index >= 0 : "Index should be non-negative";
books.markDoneByIndex(index);
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + inputArray[1] + " is not a valid number. " +
Expand All @@ -67,8 +72,10 @@ public static void parseCommand(String input, BookList books) {
}
break;
case UNMARK_COMMAND:
assert inputArray.length >= 2 : "Command requires additional arguments";
try {
index = Integer.parseInt(inputArray[1]);
assert index >= 0 : "Index should be non-negative";
books.markUndoneByIndex(index);
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + inputArray[1] + " is not a valid number. " +
Expand All @@ -93,6 +100,15 @@ public static void parseCommand(String input, BookList books) {
throw new InvalidBookIndexException("Book index must be an integer.");
} catch (IndexOutOfBoundsException e) {
throw new BookNotFoundException("Book not found at the provided index.");
} catch (InvalidCommandArgumentException e) {
LOGGER.log(Level.WARNING, "Invalid command argument: {0}", e.getMessage());
throw e;
} catch (UnsupportedCommandException e) {
LOGGER.log(Level.WARNING, "Command is invalid", e.getMessage());
throw e;
} catch (Exception e) { // Generic catch block for any other exceptions
LOGGER.log(Level.SEVERE, "An unexpected error occurred: {0}", e.getMessage());
System.out.println("An unexpected error occurred. Please contact support.");
}
}
}
10 changes: 5 additions & 5 deletions src/test/java/seedu/bookbuddy/BookListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void addBook() {
BookList testBookList = new BookList();
testBookList.addBook("Harry Potter");
assertEquals(1, testBookList.getSize());
assertEquals("[U] Harry Potter", testBookList.getBook(0).toString());
assertEquals("[U] Harry Potter", testBookList.getBook(1).toString());
}

@Test
Expand Down Expand Up @@ -57,25 +57,25 @@ void getBook() {
bookList.addBook("Harry Potter");
bookList.addBook("Geronimo");
bookList.addBook("Cradle");
assertEquals("[U] Cradle", bookList.getBook(2).toString());
assertEquals("[U] Cradle", bookList.getBook(3).toString());
}

@Test
void markDoneByIndex() {
BookList bookList = new BookList();
bookList.addBook("Harry Potter");
bookList.markDoneByIndex(1);
assertEquals("[R] Harry Potter", bookList.getBook(0).toString());
assertEquals("[R] Harry Potter", bookList.getBook(1).toString());
}

@Test
void markUndoneByIndex() {
BookList bookList = new BookList();
bookList.addBook("Harry Potter");
bookList.markDoneByIndex(1);
assertEquals("[R] Harry Potter", bookList.getBook(0).toString());
assertEquals("[R] Harry Potter", bookList.getBook(1).toString());
bookList.markUndoneByIndex(1);
assertEquals("[U] Harry Potter", bookList.getBook(0).toString());
assertEquals("[U] Harry Potter", bookList.getBook(1).toString());
}

}
14 changes: 7 additions & 7 deletions src/test/java/seedu/bookbuddy/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ void testParser() {
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());
assertEquals("[R] Don Quixote", books.getBook(1).toString());
assertEquals("[U] Gulliver's Travels", books.getBook(2).toString());
books.deleteBook(1);
books.markDoneByIndex(1);
assertTrue(books.getBook(0).isRead);
assertEquals("[R] Gulliver's Travels", books.getBook(0).toString());
assertTrue(books.getBook(1).isRead);
assertEquals("[R] Gulliver's Travels", books.getBook(1).toString());
}

@Test
void parseAddCommand() {
BookList testBookList = new BookList();
Parser.parseCommand("add The Great Gatsby", testBookList);
assertEquals(1, testBookList.getSize());
assertEquals("The Great Gatsby", testBookList.getBook(0).getTitle());
assertEquals("The Great Gatsby", testBookList.getBook(1).getTitle());
}

@Test
Expand All @@ -50,7 +50,7 @@ void parseMarkCommand() {
BookList books = new BookList();
books.addBook("The Great Gatsby");
Parser.parseCommand("mark 1", books);
assertTrue(books.getBook(0).isRead());
assertTrue(books.getBook(1).isRead());
}

@Test
Expand All @@ -59,7 +59,7 @@ void parseUnmarkCommand() {
books.addBook("The Great Gatsby");
Parser.parseCommand("mark 1", books);
Parser.parseCommand("unmark 1", books);
assertFalse(books.getBook(0).isRead());
assertFalse(books.getBook(1).isRead());
}

@Test
Expand Down

0 comments on commit 2414f3a

Please sign in to comment.