12 Replies - 888 Views - Last Post: 07 May 2013 - 05:34 PM Rate Topic: -----

#1 la_faker_81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-May 11

One to many relationships in Java

Posted 04 May 2013 - 05:07 AM

I've recently received an assignment that introduces a concept that I'm struggling with in Java. Assume we have customer objects and also houses. A customer can have 0 or many houses. Maintenance is performed on houses.


So I figured ok, I will have a customer class, a house class, and a maintenance class (there are different types of maintenance and I will deal with this using inheritance).

Customer class will take an id, name and age.
House class will take a street address.
Maintenance class will take an id, date, description and cost.

I'm just wondering how to implement the functionality of one customer having many houses in the OO paradigm. Ideally if I delete a customer from the system, it should also delete all the records of their houses.

What sort of data structure is best for this problem? 1/2D array? Linked list?

Any tips would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: One to many relationships in Java

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: One to many relationships in Java

Posted 04 May 2013 - 05:15 AM

The Customer class could have an ArrayList<House> field to contain a House object for each customer's houses. Whether that is the best data structure is dependent on what you'd like to do with it or the House objects in it, but the ArrayList<> is capable of doing everything you've described so far and more.
Was This Post Helpful? 1
  • +
  • -

#3 la_faker_81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-May 11

Re: One to many relationships in Java

Posted 04 May 2013 - 05:31 AM

View PostGregBrannon, on 04 May 2013 - 05:15 AM, said:

The Customer class could have an ArrayList<House> field to contain a House object for each customer's houses. Whether that is the best data structure is dependent on what you'd like to do with it or the House objects in it, but the ArrayList<> is capable of doing everything you've described so far and more.


Thanks for the quick reply. We haven't covered ArrayList in our course yet. Pretty sure I'm expected to use either:

a) Array of objects (1 or 2D)
B)/> Linked List

The houses will have some form of maintenance performed on them when the user specifies. There's minor and major maintenance (to keep it simple). So I have a maintenance superclass and then two subclasses underneath.

So just say I create a new customer object. And then in the menu (this is a console program) the user wishes to add a house for a customer. How would I design the customer class? It's just the whole idea of having more than one house that's confusing.

This post has been edited by la_faker_81: 04 May 2013 - 05:32 AM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: One to many relationships in Java

Posted 04 May 2013 - 06:13 AM

Between those two I'd recommend the linked list, especially if you have some experience with the data structure. Perhaps at this point you've written your own version of the linked list and built the methods that make them easy to use so that you're familiar and comfortable with the many things that can be done with a linked list. If you haven't, this is the opportunity.

Using parallel arrays, 1D or 2D, may seem easier because of your comfort level with them, but getting away from using parallel arrays to organize and retrieve data is an important step to take and best taken as early as possible.
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,682
  • Joined: 19-March 11

Re: One to many relationships in Java

Posted 04 May 2013 - 06:27 AM

Of the choices offered, I'd suggest LinkedList would be the best. For a collection of values whose extent is not known, a list is your first stop. It might be interesting for you to try implementing this with an array, to get an idea of the problems this presents, but in the end using an array for this will more or less require that you duplicate significant parts of the ArrayList functionality. Not a bad thing to do, if you have the time, but probably not part of this assignment.

View PostGregBrannon, on 04 May 2013 - 08:13 AM, said:

Using parallel arrays, 1D or 2D, may seem easier because of your comfort level with them, but getting away from using parallel arrays to organize and retrieve data is an important step to take and best taken as early as possible.


Why parallel? The OP is talking about an array of Houses as a field of the Customer class....
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: One to many relationships in Java

Posted 04 May 2013 - 06:29 AM

View Postla_faker_81, on 04 May 2013 - 08:07 AM, said:

Ideally if I delete a customer from the system, it should also delete all the records of their houses.

If the houses are help in a LinkedList<House> that belongs to a Customer object this is what would happen if you delete a Customer.
I am not sure you want that thought. In the real life, when a person passes away, its houses do not vanish/disappear. You migt need a HouseRepository helding all houses with the name of their owner or not.
Was This Post Helpful? 1
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: One to many relationships in Java

Posted 04 May 2013 - 06:34 AM

Quote

Why parallel? The OP is talking about an array of Houses as a field of the Customer class....

I extrapolated to where I think it's going . . . I could be wrong, often am.
Was This Post Helpful? 1
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,682
  • Joined: 19-March 11

