Skip to content

Commit

Permalink
Bug fixes -
Browse files Browse the repository at this point in the history
1. Set-genre does not take into trailling and leading spaces
2. Set-genre does not detect existing genres in the genre list
  • Loading branch information
yeozongyao committed Apr 9, 2024
1 parent e469438 commit a5e0f9a
Showing 1 changed file with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static void parseSetGenre(BookList books, String[] inputArray) {
Exceptions.validateCommandArguments(inputArray, 2, "The set-genre command requires " +
"at least a book index.");

String[] parts = inputArray[1].split(" ", 2); // Attempt to split inputArray[1] into two parts
String[] parts = inputArray[1].trim().split(" ", 2); //Attempt to split inputArray[1] into two parts
int index;
try {
index = Integer.parseInt(parts[0]); // The first part should be the index
Expand Down Expand Up @@ -49,25 +49,11 @@ private static void multiStepSetGenre(BookList books, int index) {
}

private static void singleStepSetGenre(BookList books, String[] parts, int index) {
String genreInput = parts[1];
boolean genreExists = false;
for (String existingGenre : BookList.getAvailableGenres()) {
if (existingGenre.equalsIgnoreCase(genreInput)) {
genreExists = true;
genreInput = existingGenre; // Normalize to the existing genre's case
break;
}
}

if (!genreExists) {
BookList.getAvailableGenres().add(genreInput);
System.out.println("Added new genre to the list: " + genreInput);
}
String genreInput = parts[1].trim();
genreInput = duplicateChecker(genreInput);
BookGenre.setBookGenreByIndex(index, genreInput, books);
System.out.println("Genre set to " + genreInput + " for book at index " + index);
}


static void genreSelectionPrinter() {
System.out.println("Available genres:");
for (int i = 0; i < BookList.getAvailableGenres().size(); i++) {
Expand All @@ -79,7 +65,7 @@ static void genreSelectionPrinter() {
public static String invalidInputLooper(String input, Scanner scanner) {
while (input == null) {
while (!scanner.hasNextInt()) { // Ensure the next input is an integer
String newInput = scanner.nextLine();
String newInput = scanner.nextLine().trim();
if ("exit".equalsIgnoreCase(newInput)) {
Ui.exitCommandMessage();
return null;
Expand All @@ -94,8 +80,8 @@ public static String invalidInputLooper(String input, Scanner scanner) {

if (genreSelection == BookList.getAvailableGenres().size() + 1) {
System.out.println("Enter the new genre:");
input = scanner.nextLine();
BookList.getAvailableGenres().add(input); // Add the new genre to the list
input = scanner.nextLine().trim();
input = duplicateChecker(input);
} else if (genreSelection > 0 && genreSelection <= BookList.getAvailableGenres().size()) {
input = BookList.getAvailableGenres().get(genreSelection - 1);
} else {
Expand All @@ -107,6 +93,26 @@ public static String invalidInputLooper(String input, Scanner scanner) {
return input;
}

private static String duplicateChecker(String input) {
boolean genreExists = false;
for (String existingGenre : BookList.getAvailableGenres()) {
if (existingGenre.equalsIgnoreCase(input)) {
genreExists = true;
input = existingGenre; // Normalize to the existing genre's case
break;
}
}
if (!genreExists) {
BookList.getAvailableGenres().add(input);
Ui.printLine();
System.out.println("Added new genre to the list: " + input);
} else {
Ui.printLine();
System.out.println("[" + input + "] exists in the existing genre list!");
}
return input;
}

public static void executeParseSetGenre (BookList books, String[] inputArray) {
parseSetGenre(books, inputArray);
}
Expand Down

0 comments on commit a5e0f9a

Please sign in to comment.