?
Solved

OOP approaches

Posted on 2003-11-26
15
Medium Priority
?
490 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
15 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 160 total points
ID: 9828948
>>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
 
LVL 92

Expert Comment

by:objects
ID: 9829198
> 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
 

Author Comment

by:dkim18
ID: 9829682
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 9829716
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
 
LVL 92

Expert Comment

by:objects
ID: 9829727
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
 

Author Comment

by:dkim18
ID: 9829806
How do I access book's public method from other class(LibraryApplet class)?
0
 
LVL 92

Expert Comment

by:objects
ID: 9829836
eg. if you have a book variable b, you would do:

String title = b.getTitle();
0
 

Author Comment

by:dkim18
ID: 9829881
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
 
LVL 92

Expert Comment

by:objects
ID: 9829893
> 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
 
LVL 92

Expert Comment

by:objects
ID: 9829901
I imaging in your application you would be accessing Book objects from the Library.
0
 

Author Comment

by:dkim18
ID: 9829956
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
 
LVL 92

Accepted Solution

by:
objects earned 1440 total points
ID: 9829981
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
 

Author Comment

by:dkim18
ID: 9830026
That was what I wanted to know. I still don't have clear concepts about package.
Thank you for your continuning answer.
0
 
LVL 92

Expert Comment

by:objects
ID: 9830072
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question