• Post Reply Bookmark Topic Watch Topic
  • New Topic

maps of classes of enums  RSS feed

 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seasons' Greetings to all,

I'm building a class to generate reports. The report's format will be different based on which state requests the report.
To this end my class has several enums which define the structure of the output line for a specific state.

I'd like to have a mapping between stateCodes and their associated enums. Then the report-writing method accepts a stateCode and a row of data, uses the statecode to lookup which enum to use, apply that enum's formatting to the row of data, and return a string.

Here's a really stripped-down version:


As usual, any ideas, suggestiong, pointers, examples and positive criticism are always welcome!

TIA,

Still-learning Steve
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why those classes are enums. Why not just provide an abstract class and have each state override it appropriately?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that your Layout_AZ and Layout_TX are exactly the same except for two integer values suggests that there is something not quite right about this approach. Also, unless you're designing framework, resorting to reflection is usually a huge clue that one is looking at a problem the wrong way.

You don't need enums. You need an abstract Layout class that has concrete implementations for AZ and TX.
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul: jinx!
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your suggestions! I got a little further with my approach with


but will try it your way as well. I went with enums because they are inherently static.

Thanks again for your replies!

Happy New Year,

Still-learning Steve
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart Rogers wrote:I went with enums because they are inherently static.


That really isn't a good reason for choosing enums. And looking for something which is static isn't usually a good idea, either.
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and besides, no matter how you slice it, something's going to have map the state being used to the associated enum/ extended class
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I got a little further
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What now? You need some methods to override. Let me suggest "formatStateCode" and "formatDistrictCode" to start with? Of course I'm just guessing because your original code didn't actually do anything.

Also let me suggest this for your startup code, to reduce the need for reflection elsewhere in your code:
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah, clever. I'll tinker with this new approach over the next few days.

Thanks and Happy New Year!


Still-learning Steve
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hacked at this a bit more, stripped out all the enum stuff, to come up with


This looks syntactically correct but doesn't give me what I want:

In Eclipse I can get as far as RptXXX.Layoutt.lookupByState("AZ"). but it can't see Layoutt_AZ's buildOutputRecord method., probably because the map sets its objects to Layoutt and loses further class data

Any more suggestions/ideas/hints/examples out there to finish this off?

TIA,

Still-learning Steve

 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's the piece I was missing


I can test it once I've got some data in my tables to run the reports against. Will post my results.


Thanks to all,

Still-learning Steve
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works great:


so now in my application I can call forth:


Nice!

Thanks to all who responded!

CASE CLOSED

Still-learning Steve
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!