How to convert object value into string containing multiple data in objects?

Javed Ansari :
  class Author
  {
    String name;
    String email;
    char gender;

    Author(String name,String email,char gender)
    {
        this.name = name;
        this.email = email;
        this.gender = gender;
    }
  }

  class Book
  {
    String name;
    Author author;
    double price;
    int qtyInStock;

    Book(String name,Author author,double price,int qtyInStock)
    {
        this.name = name;
        this.author = author;
        this.price = price;
        this.qtyInStock = qtyInStock;
    }

    void setName(String name)
    {
        this.name = name;
    }

    void setAuthor(Author author)
    {
            this.author = author;
    }

    void setPrice(double price)
    {
        this.price = price;
    }

    void setQtyInStock(int qtyInStock)
    {
        this.qtyInStock = qtyInStock;
    }

    String getName()
    {
        return name;
    }

    Author getAuthor()
    {
        return author;
    }

    double getPrice()
    {
        return price;
    }

    int getQtyInStock()
    {
        return qtyInStock;
    }   
  }

  public class HOA1
  {
    public static void main(String[] args)
    {
        Author author = new Author("Javed","[email protected]",'M');
        Book book = new Book("WiproS",author,500.99,3);

        book.setName("JavaWorld");
        System.out.println(book.getName());
        String[] authDetails = book.getAuthor().toString();
        for(String strr: authDetails)
        {
           System.out.println(strr);
        }


    }
  }

In the above code, I have to provide solution for the question stated as

"Create a class Author with the following information.
Member variables : name (String), email (String), and gender (char)
Parameterized Constructor: To initialize the variables
Create a class Book with the following information.
Member variables : name (String), author (of the class Author you have just created), price (double), and qtyInStock (int)
[Assumption: Each book will be written by exactly one Author]
Parameterized Constructor: To initialize the variables
Getters and Setters for all the member variables
In the main method, create a book object and print all details of the book (including the author details) "

I am not able to solve it ! I want to print author details using book object .Please help.

Alex Somai :

First, you need to override the toString() method in your Author class, to pretty print the fields. An example could be:

class Author {

    // ... your fields ...

    @Override
    public String toString() {
        return "Author{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", gender=" + gender +
                '}';
    }
}

Then, here's the mistake that you are making. The toString() method from the Author class returns a String, not an array of String (String[] as you did). So, you could just do:

String authDetails = book.getAuthor().toString();
System.out.println(authDetails);

But, the recommended way to do this is the following. Just use an object for the Author class, and pass that object to the System.out.println(...) method. This will automatically call the toString() method on the given object.

Author author = book.getAuthor();
System.out.println(author);

Has the same effect as:

System.out.println(author.toString());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=194075&siteId=1