The moose likes Beginning Java and the fly likes Help needed on best structure to use. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help needed on best structure to use." Watch "Help needed on best structure to use." New topic
Author

Help needed on best structure to use.

John Lockett
Greenhorn

Joined: Nov 10, 2010
Posts: 3
Hi all

I'm a complete Java newbie coming from Python.

I would like advice please on a suitable structure to model the following situation.

A 'Box' contains an unlimited number of named 'Envelopes'.
Each 'Envelope' contains an unlimited number of 'RecordCards.
Each 'RecordCard' has a limited number (5) of DataItems.

Requirements.
A Box will be able to add and remove Envelopes.
An Envelope will be able to add and remove RecordCards
RecordCards will be able to change data held in the data items.
Data can be accessed using [Evelope][RecordCard][DataItems]
The Box can be serialized for persistence.

This is easily done in Python using Lists (which can hold mixed types) and the pickle methods for serialization but I would like to do it in Java for easy cross-platform distribution.

Any guidance would be very welcome

Thanks in advance

John
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

I don't understand why you would want a list to hold different things. A box only holds one thing - envelopes.

Envelopes only hold one thing - recordCards.

So, i would define a class called Box that contains a collection of envelopes. Then do the same for envelopes and recordCards, although you'd have to put in a check in your 'add' method to verify the size was under 5.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John Lockett
Greenhorn

Joined: Nov 10, 2010
Posts: 3
Thanks for the reply.

As to why I might want a list to hold different things, this is why I emphasised that I was coming from Python. Imagine that a user wants to add a new 'envelope' called "Advanced Brain Surgery Course Students" and then add Student Record Cards to the new 'envelope' in Python a new list could be created which had as its first element a string "Advanced Brain Surgery Course Students" and subsequent elements could be lists containing 'RecordCards' which are themselves lists containing the data items as their elements. These data items could also be different types - strings, integers etc. So to access individual DataItems one could just use something like : required_data_item = Box[Envelope_index] [RecordCard index] [DataItem index]

This makes getting the RecordCards for processing very easy as they are in the same list as the string which identifies the list.

Now as it seems that collections in Java cannot hold different types I am really asking what kind of objects would be best to use for the classes, ArrayLists, HashMaps or what, and an indication to how the data items could be accessed.

Sorry if I seem particularly stupid

John
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

There is an expression "Code to an interface, not an implementation". What that means is you design your box to hold 'envelopes' - which is an interface, not a class.

You then creates a class called "studentEnvelopes" that implement the Envelope interface. You can now put these in your box because they are envelopes.

When you then create your "Advanced Brain Surgery Course Students" class, you again have it implement the Envelope interface. You can now put them into your box.

You treat everything in your box as an Envelope, even though the specific class may be anything - as long as it implements the Envelope interface.
John Lockett
Greenhorn

Joined: Nov 10, 2010
Posts: 3
OK thanks fred

I'll just go and sit in a darkened room while I try to get my head round that

John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed on best structure to use.