diff --git a/src/main/java/seedu/bookbuddy/Ui.java b/src/main/java/seedu/bookbuddy/Ui.java index adc7fbf991..5a0584b4af 100644 --- a/src/main/java/seedu/bookbuddy/Ui.java +++ b/src/main/java/seedu/bookbuddy/Ui.java @@ -170,4 +170,10 @@ public static void printAuthorFound(ArrayList bookAuthor) { System.out.println(i + 1 + ". " + bookAuthor.get(i)); } } + public static void printSingleIndentation() { + System.out.print("----"); + } + public static void printDoubleIndentation() { + System.out.print("--------"); + } } diff --git a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/InputLooper.java b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/InputLooper.java index 952853125a..25643c5e24 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/InputLooper.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/InputLooper.java @@ -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) } @@ -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."); } } @@ -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()) { diff --git a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/NewGenreModifier.java b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/NewGenreModifier.java index 98b997d4f5..cfa925859e 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/NewGenreModifier.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/NewGenreModifier.java @@ -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)) { diff --git a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/ParserGenre.java b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/ParserGenre.java index 1a6336ab16..9fad84baee 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/ParserGenre.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/ParserGenre.java @@ -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; @@ -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);