Skip to content

Commit

Permalink
Bug fix - change availablegenre arraylist to be non-static. Reformatt…
Browse files Browse the repository at this point in the history
…ed for the rest of the code base
  • Loading branch information
yeozongyao committed Apr 13, 2024
1 parent 72c68bf commit 5cf817e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 37 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(","));
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.");
}
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 = BookList.saveGenresFormat();
String genresLine = books.saveGenresFormat();
fw.write("Genres: " + genresLine + '\n');

for (int i = 1; i <= books.getSize(); i += 1) {
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/seedu/bookbuddy/booklist/BookList.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
* and marking book as read or unread.
*/
public class BookList {
protected static List<String> availableGenres = new ArrayList<>(Arrays.asList("Fiction", "Non-Fiction",
"Mystery", "Science Fiction", "Fantasy"));
protected ArrayList<String> availableGenres = new ArrayList<>(Arrays.asList(
"Fiction", "Non-Fiction", "Mystery", "Science Fiction", "Fantasy"
));
protected ArrayList<BookMain> books;
public BookList() {
this.books = new ArrayList<BookMain>(); // Use ArrayList instead of array
}
public static List<String> getAvailableGenres() {
return BookList.availableGenres;

public ArrayList<String> getAvailableGenres() {
return availableGenres;
}
public static void setAvailableGenres(List<String> availableGenres) {
BookList.availableGenres = new ArrayList<>(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 @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@
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());
}
}
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+");
Expand Down Expand Up @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
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
break;
}
}
if (!genreExists) {
BookList.getAvailableGenres().add(input);
books.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 @@ -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.");

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/seedu/bookbuddy/ParserMainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit 5cf817e

Please sign in to comment.