Solved

OOP approaches

Posted on 2003-11-26
15
485 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
  • 9
  • 5
15 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 40 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 360 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: 9830058
0
 
LVL 92

Expert Comment

by:objects
ID: 9830072
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Yen format 3 33
nextBoolean(double p) for Random class 3 40
servlet example 17 29
Java: anonymous class 4 17
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

776 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