Skip to content

Commit

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

Bug fixes for unsupported commands - index for mark, unmark and remov…
  • Loading branch information
yeozongyao authored Mar 21, 2024
2 parents a0d65a9 + 338b841 commit 7e1fc6c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/bookbuddy/BookList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.bookbuddy;


import java.util.ArrayList;

/**
Expand Down
33 changes: 27 additions & 6 deletions src/main/java/seedu/bookbuddy/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,40 @@ public static void parseCommand(String input, BookList books) {
books.addBook(inputArray[1]);
break;
case REMOVE_COMMAND:
index = Integer.parseInt(inputArray[1]);
books.deleteBook(index);
try {
index = Integer.parseInt(inputArray[1]);
books.deleteBook(index);
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + inputArray[1] + " is not a valid number. " +
"Please enter a valid numeric index.");
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid book index. Please enter a valid index.");
}
break;
case LIST_COMMAND:
books.printAllBooks();
break;
case MARK_COMMAND:
index = Integer.parseInt(inputArray[1]);
books.markDoneByIndex(index);
try {
index = Integer.parseInt(inputArray[1]);
books.markDoneByIndex(index);
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + inputArray[1] + " is not a valid number. " +
"Please enter a valid numeric index.");
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid book index. Please enter a valid index.");
}
break;
case UNMARK_COMMAND:
index = Integer.parseInt(inputArray[1]);
books.markUndoneByIndex(index);
try {
index = Integer.parseInt(inputArray[1]);
books.markUndoneByIndex(index);
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + inputArray[1] + " is not a valid number. " +
"Please enter a valid numeric index.");
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid book index. Please enter a valid index.");
}
break;
case HELP_COMMAND:
Ui.helpMessage();
Expand Down
23 changes: 18 additions & 5 deletions src/test/java/seedu/bookbuddy/ParserTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package seedu.bookbuddy;

import exceptions.InvalidBookIndexException;
import exceptions.InvalidCommandArgumentException;
import exceptions.UnsupportedCommandException;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -68,11 +71,21 @@ void parseInvalidAddCommandThrowsException() {
}

@Test
void parseInvalidRemoveCommandThrowsException() {
void parseInvalidRemoveCommandPrintsError() {
// Set up
BookList books = new BookList();
String input = "remove notAnIndex"; // Invalid index provided
assertThrows(InvalidBookIndexException.class,
() -> Parser.parseCommand(input, books), "Book index must be an integer.");
String input = "remove notAnIndex";
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent)); // Redirect standard out to capture console output
// Act
Parser.parseCommand(input, books);

// Assert
String output = outContent.toString();
assertTrue(output.contains("not a valid number"), "Error message should contain 'not a valid number'");

// Cleanup
System.setOut(System.out); // Reset standard out
}

@Test
Expand Down

0 comments on commit 7e1fc6c

Please sign in to comment.