无法在 toString 方法 Java 中调用其他 class

Trouble calling other class in toString method Java

我觉得这很愚蠢,但我在阅读本书的最后一部分时遇到了问题 class。我似乎无法弄清楚在这两个字符串的最后一段中放什么。它应该输入书名、出版商名称和书评。如果没有评论,它应该打印 0.00(0) 但我一直返回 null。任何帮助将不胜感激。

图书Class

import java.text.DecimalFormat;
import java.util.*;

public class Book {

private String title;
private String publisher;
private Review bookReview;
DecimalFormat format = new DecimalFormat("0.00");

public Book(String title, String publisher, Review bookRev) {

    this.title = title;
    this.publisher = publisher;
    bookReview = bookRev;
}

public Book() {

    title = "?";
    publisher = "?";
    Review rev = new Review();
}

public String getTitle() {
    return title;
}
public String getPublisher() {
    return publisher;
}
public Review getReview() {
    return bookReview;
}
public void setTitle(String aTitle) {
    title = aTitle;
}
public void setPublisher(String aPublisher) {
    publisher = aPublisher;
}
public void addRating(double rate) {
    Review rev = new Review();
    rev.updateRating(rate);
}
public String toString() {
    return "\n" + "Title:" + "\t" + title + "," + "\n" + 
            "Publisher:" + "\t" + publisher + "," + "\n" +
            "Reviews:" + "\t" + bookReview + "\n\n";
}
}

评论Class

public class Review {

private int numberOfReviews;
private double sumOfRatings;
private double average;
DecimalFormat format = new DecimalFormat("0.00");

public Review(int numRev, double sumRat, double average) {

    numberOfReviews = numRev;
    sumOfRatings = sumRat;
    this.average = average;
}

public Review() {

    numberOfReviews = 0;
    sumOfRatings = 0.0;
    average = 0.0;
}

public void updateRating(double rating) {

    numberOfReviews ++;
    sumOfRatings = sumOfRatings + rating;
    if (numberOfReviews > 0) {
        average = sumOfRatings / numberOfReviews;
    }
}

public String toString() {

    return "Reviews:" + "\t" + format.format(average) + "(" + numberOfReviews + ")";
}
}

import java.io.*;
import java.util.*;

主要Class

public class Assignment4
{
public static void main (String[] args)
{
   // local variables, can be accessed anywhere from the main method
   char input1 = 'Z';
   String inputInfo;
   String bookTitle;
   String bookPublisher;
   double rating;
   String line = new String();

   // instantiate a Book object
   Book book1 = new Book();

   printMenu();

   //Create a Scanner object to read user input
   Scanner scan = new Scanner(System.in);


   do  // will ask for user input
    {
     System.out.println("What action would you like to perform?");
     line = scan.nextLine();

     if (line.length() == 1)
      {
       input1 = line.charAt(0);
       input1 = Character.toUpperCase(input1);

       // matches one of the case statement
       switch (input1)
        {
         case 'A':   //Add Book
           book1 = new Book();
           System.out.print("Please enter the book information:\n");
           System.out.print("Enter a book title:\n");
           bookTitle = scan.nextLine();
           book1.setTitle(bookTitle);

           System.out.print("Enter its publisher:\n");
           bookPublisher = scan.nextLine();
           book1.setPublisher(bookPublisher);
           break;
         case 'D':   //Display Book
           System.out.print(book1);
           break;
         case 'Q':   //Quit
           break;
         case 'R':   //Add Rating
           System.out.print("Please give a rating of the book:\n");
           rating = Double.parseDouble(scan.nextLine());
           book1.addRating(rating);
           break;
         case '?':   //Display Menu
           printMenu();
           break;
         default:
           System.out.print("Unknown action\n");
           break;
        }
      }
     else
      {
       System.out.print("Unknown action\n");
      }
    } while (input1 != 'Q' || line.length() != 1);
  }


 /** The method printMenu displays the menu to a user**/
public static void printMenu()
{
 System.out.print("Choice\t\tAction\n" +
                    "------\t\t------\n" +
                    "A\t\tAdd Book\n" +
                    "D\t\tDisplay Book\n" +
                    "Q\t\tQuit\n" +
                    "R\t\tGive Rating\n" +
                    "?\t\tDisplay Help\n\n");
  }
 }

罪魁祸首

public void addRating(double rate) {
    Review rev = new Review();
    rev.updateRating(rate);
}

你正在做的是创建一个新对象,它在方法调用后死亡 每当您添加评级时,将其添加到您的实例变量而不是局部变量,即像这样的东西

 public void addRating(double rate) {
        bookReview = new Review();
        bookReview.updateRating(rate);
    }

因此,您的 reviewBook 将得到更新并需要打印,结果

<---新编辑--->

查看您的图书 class 默认构造函数

您正在使用 Review rev = new Review(); 创建一个新对象,但实际上应该是 bookReview = new Review(); 您正在创建一个新对象,但在 toString 中只能访问 bookReview,因此您需要初始化它。

您必须初始化 "Review" 对象。

我只是使用一些示例数据来初始化 Book And Review :

Review reviewObj = new Review(23, 5, 133);
Mainclass obj  = new Mainclass("first things first", "s.covey", reviewObj);

下面是输出:

Title:  first things first,
Publisher:  s.covey,
Reviews:    Reviews:    133.00(23)