Skip to content

Commit

Permalink
Bug fixes -
Browse files Browse the repository at this point in the history
1. [PE-D][Tester F] Inproper Exception Handling nus-cs2113-AY2324S2#133
2. [PE-D][Tester B] Redundant output in the label command nus-cs2113-AY2324S2#112

Enhanced exception handling to capture the different wrong input scenarios
  • Loading branch information
yeozongyao committed Apr 9, 2024
1 parent 2f4958c commit dd5b2d6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package seedu.bookbuddy.bookdetailsmodifier;

import seedu.bookbuddy.Ui;
import seedu.bookbuddy.book.Label;
import seedu.bookbuddy.book.Title;
import seedu.bookbuddy.booklist.BookList;
import seedu.bookbuddy.Ui;

public class BookLabel {

Expand All @@ -15,11 +15,6 @@ public class BookLabel {
* @throws IndexOutOfBoundsException if the index is out of range.
*/
public static void setBookLabelByIndex(int index, String label, BookList books) throws IndexOutOfBoundsException {
// Check for valid index
if (index < 0 || index > books.getSize()) {
throw new IndexOutOfBoundsException("Invalid book index. Please enter a valid index. nows");
}

// Set the label for the book at the specified index
Label.setLabel(books.getBook(index), label);
String title = Title.getTitle(books.getBook(index));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/bookbuddy/booklist/BookList.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public int getSize(){
* @param index The index of the book to retrieve.
* @return The Book at the specified index.
*/
public BookMain getBook(int index) throws BookNotFoundException{
public BookMain getBook(int index) throws BookNotFoundException {
if (index < 0 || index > books.size()) {
throw new BookNotFoundException("Book index out of range.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void parseSetGenre(BookList books, String[] inputArray) throws IOExceptio
try {
index = Integer.parseInt(parts[0]); // The first part should be the index
} catch (NumberFormatException e) {
System.out.println("Invalid book index. Please enter a valid numeric index.");
System.out.println("Invalid book index format. Please enter a valid numeric index.");
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package seedu.bookbuddy.parser.parsercommands;

import seedu.bookbuddy.booklist.BookList;
import seedu.bookbuddy.bookdetailsmodifier.BookLabel;
import seedu.bookbuddy.booklist.BookList;
import seedu.bookbuddy.parser.parservalidation.Exceptions;

public class ParserLabel {
static void parseSetLabel(BookList books, String[] inputArray) {
int index;
int index = -1;
assert inputArray.length == 2 : "Command requires additional arguments";
Exceptions.validateCommandArguments(inputArray,2, "The Label " +
"Command requires a book index and label");
String[] labelMessageParts = inputArray[1].split(" ", 2);
String[] labelMessageParts = inputArray[1].trim().split(" ", 2);
// Split the message into index and label message
assert labelMessageParts.length == 2 : "Command requires an index and a label message";
try {
index = Integer.parseInt(labelMessageParts[0].trim());
if (index < 0 || index > books.getSize()) {
throw new IndexOutOfBoundsException("Invalid book index (out of range). Please enter a valid index.");
}
assert index > 0 : "Index should be non-negative";
} catch (NumberFormatException e) {
System.out.println(labelMessageParts[0] + " is not a valid index format.");
} catch (IndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
Exceptions.validateCommandArguments(labelMessageParts, 2, "You " +
"need to have a label message");
index = Integer.parseInt(labelMessageParts[0]);
assert index > 0 : "Index should be non-negative";
String label = labelMessageParts[1];
String label = labelMessageParts[1].trim();
BookLabel.setBookLabelByIndex(index, label, books);
//test
}

public static void executeParseSetLabel (BookList books, String[] inputArray) {
Expand Down

0 comments on commit dd5b2d6

Please sign in to comment.