Skip to content

Commit

Permalink
Add reviewing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedsiam0 committed Dec 10, 2023
1 parent 907e2ee commit fc54dd7
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/asu/librarysystem/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Library
protected static ArrayList<Borrower> borrowers = new ArrayList();
private static Account activeAccount;
private static Admin admin ;
private ReviewHandler reviewHandler;

//########################## Start for book #########################//
public static void addBook(Book book) {
Expand Down Expand Up @@ -257,4 +258,8 @@ public static boolean logInByphoneNumber(String phoneNumber, String password){
}
return false;
}

ReviewHandler getReviewHandler() {
return reviewHandler;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/asu/librarysystem/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,21 @@ public static void main(String[] args) {

System.out.println(b.get(2));

// Testing Cover
Book book = new Book("The Old Man And The Sea", "Ernest Hemingway", 1952, true, 200, 0, "book-covers/the_old_man_and_the_sea.jpg");
System.out.println(book.getCoverPath());

// Testing ReviewHandler
ReviewHandler reviewHandler = new ReviewHandler();
reviewHandler.addReview(1, 1, 1, "Nice Book");
reviewHandler.addReview(2, 1, 1, "");
reviewHandler.addReview(3, 1, 1, "");
reviewHandler.addReview(4, 1, 1, "");
reviewHandler.addReview(5, 1, 1, "Bad Book");
reviewHandler.deleteReview(3, 1);
ArrayList<Integer> ratings = reviewHandler.getBookRatings(1);
System.out.println(ratings.get(1));
System.out.println(reviewHandler.getReviewText(1, 1));
System.out.println(reviewHandler.getReviewText(5, 1));
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/asu/librarysystem/Review.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.asu.librarysystem;

public class Review {
private static int idCounter = 0;
private final int id;
private int reviewerId;
private int bookId;
private int rating;
private String text;

public Review(int reviewerId, int bookId, int rating, String text) {
id = ++idCounter;
this.reviewerId = reviewerId;
this.bookId = bookId;
setRating(rating);
this.text = text;
}
public Review(Review review) {
this.id = review.id;
this.reviewerId = review.reviewerId;
this.bookId = review.bookId;
this.rating = review.rating;
this.text = review.text;
}
public void setRating(int rating) {
if (rating > 5)
this.rating = 5;
else if (rating < 1)
this.rating = 1;
else
this.rating = rating;
}
public int getRating() {
return rating;
}
public int getId() {
return id;
}
public int getBookId() {
return bookId;
}
public int getReviewerId() {
return reviewerId;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
98 changes: 98 additions & 0 deletions src/main/java/com/asu/librarysystem/ReviewHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.asu.librarysystem;

import java.util.ArrayList;
import java.util.HashMap;

public class ReviewHandler {
private ArrayList<Review> reviews;
private HashMap<Integer, ArrayList<Integer>> bookRatings;

public ReviewHandler() {
reviews = new ArrayList<Review>();
bookRatings = new HashMap<Integer, ArrayList<Integer>>();
}

public void addReview(int reviewerId, int bookId, int rating, String text) {
Review review = findReview(reviewerId, bookId);
if (review.getReviewerId() != -1) {
updateReview(reviewerId, bookId, rating, text);
return;
}
review = new Review(reviewerId, bookId, rating, text);

reviews.add(review);
if (!bookRatings.containsKey(bookId)) {
bookRatings.put(bookId, new ArrayList<Integer>());
for (int i = 0; i < 6; i++)
bookRatings.get(bookId).add(0);
}
addBookRating(bookId, review.getRating());
}

public void updateReview(int reviewerId, int bookId, int rating, String text) {
Review review = findReview(reviewerId, bookId);
if (review.getRating() != rating) {
removeBookRating(bookId, review.getRating());
review.setRating(rating);
addBookRating(bookId, review.getRating());
}
if (!review.getText().equals(text)) {
review.setText(text);
}
}
public void deleteReview(int reviewerId, int bookId) {
int index = findReviewIndex(reviewerId, bookId);
if (index == -1)
return;
Review review = reviews.get(index);
removeBookRating(bookId, review.getRating());
reviews.remove(index);
}
public ArrayList<Review> getBookReviews(int bookId) {
ArrayList<Review> bookReviews = new ArrayList<Review>();

for (int i = 0; i < reviews.size(); i++) {
if (reviews.get(i).getBookId() == bookId) {
bookReviews.add(new Review(reviews.get(i)));
}
}

return bookReviews;
}

public ArrayList<Integer> getBookRatings(int bookId) {
return bookRatings.get(bookId);
}

public String getReviewText(int reviewerId, int bookId) {
return (new String(findReview(reviewerId, bookId).getText()));
}

public int getReviewRating(int reviewerId, int bookId) {
return findReview(reviewerId, bookId).getRating();
}

private void addBookRating(int bookId, int rating) {
Integer number = bookRatings.get(bookId).get(rating);
bookRatings.get(bookId).set(rating, number + 1);
}
private void removeBookRating(int bookId, int rating) {
Integer number = bookRatings.get(bookId).get(rating);
bookRatings.get(bookId).set(rating, number - 1);
}
private int findReviewIndex(int reviewerId, int bookId) {
for (int i = 0; i < reviews.size(); i++) {
if (reviews.get(i).getReviewerId() == reviewerId && reviews.get(i).getBookId() == bookId) {
return i;
}
}
return -1;
}
private Review findReview(int reviewerId, int bookId) {
int index = findReviewIndex(reviewerId, bookId);
if (index == -1)
return (new Review(-1, -1, -1, ""));
else
return reviews.get(index);
}
}

0 comments on commit fc54dd7

Please sign in to comment.