Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2324S2#57 from yeozongyao/branch-zong…
Browse files Browse the repository at this point in the history
…yao-setgenre

Update set-genre function for a more user-friendly user experience
  • Loading branch information
yeozongyao authored Apr 1, 2024
2 parents 7c9dc4b + b14c200 commit 2e20f2f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 26 deletions.
9 changes: 7 additions & 2 deletions src/main/java/seedu/bookbuddy/BookDetails.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package seedu.bookbuddy;

public class BookDetails {
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BookDetails {
protected static List<String> availableGenres = new ArrayList<>(Arrays.asList("Fiction", "Non-Fiction",
"Mystery", "Science Fiction", "Fantasy"));
protected String summary;

/**
* Sets the summary of the book at the specified index.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/bookbuddy/BookList.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void printAllBooks() {
System.out.println(currentBook.toString());
}
} else {
System.out.println("The list is empty.");
System.out.println("The list is empty. Add books by 'add [book]'");
}
}
}
74 changes: 53 additions & 21 deletions src/main/java/seedu/bookbuddy/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import exceptions.InvalidCommandArgumentException;
import exceptions.UnsupportedCommandException;

import java.util.Scanner;
import java.util.logging.Level;

import static seedu.bookbuddy.BookBuddy.LOGGER;

/**
Expand Down Expand Up @@ -157,34 +159,64 @@ public static void parseCommand(String input, BookList books) {
}
break;
case GENRE_COMMAND:
assert inputArray.length >= 2 : "Command requires additional arguments";
if (inputArray.length < 2) {
LOGGER.log(Level.WARNING, "The genre Command requires a book index and genre", inputArray);
throw new InvalidCommandArgumentException("The genre command requires a book index and genre.");
}
String[] genreMessageParts = inputArray[1].split(" ", 2);
try {
if (inputArray.length < 2) {
throw new InvalidCommandArgumentException("Usage: set-genre [index]");
}

if (genreMessageParts.length < 2) {
throw new InvalidCommandArgumentException("You need to have a genre message");
}
// Split the message into index and genre message
assert genreMessageParts.length == 2 : "Command requires an index and a genre message";
index = Integer.parseInt(inputArray[1]);
if (index < 0 || index > books.getSize()) {
throw new IndexOutOfBoundsException("Invalid book index. Please enter a valid index. " +
"Type 'list' to view the list of books.");
}
System.out.println("Available genres:");
for (int i = 0; i < BookDetails.availableGenres.size(); i++) {
System.out.println((i + 1) + ". " + BookDetails.availableGenres.get(i));
}
System.out.println((BookDetails.availableGenres.size() + 1) + ". Add a new genre");

try {
index = Integer.parseInt(genreMessageParts[0]);
assert index >= 0 : "Index should be non-negative";
String label = genreMessageParts[1];
BookDetails.setBookGenreByIndex(index, label, books);
System.out.println("Enter the number for the desired genre, or add a new one:");
Scanner scanner = new Scanner(System.in);

String selectedGenre = null;
while (selectedGenre == null) {
while (!scanner.hasNextInt()) { // Ensure the next input is an integer
String newInput = scanner.nextLine();
if ("exit".equalsIgnoreCase(newInput)) {
return; // Exit the command if user types 'exit'
} else {
System.out.println("Invalid input. Please enter a valid number or type 'exit'" +
" to cancel.");
}
}

int genreSelection = scanner.nextInt();
scanner.nextLine(); // Consume the newline after the number

if (genreSelection == BookDetails.availableGenres.size() + 1) {
System.out.println("Enter the new genre:");
selectedGenre = scanner.nextLine();
BookDetails.availableGenres.add(selectedGenre); // Add the new genre to the list
} else if (genreSelection > 0 && genreSelection <= BookDetails.availableGenres.size()) {
selectedGenre = BookDetails.availableGenres.get(genreSelection - 1);
} else {
System.out.println("Invalid selection. Please enter a valid number " +
"or type 'exit' to cancel.");
// No need for the nextLine or parsing logic here, the while loop will continue
}
}

BookDetails.setBookGenreByIndex(index, selectedGenre, books);
System.out.println("Genre set to " + selectedGenre + " for book at index " + index);
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + genreMessageParts[0]
+ " is not a valid number. Please enter a valid numeric index.");
} catch (InvalidCommandArgumentException e) {
System.out.println("Invalid input: " + inputArray[1] + " is not a valid number. " +
"Please enter a valid numeric index. Type 'list' to view the list of books.") ;
} catch (InvalidCommandArgumentException | IndexOutOfBoundsException e) {
System.out.println(e.getMessage());
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid book index. Please enter a valid index.");
} catch (Exception e) {
System.out.println("An error occurred while setting the genre: " + e.getMessage());
}

break;
case DISPLAY_COMMAND:
assert inputArray.length >= 2 : "Command requires additional arguments";
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/seedu/bookbuddy/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import exceptions.UnsupportedCommandException;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -13,6 +15,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;



public class ParserTest {
@Test
void testParser() {
Expand Down Expand Up @@ -78,8 +81,19 @@ void parseLabelCommand() {
void parseGenreCommand() {
BookList books = new BookList();
books.addBook("The Great Gatsby");
Parser.parseCommand("set-genre 1 Classic", books);
assertEquals("Classic", books.getBook(1).getGenre());
// Simulate user input for genre selection "Classic"
String simulatedUserInput = "6\nClassic\n"; // Assuming '3' is the option to add a new genre
InputStream savedStandardInputStream = System.in;
System.setIn(new ByteArrayInputStream(simulatedUserInput.getBytes()));
Parser.parseCommand("set-genre 1", books); // Changed to fit your updated command-handling logic
assertEquals("Classic", books.getBook(1).getGenre()); // Indexes are typically 0-based in lists

books.addBook("Geronimo");
String nextSimulatedUserInput = "3\n";
System.setIn(new ByteArrayInputStream(nextSimulatedUserInput.getBytes()));
Parser.parseCommand("set-genre 2", books);
assertEquals("Mystery", books.getBook(2).getGenre());
System.setIn(savedStandardInputStream);
}

@Test
Expand Down

0 comments on commit 2e20f2f

Please sign in to comment.