-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOwnerCustomersScreen.java
More file actions
128 lines (99 loc) · 4.53 KB
/
Copy pathOwnerCustomersScreen.java
File metadata and controls
128 lines (99 loc) · 4.53 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
package bookstoreapp;
import static bookstoreapp.BookStore.*;
import javafx.collections.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.stage.Stage;
public class OwnerCustomersScreen {
public Group display(Stage primaryStage) {
Group ct = new Group();
VBox vbox = new VBox();
vbox.setPadding(new Insets(0, 150, 60, 0));
vbox.setAlignment(Pos.CENTER);
Label label = new Label("Customers");
label.setFont(new Font("Arial", 24));
// Set up table columns
TableColumn<Customer, String> usernameCol = new TableColumn<>("Username");
usernameCol.setMinWidth(150);
usernameCol.setCellValueFactory(new PropertyValueFactory<>("user"));
TableColumn<Customer, String> passwordCol = new TableColumn<>("Password");
passwordCol.setMinWidth(150);
passwordCol.setCellValueFactory(new PropertyValueFactory<>("pass"));
TableColumn<Customer, Integer> pointsCol = new TableColumn<>("Points");
pointsCol.setMinWidth(120);
pointsCol.setCellValueFactory(new PropertyValueFactory<>("pts"));
TableView<Customer> customersTable = new TableView<>();
customersTable.setItems(FXCollections.observableArrayList(getCustomers()));
customersTable.getColumns().addAll(usernameCol, passwordCol, pointsCol);
// Set up add and delete buttons
final TextField addUsername = new TextField();
addUsername.setPromptText("Username");
addUsername.setMaxWidth(usernameCol.getPrefWidth());
final TextField addPassword = new TextField();
addPassword.setPromptText("Password");
addPassword.setMaxWidth(passwordCol.getPrefWidth());
final Button addButton = new Button("Add");
addButton.setOnAction(e -> {
String newUsername = addUsername.getText().trim();
String newPassword = addPassword.getText().trim();
if (newUsername.isEmpty() || newPassword.isEmpty()) {
// Show error message if either field is empty
Alert alert = new Alert(Alert.AlertType.ERROR, "Username and password are required.");
alert.showAndWait();
return;
}
for (Customer c : getCustomers()) {
if (c.getUser() .equals(newUsername)) {
// Show error message if username already exists
Alert alert = new Alert(Alert.AlertType.ERROR, "Username already exists.");
alert.showAndWait();
return;
}
}
// Add new customer to owner's list and refresh table
Customer newCustomer = new Customer(newUsername, newPassword, 0);
addCustomer(newCustomer);
customersTable.getItems().add(newCustomer);
addUsername.clear();
addPassword.clear();
});
final Button deleteButton = new Button("Delete");
deleteButton.setOnAction(e -> {
Customer selectedCustomer = customersTable.getSelectionModel().getSelectedItem();
if (selectedCustomer == null) {
// Show error message if no customer is selected
Alert alert = new Alert(Alert.AlertType.ERROR, "Please select a customer to delete.");
alert.showAndWait();
return;
}
deleteCustomer(selectedCustomer);
customersTable.getItems().remove(selectedCustomer);
});
HBox hb = new HBox(5);
hb.setAlignment(Pos.CENTER);
hb.getChildren().addAll(addUsername, addPassword, addButton, deleteButton);
VBox core = new VBox(5);
core.setAlignment(Pos.CENTER);
core.setPadding(new Insets(0, 0, 0, 100));
core.getChildren().addAll(label, customersTable, hb);
Button backButton = new Button("Back");
backButton.setOnAction(e -> {
// TODO: navigate back to previous screen
BookstoreApp app = (BookstoreApp) primaryStage.getUserData();
if (app != null) {
app.showOwnerStartScreen();
}
});
HBox back = new HBox(5);
back.setPadding(new Insets(5));
back.setAlignment(Pos.CENTER_LEFT);
back.getChildren().addAll(backButton);
vbox.getChildren().addAll(back, core);
ct.getChildren().addAll(vbox);
return ct;
}
}