Could anyone help with solving the tasks from 15 downwards in the 'Let’s Get Takeout! project?
How should I deal with this: " Call getOutputOnIntUserInput() and passing userPromptand the created implementation of IntUserInputRetriever. " ??
Or maybe someone knows where to look for a ready (exemplary) solution to the exercise?
If there is no such, could Codecademy prepare one and add to the folder where all the files of the project are?
Furthermore, could Codecademy provide another section in DOCS (Java Concepts) dealing with lambda expressions / functions?? That would help!
Please, provide broader (more specific and including also more advanced concepts) list of tags to choose from when creating / searching for relevant topics. There are over 7.000 usages of the ‘general’ tag while the second most common ‘debugging’ has only about 50!!
This one was a bit painful, I’ll admit. I think I copied everything to IntelliJ in order to complete this one. It wasn’t going to happen in the online IDE.
That was my plan for today - to copy all the files to IntelliJ and try there, and you saved me!
The rest (step 16 and 17) will be much easier now - analogous.
I was trying with something like the code below, that is so far from your solution!
public boolean shouldSimulate() {
String userPrompt = new String("Please, type 1 to proceed or 0 to stop the simulation.\n Your choice: ");
@ Override
public boolean produceOutputOnUserInput(int selection) {
if (selection == 1 && customer.money >= menu.getLowestCostFood()) {
System.out.println("Your choice was 1 and you've got some cash, nice, let us continue...");
return true;
}
else if (customer.getMoney() < menu.getLowestCostFood()) {
System.out.println(selection);
System.out.println("Sorry, you do not have enough money to continue shopping (only $" + customer.getMoney() + "), so go and find a better job. End of simulation..." );
return false;
}
else if (selection == 0 ) {
System.out.println(selection);
System.out.println("As you wish, simulation ending...");
return false;
}
else {
throw IllegalArgumentException;
}
}
getOutputOnIntUserInput(userPrompt, produceOutputOnUserInput(input)); // ?????
}
An e-mail came to me regarding this project (exactly the task no. 14), but I can’t see the question here (on the forum), strange… Nevertheless, I will leave my solution here.
private <T> T getOutputOnIntUserInput(String userInputPrompt, IntUserInputRetriever<?> intUserInputRetriever) {
while (true) {
System.out.println(userInputPrompt);
if (input.hasNextInt()) {
int inputInt = input.nextInt();
try {
return (T) intUserInputRetriever.produceOutputOnIntUserInput(inputInt); // ???
}
catch (IllegalArgumentException ex) {
System.out.println("Sorry, provided input (" + inputInt + ") is not valid, try another number...");
}
}
else {
System.out.println("Sorry, provided input is not valid, it should be an 'int' type...\n");
input.next();
}
}
}
public boolean shouldSimulate() {
String userPrompt = "Please, type 1 to PROCEED or 0 to STOP the simulation...\n";
IntUserInputRetriever<?> intUserInputRetreiver = (selection) -> {
if (selection == 1 && customer.getMoney() >= menu.getLowestCostFood().getPrice()) {
System.out.printf("Your choice was %s to proceed, and you've got some cash - nice! Let us continue...%n", selection);
return true;
} else if (selection == 1 && customer.getMoney() < menu.getLowestCostFood().getPrice()) {
System.out.printf("Your choice was %s, but... SORRY!\n", selection);
System.out.println("You do not have enough money to continue shopping (only $"
+ customer.getMoney()
+ "), so go and find a better job. END OF SIMULATION..." );
return false;
}
else if (selection == 0) {
System.out.printf("You've chosen %s, as you wish!\nSIMULATION ENDING...", selection);
return false;
} else {
throw new IllegalArgumentException("The provided selection (" // but what is that 'new' here for ???
+ selection
+") is invalid, please, try one more time...\n");
}
};
return getOutputOnIntUserInput(userPrompt, intUserInputRetreiver);
}
I do not know if you will ever see this, but I was wondering how your last return statement is possible. The parameter intUserInputRetriever in the last statement is of the wildcard type and not generic. It keeeps throwing errors for me in InTELLiJ