-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPizzaStoreApp.java
More file actions
169 lines (145 loc) · 6.6 KB
/
Copy pathPizzaStoreApp.java
File metadata and controls
169 lines (145 loc) · 6.6 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//CSC210
//Group 11: Asmaa Ait Hammou – Caleb Adjei
//Project Version 1
package com.example.csc210gui;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class PizzaStoreApp extends Application {
private PizzaStore pizzaStore;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Create pizzaStore object
pizzaStore = new PizzaStore();
// Ask for user authentication
login(primaryStage);
}
private void login(Stage primaryStage) {
// Create UI elements for username and password input
Label usernameLabel = new Label("Username:");
TextField usernameField = new TextField();
Label passwordLabel = new Label("Password:");
PasswordField passwordField = new PasswordField();
Button loginButton = new Button("Login");
// Set up the layout for the login screen
GridPane loginGrid = new GridPane();
loginGrid.setHgap(10);
loginGrid.setVgap(10);
loginGrid.setPadding(new Insets(40, 40, 40, 40));
loginGrid.add(usernameLabel, 0, 0);
loginGrid.add(usernameField, 1, 0);
loginGrid.add(passwordLabel, 0, 1);
loginGrid.add(passwordField, 1, 1);
loginGrid.add(loginButton, 1, 2);
// Set the scene for the login screen
Scene loginScene = new Scene(loginGrid, 350, 200);
// Handle login button action
loginButton.setOnAction(e -> {
// Validate login credentials
if (validateLogin(usernameField.getText(), passwordField.getText())) {
// Display menu
displayMenu(primaryStage);
} else {
// Show an alert for invalid login
showAlert("Invalid Login", "Incorrect username or password. Try again.");
}
});
// Set button event handler for login
loginButton.setOnAction(e -> {
// Validate login credentials
if (validateLogin(usernameField.getText(), passwordField.getText())) {
// Display menu
displayMenu(primaryStage);
} else {
// Show an alert for invalid login
showAlert("Invalid Login", "Incorrect username or password. Try again.");
}
});
// Show login screen
primaryStage.setScene(loginScene);
primaryStage.setTitle("Pizza Store Login");
primaryStage.show();
}
private boolean validateLogin(String username, String password) {
// Check if input matches the stored login credentials
// Use the Pizza Store class methods for validation
return username.equals(pizzaStore.getUsername()) && password.equals(pizzaStore.getPassword());
}
private void displayMenu(Stage primaryStage) {
// Create layout for pizza menu
GridPane menuGrid = new GridPane();
menuGrid.setHgap(10);
menuGrid.setVgap(10);
menuGrid.setPadding(new Insets(40, 40, 40, 40));
// Populate menu with item names and prices
String[] itemNames = pizzaStore.getItemNames();
for (int i = 0; i < itemNames.length; i++) {
Label itemNameLabel = new Label(itemNames[i]);
double itemPrice = pizzaStore.getPrice(itemNames[i]);
Label itemPriceLabel = new Label("$" + itemPrice);
menuGrid.add(itemNameLabel, 0, i);
menuGrid.add(itemPriceLabel, 1, i);
}
// Create input fields, button, and label for cost display
TextField itemTextField = new TextField();
TextField quantityTextField = new TextField();
Button calculateButton = new Button("Calculate Cost");
Label costDetailsLabel = new Label();
// Add input fields, button, and cost details label to the layout
menuGrid.add(new Label("Select Item:"), 0, itemNames.length);
menuGrid.add(itemTextField, 1, itemNames.length);
menuGrid.add(new Label("Enter Quantity:"), 0, itemNames.length + 1);
menuGrid.add(quantityTextField, 1, itemNames.length + 1);
menuGrid.add(calculateButton, 0, itemNames.length + 2);
menuGrid.add(costDetailsLabel, 0, itemNames.length + 3, 2, 1); // Span across two columns
// Set up the menu scene
Scene menuScene = new Scene(menuGrid, 500, 500);
// Handle calculation button action
calculateButton.setOnAction(e -> {
// Calculate and display cost
String costDetails = calculateCost(itemTextField.getText(), quantityTextField.getText());
costDetailsLabel.setText(costDetails); // Update the label with cost details
});
// Show the menu screen
primaryStage.setScene(menuScene);
primaryStage.setTitle("Pizza Store Menu");
primaryStage.show();
}
private String calculateCost(String selectedItem, String quantity) {
try {
// Validate the quantity input
int quantityValue = Integer.parseInt(quantity);
double unitCost = pizzaStore.getPrice(selectedItem);
if (unitCost != -1) {
// Calculate pre-cost, tax, and total cost
double preCost = pizzaStore.calculateCost(selectedItem, quantityValue);
double taxes = pizzaStore.calculateTaxes(preCost);
double totalCost = pizzaStore.calculateTotalCost(preCost, taxes);
// Return cost details
return String.format("Pre-cost: $%.2f\nTaxes: $%.2f\nTotal Cost: $%.2f", preCost, taxes, totalCost);
} else {
// Return an error message for invalid pizza selection
return "Invalid Item: Please select a valid item from the menu.";
}
} catch (NumberFormatException e) {
// Return an error message for invalid quantity
return "Invalid Input: Please enter a valid quantity.";
}
}
private void showAlert(String title, String content) {
// // Display an alert dialog with the given title and content
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(content);
alert.showAndWait();
}
}