Skip to content

Commit

Permalink
Merge pull request #189 from yeozongyao/branch-zongyao-testcodes
Browse files Browse the repository at this point in the history
Fixed bug where user is unable to exit from secondary input to primary input in set-genre function
  • Loading branch information
yeozongyao authored Apr 13, 2024
2 parents 107ecf5 + 1416bed commit 0d34f56
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/main/java/seedu/bookbuddy/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,10 @@ public static void printAuthorFound(ArrayList<BookMain> bookAuthor) {
System.out.println(i + 1 + ". " + bookAuthor.get(i));
}
}
public static void printSingleIndentation() {
System.out.print("----");
}
public static void printDoubleIndentation() {
System.out.print("--------");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class InputLooper {
public String inputLooper(String input, Scanner scanner, BookList books) throws IOException {
while (input == null) {
try {
input = handleInput(scanner);
input = handleInitialInput(scanner);
if (input == null) {
return null; // Break out of the loop if input is null (exit command was used)
}
Expand All @@ -27,23 +27,54 @@ public String inputLooper(String input, Scanner scanner, BookList books) throws
return input;
}

private String handleInput(Scanner scanner) throws InvalidInputException, IOException {
private boolean handleExitCommands(String input, Integer indentationValue) throws IOException {
if (isExitCommand(input)) {
for (int i = 0; i < indentationValue; i++) {
Ui.printSingleIndentation();
}
Ui.exitCommandMessage();
for (int i = 0; i < indentationValue - 1; i++) {
Ui.printSingleIndentation();
}
if (indentationValue == 2) {
System.out.print("Enter the number for the desired genre, or add a new one:\n");
Ui.printSingleIndentation();
}
return true;
} else if (isByeCommand(input)) {
BookBuddy.performExit();
return true;
}
return false;
}

private String handleInitialInput(Scanner scanner) throws InvalidInputException, IOException {
while (true) {
String newInput = scanner.nextLine().trim();
Integer indentationValue = 1;
if (handleExitCommands(newInput, indentationValue)) {
return null;
}

String[] parts = newInput.split("\\s+");
if (parts.length == 1 && isValidNumber(parts[0])) {
return newInput;
}
if (isExitCommand(newInput)) {
Ui.exitCommandMessage();
return null;
}
if (isByeCommand(newInput)) {
BookBuddy.performExit();

throw new InvalidInputException(newInput + " is an invalid input. Please enter a " +
"valid number or type 'exit' to cancel or 'bye' to exit the programme.");
}
}

private String handleSecondaryInput(Scanner scanner) throws IOException {
while (true) {
String newInput = scanner.nextLine().trim();
Integer indentationValue = 2;
if (handleExitCommands(newInput, indentationValue)) {
return null;
} else {
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.");
}
}

Expand All @@ -59,11 +90,14 @@ private static boolean isByeCommand(String input) {
return "bye".equalsIgnoreCase(input);
}

private String processSelection(String newInput, Scanner scanner, BookList books) throws InvalidInputException {
private String processSelection(String newInput, Scanner scanner, BookList books)
throws InvalidInputException, IOException {
int selection = Integer.parseInt(newInput);
if (selection == books.getAvailableGenres().size() + 1) {
System.out.println("Enter the new genre:");
String genre = scanner.nextLine().trim();
Ui.printDoubleIndentation();
System.out.print("Enter the new genre:\n");
Ui.printDoubleIndentation();
String genre = handleSecondaryInput(scanner);
return NewGenreModifier.duplicateChecker(genre, books);
}
if (selection > 0 && selection <= books.getAvailableGenres().size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ static void genreSelectionPrinter(BookList books) {
}

static String duplicateChecker(String input, BookList books) {
if (input == null) {
return null;
}
boolean genreExists = false;
for (String existingGenre : books.getAvailableGenres()) {
if (existingGenre.equalsIgnoreCase(input)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.bookbuddy.parser.parsercommands.parsegenre;

import exceptions.InvalidCommandArgumentException;
import seedu.bookbuddy.Ui;
import seedu.bookbuddy.bookdetailsmodifier.BookGenre;
import seedu.bookbuddy.booklist.BookList;
import seedu.bookbuddy.parser.parservalidation.Exceptions;
Expand Down Expand Up @@ -38,7 +39,9 @@ static void parseSetGenre(BookList books, String[] inputArray) throws IOExceptio

private static void multiStepSetGenre(BookList books, int index) throws IOException {
NewGenreModifier.genreSelectionPrinter(books);
System.out.println("Enter the number for the desired genre, or add a new one:");
Ui.printSingleIndentation();
System.out.print("Enter the number for the desired genre, or add a new one:\n");
Ui.printSingleIndentation();
Scanner scanner = new Scanner(System.in);
InputLooper looper = new InputLooper();
String selectedGenre = looper.inputLooper(null, scanner, books);
Expand Down

0 comments on commit 0d34f56

Please sign in to comment.