0

This is the output: Customer@682a0b20

Code:

Book class:

public class Book { 
  public  String title;
  
  public  String author;

  public  int genre;
  
  public Book(String title, String author, int genre) {
    this.title = title;
    this.author = author;
    this.genre = genre;
  }
  

  public String getBookTitle() {
    return  title;
  }
  public String getBookAuthor() {
    return author;
  }
  public int getBookGenre() {
    return genre;
  }
  
  

  
}

LibraryDatabase class:

import java.util.*;

public class LibraryDatabase extends Book {
  ArrayList<Book> bookDatabase;

  public LibraryDatabase(String title, String author, int genre) {
    super("1", "2", 3);
  }

  public ArrayList<Book> books(ArrayList<Book> bookDatabase) {
    Book book1 = new Book("Harry Potter", "J.K. Rowling", 1);

    bookDatabase.add(book1);

    return bookDatabase;
  }

  public String toString() {
    return ("Title: " + this.getBookTitle() + "Author: " + this.getBookAuthor() + "Genre: " + this.getBookGenre());
  }
}

Customer class:

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

public class Customer {
  
  public Customer() {
  }

  public void run() {
  borrow();
 }
public void borrow() {
titlesNow();
}
public void titlesNow() {
    System.out.println(toString());

  }

Tester class:

import java.util.*;
import java.io.*;
public class Tester {
    public static void main(String[] args) {
    Customer customer = new Customer();
    customer.run();
}
}

Why is the output like that? What is wrong in the code that is causing the console to print something like that for the ArrayList? I searched through many questions, and it usually seems like the problem is something to do with not having a toString() method. But I have that in the LibraryDatabase class which extends the Book class. So what's the problem?

cigien
  • 57,834
  • 11
  • 73
  • 112
  • 1
    Why does `LibraryDatabase` extend `Book` if it is a container for a list of `Book`s? – Federico klez Culloca Jun 03 '21 at 19:28
  • Try adding `@Override` in the toString method, anyway you have not relation between Customer and LibraryDataBase. Furthermore, in books method you will get a NullPointerException, you have to initialize the list. – cjgmj Jun 03 '21 at 19:29
  • I want to inherit the getBook() methods, @FedericoklezCulloca – Fouad Saffar Jun 03 '21 at 19:29
  • It didn't change anything. @cjgmj – Fouad Saffar Jun 03 '21 at 19:31
  • Check my comment again, I just edited it. – cjgmj Jun 03 '21 at 19:31
  • How exactly should I relate Customer and LibraryDatabase, and what do you mean by "initialize the list" – Fouad Saffar Jun 03 '21 at 19:34
  • Initialize the list like `List bookDatabase; = new ArrayList<>();`. When you are calling `public void titlesNow() { System.out.println(toString()); }` you are calling to the toString method from Customer that's why you got `Customer@682a0b20`. What do you wanna do? – cjgmj Jun 03 '21 at 19:37
  • I wanna print out the ArrayList. How do I do this. – Fouad Saffar Jun 03 '21 at 19:43
  • I initialized it and it still doesn't work. – Fouad Saffar Jun 03 '21 at 19:45
  • You have a messy code, I will try to clean up your code. – cjgmj Jun 03 '21 at 19:48
  • 1
    Please don't vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed, and thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](/help/what-to-do-instead-of-deleting-question). – cigien Jun 07 '21 at 17:38

2 Answers2

0

If you just wanna print the List your classes have to look like this.

Book

public class Book {
    public String title;
  
    public  String author;

    public  int genre;
    
    public Book(){}
  
    public Book(String title, String author, int genre) {
        this.title = title;
        this.author = author;
        this.genre = genre;
    }
  

    public String getBookTitle() {
        return  title;
    }
    
    public String getBookAuthor() {
        return author;
    }
    
    public int getBookGenre() {
        return genre;
    }

    @Override
    public String toString() {
        return ("Title: " + this.getBookTitle() + "Author: " + this.getBookAuthor() + "Genre: " + this.getBookGenre());
    }
}

LibraryDatabase

public class LibraryDatabase extends Book {
  List<Book> bookDatabase = new ArrayList<>();

  public LibraryDatabase() {
      super();
  }

  public List<Book> books() {
    Book book1 = new Book("Harry Potter", "J.K. Rowling", 1);

    bookDatabase.add(book1);

    return bookDatabase;
  }
}

Customer

public class Customer {
  
    public Customer() {
    }

    public void run() {
        borrow();
    }
    
    public void borrow() {
        titlesNow();
    }
    
    public void titlesNow() {
        LibraryDatabase library = new LibraryDatabase();
        library.books().forEach(System.out::println);
    }
}

I don't know why you have to extends Library Database from Book and why you have three methods in Customer calling each other to print the list. Furthermore I don't know if you want to get the same list from different classes, if so you have to implement singleton pattern.

cjgmj
  • 152
  • 1
  • 2
  • 10
0

You need to add overrides for toString in each class. In most IDE's you can automatically generate them if that makes things easier.

rubenpoppe
  • 307
  • 2
  • 10