-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMenuFrame.java
305 lines (270 loc) · 13.1 KB
/
MenuFrame.java
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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
//package com.mycompany.food_delivery;
package com.mycompany.food_delivery;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;
import javax.swing.ToolTipManager;
import java.util.ArrayList;
import java.util.List;
import com.mycompany.food_delivery.CartFramee;
/**
*
* @author karpa
*/
public class MenuFrame extends javax.swing.JFrame {
private String restaurantName;
private Customer prevFrame;
String custID;
String restID;
private String[][] menuData;
private String[] columnNames = {"Item Name", "Price"};
private List<String[]> cartItems;
/**
* Creates new form MenuFrame
*/
public MenuFrame(){
initComponents();
}
public MenuFrame(String[][] menuData, String restaurantName, Customer prevFrame, String custID, String restID) {
initComponents();
this.restaurantName = restaurantName;
this.prevFrame = prevFrame;
this.menuData = menuData;
this.custID = custID;
this.restID = restID;
this.cartItems = new ArrayList<>();
setTitle(restaurantName + " Menu");
displayRestaurantName();
populateMenuTable();
}
private void displayRestaurantName() {
// Assuming you have a jLabelRestName JLabel component to display the restaurant name
jLabelRestName.setText(restaurantName + "Menu");
}
private void populateMenuTable() {
DefaultTableModel model = new DefaultTableModel(menuData, columnNames);
menuTable.setModel(model);
// Set tooltips for the table
ToolTipManager.sharedInstance().registerComponent(menuTable);
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
Back = new javax.swing.JButton();
jLabelRestName = new java.awt.Label();
jScrollPane2 = new javax.swing.JScrollPane();
menuTable = new javax.swing.JTable();
addToCartButton = new javax.swing.JButton();
opencart = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
Back.setText("Back");
Back.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BackActionPerformed(evt);
}
});
jLabelRestName.setText("label1");
menuTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"Item Name", "Price"
}
) {
boolean[] canEdit = new boolean [] {
false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
menuTable.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
menuTableMouseMoved(evt);
}
});
jScrollPane2.setViewportView(menuTable);
addToCartButton.setText("Add to cart");
addToCartButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addToCartButtonActionPerformed(evt);
}
});
opencart.setText("Open Cart");
opencart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
opencartActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(51, 51, 51)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(addToCartButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
.addComponent(opencart)
.addGap(42, 42, 42)
.addComponent(Back, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(83, 83, 83))
.addComponent(jLabelRestName, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(42, 42, 42)
.addComponent(jLabelRestName, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(37, 37, 37)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(addToCartButton)
.addComponent(Back, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(opencart))
.addContainerGap(89, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackActionPerformed
// TODO add your handling code here:
dispose(); // Close the current frame
prevFrame.setVisible(true);
}//GEN-LAST:event_BackActionPerformed
private void menuTableMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuTableMouseMoved
// TODO add your handling code here:
int row = menuTable.rowAtPoint(evt.getPoint());
int column = menuTable.columnAtPoint(evt.getPoint());
if (row > -1 && column == 0) {
menuTable.setToolTipText(menuData[row][2]); // Assuming the description is in the third column of menuData
} else {
menuTable.setToolTipText(null);
}
}//GEN-LAST:event_menuTableMouseMoved
private void addToCartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addToCartButtonActionPerformed
// TODO add your handling code here:
int selectedRow = menuTable.getSelectedRow();
if (selectedRow != -1) {
String itemName = (String) menuTable.getValueAt(selectedRow, 0);
String price = (String) menuTable.getValueAt(selectedRow, 1);
String[] newItem = {itemName, price, "1", price};
addItemToCart(newItem);
}
}//GEN-LAST:event_addToCartButtonActionPerformed
private void opencartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_opencartActionPerformed
// TODO add your handling code here:
CartFramee cartFrame = new CartFramee(cartItems, this, this.custID, this.restID, prevFrame);
cartFrame.setVisible(true);
}//GEN-LAST:event_opencartActionPerformed
private void addItemToCart(String[] newItem) {
boolean itemExists = false;
for (String[] item : cartItems) {
if (item[0].equals(newItem[0])) {
int quantity = Integer.parseInt(item[2]) + 1;
item[2] = String.valueOf(quantity);
item[3] = String.valueOf(Double.parseDouble(item[1]) * quantity);
itemExists = true;
break;
}
}
if (!itemExists) {
cartItems.add(newItem);
}
CartFramee cartFrame = new CartFramee(cartItems, this, this.custID, this.restID, prevFrame);
cartFrame.setVisible(true);
}
/**
* @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(MenuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MenuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MenuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MenuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
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(MenuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MenuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MenuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MenuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MenuFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton Back;
private javax.swing.JButton addToCartButton;
private java.awt.Label jLabelRestName;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable menuTable;
private javax.swing.JButton opencart;
// End of variables declaration//GEN-END:variables
}