aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes how to model person and company? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "how to model person and company?" Watch "how to model person and company?" New topic
Author

how to model person and company?

titan sim
Greenhorn

Joined: Oct 06, 2003
Posts: 9
hi, I'm from structural programming background and is hoping to pick up OO concept. I've read up some OO books, however, when I try to model real life example using the OO concept, i find it very hard to think OO. Maybe the expert here can guide me.
Basically, I want to model a person and company (he works for). Thus, I've the Person and Company class. A person can work for many company and a company can have many employees (Person object). How can I actually model this type of real world example?
I've try to do some coding, however, is totally stuck here when I want to do further coding (ie how to get the person's company, and how to get the company's employee).
Hope the expert here can give me pointers. And if there is any url where I can learn about this type of real world example, pls let me know. thanks.
public class Person{
private String name;
private Company company;
//... other person attributes
}
public class Company{
private String name;
//... other company attributes
}
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by titan sim:
Basically, I want to model a person and company (he works for). Thus, I've the Person and Company class. A person can work for many company and a company can have many employees (Person object). How can I actually model this type of real world example?

Depends on what you mean by "modeling". Depicting it in a UML diagram is quite straightforward:


I've try to do some coding, however, is totally stuck here when I want to do further coding (ie how to get the person's company, and how to get the company's employee).

How do you *want* to get the company/employee? The responsibility of your classes is *not* to model the real world, but to provide the behaviour you need in your program. So you need to *first* think what you want your classes *to do*, then what they should look like. (Actually the easiest way is to go forth and back in very tiny increments.)
So, can you show us some of your existing code - especially the client code?
And if there is any url where I can learn about this type of real world example, pls let me know. thanks.

Not an URL, but a book: http://www.javaranch.com/bunkhouse/Process.jsp#0135974445


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
As in the relational world, a logical many-to-many relationship takes some thing (table or object) to represent the relationship in the concrete design.
Party 1-----* Relationship *-----1 Company
Relationships might say I'm president of one company, janitor at another. To dive into the deep end of some pretty advanced modeling, look at the Object Management Group (OMG) models for Party Management. My company (relationship=flunky) uses this model in places, and the next major revision to my project will be moving very close to it, so I need to study up myself!
The OO, Patterns, UML and Refactoring forum here at the ranch talks about this kind of stuff a length. Join in for a good time!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the OO, Patterns, UML and Refactoring forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Chetan Mishra
Greenhorn

Joined: Sep 09, 2003
Posts: 8
Hi Titan,


Basically, I want to model a person and company (he works for). Thus, I've the Person and Company class. A person can work for many company and a company can have many employees (Person object). How can I actually model this type of real world example?
I've try to do some coding, however, is totally stuck here when I want to do further coding (ie how to get the person's company, and how to get the company's employee).


Just think on these lines.
You have person with his details, you have company with its details.
A person when he enters a company, he is an employee.
So the realtion goes something like this.
Person </t> Company
^ ^
| is a |
| |
Employee |
| |
| ------------------------ |
works for
So person n employee --> 'is a' (1 - 1)relation --> inheritance -->
company and employee --> 'has a' (1 - many)relation --> association (aggregation to be precise).
As long as company exists its employee exists and as long a person exists employee can exist.
Hope it helps. Let me know if this helped you.
Regards,
Chetan M
" Pick battles big enough to matter, small enough to win "
[ October 23, 2003: Message edited by: Chetan Mishra ]
titan sim
Greenhorn

Joined: Oct 06, 2003
Posts: 9
Originally posted by Ilja Preuss:


Not an URL, but a book: http://www.javaranch.com/bunkhouse/Process.jsp#0135974445


As an employee can holds different at different time (promotion etc). So can I draw the uml in this manner.
+---------+ 1 * +-----------+* 1 +--------+
| Company |------| Employment|-----| Person |
+---------+ +-----------+ +--------+
I've no code yet... However, the behaviour I'm expecting should be something like to get the company a person working for, get the employees of a company.

class Comapny{
...
private String name;
private String address;
...
Company comp = new Company()
comp.getEmployee() //in simplest meaning, this is to get all employee
public String getName(int CompID){...}
public String getAddress(int CompID){...}
...
}

class Person{
...
Person per = new Person();
per.getCompany(); // get the company this person is working for
...
}
the above are skeleton code I can think of. Do they make sense.
I'm not sure how to phase this, but I want to be able to seach a company by its name, and company name may be the same for more than 1 company.
eg,
comp.Search(name) //this will return all the company with the name specified (can be part of a company name, eg search for "Micro" will returns Microsoft, Micropolis, Micro...)
On the client side, I want to display things like
Company Name, Address, Business Type etc
How do I return a company object, which contains all these information. How will the code look like?
thanks
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
It looks as if you might be confusing the idea of a collection of companies, with the idea of a single company.
Consider the following simplified example:

In this example, a Company object represents a single company. it has no need to know that any other Company objects exist. If you want to be able to search for companies by name, you probably need some sort of collection of companies:

Notice that these two classes are broadly similar in structure (a company has a list of employees, a company collection has a list of companies), but different in the detail of their behaviour.
Has this helped, at all?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
Originally posted by Frank Carver:
It looks as if you might be confusing the idea of a collection of companies, with the idea of a single company.
Consider the following simplified example:

In this example, a Company object represents a single company. it has no need to know that any other Company objects exist. If you want to be able to search for companies by name, you probably need some sort of collection of companies:

Notice that these two classes are broadly similar in structure (a company has a list of employees, a company collection has a list of companies), but different in the detail of their behaviour.
Has this helped, at all?

One can also use a set as collection of companies that way you can ensure that all the entries in you CompanyCollection are unique.Or even better option is to use a TreeSet (if you are using java) that way you can also ensure sorting of entries for display.
How abt CompanyCollection extends TreeSet???


Help me!Help you!!!
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
Originally posted by Frank Carver:
It looks as if you might be confusing the idea of a collection of companies, with the idea of a single company.
Consider the following simplified example:

In this example, a Company object represents a single company. it has no need to know that any other Company objects exist. If you want to be able to search for companies by name, you probably need some sort of collection of companies:

Notice that these two classes are broadly similar in structure (a company has a list of employees, a company collection has a list of companies), but different in the detail of their behaviour.
Has this helped, at all?

This is just to double check my understanding a person/employee will have a reference to his CompanyCollection right ???
titan sim
Greenhorn

Joined: Oct 06, 2003
Posts: 9
Originally posted by Frank Carver:
It looks as if you might be confusing the idea of a collection of companies, with the idea of a single company.
Consider the following simplified example:

In this example, a Company object represents a single company. it has no need to know that any other Company objects exist. If you want to be able to search for companies by name, you probably need some sort of collection of companies:

Notice that these two classes are broadly similar in structure (a company has a list of employees, a company collection has a list of companies), but different in the detail of their behaviour.
Has this helped, at all?



A great thanks to you, you've help me see the differences b/w collections and single object. Is really useful for java newbie like myself.
some more questions so the client side will be something like this rite...
...
Company comp = new Company();
comp.setName("Sun");
comp.setAddress("...");
CompanyCollection compCol = new CompanyCollection();
compCol.addCompany(comp);
...
If the company is retrieved from database, should it look something like that
titan sim
Greenhorn

Joined: Oct 06, 2003
Posts: 9
How should the uml be drawn to show company and companyCollection?
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
Originally posted by titan sim:
How should the uml be drawn to show company and companyCollection?

CompanyCollection has companies
CompanyCollection <>---------------------- Company
(symbol for aggregation)
Roger Zacharias
Ranch Hand

Joined: Jul 01, 2003
Posts: 49
Hi!
I have a question on which I am very uncertain:
Imagine you have two classes, e.g. Customer and CreditCard and the Customer can only have one CreditCard. So you have an 1:1 association "owns" between Customer and CreditCard and navigability in both directions.
I am uncertain if I have to show the customerID as an attribute to CreditCard or if this is done implicit???
Or must this be modelled with aggregation???
What do you think?
Roger


SCEA
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
Originally posted by Roger Zacharias:
Hi!
I have a question on which I am very uncertain:
Imagine you have two classes, e.g. Customer and CreditCard and the Customer can only have one CreditCard. So you have an 1:1 association "owns" between Customer and CreditCard and navigability in both directions.
I am uncertain if I have to show the customerID as an attribute to CreditCard or if this is done implicit???
Or must this be modelled with aggregation???
What do you think?
Roger

I would not model my credit card with the customerID as an attribute.I would rather have a getCustomer() method in the credit card that would return maybe a Customer (but ideally a CustomerInfo) object.The Credit card may be associated with a customer during creation.
new CreditCard(String cardNumber,Customer owner) you can therefore have a two way association with methods getCustomer and getCreditCard modelled in the Credit Card Class and the Customer Class respectively.
Hope that helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to model person and company?