Abdelrahman Ali Abdelrahman Ali - 2 months ago 17
Java Question

how to make result in one path?


  1. this my code :

    import java.util.Arrays;

    public class BookClasss {
    private String name;
    public AuthorClass[] author ;
    private double price ;
    private int qty =0;

    public BookClasss(String names, AuthorClass[] authors, double price2,
    int qtyes)
    {

    this.name = names;

    this.author = authors;

    this.price = price2;

    this.qty = qtyes;
    }


    public void SetBookDetails (String names, AuthorClass[] authors,double price2 , int qtyes)
    {
    this.name = names;
    this.author = authors;
    this.price = price2;
    this.qty = qtyes;
    }


    public String Getbookname()
    {
    return name;
    }


    public double getprice()
    {
    return price;
    }

    public void setprice (double prices)
    {
    price = prices;
    }

    public void setQty (int qtnties)
    {
    qty = qtnties;
    }

    public int getQty ()
    {
    return qty;
    }

    public void GetAll ()
    {
    for (int i=0; i<author.length;i++)
    {
    System.out.println("Book name : "+name+", Authors : "+Arrays.asList(author[i].ToAll())+" , Price : "+price+"");
    }

    }

    }

  2. And that's the main method :

    public class TestBook
    {
    public static void main(String[] args)
    {

    AuthorClass[] authors = new AuthorClass[2];
    authors[0] = new AuthorClass( "John" , "John@yahoo.com" , 'M');
    authors[1] = new AuthorClass( "Sam" , "Sam@yahoo.com" , 'M');
    BookClasss book = new BookClasss("Java OOP",authors, 200.5, 20);
    book.GetAll();

    }

    }

  3. And the output is :

    Book name : Java OOP, Authors : [Author Name=John , Mail = John@yahoo.com] , Price : 120.2
    Book name : Java OOP, Authors : [Author Name=Sam , Mail = Sam@yahoo.com] , Price : 120.2


    i used forloop to get all of authors array result but i want to print the result be like :

    Book name : Java OOP, Authors : [Author Name=John , Mail = John@yahoo.com] , [Author Name=Sam , Mail = Sam@yahoo.com] , Price : 120.2


    in one result without repeating book name and price .. how can i do it ?

    Can anyone Help me ???


GC_ GC_
Answer
public void GetAll ()
 {
    System.out.println("Book name : "+name+", Authors :   ");
    for (int i=0; i<author.length;i++)
     {
          System.out.print(Arrays.asList(author[i].ToAll())+" ,"); 
     }
     System.out.println("Price : "+price+"");
  }