I am working on a program that is supposed to describe a takeout system. I have a specific method that is supposed to simulate whether a customer has enough money to buy something using an interface. The problem I encountered was that while I was trying to implement an interface, UserInputRetriever, using lambda..I could only use final variables within the lambda expression. When it got to making a return statement for the method itself, shouldSimulate(), I realized I couldnt because what I returned is only visible in the lambda. Now IntelliJ, suggested using a final array which worked, because I was able to use it within the lambda expression and outside of it. I just need understanding as to why using array worked as compared to using a variable. Can you please explain?
The method is as follows:
public boolean shouldSimulate() {
boolean[] hasMoney = new boolean[1];
String userPrompt = """
Please choose an option from the following:
0. To end simulation process
1. To proceed with simulation process""";
UserInputRetriever<?> intUserInputRetriever = selection -> {
if(selection == 1 && customer.money >= menu.getLowestFoodCost()){
System.out.println("You have enough money.\nProceeding with " +
"simulation");
hasMoney[0] = true;
}
else if (selection == 1 && customer.money < menu.getLowestFoodCost()){
System.out.println("You do not have enough money.\nEnding the" +
" simulation");
hasMoney[0] = false;
}
else if(selection == 0){
System.out.println("Terminating simulation");
hasMoney[0] = false;
}
else
throw new IllegalArgumentException("Please choose a valid " +
"option");
return hasMoney[0];// if hasMoney was a variable, this would not throw a compile time error
};
getOutputOnInput(userPrompt, intUserInputRetriever);// you can ignore this line
return hasMoney[0];// if hasMoney was a variable, this would throw a compile time error
}
I tried using a variable first locally and tried to also use that in my interface implementation (which used lambda), but it threw errors. But upon using suggestion from IntelliJ which suggested using an array, it worked. I would like to know why