-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestaurantOrderMethodsLab.java
More file actions
90 lines (78 loc) · 2.8 KB
/
RestaurantOrderMethodsLab.java
File metadata and controls
90 lines (78 loc) · 2.8 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.Scanner;
public class RestaurantOrder {
// Method to display the menu
public static void displayMenu() {
System.out.println("Menu:");
System.out.println("1. Burger - $5.99");
System.out.println("2. Pizza - $8.99");
System.out.println("3. Salad - $4.99");
System.out.println("4. Soda - $1.99");
}
// Method to get the price of an item based on selection
public static double getItemPrice(int itemNumber) {
// TODO: Implement logic to return the price based on itemNumber
double itemPrice;
if (itemNumber == 1)
{
itemPrice = 5.99;
}
else if (itemNumber == 2)
{
itemPrice = 8.99;
}
else if (itemNumber == 3)
{
itemPrice = 4.99;
}
else if (itemNumber == 4)
{
itemPrice = 1.99;
}
else itemPrice = 0.0;
return itemPrice;
}
// Method to take an order from the user
public static void takeOrder() {
Scanner scanner = new Scanner(System.in);
double totalCost = 0.0;
System.out.println("Enter the item number to order (0 to finish): ");
while (true) {
int choice = scanner.nextInt();
if (choice == 0) break;
else if (choice >= 1 && choice <= 4)
totalCost = totalCost + getItemPrice(choice);
// TODO: Call getItemPrice() and update totalCost
}
// TODO: Display total cost
System.out.println("Calculating total now...");
totalCost = calculateTotal(totalCost);
System.out.println("Your total is: " + totalCost + ".");
}
// Overloaded method to apply a discount
public static double calculateTotal(double totalCost) {
// TODO: Implement method to return total cost
Scanner scanner = new Scanner(System.in);
System.out.println("Do you plan to use a discount? (True for yes, False for no): ");
boolean isDiscount = scanner.nextBoolean();
if (isDiscount == true)
{
totalCost = calculateTotal(totalCost, 0.15);
}
else if (isDiscount == false)
{
totalCost = calculateTotal(totalCost, 0.0);
}
return totalCost;
}
public static double calculateTotal(double totalCost, double discount) {
// TODO: Implement method to return total cost after discount
double discountCost = (totalCost * discount);
totalCost = totalCost - discountCost;
return totalCost;
}
// Main method to run the program
public static void main(String[] args) {
displayMenu();
takeOrder();
}
}