-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithdraw_Panel.java
More file actions
338 lines (290 loc) · 14 KB
/
Withdraw_Panel.java
File metadata and controls
338 lines (290 loc) · 14 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package banking_app;
import java.awt.event.KeyEvent;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.swing.JOptionPane;
/**
*
* @author CLEMENT O. A-U
*/
public class Withdraw_Panel extends javax.swing.JPanel {
Connection conn;
PreparedStatement ps;
String url = "jdbc:mysql://localhost:3306/banking_app";
String user = "root";
String pass = "";
/**
* Creates new form Withdraw_Panel
*/
public Withdraw_Panel() {
initComponents();
doConn();
}
public final void doConn() {
try {
conn = DriverManager.getConnection(url, user, pass);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
AccountNumber = new javax.swing.JTextField();
Withdrawal_Amount = new javax.swing.JTextField();
Narration = new javax.swing.JTextField();
Account_Bal = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
withdrawButton = new javax.swing.JButton();
AccountName = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setBackground(new java.awt.Color(255, 255, 153));
jLabel1.setFont(new java.awt.Font("Myanmar Text", 1, 18)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Withdraw");
add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(33, 24, 100, -1));
jLabel2.setForeground(new java.awt.Color(255, 255, 204));
jLabel2.setText("Account Number");
add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 134, 90, 20));
jLabel3.setForeground(new java.awt.Color(255, 255, 204));
jLabel3.setText("Withdraw Amount");
add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 180, 90, 20));
jLabel4.setForeground(new java.awt.Color(255, 255, 204));
jLabel4.setText("Narration");
add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 230, 80, 20));
AccountNumber.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
AccountNumberFocusLost(evt);
}
});
AccountNumber.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AccountNumberActionPerformed(evt);
}
});
AccountNumber.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
AccountNumberKeyPressed(evt);
}
public void keyReleased(java.awt.event.KeyEvent evt) {
AccountNumberKeyReleased(evt);
}
});
add(AccountNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 130, 102, -1));
add(Withdrawal_Amount, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 180, 102, -1));
add(Narration, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 233, 102, 20));
Account_Bal.setEditable(false);
Account_Bal.setBackground(new java.awt.Color(102, 102, 102));
Account_Bal.setForeground(new java.awt.Color(255, 255, 255));
Account_Bal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Account_BalActionPerformed(evt);
}
});
add(Account_Bal, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 290, 102, -1));
jLabel5.setForeground(new java.awt.Color(255, 255, 204));
jLabel5.setText("Account Balance");
add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 290, 84, 20));
withdrawButton.setText("Withdraw");
withdrawButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
withdrawButtonActionPerformed(evt);
}
});
add(withdrawButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 350, -1, -1));
AccountName.setBackground(new java.awt.Color(51, 0, 51));
AccountName.setForeground(new java.awt.Color(255, 255, 255));
AccountName.setEnabled(false);
add(AccountName, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 110, 138, 40));
jLabel7.setBackground(new java.awt.Color(255, 255, 153));
jLabel7.setFont(new java.awt.Font("Myanmar Text", 1, 14)); // NOI18N
jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/banking_app/images/6.jpg"))); // NOI18N
add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(1, 3, 560, 460));
}// </editor-fold>//GEN-END:initComponents
private void withdrawButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_withdrawButtonActionPerformed
// TODO add your handling code here:
String acct_no = AccountNumber.getText();
String selectData = "select acct_no, acct_name, acct_balance from account where acct_no = ? "
+ "order by trans_date limit 1";
try {
if (acct_no.equals("")) {
JOptionPane.showMessageDialog(this, "Account Number Field is Empty");
} else {
ps = conn.prepareStatement(selectData);
ps.setString(1, acct_no);
ResultSet rs = ps.executeQuery();
if (rs.next() ) {
Double withdrawal = checkForNullOrString(Withdrawal_Amount.getText());
Double cur_bal = checkForNullOrString2(Account_Bal.getText());
Double newAmount = 0.0;
if (withdrawal > 0 || withdrawal < cur_bal) {
newAmount = cur_bal - withdrawal;
} else {
JOptionPane.showMessageDialog(this, "Entered Withdrawal Amount is not allowed");
newAmount = cur_bal - 0;
}
String insert = "insert into account values(null, ?, ?, ?, ?, ?, ?, ?)";
String tacct_no = AccountNumber.getText();
String tacct_type = "";
if (tacct_no.startsWith("01")) {
tacct_type = "Current";
} else if (tacct_no.startsWith("02")) {
tacct_type = "Savings";
}
String tacct_name = AccountName.getText();
String trans_type = "W - Withdrawal";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-DD HH-MM-SS");
LocalDateTime trans_date = LocalDateTime.now();
try {
if (tacct_no.equals("") || tacct_name.equals("") || Withdrawal_Amount.getText().equals("")
|| Account_Bal.getText().equals("") || withdrawal < 0 || withdrawal > cur_bal) {
JOptionPane.showMessageDialog(this, "Invalid Entry! Withdrawal cancelled");
} else {
ps = conn.prepareStatement(insert, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ps.setString(1, tacct_no);
ps.setString(2, tacct_name);
ps.setString(3, tacct_type);
ps.setDouble(4, withdrawal);
ps.setString(5, trans_type);
ps.setDouble(6, newAmount);
ps.setString(7, trans_date.toString());
ps.executeUpdate();
JOptionPane.showMessageDialog(this, "Withdrawal has been made from the Account " + tacct_no);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_withdrawButtonActionPerformed
private void Account_BalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Account_BalActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_Account_BalActionPerformed
private void AccountNumberKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_AccountNumberKeyReleased
// TODO add your handling code here:
}//GEN-LAST:event_AccountNumberKeyReleased
private void AccountNumberKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_AccountNumberKeyPressed
// TODO add your handling code here:
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
// TODO add your handling code here:
String tacct_no = AccountNumber.getText();
String select = "Select acct_no, acct_name, acct_balance from account where"
+ " acct_no = ? order by trans_date desc limit 1";
try {
ps = conn.prepareStatement(select);
ps.setString(1, tacct_no);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
//
//Note that we don't use the while loop while (rs.next() ) { } inside the if statement anymore as it has alraedy moved the cursor to the other row and we did
//already limit the number of rows to just 1. Thus the while loop had nothing to show. Had we included the
//while loop we wouldn't have been getting a value returned to our Account Name Label!
getAcctName(rs, "acct_no");
// }
} else {
AccountNumber.setText("");
Account_Bal.setText("");
JOptionPane.showMessageDialog(null, "Account Not Found");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}//GEN-LAST:event_AccountNumberKeyPressed
private void AccountNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AccountNumberActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_AccountNumberActionPerformed
private void AccountNumberFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_AccountNumberFocusLost
// TODO add your handling code here:
String tacct_no = AccountNumber.getText();
String select = "Select acct_no, acct_name, acct_balance from account where"
+ " acct_no = ? order by trans_date desc limit 1";
try {
ps = conn.prepareStatement(select);
ps.setString(1, tacct_no);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
getAcctName(rs, "acct_no");
// }
} else {
AccountNumber.setText("");
Account_Bal.setText("");
JOptionPane.showMessageDialog(null, "Account Not Found");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_AccountNumberFocusLost
public double checkForNullOrString(String value) {
if (value.equals("") && value.length() == 0) {
return 0;
} else {
try {
double dValue = Double.parseDouble(value);
return dValue;
} catch (NumberFormatException nf) {
JOptionPane.showMessageDialog(null, "Invalid Entry! Check the Withdrawal Amount");
double dvalue = 0;
return dvalue;
}
}
}
public double checkForNullOrString2(String value) {
if (value.equals("") && value.length() == 0) {
return 0;
} else {
try {
double dValue = Double.parseDouble(value);
return dValue;
} catch (NumberFormatException nf) {
JOptionPane.showMessageDialog(null, "Invalid Entry! Check the Current Balance");
double dvalue = 0;
return dvalue;
}
}
}
public void getAcctName(ResultSet rst, String str) {
try {
String AcctNo = rst.getString(str);
if (AccountNumber.getText().equals(AcctNo)) {
AccountName.setText(rst.getString("acct_name"));
Account_Bal.setText(Double.toString(rst.getDouble("acct_balance")));
}
} catch (SQLException EX) {
EX.printStackTrace();
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel AccountName;
private javax.swing.JTextField AccountNumber;
private javax.swing.JTextField Account_Bal;
private javax.swing.JTextField Narration;
private javax.swing.JTextField Withdrawal_Amount;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel7;
private javax.swing.JButton withdrawButton;
// End of variables declaration//GEN-END:variables
}