In: Computer Science
Java Programing
Display any welcome message at the top of the output screen
Create following variables:
Price for a cup of coffee (make it adjustable within the program)
Number of cups (response from User)
Tax Rate in San Diego is 8%. Define 8% as a CONSTANT variable that never changes
Payment Method (Cash, Credit Card, Store Credit, Gold)
Ask the customer for their first name, and store it as a String object.
Ask the customer (by name) how many cups of coffee they would like to order and save that value.
Calculate the Total (# of cups * Price per Cup) * Sales tax
Using the printf, make all currency data printed to the screen display 2 decimal places, and also a '$" sign.
Also use at least one Decimal Format print method .
Using a switch statement on the payment type
If the user entered 'm' or 'M', display "You are paying using money" to the screen.
If the user entered 'c' or 'C', display "You are paying by Credit Card" to the screen .
If the user entered 's' or 'S;, dispay "You are paying with Store Credit" to the screen.
If the user entered 'g' or 'G', display "You are paying with Gold" to the screen.
If the user entered anything else, display "Invalid payment type, no lemonade for you!", and end the program
Using an if statement, If the quantity is less than 1, then display "Get out of my store, you bum!" and exit the program
Using a single if-else-if statement (also called a multi-branch if-else statement)
If the total is less than $10 and the user is paying by credit card, display "Sorry, there is a $10 minimum purchase for credit card"
If the total is between $10 and $100 (including $10 and $100), and display "Thank you, you earned a free glass of Lemonade!"
If the quantity is greater than 50 or the total is greater than $100, display "I'm going to Disneyland!"
Example Output 1
Welcome to Dave's Java Hut.
Cup of Coffee: $2.00
What is your name? Billy
Hi Billy, how many cups of coffee would you like to order? 5
Subtotal: $10.00
Tax: $0.80
Total: $10.80
How will you be paying, Billy? (Enter 'm' for money, 'c' for credit card, 's' for store credit or 'g' for gold): m
** You are paying using money
** Thank you, you earned a free cup of coffee!
Thanks Billy, and have a nice day!
import java.util.*;
class CoffeeShop
{
public static void main (String[] args)
{
Scanner scan = new
Scanner(System.in);
double price = 2;
char paymentType;
final double tax =
0.08;
System.out.println("Welcome to
Dave's Java Hut.");
System.out.println("Cup of Coffee: $"+price);
System.out.println("What is your name? ");
String name =
scan.next();
System.out.println("Hi "+name+", how many cups of coffee would you
like to order?");
int quantity =
scan.nextInt();
if(quantity <
1)
{
System.out.println("Get out of
my store, you bum!" );
System.exit(0);
}
double subTotal =
quantity*price;
System.out.printf("\nSubtotal: $ %.2f",subTotal);
System.out.printf("\nTax: $
%.2f",tax);
double total =
subTotal+tax;
System.out.printf("\nTotal: $ %.2f",total);
System.out.println("\nHow will you be paying, "+name+"? (Enter 'm'
for money, 'c' for credit card, 's' for store credit or 'g' for
gold");
paymentType =
scan.next().charAt(0); //extract first character of the
string
switch(paymentType)
{
case 'm':
case 'M' : System.out.println("\n** You are paying using
money");
break;
case 'c':
case 'C' : System.out.println("\n** You are paying using Credit
Card");
break;
case 's':
case 'S' : System.out.println("\n** You are paying using Store
Card");
break;
case 'g':
case 'G': System.out.println("\n** You are paying using
Gold");
break;
default: System.out.println("\nInvalid payment type, no lemonade
for you!");
break;
}
if(total < 10
&& paymentType == 'C'|| paymentType == 'c')
System.out.println("\nSorry, there is a $10 minimum purchase for
credit card");
else if(total > 10
&& total <100)
System.out.println("\n**
Thank you, you earned a free cup of coffee!");
else if(quantity > 50
|| total > 100)
System.out.println("\nI'm going to Disneyland!");
System.out.println("\nThanks "+name+", and have a nice
day!");
}
}
Output:
Welcome to Dave's Java Hut.
Cup of Coffee: $2.00
What is your name? Billy
Hi Billy, how many cups of coffee would you like to order? 5
Subtotal: $10.00
Tax: $0.80
Total: $10.80
How will you be paying, Billy? (Enter 'm' for money, 'c' for credit card, 's' for store credit or 'g' for gold): m
** You are paying using money
** Thank you, you earned a free cup of coffee!
Thanks Billy, and have a nice day!