I was trying to come up with the most dynamic fizzbuzz answer I could.. and well, this is what I came up with:
public static void main(String[] args) {
ultraFizzBuzz((Integer arg) -> {
String retVal = "";
if(arg % 3 == 0) { retVal += "fizz"; }
if(arg % 5 == 0) { retVal += "buzz"; }
return retVal;
});
}
public static void ultraFizzBuzz(modConditions<Integer> tester) {
for(int i = 1; i < 100; i++) {
String result = tester.test(i);
if(result != "")
System.out.println(tester.test(i));
else
System.out.println(i);
}
}
In here the modConditions is an interface with a method (test) that returns type String.
What I was basically trying to do is create a super dynamic function that lets you add as many fizzes and buzzes and pops as you wanted.. but I think in the process accidentally made it so you end up writing 90% of the code in a lambda statement everytime you want to call it.
Am I overcomplicating things? Would this be considered bad programming practice?
modConditions
? Also, in traditional FizzBuzz, the program should echo every number that is not a multiple of 3 or 5 — does your code do that? (Putting this question on hold until these issues are resolved.) \$\endgroup\$modConditions
is just an interface with one abstract method that returns a string and takes an argument of type T. \$\endgroup\$