• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to model person and company?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the OO, Patterns, UML and Refactoring forum...
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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???
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How should the uml be drawn to show company and companyCollection?
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by titan sim:
How should the uml be drawn to show company and companyCollection?


CompanyCollection has companies
CompanyCollection <>---------------------- Company
(symbol for aggregation)
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
She'll be back. I'm just gonna wait here. With this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic