Skip to content

Commit

Permalink
Bug fix - set-genre feature loop does not continue when a wrong input…
Browse files Browse the repository at this point in the history
… is entered
  • Loading branch information
yeozongyao committed Apr 14, 2024
1 parent 136b5eb commit 02fddf2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import seedu.bookbuddy.booklist.BookList;

import java.io.IOException;
import java.util.Objects;
import java.util.Scanner;

//@@author yeozongyao
Expand All @@ -25,10 +26,10 @@ public String inputLooper(String input, Scanner scanner, BookList books) throws
while (input == null) {
try {
input = handleInitialInput(scanner);
if (input == null) {
if (Objects.equals(input, "exit_now")) {
return null; // Break out of the loop if input is null (exit command was used)
}
if (input.isEmpty()) {
if (input == null) {
continue; // If input is empty, continue to the next iteration of the loop
}
input = processSelection(input, scanner, books);
Expand Down Expand Up @@ -81,16 +82,22 @@ private String handleInitialInput(Scanner scanner) throws InvalidInputException,
String newInput = scanner.nextLine().trim();
Integer indentationValue = 1;
if (handleExitCommands(newInput, indentationValue)) {
return null;
return "exit_now";
}

String[] parts = newInput.split("\\s+");
if (parts.length == 1 && isValidNumber(parts[0])) {
return newInput;
}

throw new InvalidInputException(newInput + " is an invalid input. Please enter a " +
"valid number or type 'exit' to cancel or 'bye' to exit the programme.");
try {
throw new InvalidInputException(newInput+ " is an invalid selection. Please enter a " +
"valid number or type 'exit' to go back");
} catch (InvalidInputException e) {
System.out.println(newInput + " is not a valid integer man.. try again.. or 'exit' to go back");
Ui.printSingleIndentation();
return null;
}
}
}

Expand Down Expand Up @@ -167,8 +174,21 @@ String processSelection(String newInput, Scanner scanner, BookList books)
if (selection > 0 && selection <= books.genreList.getAvailableGenres().size()) {
return books.genreList.getAvailableGenres().get(selection - 1);
}
throw new InvalidInputException(selection + " is an invalid selection. Please enter a " +
"valid number or type 'exit' to cancel or 'bye' to exit the programme.");

if (selection > books.genreList.getAvailableGenres().size() + 1) {
System.out.println("That's not within the options man... try again.. or 'exit' to go back " +
"or 'bye' to close program");
Ui.printSingleIndentation();
return null;
}
try {
throw new InvalidInputException(selection + " is an invalid selection. Please enter a " +
"valid number or type 'exit' to go back");
} catch (InvalidInputException e) {
System.out.println(selection + " is not a valid integer man.. try again.. or 'exit' to go back");
Ui.printSingleIndentation();
return null;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static void parseSetGenre(BookList books, String[] inputArray) throws IOExceptio
private static void multiStepSetGenre(BookList books, int index) throws IOException {
NewGenreModifier.genreSelectionPrinter(books);
Ui.printSingleIndentation();
System.out.print("Enter the number for the desired genre, or add a new one:\n");
System.out.print("Enter the number for the desired genre, or add a new one, or 'exit' to go back:\n");
Ui.printSingleIndentation();
Scanner scanner = new Scanner(System.in);
InputLooper looper = new InputLooper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static void handleException(Exception e, String command, String[] inputAr
new Object[]{command, e.getMessage()});
System.out.println("An unexpected error occurred while executing " + command
+ ". Please contact support.");
System.out.println(e.getMessage());
}
}
}

0 comments on commit 02fddf2

Please sign in to comment.