Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Enums in getters and setters?

Tags:

java

enums

So what I am trying to do is this:

Write a User class

A User:

  • has a username e.g 'fj3'
  • has a userType which can be: 'user', 'editor' or 'admin'
  • has a name e.g 'Francis'
  • has a constructor which takes the username, userType and name as parameters
  • has a getUsername() method
  • has a getUserType() method
  • has a getName() method
  • has a setUserType() method which takes one of the user types as a parameter

My code so far:

public class User{

     public String id;
     public String userPermissions;
     public String actualName;

     public User(String username, String userType, String name){
         id = username;
         userPermissions = userType;
         actualName= name;
     }

    public String getUsername(){
        return id;
    }

    public String getUserType(){
        return userPermissions;
    }       

    public String getName(){
        return actualName;
    }

    public enum UserType{
       admin, editor, user;
    }

    public void setUserType(String input){
        userPermissions = input;
    }
}

What do I need to do to get this to work? I do not know how to make it so the only usertypes that can be chosen are admin, editor or user.

like image 451
user2973447 Avatar asked Nov 09 '13 17:11

user2973447


People also ask

How do you use enums correctly?

You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: "permanent", "temp", "apprentice"), or flags ("execute now", "defer execution").

Can you use an enum as a map key?

You can use Enum as well as any other Object as key. 2. Performance : EnumMap is a specialized Map implementation for use with enum type keys. EnumMaps are represented internally as arrays.

Can enums have setters?

Update: It's possible to have setters in enum types. Save this answer.

Where do I put enum files?

If the enum is only used within one class, it should be placed within that class, but if the enum is used between two or more classes, it ought to either be in it's own code file, or in a conglomerated code file that contains all the enum for a particular assembly.


2 Answers

You have to change your types to this enum:

public class User {
     public enum UserType {
        ADMIN, EDITOR, USER;
     }

     public String id;
     public UserType userPermissions;
     public String actualName;

     public User(String username, UserType userType, String name) {
         id = username;
         userPermissions = userType;
         actualName= name;
     }

    public String getUsername() {
        return id;
    }

    public UserType getUserType() {
        return userPermissions;
    }       

    public String getName() {
        return actualName;
    }

    public void setUserType(UserType input) {
        userPermissions = input;
    }
}
like image 139
kelunik Avatar answered Sep 28 '22 06:09

kelunik


Because you have already declared an enum type to represent the possible values for userType, you've already gone a long way to solving this problem.

If you declare a variable of type UserType, then the only possible values must be one of the defined enum constants.

To restrict the input to your setPermissions method, all you have to do is change to the following:

public class User{

    public String id;
    public String userPermissions;
    public String actualName;

    public User(String username, String userType, String name){
        id = username;
        userPermissions = userType;
        actualName = name;
    }

    public enum UserType{
        ADMIN, EDITOR, USER;
    }

    public void setUserType(UserType type){
        userPermissions = type.toString();
    }
}
like image 24
scottb Avatar answered Sep 28 '22 06:09

scottb