• 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

Splitting an Enum

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to split an Enum into smaller Enums? For example: I have an Enum for all currencies of some countries (i.e. dollar, cents for US, pounds, shilling, pence for UK, etc.). Is it possible for me to split up the Enum into smaller Enums based on country? What I have now is:


The problem with this approach is that every time I want to add another country I need to rewrite a enum constructor that takes a Currencies as a parameter. What I want to do is change Currencies class to an enum, yet have the currencies from different countries in their own mini enum. Is this possible?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • 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 pass "new Currencies()" as a parameter to the constructor of the enum. Why not just like this:



However having said that, I'm not sure that it helps in answering your question. (In fact I'm not quite sure what your question is, to be honest.)
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact I don't even see the need for the enum values to have a reference to a Currencies object, since as far as I can see Currencies doesn't have any behaviour at all. Perhaps there's some confusion about how enums work?
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Currencies has behavior I just omitted that behavior because it is not relevant. In fact Currencies is very complicated and that is why I omitted it. I'll try to explain my problem a bit better. I have an enum of currencies, I want them split up according to country. My first approach was to create separate enums for all countries, this was problematic because I needed to redefine the whole class for each country. So I created a class that has all the complicated logic in it (Currencies) and enums for each country, and each object of enum HAS-A Currencies, thus allowing me to access all the logic of Currencies in each enum. This approach just feels wrong, First of all the enum does not have a HAS-A relationship with Currencies, rather it has a IS-A relationship, second it messes up the code, and third it still requires a separate constructor for each enum, it is just a simpler constructor.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Honestly, it all sounds like a Rube Goldberg design. Sounds to me like your "solution" has itself become a problem and you're losing sight of the real problem. What's the larger problem that you are trying to solve anyway? How do you intend to use these enums and currencies? Just how "complicated" is that currency logic?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then if each enum element is supposed to have a different behaviour, why don't you just put that behaviour in methods which belong to the individual elements?

Sort of like this:



(Sorry, I had to change the name of the enum to "Currency" because it seemed to me like each enum element was going to be a currency. Having to call it "Currencies" was just messing with my head too much.)


 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Then if each enum element is supposed to have a different behaviour, why don't you just put that behaviour in methods which belong to the individual elements?



No, each object of the Currency class is supposed to have the same behavior just different values (again I have omitted most of the Currency class). The reason I want to split the enum is that when I iterate over Currency.values() I get just the applicable Currency Objects.

Paul Clapham wrote:(Sorry, I had to change the name of the enum to "Currency" because it seemed to me like each enum element was going to be a currency. Having to call it "Currencies" was just messing with my head too much.)



You are right I have refactored my class.
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I have found a simple solution.

Thanks all for your help.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me in your efforts to simplify the problem so that it can be posted here without overwhelming us (which is a good thing), you've gone too far. For example you've said that Currency is a complicated class, but your simplified version gives us no idea of why the complications affect your design.

And I still don't understand what you mean by "splitting" the enums. I can see in your example that you can refer to Currency.US.CENT, for example, so what would it look like after splitting?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or alternatively, as I suggested earlier:


 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul, your solution is better than mine.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this pre-decimalisation?
Haven't used shillings in over 40 years...
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems like a situation where the choice of names is misleading the design thinking. In this case, I would first list out the different terms that are used in the problem domain, which I would bet is about denomination equivalence and/or conversion. Terms would include currency, base unit, coins or coinage, bills or banknotes, and denominations. For example, in the US, coin denominations are the penny, nickle, dime, quarter, half dollar, and dollar. The base unit for US currency is the dollar. The base unit of currency in Singapore is also a dollar but with different coin denominations: 5, 10, 20, 50 cents, and $1.

With this in mind, I would then start trying to solve the larger problem at hand, which is still not quite clear to me, rather than trying to model the currency and denominations as enums up front. This will probably give me a better sense of how I would like to use currencies and denominations in the problem domain.

So again, how exactly are you planning to use these classes and enums? What is the context here?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic