Reputation: 83
I want to pass a specific enum class to a method. Is this possible? The method would accept any enum. I am trying to create a function that will take a type of enum and make a menu out of it, returning the enum that was selected.
An example enum class
public enum METHODOFTRANSPORTATION {
CAR("car"), BUS("bus"), COMMUTTER_RAIL("commuter rail"),MAINMENU("GO TO MAIN MENU");
private String choice;
METHODOFTRANSPORTATION(String choice){
this.choice = choice;
}
public String getMethodOfTransportation(){
return choice;
}
}
My Example class implementing the enum
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Ticket{
private static HashMap<Integer, Ticket> hm= new HashMap<>();
private int ticketID = 0;
private METHODOFTRANSPORTATION methodOfTransportation;
Ticket(){
//Call method of Transportation to create ticket
//GETTING ERROR HERE!
this.methodOfTransportation = createMethodOfTransportation(METHODOFTRANSPORTATION);
}
Ticket(METHODOFTRANSPORTATION methodOfTransportation){
this.methodOfTransportation = methodOfTransportation;
}
public Enum<?> getMethodOfTransportation() {
return methodOfTransportation;
}
public Enum<?> createMenuChoice(Enum<?> e) {
System.out.println("**************************");
Scanner s = new Scanner(System.in);
int input = 0;
boolean intFlag = false;
Enum<?> mot;
//Validate user input for menu selection
do{
//Print our Enums allowed variables
for(int i=0; i< e.getDeclaringClass().getEnumConstants().length; i++){
System.out.printf("%d. %s",i,e.getDeclaringClass().getEnumConstants()[i]);
}
System.out.println("Please enter a valid menu entry (");
for(int i = 0; i< e.getDeclaringClass().getEnumConstants().length; i++){
if(i == e.getDeclaringClass().getEnumConstants().length){
System.out.println(i);
}
else{
System.out.print(i + ",");
}
}
System.out.print("> ");
if(s.hasNextInt()){
input = s.nextInt();
for(int i = 0; i< e.getDeclaringClass().getEnumConstants().length; i++){
if(input == i){
//*********************************************
//HOW DO I RETURN THE CORRESPONDING VALUE HERE?
//*******************************************
mot = ;
}
}
}
else{
s.next();
}
}
while(false);
//WHAT SHOULD I RETURN HERE??
return METHODOFTRANSPORTATION.CAR;
}
}
Is this even possible? I just want one method that will take any class of enum, and return the enum value. Is there a better way to do this??
Essentially my driver class would look like
public class Driver {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ticket t = new Ticket();
Enum<?> mot = t.getMethodOfTransportation();
if(mot == METHODOFTRANSPORTATION.CAR){
//....
}
//...
//OR
/*
* switch(mot){
* case(METHODOFTRANSPORTATION.CAR): ...
* ...
* ...
* }
*/
}
}
Upvotes: 1
Views: 2590
Reputation: 83
Thank you guys I have found a solution. @BoppreH THANKYOU!!
Ticket.java createMenuChoice method
//Using introspection
public Enum<?> createMenuChoice(Class<?> c) {
//Variables
int input = -1;
Enum<?> output = null;
Scanner s = new Scanner(System.in);
do{
//Print Menu
for(int i = 0; i < c.getEnumConstants().length; i++){
System.out.printf("%d. %s\n",i+1,c.getEnumConstants()[i].toString());
}
//Get Selection
System.out.println("Please enter a valid menu entry");
if(s.hasNextInt()){
input = s.nextInt();
for(int i = 0; i < c.getEnumConstants().length; i++){
if(input == i + 1){
return (Enum<?>) c.getEnumConstants()[i];
}
}
}
else{
s.next();
}
}while(true);
}
}
Driver.java I can call this using
MainMenu inputMm = (MainMenu) t.createMenuChoice(MainMenu.class);
and then check for entries with
if(inputMm == MainMenu.BUY_PASS){
} ....
Upvotes: 0
Reputation: 28133
Yes, it is possible. You can declare your method like this:
public <T extends Enum<T>> T doSomething(Class<T> clazz) {
EnumSet<T> all = EnumSet.allOf(clazz);
......
}
Or you can even avoid the whole Class<T>
part and just ask for an EnumSet
of choices:
public <T extends Enum<T>> T menu(EnumSet<T> options) {
List<T> lst = new ArrayList(options);
int idx;
// display the list and prompt for idx
return lst.get(idx);
}
And then you would call it as:
METHODOFTRANSPORTATION mot = menu(EnumSet.allOf(METHODOFTRANSPORTATION.class));
And now you don't have to mess around with reflection. Let EnumSet
do that for you.
Upvotes: 3
Reputation: 10173
The correct way of referring to a class in Java is ClassName.class
. So your line would be:
this.methodOfTransportation = createMethodOfTransportation(METHODOFTRANSPORTATION.class);
METHODOFTRANSPORTATION.class
returns a Class
-type object. You can then use reflection to inspect its name, possible values, etc. Here is more info on enum reflection.
The name createMethodOfTransportation
would not be appropriate though, since it wouldn't have anything to do with the specific enum METHODOFTRANSPORTATION
.
Upvotes: 3