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

时间:2015-01-25 04:51:15

标签: java class object tostring

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

图书类

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";
}
}

审核课程

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.*;

主要类

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");
  }
 }

2 个答案:

答案 0 :(得分:0)

这是罪魁祸首

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

你正在做的是创建一个新的对象,它在方法调用后死掉 无论何时添加评级,都要将其添加到实例变量而不是局部变量,即类似这样的

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

由于这个原因,您的reviewBook会更新,并且会打印必需的结果

< --- new Edit --->

查看您的Book类默认构造函数

您正在使用Review rev = new Review();创建一个新对象,但实际上bookReview = new Review();您正在创建一个新对象,但在toString中,只有bookReview可以访问,因此您需要初始化这一点。

答案 1 :(得分:0)

您必须初始化“审核”对象。

我只是使用一些示例数据来初始化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)