Open github-learning-lab[bot] opened 3 years ago
Alright, next let's take in some user input using our scanner object (which we called "keyboard")
The following code will ask the person what their name is, and create a personalized message for them to see the menu! The input entered in the terminal will be stored in the firstName variable.
Type the following code under Step 3.
System.out.print("What is your first name? ");
firstName = keyboard.nextLine();
System.out.print(firstName + ", please see our MENU below: ");
System.out.print("\n"); // skips a line
Let's test it out!
Re-compile the java file javac custom_order.java
, then run it java custom_order
.
Notice how we can add variables to text with the plus symbol?
You will display the menu by creating several print statements. Type the following code under Step 4. Modify spacing to align if needed:
System.out.println("_______________________________________________");
System.out.println(" MENU QUANTITY BASE COST ");
System.out.println("_______________________________________________");
System.out.println(" Cake 1 $15 ");
System.out.println(" Set of Cupcakes 6 $15 ");
System.out.println("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _");
System.out.println("Frostings (vanilla, chocolate, strawberry, coco)");
System.out.println("Fillings (mocha, mint, lemon, caramel, vanilla)");
System.out.println("Toppings (sprinkles, cinnamon, cocoa, nuts)");
System.out.println("_______________________________________________");
Ask the user if they want to order cupcakes or a cake. (Only 1 for now for the sake of simplicity.) You will save the input item ordered in the itemOrder variable.
Type the following code under Step 5.
System.out.println("Do you want CUPCAKES or a CAKE?");
itemOrder = keyboard.nextLine();
Ask the user what frosting they want based on the menu choices. You will save the input frosting in the frostingType variable.
Type the following code under Step 6.
System.out.println("What type of FROSTING do you want? ");
System.out.println("Vanilla, Chocolate, Strawberry or Coco");
frostingType = keyboard.nextLine();
Ask the user what filling they want based on the menu choices. You will save the input filling in the filingType variable.
Type the following code under Step 7.
System.out.println("What type of FILLING do you want? ");
System.out.println("Mocha, Mint, Lemon, Caramel or Raspberry");
fillingType = keyboard.nextLine();
Ask the user to choose toppings based on the menu choices. You will save the input toppings in the toppings variable.
Type the following code under Step 8.
System.out.println("What type of TOPPINGS do you want? ");
System.out.println("Sprinkles, Cinnamon, Cocoa, Nuts");
toppings = keyboard.nextLine();
Re-compile the java file javac custom_order.java
, then run it java custom_order
.
You should see the series of questions above appear one at a time, waiting for user input.
Close this issue to continue
Variables
In this section, you will create a scanner object to take in user input, declare variables that will store information, and print out a friendly message!
You can comment out our 'Hello world' statement by adding // to the start of that line:
Type the following code under Step 2. It will create your scanner object, create your string variables that store text information, and double variables that store numeric values. Most of these variables don't have any values yet (except "cost" and "TAX_RATE"). We are just declairing their name and data type.
The Scanner object has pre-built methods we will use to process user input. Notice how we imported it at the top of the file?
Add in the following comment and line of code to print friendly messages:
Before we test this code, remember to re-compile the java file
javac custom_order.java
Then run the file with this command:java custom_order
If you see our friendly greeting in your terminal, add a comment to this issue.