fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. UserRole userRole = UserRole.forDisplayNameIgnoreCase ( "seller" ) ;
  13. System.out.println( "userRole.toString() = " + userRole ) ;
  14. }
  15. }
  16.  
  17. enum UserRole
  18. {
  19. ADMIN ( "Admin" ) ,
  20. SELLER ( "Seller" ) ,
  21. BIDDER ( "Bidder" ) ;
  22.  
  23. private final String displayName ;
  24.  
  25. // Constructor
  26. UserRole ( String displayName )
  27. {
  28. this.displayName = displayName ;
  29. }
  30.  
  31. public String getDisplayName()
  32. {
  33. return this.displayName ;
  34. }
  35.  
  36. public static UserRole forDisplayNameIgnoreCase ( final String desiredDisplayName )
  37. {
  38. for ( UserRole userRole : UserRole.values() )
  39. {
  40. if ( userRole.getDisplayName().equalsIgnoreCase( desiredDisplayName ) )
  41. {
  42. return userRole ;
  43. }
  44. }
  45. throw new IllegalArgumentException( "Unknown display name" ) ; // Or return an `Optional< UserRole >`. The Optional would be my preference.
  46. }
  47.  
  48. }
Success #stdin #stdout 0.12s 52040KB
stdin
Standard input is empty
stdout
userRole.toString() = SELLER