OOP approaches

I am working on project call “LibraryApplet” and its description is the followinig:
when an employee borrows a book, they create a log entry with their NAME, BOOK TITLE and CHECK-OUT DATE. When they return the book to the shelf they cross out the log entry they had created previously. I am trying to create user interface to a Java Swing UI and I want to run that UI inside an applet.

I am thinking Book class, employee class and Library class and I need to use maps to allow me to associate a title (key) with a book object (value) or an employee name with an employee object because book titles are unique!

My questions are:
A Library contains ("HAS-A") many books and employees.
How do I accomplish "HAS-A" relationship? Can I create book object in Library.java file?

How do I create book obj for each line of books.txt?
Book objects get created after reading a line of books.txt and creating a book with that new information. A book contains title and author data so I need to provide public accessor methods (get....) to retrieve that private data.

This is books.txt file:
Core Web Programming|Hall
Core Java 2|Horstmann
Java & XML|McLaughlin
Enterprise JavaBeans|Monson-Haeffel
Inside Java 2 Security|Gong
Core Servlets and JavaServer Pages|Hall
JDBC API Tutorial|White
Introduction to Algorithms|Cormen,Leiserson,Rivest
Java Development with Ant|Hatcher
Goedel, Escher, Bach|Hofstadter
Bitter Java|Tate
Java Threads|Oaks
Effective Java|Bloch

This is my code:
while ( (line = inFile.readLine()) != null) {
        splitLocation = line.indexOf('|');
        book = line.substring(0, splitLocation);
        author = line.substring(++splitLocation);
        line = book + " by " + author;
      //How does Book object associate with book information???
        //Book obj = new Book();

        bhm.put(book, obj);
       //for testing
        System.out.println("key: " + book + "  value: " + bhm.get(book) ) ;
}

thanks.
dkim18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>How do I create book obj for each line of books.txt?

Your code is virtually complete. Just create a Book object

//How does Book object associate with book information???

// answer - like this:

Book book = new Book();
book.setTitle(title); // you should change your parsed variable from 'book' to 'title'
book.setAuthor(author);
0
Mick BarryJava DeveloperCommented:
> How do I accomplish "HAS-A" relationship?

Use a Collection class. In your case the Library class would contain a Collection Book's and a Collection of Employee's

> Can I create book object in Library.java file?

// create book

Book book = new Book(title, author);

// add to book collection

Books.add(book);

> How do I create book obj for each line of books.txt?

while ( (line = inFile.readLine()) != null) {
   StringTokenizer st = new StringTokenizer(line, "|");
   String title = st.nextToken();
   String author = st.nextToken();
   Book book = new Book(title, author);
   Books.add(book);
0
dkim18Author Commented:
My question was if I have three java files; LibraryApplet class, Book class and Employee class in same directory, can I create Book object in LibraryApplet class, so I can access data in Book class by using Book's accessors method. Since Library "HAS-A" books and employees, how shoulc I related those classes? By using package? (I am thinking something like "#include "statement in C++.)
Am I thinking too narrow?


Books.add(book);
>>Where "Books" came from?

0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Mick BarryJava DeveloperCommented:
If the accessors are public they can be accessed from any other class.

>>Where "Books" came from?

I made it up to show how you could store all the books that belong to a library.
This is how you would implement the relationship between library and books.
What package you put it in is not really related, but you could keep library and book classes in same package.

0
Mick BarryJava DeveloperCommented:
Another option would be to store the relationship in a higher level class.
So the Library and Book class's would just store attribute data about these entities.
And you would create another class to model the relationships involved, eg. LibraryBooks and LibraryEmployees.
0
dkim18Author Commented:
How do I access book's public method from other class(LibraryApplet class)?
0
Mick BarryJava DeveloperCommented:
eg. if you have a book variable b, you would do:

String title = b.getTitle();
0
dkim18Author Commented:
What do you mean by "book variable" ?
Not Book object? How do I create book variable?
I know this is a dumb question...but this is my first java UI project.
0
Mick BarryJava DeveloperCommented:
> What do you mean by "book variable" ?

I mean a variable of class Book.

> Not Book object?

The variable will reference a Book object.

> How do I create book variable?

Book b;

http://java.sun.com/docs/books/tutorial/getStarted/application/objects.html
0
Mick BarryJava DeveloperCommented:
I imaging in your application you would be accessing Book objects from the Library.
0
dkim18Author Commented:
I got error on

Book b;

The compile error that says "src\dkim18\library\LibraryApplet.java: cannot access dkim18.library.Book
.....
file does not contain class dkim18.library.Book


I imaging in your application you would be accessing Book objects from the Library.
>>Correct.
0
Mick BarryJava DeveloperCommented:
Sounds like you have no package statement in you ZBook class. If its in a package you need to add a package statement to the top of the file:

package dkim18.library;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dkim18Author Commented:
That was what I wanted to know. I still don't have clear concepts about package.
Thank you for your continuning answer.
0
Mick BarryJava DeveloperCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.