diff --git a/src/main/java/seedu/bookbuddy/FileStorage.java b/src/main/java/seedu/bookbuddy/FileStorage.java index 67472ef11a..2dfe33d820 100644 --- a/src/main/java/seedu/bookbuddy/FileStorage.java +++ b/src/main/java/seedu/bookbuddy/FileStorage.java @@ -58,7 +58,7 @@ public void readData(BookList books, File file) throws FileNotFoundException { if (genresLine.startsWith(GENRE_DATA)) { String genresData = genresLine.substring(8); // Skip "Genres: " List genres = Arrays.asList(genresData.split(",")); - BookList.setAvailableGenres(genres); // Update the available genres + books.setAvailableGenres(genres); // Update the available genres } else { System.out.println("Unable to load genres as they have been tampered with."); } @@ -83,7 +83,7 @@ public void saveData(BookList books) throws IOException { File file = new File(FILE_PATH); FileWriter fw = new FileWriter(file); - String genresLine = BookList.saveGenresFormat(); + String genresLine = books.saveGenresFormat(); fw.write("Genres: " + genresLine + '\n'); for (int i = 1; i <= books.getSize(); i += 1) { diff --git a/src/main/java/seedu/bookbuddy/booklist/BookList.java b/src/main/java/seedu/bookbuddy/booklist/BookList.java index c7d0072621..22c6865183 100644 --- a/src/main/java/seedu/bookbuddy/booklist/BookList.java +++ b/src/main/java/seedu/bookbuddy/booklist/BookList.java @@ -14,17 +14,22 @@ * and marking book as read or unread. */ public class BookList { - protected static List availableGenres = new ArrayList<>(Arrays.asList("Fiction", "Non-Fiction", - "Mystery", "Science Fiction", "Fantasy")); + protected ArrayList availableGenres = new ArrayList<>(Arrays.asList( + "Fiction", "Non-Fiction", "Mystery", "Science Fiction", "Fantasy" + )); protected ArrayList books; public BookList() { this.books = new ArrayList(); // Use ArrayList instead of array } - public static List getAvailableGenres() { - return BookList.availableGenres; + + public ArrayList getAvailableGenres() { + return availableGenres; } - public static void setAvailableGenres(List availableGenres) { - BookList.availableGenres = new ArrayList<>(availableGenres); + + public void setAvailableGenres(List newAvailableGenres) { + // Clear the existing list and add all from the new list to avoid reference issues + availableGenres.clear(); + availableGenres.addAll(newAvailableGenres); } /** * Constructs a new BookList instance with an empty list. @@ -58,7 +63,7 @@ public BookMain getBook(int index) throws BookNotFoundException { return books.get(index - 1); } - public static String saveGenresFormat() { + public String saveGenresFormat() { return String.join(",", availableGenres); } diff --git a/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserFind.java b/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserFind.java index 6c39e65290..fdd15dfe6a 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserFind.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserFind.java @@ -20,12 +20,13 @@ public static void parseFindGenre(BookList books, String inputArray) { } public static void parseGenreLong(BookList books) throws IOException { System.out.println("Available genres:"); - for (int i = 0; i < BookList.getAvailableGenres().size(); i++) { - System.out.println((i + 1) + ". " + BookList.getAvailableGenres().get(i)); + for (int i = 0; i < books.getAvailableGenres().size(); i++) { + System.out.println((i + 1) + ". " + books.getAvailableGenres().get(i)); } System.out.println("Enter the number for the desired genre:"); Scanner scanner = new Scanner(System.in); - String selectedGenre = InputLooper.inputLooper(null, scanner); + InputLooper looper = new InputLooper(); + String selectedGenre = looper.inputLooper(null, scanner, books); if (selectedGenre == null) { return; } 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 8abd3bd44d..952853125a 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/InputLooper.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/InputLooper.java @@ -9,14 +9,17 @@ import java.util.Scanner; public class InputLooper { - public static String inputLooper(String input, Scanner scanner) throws IOException { + public String inputLooper(String input, Scanner scanner, BookList books) throws IOException { while (input == null) { try { input = handleInput(scanner); - if (input == null || input.isEmpty()) { - continue; // if input is still null or empty, continue to the next iteration of the loop + if (input == null) { + return null; // Break out of the loop if input is null (exit command was used) } - input = processSelection(input, scanner); + if (input.isEmpty()) { + continue; // If input is empty, continue to the next iteration of the loop + } + input = processSelection(input, scanner, books); } catch (InvalidInputException e) { System.out.println(e.getMessage()); } @@ -24,7 +27,7 @@ public static String inputLooper(String input, Scanner scanner) throws IOExcepti return input; } - private static String handleInput(Scanner scanner) throws InvalidInputException, IOException { + private String handleInput(Scanner scanner) throws InvalidInputException, IOException { while (true) { String newInput = scanner.nextLine().trim(); String[] parts = newInput.split("\\s+"); @@ -56,15 +59,15 @@ private static boolean isByeCommand(String input) { return "bye".equalsIgnoreCase(input); } - private static String processSelection(String newInput, Scanner scanner) throws InvalidInputException { + private String processSelection(String newInput, Scanner scanner, BookList books) throws InvalidInputException { int selection = Integer.parseInt(newInput); - if (selection == BookList.getAvailableGenres().size() + 1) { + if (selection == books.getAvailableGenres().size() + 1) { System.out.println("Enter the new genre:"); String genre = scanner.nextLine().trim(); - return NewGenreModifier.duplicateChecker(genre); + return NewGenreModifier.duplicateChecker(genre, books); } - if (selection > 0 && selection <= BookList.getAvailableGenres().size()) { - return BookList.getAvailableGenres().get(selection - 1); + if (selection > 0 && selection <= books.getAvailableGenres().size()) { + return books.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."); 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 340989cec7..98b997d4f5 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/NewGenreModifier.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/NewGenreModifier.java @@ -4,17 +4,17 @@ import seedu.bookbuddy.booklist.BookList; public class NewGenreModifier { - static void genreSelectionPrinter() { + static void genreSelectionPrinter(BookList books) { System.out.println("Available genres:"); - for (int i = 0; i < BookList.getAvailableGenres().size(); i++) { - System.out.println((i + 1) + ". " + BookList.getAvailableGenres().get(i)); + for (int i = 0; i < books.getAvailableGenres().size(); i++) { + System.out.println((i + 1) + ". " + books.getAvailableGenres().get(i)); } - System.out.println((BookList.getAvailableGenres().size() + 1) + ". Add a new genre"); + System.out.println((books.getAvailableGenres().size() + 1) + ". Add a new genre"); } - static String duplicateChecker(String input) { + static String duplicateChecker(String input, BookList books) { boolean genreExists = false; - for (String existingGenre : BookList.getAvailableGenres()) { + for (String existingGenre : books.getAvailableGenres()) { if (existingGenre.equalsIgnoreCase(input)) { genreExists = true; input = existingGenre; // Normalize to the existing genre's case @@ -22,7 +22,7 @@ static String duplicateChecker(String input) { } } if (!genreExists) { - BookList.getAvailableGenres().add(input); + books.getAvailableGenres().add(input); Ui.printLine(); System.out.println("Added new genre to the list: " + input); } else { 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 10ca7f324e..584469d13b 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/ParserGenre.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/ParserGenre.java @@ -9,7 +9,7 @@ import java.util.Scanner; public class ParserGenre { - static void parseSetGenre(BookList books, String[] inputArray) throws IOException { + static void parseSetGenre(BookList books, String[] inputArray) throws IOException { Exceptions.validateCommandArguments(inputArray, 2, "The set-genre command requires " + "at least a book index."); @@ -37,11 +37,11 @@ static void parseSetGenre(BookList books, String[] inputArray) throws IOExceptio } private static void multiStepSetGenre(BookList books, int index) throws IOException { - NewGenreModifier.genreSelectionPrinter(); + NewGenreModifier.genreSelectionPrinter(books); System.out.println("Enter the number for the desired genre, or add a new one:"); Scanner scanner = new Scanner(System.in); - - String selectedGenre = InputLooper.inputLooper(null, scanner); + InputLooper looper = new InputLooper(); + String selectedGenre = looper.inputLooper(null, scanner, books); if (selectedGenre == null) { return; } @@ -50,7 +50,7 @@ private static void multiStepSetGenre(BookList books, int index) throws IOExcept private static void singleStepSetGenre(BookList books, String[] parts, int index) { String genreInput = parts[1].trim(); - genreInput = NewGenreModifier.duplicateChecker(genreInput); + genreInput = NewGenreModifier.duplicateChecker(genreInput, books); BookGenre.setBookGenreByIndex(index, genreInput, books); } diff --git a/src/test/java/seedu/bookbuddy/ParserMainTest.java b/src/test/java/seedu/bookbuddy/ParserMainTest.java index edb5ac6a06..7ac48db642 100644 --- a/src/test/java/seedu/bookbuddy/ParserMainTest.java +++ b/src/test/java/seedu/bookbuddy/ParserMainTest.java @@ -151,10 +151,10 @@ void parseLabelCommand() { @Test void parseGenreCommand() { BookList books = new BookList(); - //BookListModifier.addBook(books, "The Great Gatsby"); + BookListModifier.addBook(books, "The Great Gatsby"); // Simulate user input for genre selection "Classic" - ParserMain.parseCommand("add The Great Gatsby", books); // Changed to fit your updated command-handling logic - int genreSize = BookList.getAvailableGenres().size(); + + int genreSize = books.getAvailableGenres().size(); String simulatedUserInput = genreSize + 1 + "\nClassic\n"; InputStream savedStandardInputStream = System.in; System.setIn(new ByteArrayInputStream(simulatedUserInput.getBytes()));