• 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

Help needed on best structure to use.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Lockett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK thanks fred

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

John
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic