Java – how do I use enumerations in getters and setters?

So what I want to do is this:

Write a user class

A user:

>There is a user name, such as' fj3 '> there is a UserType, which can be:' user ',' editor 'or' admin '> there is a name, such as "Francis" > there is a constructor, which takes username, UserType and name as parameters > there is a getusername() method > there is a getusertype() method > there is a getname() method > there is a setusertype() method, It takes a user type 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 make it work? I don't know how to make it the only user type to choose is administrator, editor or user

Solution

You must change your type to this enumeration:

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;
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>