Solved

OOP approaches

Posted on 2003-11-26
15
482 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now