Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code in Java (see: Example Output 3 (user input is in green) Update the class LemonadeStand1. Using a while loop , if the user enters

Code in Java

(see: Example Output 3 (user input is in green)

Update the class LemonadeStand1. Using a while loop, if the user enters an invalid payment type (not C, c, D, d, M, m, V or v) then tell them that was invalid attempt and allow the user to try again. Allow only 3 invalid attempts, and after the 3rd invalid attempt, say "No lemonade for you, goodbye!" end the program

2. Using a do-while loop , after the program has completed, ask the user if they would like to run the program again. If the user types "Yes" or "yes" then the program should start over. If the user types anything else, the program should just end normally

Example Output 3 (user input is in green)

Welcome to the Lemonade Stand, what is your name?

Amanda

Hi Amanda, how many glasses of lemonade would you like? They are $1.75 each

2

Each glass costs $1.75, so the total due is $3.50.

How will you be paying today?

C

Credit card transaction.

There will be a $0.50 fee for using Credit Card. Your new total is $4.00.

Would you like to run the program again? (Yes or no):

Yes

Welcome to the Lemonade Stand, what is your name?

Joe

Hi Joe, how many glasses of lemonade would you like? They are $1.75 each

1

Each glass costs $1.75, so the total due is $1.75.

How will you be paying today?

M

Money transaction.

Would you like to run the program again? (Yes or no):

No

Thanks and have a nice day!

This is from Part 1 and 2

Part 1

Write a Java class called LemonadeStand 1. For the following, store each item as a variable of the appropriate data type. Must use a different data type for each one. Get the value from the user using the Scanner class.

  • Ask the customer for his or her name and store it as a variable
  • Ask the customer, by their first name, how many glasses of lemonade they would like to buy, and store that value as a variable.
  • Store the price of lemonade as a variable
  • Get the payment type from the user, as a single character (C for credit card, D for debit card, M for cash money, or V for Venmo). Please note that we are not doing anything with this variable for now, but will in the next assignment

2. Calculate the total price which is basically the number of glasses time the price.

3. Display the total due to the user in this format: Each glass costs $(price), so the total due is $(total) !

  • Where (price) is replaced with the variable you chose to store the price per glass of lemonade, and total is replaced with the variable you chose to store the total due
  • Make sure the total pay always displays 2 digits after the decimal point. Also display the dollar sign "$" before the total pay.

Example Output (user input is in green)

Welcome to the Lemonade Stand, what is your name?

Amanda

Hi Amanda, how many glasses of lemonade would you like? They are $1.75 each

2

Each glass costs $1.75, so the total due is $3.50.

How will you be paying today?

C

Thanks and have a nice day!

Part 2

Update the class LemonadeStand

1. Change the price per glass of lemonade to a constant variable. Also be sure to rename the variable to show the proper naming convention for constant variables.

2. Using a switch statement:

  • If the payment type is C or c, then display "Credit card transaction" on the screen
  • If the payment type is D or d, then display "Debit card transaction" on the screen
  • If the payment type is M or m, then display "Money transaction" on the screen
  • If the payment type is V or v, then display "Venmo transaction" on the screen
  • If the payment is none of the above, then display "Invalid payment type" on the screen, and end the program with System.exit(1);

3. Using a single if-else-if statement:

  • If the user paid via credit card or debit card, then charge a $0.50 credit/debit fee. Add that fee to the total, and display the new total on the screen.
  • If the user paid via money (cash) and the total is $5 or more, then give a 10% discount. If the total is less than $5, then no discount will be given. Subtract that discount from the total, and display that new total on the screen.
  • If the user paid via Venmo and the total is more than $10, then tell the user they earned a free glass of lemonade

Example Output (user input is in green)

Welcome to the Lemonade Stand, what is your name?

Amanda

Hi Amanda, how many glasses of lemonade would you like? They are $1.75 each

2

Each glass costs $1.75, so the total due is $3.50.

How will you be paying today?

C

Credit card transaction.

There will be a $0.50 fee for using Credit Card. Your new total is $4.00. Thanks and have a nice day!

**Part 1 Java code

import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner obj = new Scanner(System.in); System.out.println("Welcome to the Lemonade Stand, what is your name?"); String name = obj.nextLine(); System.out.println("Hi "+name+", how many glasses of lemonade would you like? They are $1.75 each "); int n = obj.nextInt(); System.out.format("Each glass costs $1.75, so the total due is %.2f ",n*1.75); System.out.println("How will you be paying today? "); char mode = obj.next().charAt(0); System.out.println("Thanks and have a nice day!"); } }

**Part 2 Code

import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner obj = new Scanner(System.in); final Double PER_GLASS = 1.75; System.out.println("Welcome to the Lemonade Stand, what is your name?"); String name = obj.nextLine(); System.out.println("Hi "+name+", how many glasses of lemonade would you like? They are "+PER_GLASS+" each "); int glasses = obj.nextInt(); double cost = glasses*PER_GLASS; System.out.format("Each glass costs "+PER_GLASS+", so the total due is %.2f ",cost); System.out.println("How will you be paying today? "); char mode = obj.next().toUpperCase().charAt(0); switch(mode){ case 'C': System.out.println("Credit card transaction"); break; case 'D': System.out.println("Debit card transaction"); break; case 'M': System.out.println("Money transaction"); break; case 'V': System.out.println("Venmo transaction"); break; default: System.out.println("Invalid payment type"); break; } if(mode=='C'){ System.out.print("There will be a $0.50 for using Credit Card. "); } else if(mode=='D'){ System.out.print("There will be $0.50 for using Debit Card. "); } double total = cost + 0.5; System.out.format(" Your new total is $%.2f. Thanks and have a nice day!",total); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions

Question

What is meant by the term industrial relations?

Answered: 1 week ago