• Post Reply Bookmark Topic Watch Topic
  • New Topic

5 Valued Variable  RSS feed

 
Richard Mittleman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As booleans can have 2 values, true & false, my program needs a new type of variable that has 5 states. The criteria for this concept must be:

1. Easy to understand.

2. short.

3. Hard to make a mistake.

I have come up with the following:

Define an Interface



include the interface in the program, where multiValue is the package containing the interface


The program can then be written:


Does anyone have any comments on how to make the concept better according to the 3 criteria?

Thank you
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the interface? enum constitutes a type.
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you should lose the interface and use the enum directly.
 
Richard Mittleman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interface is used to have a standard line of code that can always be inserted in a program rather than remembering how the enum is defined.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you need to remember how the interface is defined and the enum within the interface instead?

Collecting several enums within a class may be viable, but use a class then and not an interface.
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Mittleman wrote:The interface is used to have a standard line of code that can always be inserted in a program rather than remembering how the enum is defined.


I don't understand that. What "standard line of code" would you be inserting into a program?
 
Richard Mittleman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


would be inserted in any program that wants to use the "ExpandedBoolean".
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the enum is in a package then you can import-static it directly. You don't need it to be in an interface. Thus:

instead of

If it isn't in a package then... well, it should be in a package. Only the most trivial things don't need to be in a package, and we aren't talking about trivialities here.
 
Seb Xu
Greenhorn
Posts: 2
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not create a class to deal with it?
e.g.
Class ExpandedBoolean{
public enum Type {NO, RARE, MAYBE, OFTEN, YES}

methods to deal with the logic like below,

expandedBoolean doesThisHappen = expandedBoolean.NO;
if (doesThisHappen == expandedBoolean.NO)
System.out.println("Should be NO = " + doesThisHappen);

doesThisHappen = expandedBoolean.RARE;
if (doesThisHappen == expandedBoolean.RARE)
System.out.println("Should be RARE = " + doesThisHappen);

doesThisHappen = expandedBoolean.MAYBE;
if (doesThisHappen == expandedBoolean.MAYBE)
System.out.println("Should be MAYBE = " + doesThisHappen);

doesThisHappen = expandedBoolean.OFTEN;
if (doesThisHappen == expandedBoolean.OFTEN)
System.out.println("Should be OFTEN = " + doesThisHappen);

doesThisHappen = expandedBoolean.YES;
if (doesThisHappen == expandedBoolean.YES)
System.out.println("Should be YES = " + doesThisHappen);

}
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have problems with the name ExpandedBoolean. It makes me expect a value FileNotFound. I'd go for the following:
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: It makes me expect a value FileNotFound.


I was thinking the exact same thing -- you beat me to the punch. TRUE, FALSE, and FILE_NOT_FOUND.
 
Richard Mittleman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone,

I used the import of the enum rather than the interface.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!