From 81ba9eb66f8284d8760012973f84cdbe617ceb94 Mon Sep 17 00:00:00 2001 From: pants Date: Mon, 21 Sep 2026 14:16:34 -0700 Subject: init --- debugging/Book.java | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 debugging/Book.java (limited to 'debugging/Book.java') diff --git a/debugging/Book.java b/debugging/Book.java new file mode 100644 index 0000000..05ef36d --- /dev/null +++ b/debugging/Book.java @@ -0,0 +1,71 @@ +import java.util.*; + +public class Book implements Media { + + private String title; + private String author; + private List authors; + private List ratings; + + public Book(String title, String author) { + this.title = title; + this.author = author; + } + + public Book(String title, List authors) { + this.title = title; + this.authors = authors; + } + + public String getTitle() { + return this.title; + } + + public List getArtists() { + List artists = new ArrayList<>(); + if (this.author != null) { + artists.add(this.author); + } + + if (this.authors != null) { + for (String author : authors) { + artists.add(author); + } + } + return artists; + } + + public void addRating(int score) { + if (this.ratings == null) { + ratings = new ArrayList<>(); + } + this.ratings.add(score); + } + + public int getNumRatings() { + + if (this.ratings == null) { + return 0; + } + + return this.ratings.size(); + } + + public double getAverageRating() { + if (this.ratings == null) { + return 0; + } + + int sum = 0; + for (int rating : ratings) { + sum += rating; + } + + return (double)sum / this.ratings.size(); + } + + public String toString() { + return this.title + " by " + this.getArtists() + ": " + this.getAverageRating() + + (this.ratings.size()) + " ratings"; + } +} -- cgit v1.2.3