-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMealGUI.java
More file actions
378 lines (356 loc) · 17.3 KB
/
MealGUI.java
File metadata and controls
378 lines (356 loc) · 17.3 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
package PAT;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
/*
Class MealGUI
Front end of the Meal class and provides the user's with an interface to see what ingredients
are needed for a specific recipe
*/
public class MealGUI extends javax.swing.JFrame {
String userWelcome = ""; //Welcome message displayed in the Main GUI
String userType = ""; //The type of the current user
Meal meal = new Meal(); //An instance of the meal class
ResultSet rs; //resultset to store the data gathered by the meal class
public MealGUI(String _userType, String _userWelcome) {
userType = _userType;
userWelcome = _userWelcome;
initComponents();
//enter the recipe names into the combo box so the user can select it
populateComboBox();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
btnGroupMealType = new javax.swing.ButtonGroup();
btnMenu = new javax.swing.JButton();
lblStockGUI = new javax.swing.JLabel();
btnStockHelp = new javax.swing.JButton();
lblMealType = new javax.swing.JLabel();
radBtnBreakfast = new javax.swing.JRadioButton();
radBtnLunch = new javax.swing.JRadioButton();
radBtnDinner = new javax.swing.JRadioButton();
lblRecipe = new javax.swing.JLabel();
cmbRecipeName = new javax.swing.JComboBox<>();
btnStartMeal = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblPrep = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnMenu.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
btnMenu.setText("BACK");
btnMenu.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
btnMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnMenuActionPerformed(evt);
}
});
lblStockGUI.setBackground(new java.awt.Color(255, 255, 255));
lblStockGUI.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
lblStockGUI.setText(" Meal");
lblStockGUI.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
btnStockHelp.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
btnStockHelp.setText("Help");
btnStockHelp.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
btnStockHelp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnStockHelpActionPerformed(evt);
}
});
lblMealType.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblMealType.setText("Type: ");
btnGroupMealType.add(radBtnBreakfast);
radBtnBreakfast.setSelected(true);
radBtnBreakfast.setText("Breakfast");
radBtnBreakfast.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radBtnBreakfastActionPerformed(evt);
}
});
btnGroupMealType.add(radBtnLunch);
radBtnLunch.setText("Lunch");
radBtnLunch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radBtnLunchActionPerformed(evt);
}
});
btnGroupMealType.add(radBtnDinner);
radBtnDinner.setText("Dinner");
radBtnDinner.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radBtnDinnerActionPerformed(evt);
}
});
lblRecipe.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblRecipe.setText("Recipe:");
btnStartMeal.setText("Start Meal");
btnStartMeal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnStartMealActionPerformed(evt);
}
});
tblPrep.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null}
},
new String [] {
"Item ID", "Name", "Amount"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jScrollPane1.setViewportView(tblPrep);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(lblStockGUI, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnStockHelp, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblMealType)
.addComponent(lblRecipe))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(radBtnBreakfast)
.addGap(18, 18, 18)
.addComponent(radBtnLunch))
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(cmbRecipeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(radBtnDinner)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(btnStartMeal)
.addGap(188, 188, 188))))
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnStockHelp, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblStockGUI, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblMealType)
.addComponent(radBtnBreakfast)
.addComponent(radBtnLunch)
.addComponent(radBtnDinner))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblRecipe)
.addComponent(cmbRecipeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(btnStartMeal)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
/*
Method of the back button
Returns to the Main GUI and closes the Meal GUI
*/
private void btnMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMenuActionPerformed
dispose();
MainGUI mai = new MainGUI(userType, userWelcome);
mai.setVisible(true);
}//GEN-LAST:event_btnMenuActionPerformed
/*
Method of the Help button
Opens the help text
*/
private void btnStockHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStockHelpActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_btnStockHelpActionPerformed
/*
Method of the Lunch radio button
Updates the combo box with only recipes of the lunch type
*/
private void radBtnLunchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radBtnLunchActionPerformed
populateComboBox();
}//GEN-LAST:event_radBtnLunchActionPerformed
/*
Method of the Breakfast radio button
Updates the combo box with only recipes of the breakfast type
*/
private void radBtnBreakfastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radBtnBreakfastActionPerformed
populateComboBox();
}//GEN-LAST:event_radBtnBreakfastActionPerformed
/*
Method of the Dinner radio button
Updates the combo box with only recipes of the dinner type
*/
private void radBtnDinnerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radBtnDinnerActionPerformed
populateComboBox();
}//GEN-LAST:event_radBtnDinnerActionPerformed
/*
Method of the start meal button
Determines the amount of certain ingredients needed for a recipe
and displays it to the user
*/
private void btnStartMealActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartMealActionPerformed
String ID = meal.getRecipeID((String)cmbRecipeName.getSelectedItem());
updatePrepTable(ID);
}//GEN-LAST:event_btnStartMealActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MealGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MealGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MealGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MealGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MealGUI("","").setVisible(true);
}
});
}
/*
Method populateComboBox
Checks what recipe type is selected and then populates the combo box with
only recipes of that type
@parameters: none
@return: none
*/
private void populateComboBox(){
clearPrepTable();
cmbRecipeName.removeAllItems();
try {
//Check what recipe type is selected
String rType = selectedType();
//Get all the recipes of that type from the meal class
rs = meal.getRecipeNames(rType);
while(rs.next()){
//add all those recipes to the combo box as options
cmbRecipeName.addItem(rs.getString("recipeName"));
}
} catch (SQLException ex) {
Logger.getLogger(MealGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
/*
Method selectedType
Sees which radio button has been selected
@parameters: none
@return: String recipe type
*/
private String selectedType(){
String rType = "";
if(radBtnBreakfast.isSelected()){
rType = radBtnBreakfast.getText();
}else if(radBtnLunch.isSelected()){
rType = radBtnLunch.getText();
}else if(radBtnDinner.isSelected()){
rType = radBtnDinner.getText();
}
return rType;
}
/*
Method clearPrepTable
Clears the prep table of all its current info
@parameters: none
@return: none
*/
private void clearPrepTable(){
DefaultTableModel model = (DefaultTableModel)tblPrep.getModel();
while(model.getRowCount()> 0){
model.removeRow(0);
}
}
/*
Method updatePrepTable
Gets the ingredients needed for a recipe and displays it to the user
@parameters: String recipe ID
@return: none
*/
private void updatePrepTable(String _id){
clearPrepTable();
try {
//Get ingredients needed for recipe
rs = meal.getIngredients(_id);
while(rs.next()){
//extract ingredient info from result
String ID = rs.getString("itemID");
String itemName = meal.getItemName(ID);
int amountPerAth = rs.getInt("inQuantity");
//Determine the total amount for each ingredient needed for the recipe
String totalAmount = String.valueOf(amountPerAth*meal.getAthleteAmount());
String data[] = {ID, itemName, totalAmount};
DefaultTableModel model = (DefaultTableModel)tblPrep.getModel();
//add ingredient and amount to the table
model.addRow(data);
}
} catch (SQLException ex) {
Logger.getLogger(MealGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.ButtonGroup btnGroupMealType;
private javax.swing.JButton btnMenu;
private javax.swing.JButton btnStartMeal;
private javax.swing.JButton btnStockHelp;
private javax.swing.JComboBox<String> cmbRecipeName;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lblMealType;
private javax.swing.JLabel lblRecipe;
private javax.swing.JLabel lblStockGUI;
private javax.swing.JRadioButton radBtnBreakfast;
private javax.swing.JRadioButton radBtnDinner;
private javax.swing.JRadioButton radBtnLunch;
private javax.swing.JTable tblPrep;
// End of variables declaration//GEN-END:variables
}