0
import java.util.ArrayList;
import java.util.Collections;
import java.text.DecimalFormat;
public class PAssign7 {
public static void main(String args[]) {
ArrayList<Book> books = new ArrayList<Book>();
Book f1 = new Fiction("The Catcher in the Rye", "J.D. Salinger",1951, "Classics");
Book f2 = new Fiction("1984", "George Orwell",1949, "Classics");
Book f3 = new Fiction("The Fault in Our Stars", "John Green",2012, "Young Adult");
Book nf1= new NonFiction("The Immortal Life of Henrietta Lacks", "Rebecca Skloot",2010, "Medical History, HeLa cell line");
Book nf2= new NonFiction("Between the World and Me", "Ta-Nehisi Coates",2015, "American History, Race Relations");
Book nf3= new NonFiction("Blink", "Malcom Gladwell",2005, "Pop Science, Thinking/Neuroscience");
f1.rate(2151135, 8152802);
f2.rate(2101011, 8698186);
f3.rate(2538804, 10815305);
nf1.rate(414113, 1673017);
nf3.rate(366808, 1426833);
books.add(f1);
books.add(f2);
books.add(f3);
books.add(nf1);
books.add(nf2);
books.add(nf3);
Collections.sort(books);
for (Book book : books) {
System.out.println(book.getInfo());
}
}
interface Rateable{
public static void rate(double numRatings, double totalRatings) {
}
}
static abstract class Book implements Rateable, Comparable<Book>{
private String title;
private String author;
private int ReleaseYear;
private String genre;
private double numRatings;
private double totalRatings;
public Book(){
numRatings = 0;
totalRatings = 0;
}
public Book(String title, String author, int ReleaseYear, String genre){
this.title = title;
this.author = author;
this.ReleaseYear = ReleaseYear;
this.genre = genre;
numRatings = 0;
totalRatings = 0;
}
public abstract String getInfo();
public void rate(double numRating, double totalRating) {
this.totalRatings = totalRatings;
this.numRatings = numRatings;
}
public double getRating(){
if(numRatings == 0)
return -1;
else
return totalRatings/numRatings;
}
@Override
public int compareTo(Book book) {
if(getRating() <= book.getRating()) {
return -1;
} else {
return 1;
}
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getReleaseYear() {
return ReleaseYear;
}
public void setReleaseYear(int ReleaseYear) {
this.ReleaseYear = ReleaseYear;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public double getNumRatings() {
return numRatings;
}
public void setNumRatings(double numRatings) {
this.numRatings = numRatings;
}
public double getTotalRatings() {
return totalRatings;
}
public void setTotalRatings(double totalRatings) {
this.totalRatings = totalRatings;
}
public String toString() {
return title;
}
}
static class Fiction extends Book{
private String subGenre;
DecimalFormat df = new DecimalFormat("0.00");
public Fiction(String title, String author, int ReleaseYear, String subGenre){
super(title,author,ReleaseYear,"Fiction");
this.subGenre = subGenre;
}
public String getSubGenre(){
return subGenre;
}
public void setSubGenre(String subGenre){
this.subGenre = subGenre;
}
@Override
public String getInfo() {
String rating = "No ratings";
if (getRating() >= 1) {
rating = df.format(getNumRatings());
}
return getTitle() + " by " + getAuthor() + ", Fiction/" + getSubGenre()+", " + getReleaseYear()+ "("+ rating +"); ";
}
}
static class NonFiction extends Book{
private String topic;
DecimalFormat df = new DecimalFormat("0.00");
public NonFiction(String title, String author, int ReleaseYear, String topic){
super(title,author,ReleaseYear,"Non-Fiction");
this.topic = topic;
}
public String getTopic(){
return topic;
}
public void setTopic(String topic){
this.topic = topic;
}
@Override
public String getInfo() {
String rating = "No ratings";
if (getRating() >= 1) {
rating = df.format(getRating());
}
return getTitle() + " by " + getAuthor() + ", Non-Fiction, " + getReleaseYear()+", ("+ rating +"), Topic: " + getTopic();
}
}
}
私は私が探していますトラブル出力の本の評価を得るためのビットを持っています
、ジョン・グリーン、フィクション/ヤングアダルト、2012年までに私たちのスターでプログラムは定格を出力せず、代わりにデフォルトを使用します。
障害(4.26) 1984によりますジョージ・オーウェル、フィクション/クラシック、1949年(4.14)など、世界と私の間を除く他の本は定格を持たないためです。私に本の評価を与えるのではなく、すべての本の評定がないというだけで、理由はわかりません。どんな助けもありがとう。
に
を変更ありがとうございます!ノンフィクションの本は正しいレートを持つようになりましたが、フィクションのレートはメインメソッドのf1、f2、f3.ratesのnumRatingになりました。したがって、2151135.00,2101011.00、および2538804.00です。理由は何ですか? –
これは、あなたの 'Fiction'クラスがどのように書かれているかによるものです。 'getInfo()'を見ると、NonFictionの本は 'getRating()'を使い、あなたのフィクションの書籍は 'GetNumRatings()'を使って情報の文字列の評価を決めています。フィクションブックに平均を印刷させたい場合は、数学を行う方法を呼び出す必要があります。 – azurefrog
lolありがとうございます@azurefrog。今朝このことに取り組んできました。私はそれをスキャンするために余分な目を必要としました。 –