• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create a class which can manage a collection of objects  RSS feed

 
Vincent Broeke
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to build a class that will represent a sort of library that can manage a collection of books. The
library has opening hours, an address and a book collection (i.e., arrays of Book objects). Now i am quite new to progamming and am trying some stuff out. But not really getting in the right way i think.

To make this i need to have to implement following methods:
addBook(new Book(title)));//this should add a book to an array, also having a boolean or something else i could use in a for loop that will eventually print out available books
borrowBook(title);//giving the title this will set a boolean borrowed or some value to be true, really needs to correspond to the title though
printAvailableBooks();//print all the books that arent borrowed, coming from the array with the book objects
returnBook(title);//giving the title this will set a boolean borrowed or some value to be false, really needs to correspond to the title though

If any of you could maybe give me some pointers getting on the way. What i have tried so far is not worth that much, but here it is:


Thanks for taking time to respond in advance.

Kind regards,

Vincent
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent Broeke wrote:I need to build a class that will represent a sort of library that can manage a collection of books. The
library has opening hours, an address and a book collection (i.e., arrays of Book objects). Now i am quite new to progamming and am trying some stuff out. But not really getting in the right way i think.

OK, well the first thing to do is to StopCoding (←click).

Sit down with a pencil and paper and think about exactly what a Library does. Does it, for example:
  • Keep only 1 copy of each book?
  • Keep a separate index card for each copy of a book?

  • I suspect you already know the answer to these, since your code already makes the distinction between a book and its title; and my suggestion would be to create a Book class that reflects this.

    Second: Trying to do this with arrays is likely to be very cumbersome. Have a look at the Map interface (java.util.Map) and, more specifically, the HashMap class, because you'll probably find that they are exactly what you want to store your Book inventory.

    Just to get you started: If I were doing this, my first thought for a "collection" variable (or the base for an Inventory class) would be a

    HashMap<Book, AtomicInteger>

    I'll leave you to work out why.

    HIH

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!