-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupplier.java
301 lines (271 loc) · 14.3 KB
/
supplier.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
/*
* 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 javax.swing.*;
import java.sql.*;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
/**
*
* @author olirv
*/
public class supplier extends javax.swing.JFrame {
/**
* Creates new form supplier
*/
Connection con;
Statement st;
PreparedStatement ps;
ResultSet rs;
String sid;
/**
* Creates new form cancelorder
*/
public supplier(String inp_sid) {
sid = inp_sid;
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());
}
}
/**
* 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() {
icon = new javax.swing.JLabel();
jlabel1 = new javax.swing.JLabel();
pid = new javax.swing.JTextField();
jlabel2 = new javax.swing.JLabel();
quantity = new javax.swing.JTextField();
supply = new javax.swing.JButton();
back = new javax.swing.JButton();
home = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Supplier - Supply");
setBackground(new java.awt.Color(51, 255, 51));
setForeground(java.awt.Color.cyan);
icon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Inventory/supplier.png"))); // NOI18N
jlabel1.setFont(new java.awt.Font("Gill Sans MT", 0, 18)); // NOI18N
jlabel1.setText("Enter Product ID");
jlabel2.setFont(new java.awt.Font("Gill Sans MT", 0, 18)); // NOI18N
jlabel2.setText("Enter Quantity");
supply.setFont(new java.awt.Font("Gill Sans MT", 0, 18)); // NOI18N
supply.setText("Supply");
supply.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
supplyMouseClicked(evt);
}
});
back.setText("Back");
back.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
backMouseClicked(evt);
}
});
back.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
backActionPerformed(evt);
}
});
home.setText("Home");
home.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
homeMouseClicked(evt);
}
});
jLabel1.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(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, 686, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jlabel1)
.addComponent(jlabel2))
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(quantity, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(pid, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel1)))
.addGroup(layout.createSequentialGroup()
.addGap(127, 127, 127)
.addComponent(supply))))
.addGroup(layout.createSequentialGroup()
.addComponent(home)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(back)))
.addContainerGap(116, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(jLabel1)
.addGap(93, 93, 93)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jlabel1)
.addComponent(pid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(67, 67, 67)
.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))
.addGap(89, 89, 89)
.addComponent(supply)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(home)
.addComponent(back))
.addContainerGap(64, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void supplyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_supplyMouseClicked
String productID = pid.getText();
int enteredQuantity = Integer.parseInt(quantity.getText());
try {
// Check if max_quantity >= (max_quantity + enteredQuantity) in inventory table
String query = "SELECT maxquantity FROM inventory WHERE pid = ?";
PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, productID);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
int maxQuantity = resultSet.getInt("maxquantity");
if (maxQuantity < (Integer.parseInt(quantity.getText()) + enteredQuantity)) {
JOptionPane.showMessageDialog(this, "Insufficient space in inventory.");
} else {
try {
// Check if product with the given pid and sid exists in inventory table
String selectQuery = "SELECT * FROM inventory WHERE pid = ? AND sid = ?";
PreparedStatement selectStatement = con.prepareStatement(selectQuery);
selectStatement.setString(1, productID);
selectStatement.setString(2, sid);
ResultSet resultSet1 = selectStatement.executeQuery();
if (resultSet1.next()) {
int currentQuantity = resultSet1.getInt("quantity");
int newQuantity = currentQuantity + enteredQuantity;
// Update the quantity for the existing entry
String updateQuery = "UPDATE inventory SET quantity = ? WHERE pid = ? AND sid = ?";
PreparedStatement updateStatement = con.prepareStatement(updateQuery);
updateStatement.setInt(1, newQuantity);
updateStatement.setString(2, productID);
updateStatement.setString(3, sid);
updateStatement.executeUpdate();
JOptionPane.showMessageDialog(this, "Quantity updated in inventory.");
} else {
// Insert a new entry into the inventory table
String insertQuery = "INSERT INTO inventory VALUES (?, ?, ?, ?)";
PreparedStatement insertStatement = con.prepareStatement(insertQuery);
insertStatement.setString(1, productID);
insertStatement.setString(2, sid);
insertStatement.setInt(3, enteredQuantity);
insertStatement.setInt(4, 0); // Assuming max_quantity is initialized to 0
insertStatement.executeUpdate();
JOptionPane.showMessageDialog(this, "New entry inserted into inventory.");
}
} catch (SQLException ex) {
Logger.getLogger(supplier.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "An error occurred while updating inventory: " + 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, "An error occurred while checking inventory: " + ex.getMessage());
}
}//GEN-LAST:event_supplyMouseClicked
private void homeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_homeMouseClicked
this.dispose();
new home().setVisible(true); // TODO add your handling code here:
}//GEN-LAST:event_homeMouseClicked
private void backMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_backMouseClicked
this.dispose();
new supplier_entry().setVisible(true);
}//GEN-LAST:event_backMouseClicked
private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_backActionPerformed
/**
* @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(supplier.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(supplier.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(supplier.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(supplier.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 supplier("").setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton back;
private javax.swing.JButton home;
private javax.swing.JLabel icon;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jlabel1;
private javax.swing.JLabel jlabel2;
private javax.swing.JTextField pid;
private javax.swing.JTextField quantity;
private javax.swing.JButton supply;
// End of variables declaration//GEN-END:variables
}