-
Notifications
You must be signed in to change notification settings - Fork 1
/
placeorder.java
507 lines (446 loc) · 22.9 KB
/
placeorder.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* 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 Inventory;
import java.sql.Connection;
import java.util.Random;
import java.sql.DriverManager;
import java.sql.CallableStatement;
import java.sql.Types;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author olirv
*/
public class placeorder extends javax.swing.JFrame {
Connection con;
Statement st;
PreparedStatement ps;
ResultSet rs;
String oid;
String cid;
/**
* Creates new form placeorder
*/
public placeorder(String inp_cid) {
cid = inp_cid;
initComponents();
try{
Class.forName("oracle.jdbc.OracleDriver");
//JOptionPane.showMessageDialog(this,"Driver Loaded!");
try {
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL",
"scott","tiger");
//JOptionPane.showMessageDialog(this,"Connected to Oracle database!");
}
catch (SQLException ex) {
Logger.getLogger(cancelorder.class.getName()).log(Level.SEVERE,null, ex);
JOptionPane.showMessageDialog(this,ex.getMessage());
}
}
catch(ClassNotFoundException ex){
Logger.getLogger(cancelorder.class.getName()).log(Level.SEVERE,null, ex);
JOptionPane.showMessageDialog(this,ex.getMessage());
}
try{
try{
// Prepare the statement to check if the order ID exists
PreparedStatement checkStatement = con.prepareStatement("SELECT * FROM orders WHERE orderid = ?");
do {
// Generate a random two-digit number
oid = "O"+(new Random().nextInt(90) + 10); // Generates a number between 10 and 99 (inclusive)
// Set the generated order ID in the prepared statement
checkStatement.setString(1, oid);
// Execute the query to check if the order ID exists
ResultSet checkResult = checkStatement.executeQuery();
// Check if the order ID exists in the orders table
if (!checkResult.next()) {
break;
}
} while (true);
} catch (SQLException e) {
JOptionPane.showMessageDialog(this,"Error in generating Order ID");
}
String query = "INSERT INTO ORDERS VALUES ( ? , ? , null , null , null , null )";
PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, oid);
statement.setString(2, cid);
statement.executeQuery();
}
catch (Exception ex) {
// Logger.getLogger(cancelorder.class.getName()).log(Level.SEVERE,null, ex);
JOptionPane.showMessageDialog(this,"Error in Initialising your Order");
}
oid_display.setText ("Your Order ID : "+ oid);
}
/**
* 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() {
pid = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
quantity = new javax.swing.JTextField();
add_to_cart = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
oid_display = new javax.swing.JLabel();
totcost = new javax.swing.JTextField();
jButton4 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Place Order");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
pid.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
jLabel1.setText("Enter product ID");
jLabel1.addContainerListener(new java.awt.event.ContainerAdapter() {
public void componentAdded(java.awt.event.ContainerEvent evt) {
jLabel1ComponentAdded(evt);
}
});
jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
jLabel2.setText("Enter quantity");
quantity.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
add_to_cart.setText("Add to cart");
add_to_cart.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
add_to_cartMouseClicked(evt);
}
});
add_to_cart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
add_to_cartActionPerformed(evt);
}
});
jButton1.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
jButton1.setText("Back");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Remove from cart");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
jButton3.setText("Check Out");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
oid_display.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
oid_display.setForeground(new java.awt.Color(204, 102, 255));
oid_display.setText("Your Order ID :");
oid_display.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
oid_displayMouseClicked(evt);
}
});
totcost.setEditable(false);
totcost.setBackground(new java.awt.Color(204, 153, 255));
totcost.setFont(new java.awt.Font("OCR A Extended", 0, 14)); // NOI18N
totcost.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
totcostActionPerformed(evt);
}
});
jButton4.setText("Clear");
jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton4MouseClicked(evt);
}
});
jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Inventory/logo.png"))); // NOI18N
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(jLabel3)
.addGap(151, 151, 151))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(193, 193, 193)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(78, 78, 78)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)))
.addGroup(layout.createSequentialGroup()
.addGap(170, 170, 170)
.addComponent(jButton4)))
.addGap(15, 15, 15)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(quantity, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(pid, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addComponent(add_to_cart, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(234, 234, 234)
.addComponent(jButton3))
.addGroup(layout.createSequentialGroup()
.addGap(106, 106, 106)
.addComponent(totcost, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGap(318, 318, 318)
.addComponent(oid_display))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jLabel4)))
.addContainerGap(268, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(oid_display)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(pid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(quantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(109, 109, 109)
.addComponent(jButton4)))
.addGap(50, 50, 50)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(add_to_cart)
.addComponent(jButton2))
.addGap(43, 43, 43)
.addComponent(totcost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton3))
.addGap(52, 52, 52)
.addComponent(jLabel4)
.addGap(45, 45, 45)
.addComponent(jLabel3)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jLabel1ComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_jLabel1ComponentAdded
// TODO add your handling code here:
}//GEN-LAST:event_jLabel1ComponentAdded
private void add_to_cartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add_to_cartActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_add_to_cartActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:^
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton3ActionPerformed
private void add_to_cartMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_add_to_cartMouseClicked
String productID = pid.getText();
int enteredQuantity = Integer.parseInt(quantity.getText());
try {
// Check if max_quantity >= (max_quantity + enteredQuantity) in inventory table
String query = "SELECT SID FROM inventory WHERE pid = ? ";
PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, productID);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
try{
query = "SELECT SID FROM inventory WHERE pid = ? AND quantity >= ? ";
statement = con.prepareStatement(query);
statement.setString(1, productID);
statement.setString(2, quantity.getText());
resultSet = statement.executeQuery();
if (resultSet.next())
{
query = "INSERT INTO Ordered_item VALUES ( ? , ? , ? )";
statement = con.prepareStatement(query);
statement.setString(1, oid);
statement.setString(2, productID);
statement.setString(3, quantity.getText());
statement.executeQuery();
JOptionPane.showMessageDialog(this, "Added to cart!");
try{
String sql = "{ ? = call calculate_order_price(?) }";
CallableStatement cstmt = con.prepareCall(sql);
// Set the input parameters
cstmt.setString(2, oid);
// Register the output parameter
cstmt.registerOutParameter(1, Types.INTEGER);
cstmt.execute();
// Get the result from the output parameter
String result = cstmt.getString(1);
totcost.setText("Total price : "+result);
}
catch (SQLException ex) {
Logger.getLogger(supplier.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "Error in total price calculation!" + ex.getMessage());
}
}
}
catch (SQLException ex) {
Logger.getLogger(supplier.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "Sufficient quantity is not present!" + ex.getMessage());
}
} else {
// Product ID not found in inventory table
JOptionPane.showMessageDialog(this, "Product ID not found in inventory");
}
} catch (SQLException ex) {
Logger.getLogger(supplier.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "Product ID not found in inventory.");
}
}//GEN-LAST:event_add_to_cartMouseClicked
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
this.dispose();
new checkout(cid, oid).setVisible(true);// TODO Fadd your handling code here:
}//GEN-LAST:event_jButton3MouseClicked
private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
}//GEN-LAST:event_formWindowOpened
private void totcostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_totcostActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_totcostActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
// TODO add your handling code here:
try
{
String sql = "delete from ordered_item where PID = ? and OrderId = ?";
ps = con.prepareStatement(sql);
ps.setString(1, pid.getText().trim());
ps.setString(2, oid);
ps.execute();
JOptionPane.showMessageDialog(this,"Removed from Cart!");
try{
sql = "{ ? = call calculate_order_price(?) }";
CallableStatement cstmt = con.prepareCall(sql);
// Set the input parameters
cstmt.setString(2, oid);
// Register the output parameter
cstmt.registerOutParameter(1, Types.INTEGER);
cstmt.execute();
// Get the result from the output parameter
String result = cstmt.getString(1);
totcost.setText("Total price : "+result);
}
catch (SQLException ex) {
Logger.getLogger(supplier.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "Error in total price calculation!" + ex.getMessage());
}
}
catch (SQLException ex)
{
Logger.getLogger(placeorder.class.getName()).log(Level.SEVERE,null, ex);
JOptionPane.showMessageDialog(this,ex.getMessage());
}
// TODO add your handling code here:
}//GEN-LAST:event_jButton2MouseClicked
private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
pid.setText("");
quantity.setText(""); // TODO add your handling code here:
}//GEN-LAST:event_jButton4MouseClicked
private void oid_displayMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_oid_displayMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_oid_displayMouseClicked
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
this.dispose();
}//GEN-LAST:event_jButton1MouseClicked
/**
* @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(placeorder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(placeorder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(placeorder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(placeorder.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 placeorder("C01").setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton add_to_cart;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel oid_display;
private javax.swing.JTextField pid;
private javax.swing.JTextField quantity;
private javax.swing.JTextField totcost;
// End of variables declaration//GEN-END:variables
}