diff --git a/src/main/java/seedu/bookbuddy/FileStorage.java b/src/main/java/seedu/bookbuddy/FileStorage.java index 2dfe33d820..f2b9327297 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(",")); - books.setAvailableGenres(genres); // Update the available genres + books.genreList.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 = books.saveGenresFormat(); + String genresLine = books.genreList.saveGenresFormat(); fw.write("Genres: " + genresLine + '\n'); for (int i = 1; i <= books.getSize(); i += 1) { diff --git a/src/main/java/seedu/bookbuddy/Ui.java b/src/main/java/seedu/bookbuddy/Ui.java index f7a62854a3..e0d781eb1f 100644 --- a/src/main/java/seedu/bookbuddy/Ui.java +++ b/src/main/java/seedu/bookbuddy/Ui.java @@ -184,4 +184,8 @@ public static void printSingleIndentation() { public static void printDoubleIndentation() { System.out.print("--------"); } + public static void printGenreRemovedMessage(String genre) { + System.out.println("okii [" + genre + "] removed from the genre list!"); + printLine(); + } } diff --git a/src/main/java/seedu/bookbuddy/booklist/BookList.java b/src/main/java/seedu/bookbuddy/booklist/BookList.java index 22c6865183..2d42b0e4c1 100644 --- a/src/main/java/seedu/bookbuddy/booklist/BookList.java +++ b/src/main/java/seedu/bookbuddy/booklist/BookList.java @@ -6,7 +6,6 @@ import seedu.bookbuddy.book.Title; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; /** @@ -14,23 +13,12 @@ * and marking book as read or unread. */ public class BookList { - protected ArrayList availableGenres = new ArrayList<>(Arrays.asList( - "Fiction", "Non-Fiction", "Mystery", "Science Fiction", "Fantasy" - )); + public final GenreList genreList = new GenreList(); protected ArrayList books; public BookList() { this.books = new ArrayList(); // Use ArrayList instead of array } - public ArrayList getAvailableGenres() { - return 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. */ @@ -63,10 +51,6 @@ public BookMain getBook(int index) throws BookNotFoundException { return books.get(index - 1); } - public String saveGenresFormat() { - return String.join(",", availableGenres); - } - //@@lordgareth10 /** * Checks whether the book title is already inside the list diff --git a/src/main/java/seedu/bookbuddy/booklist/GenreList.java b/src/main/java/seedu/bookbuddy/booklist/GenreList.java new file mode 100644 index 0000000000..586db5829e --- /dev/null +++ b/src/main/java/seedu/bookbuddy/booklist/GenreList.java @@ -0,0 +1,63 @@ +package seedu.bookbuddy.booklist; + +import exceptions.BookNotFoundException; +import seedu.bookbuddy.Ui; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class GenreList { + protected ArrayList availableGenres = new ArrayList(Arrays.asList( + "Fiction", "Non-Fiction", "Mystery", "Science Fiction", "Fantasy" + )); + + public GenreList() { + + } + public ArrayList getAvailableGenres() { + return 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); + } + + public String getGenre(Integer index) { + if (index < 0 || index > availableGenres.size()) { + throw new BookNotFoundException("Genre index out of range."); + } + return availableGenres.get(index - 1); + } + /** + * Removes a specified genre from the available genres list. + * @param genreIndexToRemove The genre to remove from the list. + */ + public void removeGenre(Integer genreIndexToRemove) { + availableGenres.remove(genreIndexToRemove - 1); + } + + public void printGenreList() { + assert availableGenres != null : "Genre list should not be null since it has been initialised."; + if (!availableGenres.isEmpty()) { + Ui.printLine(); + System.out.println("All Genres:"); + for (int i = 0; i < availableGenres.size(); i++) { + String genre = availableGenres.get(i); + assert genre != null : "Book in list should not be null"; + System.out.print((i + 1) + ". "); + System.out.println(genre); + } + Ui.printShortLine(); + } else { + System.out.println("The list is empty. Add books by 'add [book]'"); + } + } + + public String saveGenresFormat() { + return String.join(",", availableGenres); + } + +} \ No newline at end of file diff --git a/src/main/java/seedu/bookbuddy/parser/ParserMain.java b/src/main/java/seedu/bookbuddy/parser/ParserMain.java index 5573077b84..7fcabc2a5e 100644 --- a/src/main/java/seedu/bookbuddy/parser/ParserMain.java +++ b/src/main/java/seedu/bookbuddy/parser/ParserMain.java @@ -4,18 +4,19 @@ import seedu.bookbuddy.booklist.BookList; import seedu.bookbuddy.Ui; -import seedu.bookbuddy.parser.parsercommands.ParserFind; import seedu.bookbuddy.parser.parsercommands.ParserAdd; -import seedu.bookbuddy.parser.parsercommands.ParserRemove; -import seedu.bookbuddy.parser.parsercommands.ParserMark; -import seedu.bookbuddy.parser.parsercommands.parsegenre.ParserGenre; +import seedu.bookbuddy.parser.parsercommands.ParserAuthor; import seedu.bookbuddy.parser.parsercommands.ParserDisplay; -import seedu.bookbuddy.parser.parsercommands.parserating.ParserRating; -import seedu.bookbuddy.parser.parsercommands.ParserUnmark; +import seedu.bookbuddy.parser.parsercommands.ParserFind; import seedu.bookbuddy.parser.parsercommands.ParserLabel; -import seedu.bookbuddy.parser.parsercommands.ParserSummary; import seedu.bookbuddy.parser.parsercommands.ParserList; -import seedu.bookbuddy.parser.parsercommands.ParserAuthor; +import seedu.bookbuddy.parser.parsercommands.ParserMark; +import seedu.bookbuddy.parser.parsercommands.ParserRemove; +import seedu.bookbuddy.parser.parsercommands.ParserRemoveGenre; +import seedu.bookbuddy.parser.parsercommands.ParserSummary; +import seedu.bookbuddy.parser.parsercommands.ParserUnmark; +import seedu.bookbuddy.parser.parsercommands.parsegenre.ParserGenre; +import seedu.bookbuddy.parser.parsercommands.parserating.ParserRating; import seedu.bookbuddy.parser.parservalidation.CommandList; import seedu.bookbuddy.parser.parservalidation.Exceptions; @@ -51,9 +52,15 @@ public static void parseCommand(String input, BookList books) { case CommandList.REMOVE_COMMAND: ParserRemove.executeParseRemove(books, inputArray); break; + case CommandList.GENRE_REMOVE_COMMAND: + ParserRemoveGenre.executeParseRemove(books, inputArray); + break; case CommandList.LIST_COMMAND: //Empty case, all list commands handled in if block break; + case CommandList.LIST_GENRE_COMMAND: + //Empty case, all list commands handled in if block + break; case CommandList.PRINT_ORDERED_COMMAND: //Empty case, all list commands handled in if block break; diff --git a/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserFind.java b/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserFind.java index fdd15dfe6a..a52b9092f7 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserFind.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserFind.java @@ -20,8 +20,8 @@ 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 < books.getAvailableGenres().size(); i++) { - System.out.println((i + 1) + ". " + books.getAvailableGenres().get(i)); + for (int i = 0; i < books.genreList.getAvailableGenres().size(); i++) { + System.out.println((i + 1) + ". " + books.genreList.getAvailableGenres().get(i)); } System.out.println("Enter the number for the desired genre:"); Scanner scanner = new Scanner(System.in); diff --git a/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserList.java b/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserList.java index c6d1c5418e..a4e3d00cf6 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserList.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserList.java @@ -5,8 +5,8 @@ import seedu.bookbuddy.bookdetailsmodifier.BookMark; import seedu.bookbuddy.bookdetailsmodifier.BookRating; import seedu.bookbuddy.booklist.BookList; -import seedu.bookbuddy.parser.parservalidation.Exceptions; import seedu.bookbuddy.parser.parservalidation.CommandList; +import seedu.bookbuddy.parser.parservalidation.Exceptions; import java.util.logging.Level; @@ -16,7 +16,7 @@ public class ParserList { private static void parseList(BookList books, String[] inputArray, String command) { Exceptions.validateCommandArguments(inputArray, 1, "ALl the list commands do not require any further arguments, just type `list`," + - " `list-rated` or `list-by-date`"); + " `list-rated`, `list-genre` or `list-by-date`"); switch (command) { case CommandList.LIST_COMMAND: BookDisplay.printAllBooks(books); @@ -27,6 +27,9 @@ private static void parseList(BookList books, String[] inputArray, String comman case CommandList.PRINT_ORDERED_DATE_COMMAND: BookMark.printBooksByDateRead(books); break; + case CommandList.LIST_GENRE_COMMAND: + books.genreList.printGenreList(); + break; default: LOGGER.log(Level.WARNING, "Sorry but that is not a valid list command. Please try again", command); throw new UnsupportedCommandException("Sorry but that is not a valid list command. " + diff --git a/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserRemoveGenre.java b/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserRemoveGenre.java new file mode 100644 index 0000000000..832c6a73e6 --- /dev/null +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/ParserRemoveGenre.java @@ -0,0 +1,34 @@ +package seedu.bookbuddy.parser.parsercommands; + +import seedu.bookbuddy.Ui; +import seedu.bookbuddy.booklist.BookList; +import seedu.bookbuddy.parser.parservalidation.Exceptions; + +public class ParserRemoveGenre { + static void parseRemoveGenre(BookList books, String[] inputArray) { + int index; + assert inputArray.length == 2 : "Command requires additional arguments"; + Exceptions.validateCommandArguments(inputArray, 2, "The remove " + + "Command requires a book index"); + try { + index = Integer.parseInt(inputArray[1].trim()); + String genre = books.genreList.getGenre(index); + if (index > 5) { + books.genreList.removeGenre(index); + Ui.printGenreRemovedMessage(genre); + } else { + System.out.println("sorrryy.. you cannot remove the default genres heehee"); + } + } catch (IndexOutOfBoundsException e) { + System.out.println("Genre list is out of bounds :( " + + "Use [list-genre] to see the genre list"); + } catch (NumberFormatException e) { + System.out.println(inputArray[1].trim() + " is not a valid index format :( "); + } + + } + + public static void executeParseRemove (BookList books, String[] inputArray) { + parseRemoveGenre(books, inputArray); + } +} 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 25643c5e24..1980d47b63 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/InputLooper.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/InputLooper.java @@ -37,7 +37,7 @@ private boolean handleExitCommands(String input, Integer indentationValue) throw Ui.printSingleIndentation(); } if (indentationValue == 2) { - System.out.print("Enter the number for the desired genre, or add a new one:\n"); + System.out.println("Enter the number for the desired genre, or add a new one:"); Ui.printSingleIndentation(); } return true; @@ -93,15 +93,16 @@ private static boolean isByeCommand(String input) { private String processSelection(String newInput, Scanner scanner, BookList books) throws InvalidInputException, IOException { int selection = Integer.parseInt(newInput); - if (selection == books.getAvailableGenres().size() + 1) { + if (selection == books.genreList.getAvailableGenres().size() + 1) { 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()) { - return books.getAvailableGenres().get(selection - 1); + + 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."); 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 cfa925859e..20948de6cf 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/NewGenreModifier.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/parsegenre/NewGenreModifier.java @@ -6,10 +6,10 @@ public class NewGenreModifier { static void genreSelectionPrinter(BookList books) { System.out.println("Available genres:"); - for (int i = 0; i < books.getAvailableGenres().size(); i++) { - System.out.println((i + 1) + ". " + books.getAvailableGenres().get(i)); + for (int i = 0; i < books.genreList.getAvailableGenres().size(); i++) { + System.out.println((i + 1) + ". " + books.genreList.getAvailableGenres().get(i)); } - System.out.println((books.getAvailableGenres().size() + 1) + ". Add a new genre"); + System.out.println((books.genreList.getAvailableGenres().size() + 1) + ". Add a new genre"); } static String duplicateChecker(String input, BookList books) { @@ -17,7 +17,7 @@ static String duplicateChecker(String input, BookList books) { return null; } boolean genreExists = false; - for (String existingGenre : books.getAvailableGenres()) { + for (String existingGenre : books.genreList.getAvailableGenres()) { if (existingGenre.equalsIgnoreCase(input)) { genreExists = true; input = existingGenre; // Normalize to the existing genre's case @@ -25,7 +25,7 @@ static String duplicateChecker(String input, BookList books) { } } if (!genreExists) { - books.getAvailableGenres().add(input); + books.genreList.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/parserating/RatingChecks.java b/src/main/java/seedu/bookbuddy/parser/parsercommands/parserating/RatingChecks.java index ad3280280d..19152019ac 100644 --- a/src/main/java/seedu/bookbuddy/parser/parsercommands/parserating/RatingChecks.java +++ b/src/main/java/seedu/bookbuddy/parser/parsercommands/parserating/RatingChecks.java @@ -18,7 +18,7 @@ static String[] getRatingParts(String[] inputArray) { return ratingParts; } - static int parseInteger(String input, String errorMessage) { + public static int parseInteger(String input, String errorMessage) { if (isNotNumeric(input.trim())) { throw new IllegalArgumentException(errorMessage); } diff --git a/src/main/java/seedu/bookbuddy/parser/parservalidation/CommandList.java b/src/main/java/seedu/bookbuddy/parser/parservalidation/CommandList.java index 26a85e2f06..18e65819a0 100644 --- a/src/main/java/seedu/bookbuddy/parser/parservalidation/CommandList.java +++ b/src/main/java/seedu/bookbuddy/parser/parservalidation/CommandList.java @@ -22,4 +22,6 @@ public class CommandList { public static final String PRINT_ORDERED_COMMAND = "list-rated"; public static final String PRINT_ORDERED_DATE_COMMAND = "list-by-date"; public static final String AUTHOR_COMMAND = "set-author"; + public static final String GENRE_REMOVE_COMMAND = "remove-genre"; + public static final String LIST_GENRE_COMMAND = "list-genre"; } diff --git a/src/test/java/seedu/bookbuddy/ParserMainTest.java b/src/test/java/seedu/bookbuddy/ParserMainTest.java index a470ba1172..5a81fc93d5 100644 --- a/src/test/java/seedu/bookbuddy/ParserMainTest.java +++ b/src/test/java/seedu/bookbuddy/ParserMainTest.java @@ -200,7 +200,7 @@ void parseGenreCommand() { BookListModifier.addBook(books, "The Great Gatsby"); // Simulate user input for genre selection "Classic" - int genreSize = books.getAvailableGenres().size(); + int genreSize = books.genreList.getAvailableGenres().size(); String simulatedUserInput = genreSize + 1 + "\nClassic\n"; InputStream savedStandardInputStream = System.in; System.setIn(new ByteArrayInputStream(simulatedUserInput.getBytes()));