This repository has been archived on 2026-03-18. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
CSE-123/debugging/Book.java
2026-03-18 00:39:35 -07:00

72 lines
1.6 KiB
Java

import java.util.*;
public class Book implements Media {
private String title;
private String author;
private List<String> authors;
private List<Integer> ratings;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public Book(String title, List<String> authors) {
this.title = title;
this.authors = authors;
}
public String getTitle() {
return this.title;
}
public List<String> getArtists() {
List<String> 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";
}
}