Good morning companions, i want to generate a top 10 of the best rated books (reviews), I have an Arraylist with all the books and another with the reviews entered related to each book (book id)
Class Book
public class Book {
private int idbook;
private String title;
private String editorial;
public Book(int idbook, String title, String editorial) {
this.idbook = idbook;
this.title = title;
this.editorial = editorial;
}
public int getIdbook() {
return idbook;
}
public void setIdbook(int idbook) {
this.idbook = idbook;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getEditorial() {
return editorial;
}
public void setEditorial(String editorial) {
this.editorial = editorial;
}
Class Reviews
public class Reviews {
private int idreview;
private int idbook;
private int rating;
public Reviews(int idreview, int idbook, int rating) {
this.idreview = idreview;
this.idbook = idbook;
this.rating = rating;
}
public int getIdreview() {
return idreview;
}
public void setIdreview(int idreview) {
this.idreview = idreview;
}
public int getIdbook() {
return idbook;
}
public void setIdbook(int idbook) {
this.idbook = idbook;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
}
then I go through the book arraylist to get its ID and match it with the reviews entered that also have the book ID and its rating
public static void main(String[] args) {
// TODO code application logic here
// Books
ArrayList<Book> book = new ArrayList<Book>();
Book book1 = new Book(123,"Dante Aligueri","Planet");
Book book2 = new Book(456,"Virginia Wolf","Planet");
book.add(book1);
book.add(book2);
// Reseñas
ArrayList<Reviews> top = new ArrayList<Reviews>();
Reviews top1 = new Reviews(1,123,9);
Reviews top2 = new Reviews(2,123,9);
Reviews top3 = new Reviews(3,123,9);
Reviews top4 = new Reviews(4, 456,10);
top.add(top1);
top.add(top2);
top.add(top3);
top.add(top4);
int sum = 0;
int rating = 0;
long idbook = 0;
for (int i = 0; i < mybook.size(); i ) {
for (int j = 0; j < mytop.size(); j ) {
if (book.get(i).getIdlibro() == top.get(j).getIdlibro()) {
idbook = book.get(i).getIdlibro();
rating = top.get(j).getRating();
sum = rating;
}
tops.setText("book id: " idbook "total rating" sum);
}
}
}
I am trying to get all each book with its rating, but it only shows me a single book and its respective score, could you please help me.
I try to get output like this:
book id: 123 total rating: 27
book id: 456 total rating: 10
CodePudding user response:
I am trying to get all each book with its rating
Are you sure there will be a rating for each book?
for (int i = 0; i < mybook.size(); i ) {
boolean ratingFound= false;
sum= 0;
for (int j = 0; j < mytop.size(); j ) {
if (mybook.get(i).getIdbook() == mytop.get(j).getIdbook()) {
idbook = mybook.get(j).getIdbook();
rating = mytop.get(j).getrating();
sum = rating;
ratingFound= true;
}
}
if (ratingFound) { // Or: if (sum>0) {
// I would store the values in a Map which you can sort in the end,
// since you are interested only in the top 10.
tops.setText("book id: " idbook "total rating" sum);
}
}
CodePudding user response:
That column can easily be generated with a sub-query, so why mess around ?? Something alike:
(
SELECT SUM(stars) FROM reviews WHERE reviews.book_id = books.book_id GROUP BY book_id;
),
Else you'll end up with a list that is not sortable.