Re: One to many relationships in Java

Posted 04 May 2013 - 06:36 AM

View Postla_faker_81, on 04 May 2013 - 07:31 AM, said:

So just say I create a new customer object. And then in the menu (this is a console program) the user wishes to add a house for a customer. How would I design the customer class? It's just the whole idea of having more than one house that's confusing.


When you ask yourself "how would I design X?", the place to start is by figuring out "what is an X?" In java, that means figuring out what data you're organizing and what you want to do with it - what does the class know, and what can it do?

If you have trouble here, it might be that you don't understand your program as a whole. Back up and think about how the user is going to interact with this and what data they care about. This will spell out functionality, and class design will start to become obvious.
Was This Post Helpful? 1
  • +
  • -

#9 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: One to many relationships in Java

Posted 04 May 2013 - 06:54 AM

View Postpbl, on 04 May 2013 - 06:29 AM, said:

View Postla_faker_81, on 04 May 2013 - 08:07 AM, said:

Ideally if I delete a customer from the system, it should also delete all the records of their houses.

In the real life, when a person passes away, its houses do not vanish/disappear.


When a customer passes away -- or is otherwise relinquished of their ownership of the house -- the house returns to the market. Just like you would do cleanup on, say, a game before you terminate it, so to must you perform cleanup on the house ownership in this scenario.

Have a separate method that removes a customer, AND puts all of their houses back on the market (which would, as pbl recommended, require an extra collection structure of houses (or a class, since it would require methods) to be listed outside of the customer-house-maintenance structure. However, once a customer chooses to buy the aforementioned house, you take it off the market, and copy it over to their collection structure of currently owned houses to be maintained (if that is the kind of functionality that you have planned, which would make a lot of sense if customers were able to own multiple houses, as suggested by your question).
Was This Post Helpful? 1
  • +
  • -

#10 la_faker_81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-May 11

Re: One to many relationships in Java

Posted 07 May 2013 - 03:35 AM

OP here,

I don't have to implement the functionality of deleting houses (isn't part of the assignment anymore).

I've done some work and come up with the following (partial code, not the whole class):

public class Client
{
   private int id;
   private String name;
   private House[] houses;
   private int houseCount;

   public Client(int id, String name)
   {
      this.id = id;
      this.name = name;
      houses = new House[10];
      houseCount = 0;
   }



In my driver program I created an array of type Client, and can successfully add houses for my client. So I've solved that part :)

Now comes the next tricky bit (as I briefly mention in my original post). Maintenance is performed on houses. There are two types of maintenance (minor and major). I have created a Maintenance superclass and then Minor and Major subclasses.

How do I link the clients houses to maintenance. Where would I create the array? How would I link a specific house (of a particular customer for example) to a specific instance of maintenance into an array data structure. If someone could point me in the right direction with an example relating to this problem, I think I can tackle the rest of my assignment on my own.

Again, thank you all for the help so far (especially with the house[] array as an instance variable for client - after some reading I think this is called composition).
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: One to many relationships in Java

Posted 07 May 2013 - 03:38 AM

You simply need a Maintenance object in your House class that will hold a Minor or Major object
Was This Post Helpful? 1
  • +
  • -

#12 la_faker_81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-May 11

Re: One to many relationships in Java

Posted 07 May 2013 - 04:00 AM

View Postpbl, on 07 May 2013 - 03:38 AM, said:

You simply need a Maintenance object in your House class that will hold a Minor or Major object


My House class so far:

public class House
{
	private String registration; // just a unique identifier
	
	public House(String registration)
	{
		this.registration = registration;
	}
	
	public String getRegistration()
	{
		return registration;
	}
}



Are you saying that I do something similar to how Clients have a collection of houses...i.e. houses have a collection of maintenance records?

public class House
{
	private String registration;
	private Maintenance[] serviceRecords;
	
	public House(String registration)
	{
		this.registration = registration;
		serviceRecords = new House[50];
	}



Do I then take advantage of polymorphic behavior (i.e. a minor service is an instance of Maintenance, and a major service is an instance of Maintenance)?
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: One to many relationships in Java

Posted 07 May 2013 - 05:34 PM

View Postla_faker_81, on 07 May 2013 - 07:00 AM, said:

[Do I then take advantage of polymorphic behavior (i.e. a minor service is an instance of Maintenance, and a major service is an instance of Maintenance)?

Yes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1