Results 1 to 8 of 8
  1. #1
    misbah6 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default Showing object details of another related class

    I have 2 classes - Library and Lender.
    In the library class, I can create multiple objects with each object storing details of a particular book like author, publisher, title,etc.
    Now the Lender class is related to/dependent on Library class. In the Lender class, I have a method called displayAllBooks which I have to use to display all the books(or objects) from the Library class.
    I have tried to do this but was unsuccessful. How do I implement this especially the displayAllBooks method ?

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    It's not clear to me what a Lender is, and how Lender relates to Library. I would think that a Library would have a List of Book objects, and would have a getBooks() method to allow another class to retrieve that List<Book>. It's not clear what you want displayAllBooks() to do, and why it's the Lender doing it, rather than the Library.

    -Gary-

  3. #3
    misbah6 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Yes, I agree it can be done easily in a single class but I have been specified to use two different class to implement the system.
    Lender class contains two methods. One is the display menu method which gives two options to the user(Option1: Show all books..Option2: Exit). Other is the display books method which is used to display all the book objects created using the book class(when the user selects 1st option).
    I'm using BlueJ as the java IDE and Lender class can use the methods of Library class once an instance of Library class is defined in Lender class.
    I have tried to use an arraylist defined in Library class and a method to retrieve objects from it. But when I tried calling the method from the lender class, it didn't work.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    OK, it makes more sense now. What exactly did you try, and how did it fail to work? You should have a getBooks() method in your Library class, and you should call it from your Lender class.
    Java Code:
    public class Library {
            private List<Book> books = new ArrayList<Book>();
            ...
            public List<Book> getBooks() {
                    ...
            }
    }
    and
    Java Code:
    public class Lender {
            private Library theLibrary; // initialize this in the constructor
            ...
            public void displayAllBooks() {
                    List<Book> books = theLibrary.getBooks();
                    for (Book b : books) {
                            // code to display books
                    }
            }
    }
    There has to be some other code somewhere to put Books in the Library in the first place, of course.

    -Gary-

  5. #5
    misbah6 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    I was using the arraylist in the wrong manner but now its fine thanks to you.
    I have implemented what you said and I just wanted to confirm one thing from you that whether getBooks method is written like this --

    public List<Book> getBooks()
    {
    return books;
    }

    Because when I run the program, nothing seems to get assigned to the list in displayAllBooks when I call the getBooks method in it.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by misbah6 View Post
    I was using the arraylist in the wrong manner but now its fine thanks to you.
    I have implemented what you said and I just wanted to confirm one thing from you that whether getBooks method is written like this --

    public List<Book> getBooks()
    {
    return books;
    }

    Because when I run the program, nothing seems to get assigned to the list in displayAllBooks when I call the getBooks method in it.
    Where, when and how do you add Books to the List?

    -Gary-

  7. #7
    misbah6 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    In the Library class, I have a method called storeBooks in which I pass a Library class object as parameter and use and use .add function to add the object to the arraylist.
    So when I create an object of Library class i.e add all the details of 1 book(or object), then I call the storeBooks method to add the object to the list. Then I create another object, enter all the book details and again call the storeBooks method from object1 and add this 2nd object to the arraylist.In this way I add all the objects. I hope its the right way as I couldn't think of any other way.
    The problem I'm facing is that when I return this list from that getBook method to assign it to another list in Lender class, its not happening.
    Well I hope its become little more clearer now.

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    You're going to need to post your code if we're to have any chance of figuring out what you're doing wrong. Remember to use CODE tags and indent properly.

    -Gary-

Similar Threads

  1. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  2. Error showing "Object Required"
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2009, 03:23 PM
  3. get the details from GUI screen into an java object.
    By venkatesh09 in forum AWT / Swing
    Replies: 3
    Last Post: 07-28-2009, 01:46 PM
  4. Class Reflection: Showing methods
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:11 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •