Totals from the objects on the list
A class that holds a list of objects can answer questions about all of them together: how many pages the books have in all, how long they are on average, how many pages one author wrote. Each answer is a loop over the list:
public class Main {
public static void main(String[] args) {
Bookshelf shelf = new Bookshelf("Study");
shelf.add(new Book("Emma", "Austen", 474));
shelf.add(new Book("Persuasion", "Austen", 249));
shelf.add(new Book("Dracula", "Stoker", 418));
shelf.add(new Book("Kindred", "Butler", 264));
System.out.println("Pages: " + shelf.totalPages());
System.out.println("Average: " + shelf.averagePages());
System.out.println("By Austen: " + shelf.pagesBy("Austen"));
Bookshelf empty = new Bookshelf("Hallway");
System.out.println("Pages: " + empty.totalPages());
System.out.println("Average: " + empty.averagePages());
}
}import java.util.ArrayList;
public class Bookshelf {
private String room;
private ArrayList<Book> books;
public Bookshelf(String room) {
this.room = room;
this.books = new ArrayList<>();
}
public void add(Book book) {
this.books.add(book);
}
public int totalPages() {
int total = 0;
for (Book book : this.books) {
total = total + book.getPages();
}
return total;
}
public double averagePages() {
if (this.books.isEmpty()) {
return 0;
}
return (double) this.totalPages() / this.books.size();
}
public int pagesBy(String author) {
int total = 0;
for (Book book : this.books) {
if (book.getAuthor().equals(author)) {
total = total + book.getPages();
}
}
return total;
}
}public class Book {
private String title;
private String author;
private int pages;
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
public String getAuthor() {
return this.author;
}
public int getPages() {
return this.pages;
}
}Pages: 1405
Average: 351.25
By Austen: 723
Pages: 0
Average: 0.0totalPagesis the sum pattern you know, over the shelf's own list: a local variable starts at 0, and each book adds its pages to it.averagePagesdoesn't write the loop again. It calls the object's owntotalPages()withthis.(module 18), and divides by the number of books. The(double)makes the division keep its decimals (module 4). Without it,1405 / 4would be351.- The empty check comes first. With no books, the division would be 0 divided by 0, which for a
doublegivesNaN, "not a number". Returning 0 is the choice made here, and since the method returns adouble, it comes out as0.0. pagesByis a total with a condition: only the books that match add their pages.
Counted when asked, not kept
The shelf could keep the total in an instance variable of its own, and add to it in add. But then every method that changes the list, such as one that removes books, would have to remember to change the total too, and one that forgets makes it wrong. Counting from the list each time can't go out of date: the list is always right, so the total is too.