-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
393 lines (329 loc) · 13.5 KB
/
GUI.java
File metadata and controls
393 lines (329 loc) · 13.5 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* @authour: muteeba jamal <a href="mailto:muteeba.jamal@ucalgary.ca">
* muteeba.jamal@ucalgary.ca</a>
* @authour: shahzill naveed <a href="mailto:shahzill.naveed@ucalgary.ca">
* shahzill.naveed@ucalgary.ca</a>
* @authour: ahsan zia <a href="mailto:ahsan.zia@ucalgary.ca">
* ahsan.zia@ucalgary.ca</a>
* @authour: hridika banik <a href="mailto:hridika.banik@ucalgary.ca">
* hridika.banik@ucalgary.ca</a>
* @version 1.4
* @since 1.0
*/
package edu.ucalgary.ensf409;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.awt.FlowLayout;
public class GUI extends JFrame implements ActionListener, MouseListener{
// variables required for implementation
private int totalFemale;
private int totalMale;
private int totalChildrenU8;
private int totalChildrenO8;
private int grainC;
private int fvc;
private int proC;
private int OthersC;
private int CaloriesC;
private int hamperNumber = 1;
// variables required ofr the GUI
private JLabel instructions;
private JLabel instructions1;
private JLabel instructions2;
private JLabel instructions3;
private JLabel option1;
private JLabel option2;
private JLabel mLabel;
private JLabel fLabel;
private JLabel coLabel;
private JLabel cuLabel;
private JLabel twgLabel;
private JLabel fvLabel;
private JLabel proLabel;
private JLabel oLabel;
private JLabel cLabel;
private javax.swing.JPanel jPanel1;
private JTextField fInput;
private JTextField mInput;
private JTextField coInput;
private JTextField cuInput;
private JTextField twgInput;
private JTextField fvInput;
private JTextField proInput;
private JTextField oInput;
private JTextField cInput;
/*
* GUI() Constructor
*/
public GUI(){
super("Group 11 Project");
setupGUI();
setSize(1200,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/*
* setupGUI() Method
*
* Sets up the GUI for users to interact with.
*/
public void setupGUI(){
jPanel1 = new javax.swing.JPanel();
jPanel1.setBackground(new java.awt.Color(30, 31, 38));
jPanel1.setForeground(new java.awt.Color(153, 255, 102));
instructions = new JLabel("Group 11 Project - ENSF409");
instructions.setFont(new java.awt.Font("Segoe UI", 0, 17));
instructions.setForeground(new java.awt.Color(0,0,0));
instructions1 = new JLabel("For Option 1: Please put a valid number of clients and click submit. Dont use negative numbers or decimal numbers." );
instructions1.setFont(new java.awt.Font("Segoe UI", 0, 17));
instructions1.setForeground(new java.awt.Color(0,0,0));
instructions2 = new JLabel("For Option 2: Please put valid number of contents for each food group and click sumbmit. Dont use negative numbers or decimal numbers." );
instructions2.setFont(new java.awt.Font("Segoe UI", 0, 17));
instructions2.setForeground(new java.awt.Color(0,0,0));
instructions3 = new JLabel("Welcome! This application will provide you with a Hamper according to your needs. You are required to fill out only one option." );
instructions3.setFont(new java.awt.Font("Segoe UI", 0, 17));
instructions3.setForeground(new java.awt.Color(0,0,0));
option1 = new JLabel("OPTION 1");
option1.setFont(new java.awt.Font("Segoe UI", 0, 17));
option1.setForeground(new java.awt.Color(0,0,0));
mLabel = new JLabel("Males:");
mLabel.setFont(new java.awt.Font("Segoe UI", 0, 14));
mLabel.setForeground(new java.awt.Color(0,0,0));
fLabel = new JLabel("Females:");
fLabel.setFont(new java.awt.Font("Segoe UI", 0, 14));
fLabel.setForeground(new java.awt.Color(0,0,0));
coLabel = new JLabel("Children Over 8:");
coLabel.setFont(new java.awt.Font("Segoe UI", 0, 14));
coLabel.setForeground(new java.awt.Color(0,0,0));
cuLabel = new JLabel("Children Under 8:");
cuLabel.setFont(new java.awt.Font("Segoe UI", 0, 14));
cuLabel.setForeground(new java.awt.Color(0,0,0));
option2 = new JLabel("OPTION 2 \n");
option2.setFont(new java.awt.Font("Segoe UI", 0, 17));
option2.setForeground(new java.awt.Color(0,0,0));
twgLabel = new JLabel("Wheat:");
twgLabel.setFont(new java.awt.Font("Segoe UI", 0, 14));
twgLabel.setForeground(new java.awt.Color(0,0,0));
fvLabel = new JLabel("Fruit Veggies");
fvLabel.setFont(new java.awt.Font("Segoe UI", 0, 14));
fvLabel.setForeground(new java.awt.Color(0,0,0));
proLabel = new JLabel("Protein");
proLabel.setFont(new java.awt.Font("Segoe UI", 0, 14));
proLabel.setForeground(new java.awt.Color(0,0,0));
oLabel = new JLabel("Others");
oLabel.setFont(new java.awt.Font("Segoe UI", 0, 14));
oLabel.setForeground(new java.awt.Color(0,0,0));
cLabel = new JLabel("Calories:");
cLabel.setFont(new java.awt.Font("Segoe UI", 0, 14));
cLabel.setForeground(new java.awt.Color(0,0,0));
mInput = new JTextField("0", 15);
fInput = new JTextField("0", 15);
coInput = new JTextField("0", 15);
cuInput = new JTextField("0", 15);
twgInput = new JTextField("0", 15);
fvInput = new JTextField("0", 15);
proInput = new JTextField("0", 15);
oInput = new JTextField("0", 15);
cInput =new JTextField("0", 15);
mInput.addMouseListener(this);
fInput.addMouseListener(this);
coInput.addMouseListener(this);
cuInput.addMouseListener(this);
twgInput.addMouseListener(this);
fvInput.addMouseListener(this);
proInput.addMouseListener(this);
oInput.addMouseListener(this);
cInput.addMouseListener(this);
JButton submitInfo = new JButton("Submit");
submitInfo.addActionListener(this);
JButton exit = new JButton("Exit");
exit.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
});
JPanel headerPanel = new JPanel();
headerPanel.setLayout(new FlowLayout());
JPanel clientPanel = new JPanel();
clientPanel.setLayout(new BoxLayout(clientPanel, 1));
JPanel submitPanel = new JPanel();
submitPanel.setLayout(new FlowLayout());
JPanel exitPanel = new JPanel();
exitPanel.setLayout(new FlowLayout());
headerPanel.add(instructions);
clientPanel.add(instructions3);
clientPanel.add(instructions1);
clientPanel.add(instructions2);
clientPanel.add(option1);
clientPanel.add(mLabel);
clientPanel.add(mInput);
clientPanel.add(fLabel);
clientPanel.add(fInput);
clientPanel.add(coLabel);
clientPanel.add(coInput);
clientPanel.add(cuLabel);
clientPanel.add(cuInput);
clientPanel.add(option2);
clientPanel.add(twgLabel);
clientPanel.add(twgInput);
clientPanel.add(fvLabel);
clientPanel.add(fvInput);
clientPanel.add(proLabel);
clientPanel.add(proInput);
clientPanel.add(oLabel);
clientPanel.add(oInput);
clientPanel.add(cLabel);
clientPanel.add(cInput);
submitPanel.add(submitInfo);
exitPanel.add(exit);
this.add(headerPanel, BorderLayout.NORTH);
this.add(clientPanel, BorderLayout.CENTER);
this.add(submitPanel, BorderLayout.SOUTH);
this.add(exitPanel, BorderLayout.EAST);
}
/*
* actionPerformed(ActionEvent event) Method
*
* Once the user inputs data and presses submit, the data is assigned
* to the variables we defined above.
*
* Depending on which option the user decided to fill out, the
* corresponding Hamper constructor is called to create an object of
* Hamper.Then an order object is created.
*
* The details of the hamper created or an error messgae is displayed
* depending on wether or not a hamper was able to be created.
*
* There is a popup error message if:
* - both options are filled out,
* - invalid data (e.g. negative numbers) is inputted.
*/
public void actionPerformed(ActionEvent event){
totalMale = Integer.parseInt(mInput.getText());
totalFemale = Integer.parseInt(fInput.getText());
totalChildrenO8 = Integer.parseInt(coInput.getText());
totalChildrenU8 = Integer.parseInt(cuInput.getText());
grainC = Integer.parseInt(twgInput.getText());
fvc = Integer.parseInt(fvInput.getText());
proC = Integer.parseInt(proInput.getText());
OthersC = Integer.parseInt(oInput.getText());
CaloriesC = Integer.parseInt(cInput.getText());
String orders;
String request =null;
int decision = validateInput();
if(decision == 1){
int option = validateOption();
if(option == 1){
Hamper testHamper = new Hamper(totalMale, totalFemale,
totalChildrenO8, totalChildrenU8);
orders = testHamper.getHamper();
request = totalMale + " Males, " + totalFemale + " Females, "
+ totalChildrenO8 + " Children over 8, " + totalChildrenU8 + " Children under 8";
try {
Order orderrr = new Order(request, orders, hamperNumber);
JOptionPane.showMessageDialog(this,orderrr.returnFinalOrder());
hamperNumber ++;
} catch (IOException e) {
e.printStackTrace();
}
}
else if(option == 2){
Hamper testHamper = new Hamper(grainC, fvc, proC,OthersC, CaloriesC);
orders = testHamper.getHamper();
request = grainC + " Wheat Content, " + fvc + " Fruit Veggies Content, "
+ proC + " Protein Content, " + OthersC + " Other Content, " + CaloriesC +
" Calories Content";
try {
Order orderrr = new Order(request, orders, hamperNumber);
JOptionPane.showMessageDialog(this,orderrr.returnFinalOrder());
hamperNumber++;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else{
JOptionPane.showMessageDialog(this,
"Invalid Input: Ummm looks like you filled out both options. Please only fill out the one you need. :(");
}
}
else{
JOptionPane.showMessageDialog(this,
"Invalid Input: Ummm looks like you entered negative number(s) :(");
}
}
/*
* validateInput() Method
*
* Ensures data entered by the user is valid (e.g. not negative values)
*/
public int validateInput(){
if(totalMale < 0 || totalFemale < 0 || totalChildrenU8 < 0 || totalChildrenO8 < 0 ){
return 0;
}
else if(grainC < 0 || fvc < 0 || proC < 0 || OthersC < 0 || CaloriesC < 0 ){
return 0;
}
else{
return 1;
}
}
/*
* validateOption() Method
*
* Ensures user only inputted data for a single option.
*/
public int validateOption(){
if(totalMale ==0 && totalFemale == 0 && totalChildrenU8 == 0 && totalChildrenO8 == 0 ){
return 2;
}
else if(grainC == 0&& fvc == 0 && proC == 0 && OthersC == 0 && CaloriesC == 0 ){
return 1;
}
else{
return 0;
}
}
public void mouseClicked(MouseEvent event){
if(event.getSource().equals(mInput))
mInput.setText("");
if(event.getSource().equals(fInput))
fInput.setText("");
if(event.getSource().equals(coInput))
coInput.setText("");
if(event.getSource().equals(cuInput))
cuInput.setText("");
if(event.getSource().equals(twgInput))
twgInput.setText("");
if(event.getSource().equals(fvInput))
fvInput.setText("");
if(event.getSource().equals(proInput))
proInput.setText("");
if(event.getSource().equals(oInput))
oInput.setText("");
if(event.getSource().equals(cInput))
cInput.setText("");
}
public void mouseEntered(MouseEvent event){
}
public void mouseExited(MouseEvent event){
}
public void mousePressed(MouseEvent event){
}
public void mouseReleased(MouseEvent event){
}
/*
* run() Method
*
* Runs the GUI.
*/
public static void run() {
GUI X = new GUI();
X.setVisible(true);
}
}