Skip to content

Commit

Permalink
Added features to remove genre and to list the current available genres
Browse files Browse the repository at this point in the history
  • Loading branch information
yeozongyao committed Apr 13, 2024
1 parent 3651acc commit 2142474
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/main/java/seedu/bookbuddy/FileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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.");
}
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/bookbuddy/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
18 changes: 1 addition & 17 deletions src/main/java/seedu/bookbuddy/booklist/BookList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,19 @@
import seedu.bookbuddy.book.Title;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Manages a list of books, allowing for operations such as adding, deleting,
* and marking book as read or unread.
*/
public class BookList {
protected ArrayList<String> availableGenres = new ArrayList<>(Arrays.asList(
"Fiction", "Non-Fiction", "Mystery", "Science Fiction", "Fantasy"
));
public final GenreList genreList = new GenreList();
protected ArrayList<BookMain> books;
public BookList() {
this.books = new ArrayList<BookMain>(); // Use ArrayList instead of array
}

public ArrayList<String> getAvailableGenres() {
return availableGenres;
}

public void setAvailableGenres(List<String> 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.
*/
Expand Down Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/seedu/bookbuddy/booklist/GenreList.java
Original file line number Diff line number Diff line change
@@ -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<String> availableGenres = new ArrayList<String>(Arrays.asList(
"Fiction", "Non-Fiction", "Mystery", "Science Fiction", "Fantasy"
));

public GenreList() {

}
public ArrayList<String> getAvailableGenres() {
return availableGenres;
}

public void setAvailableGenres(List<String> 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);
}

}
23 changes: 15 additions & 8 deletions src/main/java/seedu/bookbuddy/parser/ParserMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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. " +
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
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) {
if (input == null) {
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
break;
}
}
if (!genreExists) {
books.getAvailableGenres().add(input);
books.genreList.getAvailableGenres().add(input);
Ui.printLine();
System.out.println("Added new genre to the list: " + input);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
2 changes: 1 addition & 1 deletion src/test/java/seedu/bookbuddy/ParserMainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit 2142474

Please sign in to comment.