Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatronView.java
More file actions
332 lines (275 loc) · 11.6 KB
/
Copy pathPatronView.java
File metadata and controls
332 lines (275 loc) · 11.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// specify the package
package userinterface;
// system imports
import impresario.IModel;
import javafx.event.Event;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import java.time.LocalDate;
import java.util.Properties;
import java.util.regex.Pattern;
// project imports
/**
* The class containing the Book View for the Library System application
*/
public class PatronView extends View {
// GUI components
private TextField nameField;
private TextField addressField;
private TextField cityField;
private TextField stateField;
private TextField zipField;
private TextField emailField;
private TextField dobField;
private ComboBox statusCombo;
protected Button submitButton;
private LocalDate oldest = LocalDate.of(1917, 01, 01);
// For showing error message
protected MessageView statusLog;
// constructor for this class -- takes a model object
PatronView(IModel book) {
super(book, "PatronView");
// create a container for showing the contents
VBox container = new VBox(10);
container.setPadding(new Insets(15, 5, 5, 5));
// Add a title for this panel
container.getChildren().add(createTitle());
// create our GUI components, add them to this Container
container.getChildren().add(createFormContent());
container.getChildren().add(createStatusLog(" "));
getChildren().add(container);
myModel.subscribe("UpdateStatusMessage", this);
}
// Create the title container
private Node createTitle() {
HBox container = new HBox();
container.setAlignment(Pos.CENTER);
Text titleText = new Text(" Brockport Library System ");
titleText.setFont(Font.font("Arial", FontWeight.BOLD, 20));
titleText.setWrappingWidth(300);
titleText.setTextAlignment(TextAlignment.CENTER);
titleText.setFill(Color.DARKGREEN);
container.getChildren().add(titleText);
return container;
}
// Create the main form content
private VBox createFormContent() {
VBox vbox = new VBox(10);
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 10, 25));
Text prompt = new Text("Enter New Patron Information");
prompt.setWrappingWidth(300);
prompt.setTextAlignment(TextAlignment.CENTER);
prompt.setFill(Color.BLACK);
grid.add(prompt, 0, 0, 2, 1);
Text nameLabel = new Text(" Name : ");
Font myFont = Font.font("Helvetica", FontWeight.BOLD, 12);
nameLabel.setFont(myFont);
nameLabel.setWrappingWidth(110);
nameLabel.setTextAlignment(TextAlignment.RIGHT);
grid.add(nameLabel, 0, 1);
nameField = new TextField();
nameField.setOnAction(this::processAction);
grid.add(nameField, 1, 1);
Text addressLabel = new Text(" Address : ");
addressLabel.setFont(myFont);
addressLabel.setWrappingWidth(110);
addressLabel.setTextAlignment(TextAlignment.RIGHT);
grid.add(addressLabel, 0, 2);
addressField = new TextField();
addressField.setOnAction(this::processAction);
grid.add(addressField, 1, 2);
Text cityLabel = new Text(" City : ");
cityLabel.setFont(myFont);
cityLabel.setWrappingWidth(110);
cityLabel.setTextAlignment(TextAlignment.RIGHT);
grid.add(cityLabel, 0, 3);
cityField = new TextField();
cityField.setOnAction(this::processAction);
grid.add(cityField, 1, 3);
Text stateLabel = new Text(" State Code : ");
stateLabel.setFont(myFont);
stateLabel.setWrappingWidth(110);
stateLabel.setTextAlignment(TextAlignment.RIGHT);
grid.add(stateLabel, 0, 4);
stateField = new TextField();
stateField.setOnAction(this::processAction);
stateField.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches("[a-zA-Z]{0,2}")) {
stateField.setText(oldValue);
}
});
grid.add(stateField, 1, 4);
Text zipLabel = new Text(" ZIP : ");
zipLabel.setFont(myFont);
zipLabel.setWrappingWidth(110);
zipLabel.setTextAlignment(TextAlignment.RIGHT);
grid.add(zipLabel, 0, 5);
zipField = new TextField();
zipField.setOnAction(this::processAction);
zipField.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches("\\d{0,5}")) {
zipField.setText(oldValue);
}
});
grid.add(zipField, 1, 5);
Text emailLabel = new Text(" E-mail Address : ");
emailLabel.setFont(myFont);
emailLabel.setWrappingWidth(110);
emailLabel.setTextAlignment(TextAlignment.RIGHT);
grid.add(emailLabel, 0, 6);
emailField = new TextField();
emailField.setOnAction(this::processAction);
grid.add(emailField, 1, 6);
Text dobLabel = new Text(" Date of Birth : ");
dobLabel.setFont(myFont);
dobLabel.setWrappingWidth(110);
dobLabel.setTextAlignment(TextAlignment.RIGHT);
grid.add(dobLabel, 0, 7);
dobField = new TextField();
dobField.setOnAction(this::processAction);
// dobField.textProperty().addListener((observable, oldValue, newValue) -> {
// if (!newValue.matches("\\d{0,4}[-]\\d{0,2}[-]\\d{0,2}")) {
// dobField.setText(oldValue);
// }
// });
grid.add(dobField, 1, 7);
Text statusLabel = new Text(" Status : ");
statusLabel.setFont(myFont);
statusLabel.setWrappingWidth(110);
statusLabel.setTextAlignment(TextAlignment.RIGHT);
grid.add(statusLabel, 0, 8);
statusCombo = new ComboBox();
statusCombo.getItems().addAll("Active", "Inactive");
statusCombo.setValue("Active");
grid.add(statusCombo, 1, 8);
HBox doneCont = new HBox(100);
doneCont.setAlignment(Pos.CENTER);
submitButton = new Button("Submit");
// submitButton.setFont(Font.font("Arial", FontWeight.BOLD, 14));
submitButton.setOnAction(this::processAction);
Button doneButton = new Button("Back");
// doneButton.setFont(Font.font("Arial", FontWeight.BOLD, 14));
doneButton.setOnAction(e -> {
clearErrorMessage();
myModel.stateChangeRequest("CancelTransaction", null);
});
doneCont.getChildren().add(submitButton);
doneCont.getChildren().add(doneButton);
vbox.getChildren().add(grid);
vbox.getChildren().add(doneCont);
return vbox;
}
// Create the status log field
protected MessageView createStatusLog(String initialMessage) {
statusLog = new MessageView(initialMessage);
return statusLog;
}
// public void populateFields() {
// nameField.setText((String) myModel.getState("author"));
// addressField.setText((String) myModel.getState("title"));
// cityField.setText((String) myModel.getState("pubYear"));
// serviceCharge.setText((String) myModel.getState("status"));
// }
private void processAction(Event evt) {
// DEBUG: System.out.println("TellerView.actionPerformed()");
clearErrorMessage();
String nameEntered = nameField.getText();
String addressEntered = addressField.getText();
String cityEntered = cityField.getText();
String stateEntered = stateField.getText();
String zipEntered = zipField.getText();
String emailEntered = emailField.getText();
String dateEntered = dobField.getText();
String statusSelected = (String) statusCombo.getValue();
Pattern dateValidation = Pattern.compile("^((19|2[0-9])[0-9]{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
Pattern emailValidation = Pattern.compile("^([\\w \\._]+\\<[a-z0-9!#$%&'*+/=?^_`{|}~-]+" +
"(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a" +
"-z0-9](?:[a-z0-9-]*[a-z0-9])?\\>|[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%" +
"&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-" +
"]*[a-z0-9])?)$");
if ((nameEntered == null) || (nameEntered.length() == 0)) {
displayErrorMessage("Please enter a name!");
nameField.requestFocus();
} else if ((addressEntered == null) || (addressEntered.length() == 0)) {
displayErrorMessage("Please enter an address!");
addressField.requestFocus();
} else if ((cityEntered == null) || (cityEntered.length() == 0)) {
displayErrorMessage("Please enter a city!");
cityField.requestFocus();
} else if ((stateEntered == null) || (stateEntered.length() == 0)) {
displayErrorMessage("Please enter a state code!");
stateField.requestFocus();
} else if ((zipEntered == null) || (zipEntered.length() == 0)) {
displayErrorMessage("Please enter a ZIP code!");
zipField.requestFocus();
} else if (!emailValidation.matcher(emailEntered.toLowerCase()).matches()) {
displayErrorMessage("Email not in address@server.com format!");
emailField.requestFocus();
} else if (!dateValidation.matcher(dateEntered).matches()) {
displayErrorMessage("Date must be in YYYY-MM-DD");
dobField.requestFocus();
} else {
LocalDate dob = LocalDate.parse(dobField.getText());
LocalDate youngest = LocalDate.now().minusYears(18);
if (dob.isBefore(oldest)) {
displayErrorMessage("Date must be after " + oldest);
dobField.requestFocus();
} else if (dob.isAfter(youngest)) {
displayErrorMessage("Date must be before " + youngest.plusDays(1));
dobField.requestFocus();
} else {
Properties props = new Properties();
props.setProperty("name", nameEntered);
props.setProperty("address", addressEntered);
props.setProperty("city", cityEntered);
props.setProperty("stateCode", stateEntered.toUpperCase());
props.setProperty("zip", zipEntered);
props.setProperty("email", emailEntered);
props.setProperty("dateOfBirth", dateEntered);
props.setProperty("status", statusSelected.toLowerCase());
myModel.stateChangeRequest("InsertPatron", props);
displayMessage("Success!");
}
}
}
/**
* Update method
*/
public void updateState(String key, Object value) {
clearErrorMessage();
}
/**
* Display error message
*/
public void displayErrorMessage(String message) {
statusLog.displayErrorMessage(message);
}
/**
* Display info message
*/
private void displayMessage(String message) {
statusLog.displayMessage(message);
}
/**
* Clear error message
*/
public void clearErrorMessage() {
statusLog.clearErrorMessage();
}
}
You can’t perform that action at this time.
