Urgent help required
Hi all,
I have an assignment due next week. I don't realy know simple two way association which this program requires. Here is the requirement.
This exercise is about simple two-way associations. An example of a simple two-way association is the ownership relation between a Person and a Car. To ensure consistency, the following three rules should be obeyed.
Each car is owned by exactly one person, that is, no car is without an owner, and no car is owned by more than one person.
Each Person can own at most one car.
If a person owns a car, that car is owned by that person, and vice versa.
It is common for simple two-way associations that one side X must be associated to the other side Y, while Y might be associated to an X.
Write the two classes Person and Car, such that each has a public (declaring it private will make the exercise much harder) reference field that can refer to one of the other kind, that is, class Person as a field Car car, and class Car has a field Person owner. Hint. Use the constructor of Car to make sure that a car is never created without a owner, this should take care of rule 1 & 2 above.
Add a method void newOwner(Person p) to the car class. It should make p the new owner of this car. Make sure rule 1, 2 & 3 are respected by the method. You may assume that the person p is not the owner of an other car.
I have no clue how to implement this. Could you guys give me some Idea how to design these two classes according to the requirements.
I appreciate your help.
Looking forward to your reply.
Koonda

