Home > database >  How to print this objects from start() function
How to print this objects from start() function

Time:05-08

I created 3 classes: Books, autors and main class. I created start() method and objects in it(the task is requiring). Here my cod please can someone explain it or is there better way to do it? I create array lists, is there better way to do it? I'm very grateful for any help

import java.util.ArrayList;
import java.util.Scanner;

public class BookAuthorStorage<E> {
    ArrayList<Book> bookStorage = new ArrayList <>();
    ArrayList<Author> authorsStorage = new ArrayList<>();
    public void start() {
        Book Alchemist = new Book("Alchemist", "That everything is possible", 100, "Paulo Coelho");
    Book HarryPotter = new Book("Harry Potter", "The Kind take over evil", 300, "J.K. Rowling");
    Book It = new Book("It", "Scary book", 200, "Stephen King");
    Author StephenKing = new Author("Stephen", "King", 46, "male", "[email protected]", "It");
    Author Rowling = new Author("Joan", "Rowling", 39, "female", "Rowling.com", "Harry Potter");
    Author Coelho = new Author("Paulo", "Coelho", 60, "male", "Coelho.com", "Alchemist");

        Scanner scan = new Scanner(System.in);
        System.out.println(" Enter 1 to show all books or 2 for authors and 3 for exit");
        while (true) {
            int choice = scan.nextInt();
            switch (choice) {
                case 1 -> bookStorage.forEach(System.out::println);
                case 2 -> authorsStorage.forEach(System.out::println);
                case 3 -> {
                    System.out.println(" Exiting");
                    return;
                }
                default -> System.out.println("Enter again");
            }
        }
    }

    public static void main(String[] args) {
       BookAuthorStorage bookAuthorStorage1 = new BookAuthorStorage();
        bookAuthorStorage1.start();


    }

}

When i try to print it prints null, null, null. I overided String in Book and Author classes

 public class Author {
        public  String name;
        public  String surname;
        public  int age;
        public  String gender;
        public  String email;
    
    Author(String name, String surname, int age, String gender,String email, String book){

}
       public String toString(){
          return this.name    this.surname    this.age   this.email   this.gender ;
       }
    
    
    }




    public class Book {
        public String title;
        public String description;
        public int count = 1000;
        public String author;
Book(String title, String description, int count, String author){

}
    
        @Override
       public String toString(){
            return this.author   this.description   this.title   this.count;
        }
    }

CodePudding user response:

Create parameterized constructor for Book & Author with all member variables & then initialize both classes in BookAuthorStorage something like:

    Author(String name, String surname, int age, String gender,String email, String book){
  this.name=name;
  this.surname=surname;
  this.age=age;
  this.gender=gender;
  this.email=email;
  this.book=book;
}
   

And inside BookAuthorStorage call :

    Book Alchemist = new Book("A","B",1,"C");
    ..
    Author StephenKing = new Author("D","E",1,"F","G");
    ..

Currently in original code, you are not initializing any String member variable apart from int member variables. So that is reason for your output always have null & int value.

CodePudding user response:

In a constructor, there is no implicit relationships between parameters and internal vars.
You must indicate what you want to do with parameters despite the names are the same :

Book(String title, String description, int count, String author){
this.title = title; 
this.description = description;
this.count = count;
this.author = author;
} 

It could be better to distinguish both to avoid errors, as parameters' name can be used inside the constructor.

Book(String aTitle, String aDescription, int aCount, String anAuthor){
this.title = aTitle; 
this.description = aDescription;
this.count = aCount;
this.author = anAuthor;
} 
  • Related