-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE3.java
48 lines (43 loc) · 1.32 KB
/
E3.java
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
/**
* E3.java
* Lesson 8 - OOP
* Exercise 3 - Lunch Order
*/
public class E3 extends Problem {
// Constructor to initialize fields in parent class
public E3() {
super(3, "Lunch Order", "Prompts the user to order food, displaying the food's information");
}
/**
* Main function run from outside
*/
@Override
public void run() {
// Create a new array of the foods and their information
Food foods[] = {
new Food(1.85, 9, 33, 1, "hamburger", "hamburgers"),
new Food(2.00, 1, 11, 5, "salad", "salads"),
new Food(1.30, 11, 36, 4, "french fries", "french fries"),
new Food(0.95, 0, 38, 0, "soda", "sodas"),
};
// Create a running total
double total = 0;
// Loop for each food
for (Food food: foods) {
// Display information of food
System.out.printf(
"Each %s costs $%.2f, has %dg of fat, %dg of carbs, and %dg of fiber.\n" +
"Please enter number of %s: ",
food.getSingularName(), food.getPrice(), food.getFat(), food.getCarb(), food.getFiber(),
food.getPluralName()
);
// Get the user input on the quantity of food
int quantity = super.inputReader.nextInt();
super.inputReader.nextLine();
// Add price to running total
total += food.getPrice() * (double) quantity;
}
// Output the final price
System.out.printf("Your order comes to: $%.2f.\n", total);
}
}