Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variables #2

Open
github-learning-lab bot opened this issue Oct 27, 2020 · 2 comments
Open

Variables #2

github-learning-lab bot opened this issue Oct 27, 2020 · 2 comments

Comments

@github-learning-lab
Copy link

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:

// System.out.println("Hello World!");

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?

Scanner keyboard = new Scanner (System.in); 

String firstName; // User's first name
String itemOrder; // Item ordered
String frostingType; // Frosting ordered
String fillingType; // Filling ordered
String toppings; // Toppings ordered
String input;  // User input

double cost = 15.00; // Cost of cake and cupcakes
final double TAX_RATE = .08;  // Sales tax rate
double tax; // Amount of tax

Add in the following comment and line of code to print friendly messages:

// Introduce shop and prompt user to input first name

System.out.println("Welcome to Java's Cake & Cupcake Shop!");
System.out.println("We make custom cakes with our secret cake batter!");

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.

@domincl
Copy link
Owner

domincl commented Oct 27, 2020

g

@github-learning-lab
Copy link
Author

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.

Step 3

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?

Step 4:

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("_______________________________________________");

Step 5:

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

Step 6:

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

Step 7:

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

Step 8:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant