'Let's Get Takeout!' project in 'Learn Intermediate Java' course - HELP NEEDED (or ready solution)

Link to the exercise:
(https://www.codecademy.com/courses/learn-intermediate-java/projects/intermediate-java-collections-takeout)

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 userPrompt and 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!!

Thanks in advance!

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.

For step 15 I had this:

IntUserInputRetriever intUserInputRetreiver = (selection) -> {
      if (selection == 1 && customer.getMoney() >= menu.getLowestCostFood().getPrice()) {
        return true;
      } else if (selection == 0) {
        return false;
      } else {
        throw new IllegalArgumentException();
      }
    };
  }

It’s fairly straightforward once you’ve seen it, so you should be able to do the other implementations modelling this one.

Thanks so much!

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));    //  ?????
  }

CHEERS!

1 Like

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.

The question:

My solution:

    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();
            }
        }
    }

Can you help me with the full method… shouldSimulate()

I have done this so far:

    public boolean shouldSimulate(){
        String userPrompt = "Enter 1 to CONTINUE simulation or 0 to EXIT program: 1\n" +
                "You don't have enough money to continue shopping :( - ending simulation...";

        System.out.println(userPrompt);

        IntUserInputRetriever intUserInputRetreiver = (selection) -> {
            if (selection == 1 && customer.getMoney() >= menu.getLowestCostFood().getPrice()) {
                return true;
            } else if (selection == 0) {
                return false;
            } else {
                throw new IllegalArgumentException();
            }
        };

        return intUserInputRetreiver.produceOutputOnIntUserInput(this.input.nextInt());
    }

My final version:

    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);
    }