Skip to content

Commit

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

Add rating system for books. Rate books and list books based on good …
  • Loading branch information
yeozongyao authored Apr 1, 2024
2 parents 2e20f2f + afd2e47 commit 3cff111
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 20 deletions.
25 changes: 24 additions & 1 deletion src/main/java/seedu/bookbuddy/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Book {
protected boolean isRead;
protected String label;
protected String genre;

protected int rating;
protected String summary;

/**
Expand All @@ -18,6 +18,29 @@ public Book(String title) {
this.isRead = false; //Completion status of the book (True: Read, False: Unread)
this.label = "";
this.genre = "";
this.rating = -1;
}

/**
* Sets the rating for this book. The rating must be between 1 and 5.
*
* @param rating The rating to set for the book.
* @throws IllegalArgumentException if the rating is not between 1 and 5.
*/
public void setRating(int rating) {
if (rating < 1 || rating > 5) {
throw new IllegalArgumentException("Rating must be between 1 and 5.");
}
this.rating = rating;
}

/**
* Returns the rating of the book.
*
* @return The rating of the book.
*/
public int getRating() {
return this.rating;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/bookbuddy/BookBuddy.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class BookBuddy {

private static BookList books = new BookList();
public static void main(String[] args) {
assert false : "dummy assertion set to fail";
LOGGER.log(Level.INFO, "BookBuddy application started.");
Ui.printWelcome();
assert books != null : "BookList not created";
Expand Down
53 changes: 48 additions & 5 deletions src/main/java/seedu/bookbuddy/BookDetails.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
package seedu.bookbuddy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

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



/**
* Sets the rating of the book at the specified index.
*
* @param index The index of the book in the list.
* @param rating The rating to set for the book.
* @throws IndexOutOfBoundsException if the index is out of range.
* @throws IllegalArgumentException if the rating is not between 1 and 5.
*/
public static void setBookRatingByIndex(int index, int rating, BookList books)
throws IndexOutOfBoundsException, IllegalArgumentException {
if (index < 0 || index > books.getSize()) {
throw new IndexOutOfBoundsException("Invalid book index. Please enter a valid index.");
}
if (rating < 1 || rating > 5) {
throw new IllegalArgumentException("Rating must be between 1 and 5.");
}
books.getBook(index).setRating(rating);
String title = books.getBook(index).getTitle();
Ui.setRatingBookMessage(title, rating);
}


/**
* Prints all books sorted by rating in descending order.
*/
public static void printBooksByRating(BookList books) {
if (books.books.isEmpty()) {
System.out.println("The list is empty. Add books by 'add [book]'");
return;
}

System.out.println("Books sorted by rating:");

List<Book> sortedBooks = books.books.stream()
.sorted(Comparator.comparingInt(Book::getRating).reversed())
.collect(Collectors.toList());

for (Book book : sortedBooks) {
String rating = book.getRating() >= 0 ? String.valueOf(book.getRating()) : "Not Rated";
System.out.println(book.getTitle() + " - " + rating);
}
}

/**
* Sets the summary of the book at the specified index.
*
Expand Down Expand Up @@ -79,6 +121,7 @@ public static void displayDetails(int index, BookList books) throws IndexOutOfBo
System.out.println("Status: " + (books.getBook(index).isRead ? "Read" : "Unread"));
System.out.println("Label: " + books.getBook(index).getLabel());
System.out.println("Genre: " + books.getBook(index).getGenre());
System.out.println("Rating: " + books.getBook(index).getRating());
}

}
4 changes: 4 additions & 0 deletions src/main/java/seedu/bookbuddy/BookList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import exceptions.BookUnreadAlreadyException;

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

import static seedu.bookbuddy.BookBuddy.LOGGER;
Expand All @@ -15,6 +17,8 @@
* 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<Book> books;

/**
Expand Down
52 changes: 39 additions & 13 deletions src/main/java/seedu/bookbuddy/Parser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package seedu.bookbuddy;

import exceptions.BookNotFoundException;
import exceptions.InvalidBookIndexException;
import exceptions.InvalidCommandArgumentException;
import exceptions.UnsupportedCommandException;

Expand All @@ -26,6 +25,8 @@ public class Parser {
public static final String GENRE_COMMAND = "set-genre";
public static final String SUMMARY_COMMAND = "give-summary";
public static final String DISPLAY_COMMAND = "display";
public static final String RATING_COMMAND = "rate";
public static final String PRINT_ORDERED_COMMAND = "listrated";

/**
* Scans the user input for valid commands and handles them accordingly.
Expand Down Expand Up @@ -149,7 +150,7 @@ public static void parseCommand(String input, BookList books) {
BookDetails.setBookSummaryByIndex(index, summary, books);
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + summaryMessageParts[0]
+ " is not a valid number. Please enter a valid numeric index.");
+ " is not a valid number. Please enter a valid numeric index. here");
} catch (InvalidCommandArgumentException e) {
System.out.println(e.getMessage());
} catch (IndexOutOfBoundsException e) {
Expand All @@ -170,10 +171,10 @@ public static void parseCommand(String input, BookList books) {
"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));
for (int i = 0; i < BookList.availableGenres.size(); i++) {
System.out.println((i + 1) + ". " + BookList.availableGenres.get(i));
}
System.out.println((BookDetails.availableGenres.size() + 1) + ". Add a new genre");
System.out.println((BookList.availableGenres.size() + 1) + ". Add a new genre");

System.out.println("Enter the number for the desired genre, or add a new one:");
Scanner scanner = new Scanner(System.in);
Expand All @@ -193,12 +194,12 @@ public static void parseCommand(String input, BookList books) {
int genreSelection = scanner.nextInt();
scanner.nextLine(); // Consume the newline after the number

if (genreSelection == BookDetails.availableGenres.size() + 1) {
if (genreSelection == BookList.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);
BookList.availableGenres.add(selectedGenre); // Add the new genre to the list
} else if (genreSelection > 0 && genreSelection <= BookList.availableGenres.size()) {
selectedGenre = BookList.availableGenres.get(genreSelection - 1);
} else {
System.out.println("Invalid selection. Please enter a valid number " +
"or type 'exit' to cancel.");
Expand All @@ -216,7 +217,31 @@ public static void parseCommand(String input, BookList books) {
} catch (Exception e) {
System.out.println("An error occurred while setting the genre: " + e.getMessage());
}

break;
case RATING_COMMAND:
assert inputArray.length >= 2 : "Command requires additional arguments";
if (inputArray.length < 2) {
LOGGER.log(Level.WARNING, "The rating Command requires a book index", inputArray);
throw new InvalidCommandArgumentException("The rating command requires a book index.");
}
try {
String[] ratingParts = inputArray[1].split(" ", 2);
// Split the message into index and label message
assert ratingParts.length == 2 : "Command requires an index and a rating";
if (ratingParts.length < 2) {
throw new InvalidCommandArgumentException("You need to have a book index and a rating");
}
index = Integer.parseInt(ratingParts[0]);
int rating = Integer.parseInt(ratingParts[1]);
BookDetails.setBookRatingByIndex(index, rating, books);
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + inputArray[1] + " is not a valid number. " +
"Please enter a valid numeric index.");
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid book index. Please enter a valid index.");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
break;
case DISPLAY_COMMAND:
assert inputArray.length >= 2 : "Command requires additional arguments";
Expand All @@ -236,6 +261,9 @@ public static void parseCommand(String input, BookList books) {
System.out.println(e.getMessage());
}
break;
case PRINT_ORDERED_COMMAND:
BookDetails.printBooksByRating(books);
break;
case EXIT_COMMAND:
Ui.printExitMessage();
System.exit(0);
Expand All @@ -245,9 +273,7 @@ public static void parseCommand(String input, BookList books) {
throw new UnsupportedCommandException("Sorry but that is not a valid command. " +
"Please try again or type: help");
}
} catch (NumberFormatException e) {
throw new InvalidBookIndexException("Book index must be an integer.");
} catch (IndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {
throw new BookNotFoundException("Book not found at the provided index.");
} catch (InvalidCommandArgumentException e) {
LOGGER.log(Level.WARNING, "Invalid command argument: {0}", e.getMessage());
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 @@ -48,6 +48,10 @@ public static void setGenreBookMessage(String title, String genre) {
System.out.println("okii categorised [" + title + "] as [" + genre + "]");
System.out.println("remember to read it soon....");
}
public static void setRatingBookMessage(String title, int rating) {
System.out.println("okii set rating for [" + title + "] as [" + rating +"]");
System.out.println("remember to read it soon....");
}
public static void removeBookMessage(int index, BookList books) {
System.out.println("alright.. i've removed " + books.getBook(index).getTitle() + " from the list.");
}
Expand Down

0 comments on commit 3cff111

Please sign in to comment.