-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCafeOrder.java
More file actions
50 lines (39 loc) · 2.19 KB
/
Copy pathCafeOrder.java
File metadata and controls
50 lines (39 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Write a program where you create 3 variables that represent products
// at a cafe. The products could be beverages like coffee, cappuccino,
// espresso, green tea, etc. Assign prices to each product. Create 2 more
// variables called subtotal and totalSale and complete an “order” for 3
// items of the first product, 4 items of the second product, and 2 items of
// the third product. Add them all together to calculate the subtotal.
// Create a constant called SALES_TAX and add sales tax to the subtotal
// to obtain the totalSale amount. Be sure to format the results to 2 decimal
import java.text.DecimalFormat;
public class CafeOrder {
public static void main(String[] args) {
// Define product prices
double coffeePrice = 2.99;
double cappuccinoPrice = 3.99;
double espressoPrice = 2.49;
// Define order quantities
int coffeeQty = 3;
int cappuccinoQty = 4;
int espressoQty = 2;
// Calculate subtotal
double subtotal = (coffeePrice * coffeeQty) + (cappuccinoPrice * cappuccinoQty) + (espressoPrice * espressoQty);
// Add sales tax
final double SALES_TAX = 0.07; // 7% sales tax
double totalSale = subtotal * (1 + SALES_TAX);
// Format results to 2 decimal places
DecimalFormat df = new DecimalFormat("#.##");
String formattedSubtotal = df.format(subtotal);
String formattedTotalSale = df.format(totalSale);
// Output results
System.out.println("Order Summary:");
System.out.println("--------------");
System.out.println(coffeeQty + " x Coffee @ $" + coffeePrice + " = $" + (coffeePrice * coffeeQty));
System.out.println(cappuccinoQty + " x Cappuccino @ $" + cappuccinoPrice + " = $" + (cappuccinoPrice * cappuccinoQty));
System.out.println(espressoQty + " x Espresso @ $" + espressoPrice + " = $" + (espressoPrice * espressoQty));
System.out.println("Subtotal: $" + formattedSubtotal);
System.out.println("Sales Tax (" + (SALES_TAX * 100) + "%): $" + df.format(subtotal * SALES_TAX));
System.out.println("Total Sale: $" + formattedTotalSale);
}
}