-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostel.java
More file actions
337 lines (301 loc) · 12.4 KB
/
Hostel.java
File metadata and controls
337 lines (301 loc) · 12.4 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
333
334
335
336
import java.text.NumberFormat;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.scene.control.TextInputDialog;
/**GUI for the Hostel application
*@author Charatan and Kans
*@version 7th April 2018
*/
public class Hostel extends Application
{
// the attributes
private int noOfRooms;
private TenantList list;
// WIDTH and HEIGHT of GUI stored as constants
private final int WIDTH = 800;
private final int HEIGHT = 500;
// visual components
private Label headingLabel = new Label("Hostel Application");
private Label roomLabel1 = new Label("Room");
private TextField roomField1 = new TextField();
private Label nameLabel = new Label("Name");
private TextField nameField = new TextField();
private Button addButton = new Button("Add Tenant");
private Button displayButton = new Button("Display Tenants");
private Button removeButton = new Button("Remove Tenant");
private Button saveAndQuitButton = new Button("Save and Quit");
private TextArea displayArea1 = new TextArea();
private Label roomLabel2 = new Label("Room");
private TextField roomField2 = new TextField();
private Label monthLabel = new Label("Month");
private TextField monthField = new TextField();
private Label amountLabel = new Label("Amount");
private TextField amountField = new TextField();
private Button paymentButton = new Button("Make Payment");
private Button listButton = new Button("List Payments");
private TextArea displayArea2 = new TextArea();
@Override
/** Initialises the screen
* @param stage: The scene's stage
*/
public void start(Stage stage)
{
noOfRooms = getNumberOfRooms(); // call private method
// initialise tenant list
list = new TenantList(noOfRooms);
TenantFileHandler.readRecords(list);
// create four HBoxes
HBox roomDetails = new HBox (10);
HBox tenantButtons = new HBox(10);
HBox paymentDetails = new HBox(10);
HBox paymentButtons = new HBox(10);
// add components to HBoxes
roomDetails.getChildren().addAll(roomLabel1, roomField1, nameLabel, nameField);
tenantButtons.getChildren().addAll( addButton, displayButton, removeButton,
saveAndQuitButton);
paymentDetails.getChildren().addAll( roomLabel2, roomField2, monthLabel, monthField,
amountLabel, amountField);
paymentButtons.getChildren().addAll(paymentButton, listButton);
// create VBox
VBox root = new VBox(10);
// add all components to VBox
root.getChildren().addAll( headingLabel, roomDetails, tenantButtons, displayArea1,
paymentDetails, paymentButtons, displayArea2);
// create the scene
Scene scene = new Scene(root, Color.LIGHTBLUE);
// set font of heading
Font font = new Font("Calibri", 40);
headingLabel.setFont(font);
// set alignment of HBoxes
roomDetails.setAlignment(Pos.CENTER);
tenantButtons.setAlignment(Pos.CENTER);
paymentDetails.setAlignment(Pos.CENTER);
paymentButtons.setAlignment(Pos.CENTER);
// set alignment of VBox
root.setAlignment(Pos.CENTER);
// set minimum and maximum width of components
roomField1.setMaxWidth(50);
roomField2.setMaxWidth(50);
roomDetails.setMinWidth(WIDTH);
roomDetails.setMaxWidth(WIDTH);
tenantButtons.setMinWidth(WIDTH);
tenantButtons.setMaxWidth(WIDTH);
paymentDetails.setMinWidth(WIDTH);
paymentDetails.setMaxWidth(WIDTH);
paymentButtons.setMinWidth(WIDTH);
paymentButtons.setMaxWidth(WIDTH);
root.setMinSize(WIDTH, HEIGHT);
root.setMaxSize(WIDTH, HEIGHT);
displayArea1.setMaxSize(WIDTH - 80, HEIGHT/5);
displayArea2.setMaxSize(WIDTH - 80, HEIGHT/5);
stage.setWidth(WIDTH);
stage.setHeight(HEIGHT);
// customise the visual components
// customise the VBox border and background
BorderStroke style = new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID,
new CornerRadii(0), new BorderWidths(2) );
root.setBorder(new Border (style));
root.setBackground(Background.EMPTY);
// customise buttons
addButton.setBackground(new Background(new BackgroundFill(Color.LIGHTYELLOW,
new CornerRadii(10), Insets.EMPTY)));
displayButton.setBackground( new Background(new BackgroundFill(Color.LIGHTYELLOW,
new CornerRadii(10), Insets.EMPTY)));
removeButton.setBackground( new Background(new BackgroundFill(Color.LIGHTYELLOW,
new CornerRadii(10), Insets.EMPTY)));
saveAndQuitButton.setBackground( new Background(new BackgroundFill(Color.LIGHTYELLOW,
new CornerRadii(10), Insets.EMPTY)));
paymentButton.setBackground( new Background(new BackgroundFill(Color.LIGHTYELLOW,
new CornerRadii(10), Insets.EMPTY)));
listButton.setBackground( new Background(new BackgroundFill(Color.LIGHTYELLOW,
new CornerRadii(10), Insets.EMPTY)));
// call private methods for button event handlers
addButton.setOnAction(e -> addHandler());
displayButton.setOnAction(e -> displayHandler() );
removeButton.setOnAction( e -> removeHandler());
paymentButton.setOnAction( e -> paymentHandler());
listButton.setOnAction( e -> listHandler());
saveAndQuitButton.setOnAction( e -> saveAndQuitHandler());
// configure the stage and make the stage visible
stage.setScene(scene);
stage.setTitle("Hostel Applicaton");
stage.setResizable(false); // see discussion below
stage.show();
}
/**
* Method to request number of hostel rooms from the user
* @return number of rooms
*/
private int getNumberOfRooms()
{
TextInputDialog dialog = new TextInputDialog();
dialog.setHeaderText("How many rooms?");
dialog.setTitle("Room Information Request");
String response = dialog.showAndWait().get();
return Integer.parseInt(response); }
// event handler methods
private void addHandler()
{
String roomEntered = roomField1.getText();
String nameEntered = nameField.getText();
// check for errors
if(roomEntered.length()== 0 || nameEntered.length()== 0)
{
displayArea1.setText ("Room number and name must be entered");
}
else if(Integer.parseInt(roomEntered)< 1 || Integer.parseInt(roomEntered)>noOfRooms)
{
displayArea1.setText ("There are only " + noOfRooms + " rooms");
}
else if(list.search(Integer.parseInt(roomEntered)) != null)
{
displayArea1.setText("Room number " + Integer.parseInt(roomEntered) + " is occupied");
}
else // ok to add a Tenant
{
Tenant t = new Tenant(nameEntered,Integer.parseInt(roomEntered));
list.addTenant(t);
roomField1.setText("");
nameField.setText("");
displayArea1.setText("New tenant in room " + roomEntered + " successfully added");
}
}
public void displayHandler()
{
int i;
if(list.isEmpty()) // no rooms to display
{
displayArea1.setText("All rooms are empty");
}
else // display rooms
{
displayArea1.setText("Room" + "\t" + "Name" + "\n");
for(i = 1; i <= list.getTotal(); i++ )
{
displayArea1.appendText(list.getTenant(i).getRoom()
+ "\t\t"
+ list.getTenant(i).getName() + "\n");
}
}
}
private void removeHandler()
{
String roomEntered = roomField1.getText();
// check for errors
if(roomEntered.length()== 0)
{
displayArea1.setText("Room number must be entered");
}
else if(Integer.parseInt(roomEntered) < 1 || Integer.parseInt(roomEntered)>noOfRooms)
{
displayArea1.setText("Invalid room number");
}
else if(list.search(Integer.parseInt(roomEntered))== null)
{
displayArea1.setText("Room number " + roomEntered + " is empty");
}
else // ok to remove Tenant
{
list.removeTenant(Integer.parseInt(roomEntered));
displayArea1.setText("Tenant removed from room " + Integer.parseInt(roomEntered));
}
}
private void paymentHandler()
{
String roomEntered = roomField2.getText();
String monthEntered = monthField.getText();
String amountEntered = amountField.getText();
// check for errors
if(roomEntered.length()== 0 || monthEntered.length()== 0 || amountEntered.length()== 0)
{
displayArea2.setText("Room number, month and amount must all be entered");
}
else if(Integer.parseInt(roomEntered) < 1 || Integer.parseInt(roomEntered)>noOfRooms)
{
displayArea2.setText("Invalid room number");
}
else if(list.search(Integer.parseInt(roomEntered)) == null)
{
displayArea2.setText("Room number " + roomEntered + " is empty");
}
else // ok to process payment
{
Payment p = new Payment(monthEntered,Double.parseDouble(amountEntered));
list.search(Integer.parseInt(roomEntered)).makePayment(p);
displayArea2.setText("Payment recorded");
}
}
private void listHandler()
{
int i;
String roomEntered = roomField2.getText();
// check for errors
if(roomEntered.length()== 0)
{
displayArea2.setText("Room number must be entered");
}
else if(Integer.parseInt(roomEntered) < 1 || Integer.parseInt(roomEntered) > noOfRooms)
{
displayArea2.setText("Invalid room number");
}
else if(list.search(Integer.parseInt(roomEntered)) == null)
{
displayArea2.setText("Room number " + Integer.parseInt(roomEntered) + " is empty");
}
else // ok to list payments
{
Tenant t = list.search(Integer.parseInt(roomEntered));
PaymentList p = t.getPayments();
if(t.getPayments().getTotal() == 0)
{
displayArea2.setText("No payments made for this tenant");
}
else
{
/* The NumberFormat class is similar to the DecimalFormat class that we used
previously.
The getCurrencyInstance method of this class reads the system values to find out which country we are in, then uses the correct currency symbol */
NumberFormat nf = NumberFormat.getCurrencyInstance();
String s;
displayArea2.setText("Month" + "\t\t" + "Amount" + "\n");
for(i = 1; i <= p.getTotal(); i++ )
{
s = nf.format(p.getPayment(i).getAmount());
displayArea2.appendText("" + p.getPayment(i).getMonth() + "\t\t\t" + s + "\n");
}
displayArea2.appendText("\n" + "Total paid so far : " +
nf.format(p.calculateTotalPaid()));
monthField.setText("");
amountField.setText("");
}
}
}
private void saveAndQuitHandler()
{
TenantFileHandler.saveRecords(noOfRooms,list);
Platform.exit();
}
public static void main(String[] args)
{
launch(args);
}
}