I have the following enumerator:
public enum UserChoice {
QUIT, LIST_BOOKS, CHECKOUT_BOOK, RETURN_BOOK, LIST_MOVIES,
CHECKOUT_MOVIE, RETURN_MOVIE, USER_INFORMATION
}
and I would like to use it in a switch statement which takes an int as a parameter. However, I need to get the int value of an enum, so I am doing this:
try {
int option = Reader.getUserOption();
} catch (InputMismatchException ex) {
option = 8;
}
switch (option) {
case UserChoice.QUIT.ordinal():
break;
case UserChoice.LIST_BOOKS.ordinal():
Printer.printBooks(library);
break;
case UserChoice.CHECKOUT_BOOK.ordinal():
// code
break;
case UserChoice.RETURN_BOOK.ordinal():
// code
break;
case UserChoice.LIST_MOVIES.ordinal():
Printer.printMovies(library);
break;
case UserChoice.CHECKOUT_MOVIE.ordinal():
// code
break;
case UserChoice.RETURN_MOVIE.ordinal():
// code
break;
case UserChoice.USER_INFORMATION.ordinal():
System.out.println(currentUser);
break;
default:
Printer.printInvalidOptionMessage();
break;
}
Is there any way to cast an int into an enumeration value or who can I achieve this using enumerations. In the end, my point is to have the name of the enumerator for each case, so that I can clearly understand what each case is doing because previously I was using int values to do it.