简体   繁体   中英

Unable to pass in getter from class as argument using method

I'm trying to do like a duplicate check for my application.

For example, the user wants to change his username and hence, i got a getter function that basically gets the username from my class and compare to the user input via a duplicate function check.

However, i have up to 10 fields that require such checking and the long way is to create 10 different duplicate function check for 10 different field which makes my code very long and messy.

Hence, i used this method library to pass in the method into the function instead of creating multiple function.

Here's how i attempted to use the library

//place where i read input and display any necessary info.
import java.lang.reflect.Method;

public void editUser()
{
    Method method;
    boolean found = false;
    String userName = Keyboard.readString("Input username to change : ");

    for(User u : userData)
    {
        method = u.getuserName(); // error occur here, cannot convert string to method
        found = checkExist(userData, userName, method);
        if(!found)
        {
            System.out.println("does not exist");   
        }

        //do whatever stuffs if exist

    }
}

//function that check for existance.
public void boolean checkExist(ArrayList<User> userData, String userName, Method method)
{
    boolean found = false;

    for(User u : userData)
    {                       
        if(userName.equals(method))
        {
            found=true;
            break;
        }           
        else 
            found=false;            
    }   
    return found;
}

When i attempt to initialize method variable with my getter method from my class which i declared it as a string, it says, cannot convert string to method

Hence, what is the way where i can pass in my getter function from my other class without changing string to method as other function also reliable on this getter method.

here's the constructor and the getter method

public Player(String userName)
{
    this.userName=userName; 
}

public String getuserName() 
{
    return userName;
}

If I understand your requirements correctly and you hava Java 8, lambdas and method references can help.

A slightly generalized version:

//function that check for existance.
public void boolean checkExist(ArrayList<User> userData, String value, Function<User, String> getter)
{
    boolean found = false;

    for(User u : userData)
    {
        String userValue = getter.apply(user);
        if(value.equals(userValue))
        {
            found=true;
            break;
        }           
        else 
            found=false;            
    }   
    return found;
}

Then you call it like this:

found = checkExist(userData, userName, User::getuserName);

And then

found &= checkExist(userData, password, User::getPassword);

if you have getPassword() method in User class, and so on.

Here I assume that all your getter methods return String values. If not, you could generalize it further with something like

public void <T> boolean checkExist(ArrayList<User> userData, String value, Function<User, T> getter)

If you don't have Java 8, you could use anonymous class syntax:

    new Function<User, String>() {
        public String apply(User user) {
            return user.getuserName();
        }
    };

instead of User::getuserName .

In your case doesn't need to use Method.

Just compare the String value of the username from userData with the user inputted username.

So just change the Method to String .

Edit (what you need to change to):

public void editUser()
{
    String method;
    boolean found = false;
    String userName = Keyboard.readString("Input username to change : ");

    for(User u : userData)
    {
        method = u.getuserName(); // error occur here, cannot convert string to method
        found = checkExist(userData, userName, method);
        if(!found)
        {
            System.out.println("does not exist");   
        }

        //do whatever stuffs if exist

    }
}

//function that check for existance.
public void boolean checkExist(ArrayList<User> userData, String userName, String method)
{
    boolean found = false;

    for(User u : userData)
    {                       
        if(userName.equals(method))
        {
            found=true;
            break;
        }           
        else 
            found=false;            
    }   
    return found;
}

So your concern is code reusability, you can do like this,

public void editUser(){
    String userName = Keyboard.readString("Input username to change : ");
    if(!checkExist(userList,userName,"name")){
         System.out.println("does not exist");   
    }
}

public void boolean checkExist(ArrayList<User> userList, String fieldName, String fieldValue){
    boolean isExists =false;
    for(User u : userList){  
        switch(fieldName){
            case "name":
                 if(fieldValue.equals(u.getName())
                    isExists = true;// username already exists...
                 break;// to exit from switch...
            case "someotherfield":
            .
            .
            .
        }                     

    }  
    return isExists;         
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM