Answer the question
In order to leave comments, you need to log in
Can I use the Random class to call a random class method?
For example, I have a password generation class with 3 different methods that generate upper and lower case alphabetic characters, as well as numbers, in the amount specified in the user input. The 4th user input argument is the length of the final password, and if the length of the password is greater than the sum of the 3 previous arguments, then a random character generation method must be called that will complement the missing password length. Is it possible to do this with the Random class?
Answer the question
In order to leave comments, you need to log in
More or less like this:
import java.util.List;
import java.util.Random;
import java.util.function.Function;
class RandomFunctor {
private final List<Function<Integer, String>> functionList = List.of(
integer -> {
return "this is from first " + integer; //TODO :: implement me!
},
integer -> {
return "this is from second " + integer; //TODO :: implement me!
},
integer -> {
return "this is from third " + integer; //TODO :: implement me!
});
public String getRandomString(Integer input) {
var rand = new Random();
var function = functionList.get(rand.nextInt(functionMap.size()));
return function.apply(input);
}
}
Possibly. But not directly.
In general, because you have three methods in total - then a banal switch-case will do here. In more complex cases - an array of methods (if there is no such thing in Java - an array of instances of generator classes with a common base).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question