-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
907e2ee
commit fc54dd7
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |