From fc54dd77809811cc0d7eea253f4dd981d6e51ce0 Mon Sep 17 00:00:00 2001 From: Ahmed Siam Date: Sun, 10 Dec 2023 02:22:12 +0200 Subject: [PATCH] Add reviewing functionality --- .../java/com/asu/librarysystem/Library.java | 5 + .../asu/librarysystem/MainApplication.java | 13 +++ .../java/com/asu/librarysystem/Review.java | 51 ++++++++++ .../com/asu/librarysystem/ReviewHandler.java | 98 +++++++++++++++++++ 4 files changed, 167 insertions(+) create mode 100644 src/main/java/com/asu/librarysystem/Review.java create mode 100644 src/main/java/com/asu/librarysystem/ReviewHandler.java diff --git a/src/main/java/com/asu/librarysystem/Library.java b/src/main/java/com/asu/librarysystem/Library.java index b6079cc..c61158e 100644 --- a/src/main/java/com/asu/librarysystem/Library.java +++ b/src/main/java/com/asu/librarysystem/Library.java @@ -9,6 +9,7 @@ public class Library protected static ArrayList borrowers = new ArrayList(); private static Account activeAccount; private static Admin admin ; + private ReviewHandler reviewHandler; //########################## Start for book #########################// public static void addBook(Book book) { @@ -257,4 +258,8 @@ public static boolean logInByphoneNumber(String phoneNumber, String password){ } return false; } + + ReviewHandler getReviewHandler() { + return reviewHandler; + } } diff --git a/src/main/java/com/asu/librarysystem/MainApplication.java b/src/main/java/com/asu/librarysystem/MainApplication.java index bafb276..ef57e87 100644 --- a/src/main/java/com/asu/librarysystem/MainApplication.java +++ b/src/main/java/com/asu/librarysystem/MainApplication.java @@ -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 ratings = reviewHandler.getBookRatings(1); + System.out.println(ratings.get(1)); + System.out.println(reviewHandler.getReviewText(1, 1)); + System.out.println(reviewHandler.getReviewText(5, 1)); } } diff --git a/src/main/java/com/asu/librarysystem/Review.java b/src/main/java/com/asu/librarysystem/Review.java new file mode 100644 index 0000000..f803113 --- /dev/null +++ b/src/main/java/com/asu/librarysystem/Review.java @@ -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; + } +} diff --git a/src/main/java/com/asu/librarysystem/ReviewHandler.java b/src/main/java/com/asu/librarysystem/ReviewHandler.java new file mode 100644 index 0000000..dda62a8 --- /dev/null +++ b/src/main/java/com/asu/librarysystem/ReviewHandler.java @@ -0,0 +1,98 @@ +package com.asu.librarysystem; + +import java.util.ArrayList; +import java.util.HashMap; + +public class ReviewHandler { + private ArrayList reviews; + private HashMap> bookRatings; + + public ReviewHandler() { + reviews = new ArrayList(); + bookRatings = new HashMap>(); + } + + 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()); + 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 getBookReviews(int bookId) { + ArrayList bookReviews = new ArrayList(); + + 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 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); + } +}