Solved

OOP approaches

Posted on 2003-11-26
15
488 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 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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: 9830072
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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: …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

696 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