-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransfer_Panel.java
More file actions
450 lines (401 loc) · 19.4 KB
/
Transfer_Panel.java
File metadata and controls
450 lines (401 loc) · 19.4 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
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
/*
* 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 Transfer_Panel extends javax.swing.JPanel {
Connection conn;
PreparedStatement ps, pst;
ResultSet res, rest;
String url = "jdbc:mysql://localhost:3306/banking_app";
String user = "root";
String pwd = "";
Double destCur_bal;
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-DD HH-MM-SS");
/**
* Creates new form Transfer_Panel
*/
public Transfer_Panel() {
initComponents();
dbConn();
}
public final void dbConn() {
try {
conn = DriverManager.getConnection(url, user, pwd);
} 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() {
DestAccountNumber = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
AcctName = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
currentBalance = new javax.swing.JLabel();
AccountNumber = new javax.swing.JTextField();
Transfer_Amount = new javax.swing.JTextField();
currentAcctBal = new javax.swing.JTextField();
destAcctName = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
DestAccountNumber.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
DestAccountNumberFocusLost(evt);
}
});
DestAccountNumber.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DestAccountNumberActionPerformed(evt);
}
});
DestAccountNumber.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
DestAccountNumberKeyPressed(evt);
}
});
add(DestAccountNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(152, 213, 132, -1));
jLabel5.setText("Destination Account");
add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(19, 213, -1, 20));
jButton1.setFont(new java.awt.Font("Microsoft YaHei", 1, 14)); // NOI18N
jButton1.setForeground(new java.awt.Color(102, 102, 102));
jButton1.setText("Transfer");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(162, 296, -1, -1));
AcctName.setEnabled(false);
add(AcctName, new org.netbeans.lib.awtextra.AbsoluteConstraints(302, 55, 153, 43));
jLabel1.setBackground(new java.awt.Color(204, 204, 204));
jLabel1.setFont(new java.awt.Font("Calisto MT", 1, 14)); // NOI18N
jLabel1.setForeground(new java.awt.Color(102, 102, 102));
jLabel1.setText("Transfer");
add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(31, 15, -1, -1));
jLabel2.setText("Account Number");
add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(19, 81, -1, -1));
jLabel3.setText("Transfer Amount");
add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(19, 126, -1, -1));
currentBalance.setText("Current Acct Balance");
add(currentBalance, new org.netbeans.lib.awtextra.AbsoluteConstraints(19, 178, -1, -1));
AccountNumber.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
AccountNumberFocusLost(evt);
}
});
AccountNumber.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
AccountNumberKeyPressed(evt);
}
});
add(AccountNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(152, 78, 102, -1));
add(Transfer_Amount, new org.netbeans.lib.awtextra.AbsoluteConstraints(152, 123, 102, -1));
currentAcctBal.setEditable(false);
currentAcctBal.setBackground(new java.awt.Color(153, 153, 153));
currentAcctBal.setForeground(new java.awt.Color(255, 255, 255));
add(currentAcctBal, new org.netbeans.lib.awtextra.AbsoluteConstraints(152, 175, 102, -1));
destAcctName.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
add(destAcctName, new org.netbeans.lib.awtextra.AbsoluteConstraints(302, 187, 153, 46));
jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/banking_app/images/background2.jpg"))); // NOI18N
add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 490, 420));
}// </editor-fold>//GEN-END:initComponents
private void DestAccountNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DestAccountNumberActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_DestAccountNumberActionPerformed
private void AccountNumberKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_AccountNumberKeyPressed
// TODO add your handling code here:
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
String select = "select acct_no, acct_name, acct_balance from account where acct_no = ? "
+ "order by trans_date desc limit 1";
String tacct_no = AccountNumber.getText();
try {
ps = conn.prepareStatement(select);
ps.setString(1, tacct_no);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
getAcctName(rs, "acct_no");
} else {
int choice = JOptionPane.showConfirmDialog(null, "Account Not Found. Enter another?",
"Error", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
AccountNumber.setText(""); //THIS BLOCK OF CODE DOESN'T WORK. COMPARE NOW AS IT IS COMMENTED
AcctName.setText(""); //OUT WITH THE BLOCK OF CODE AT ... COMMENTED OUT!
currentAcctBal.setText("");
} else {
new Transfer_Panel().setVisible(false); }
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}//GEN-LAST:event_AccountNumberKeyPressed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
String selectAcc = "select acct_no, acct_name, acct_balance from account where acct_no = ? "
+ "order by trans_date desc limit 1";
String acctNum = AccountNumber.getText();
String destAcctNum = DestAccountNumber.getText();
if (acctNum.equals("") || acctNum.isEmpty()) {
JOptionPane.showMessageDialog(null, "Account Number Field is Empty!");
} if (destAcctNum.equals("") || destAcctNum.isEmpty()) {
JOptionPane.showMessageDialog(null, "Destination Account Number Field is Empty!");
} else {
try {
ps = conn.prepareStatement(selectAcc);
ps.setString(1, acctNum);
ResultSet res = ps.executeQuery();
pst = conn.prepareStatement(selectAcc);
pst.setString(1, destAcctNum);
ResultSet rest = pst.executeQuery();
// res.next();
// rest.next(); // To go to the first row of the resultset rst, else you'd have this
//exception "java.sql.SQLException: Before start of result set" whilie trying to obtain the data "acct_balance"
if (res.next() && rest.next() ) {
destCur_bal = checkForNullOrString2(rest.getString("acct_balance"));
String insertData = "insert into account values (null, ?, ?, ?, ?, ?, ?, ?)";
String tacct_name = AcctName.getText();
String tacct_type = "";
if (acctNum.startsWith("01")) {
tacct_type = "Current";
} else if (acctNum.startsWith("02")) {
tacct_type = "Savings";
}
String trans_type = "T - Transfer";
String tdest_acct_type = "";
if (destAcctNum.startsWith("01")) {
tdest_acct_type = "Current";
} else if (destAcctNum.startsWith("02")) {
tdest_acct_type = "Savings";
}
Double cur_bal = checkForNullOrString2(currentAcctBal.getText());// We've made the Textfield currentAcctBal 'NOT EDITABLE' from Properties
Double transferAmt = checkForNullOrString(Transfer_Amount.getText());
Double newAmt = 0.0;
if (transferAmt > 0 || cur_bal > transferAmt) {
newAmt = cur_bal - transferAmt;
} else {
newAmt += cur_bal;
}
String tdestAcctName = destAcctName.getText();
Double destNewAmt;
if (transferAmt > 0) {
destNewAmt = destCur_bal + transferAmt;
} else {
destNewAmt = destCur_bal;
}
try {
if (acctNum.equals("") || tacct_name.equals("") || currentAcctBal.getText().equals("")
|| Transfer_Amount.getText().equals("") || transferAmt < 0 || transferAmt > cur_bal) {
JOptionPane.showMessageDialog(this, "Invalid or Empty Entry/ies! Transfer was not made.");
} else {
ps = conn.prepareStatement(insertData);
ps.setString(1, acctNum);
ps.setString(2, tacct_name);
ps.setString(3, tacct_type);
ps.setDouble(4, transferAmt);
ps.setString(5, trans_type);
ps.setDouble(6, newAmt);
LocalDateTime trans_date = LocalDateTime.now();
ps.setString(7, trans_date.toString());
ps.executeUpdate();
pst = conn.prepareStatement(insertData);
pst.setString(1, destAcctNum);
pst.setString(2, tdestAcctName);
pst.setString(3, tdest_acct_type);
pst.setDouble(4, transferAmt);
pst.setString(5, trans_type);
pst.setDouble(6, destNewAmt);
LocalDateTime trans_date1 = LocalDateTime.now();
pst.setString(7, trans_date1.toString());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, transferAmt + " has been transferred from this account "
+ acctNum + " to the account " + destAcctNum);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}//GEN-LAST:event_jButton1ActionPerformed
private void DestAccountNumberKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_DestAccountNumberKeyPressed
// TODO add your handling code here:
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
String select = "select acct_no, acct_name, acct_balance from account where acct_no = ? "
+ "order by trans_date desc limit 1";
String tdesc_acct_no = DestAccountNumber.getText();
try {
ps = conn.prepareStatement(select);
ps.setString(1, tdesc_acct_no);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
getDestAcctName(rs, "acct_no");
} else {
int choice = JOptionPane.showConfirmDialog(null, "Account Not Found. Enter another?",
"Error", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) { //THIS BLOCK OF CODE DOESN'T WORK. COMPARE NOW AS IT IS COMMENTED
DestAccountNumber.setText(""); // OUT WITH THE BLOCK OF CODE AT ... COMMENTED OUT!
destAcctName.setText("");
} else {
new Transfer_Panel().setVisible(false);
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}//GEN-LAST:event_DestAccountNumberKeyPressed
private void DestAccountNumberFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_DestAccountNumberFocusLost
// TODO add your handling code here:
String select = "select acct_no, acct_name, acct_balance from account where acct_no = ? "
+ "order by trans_date desc limit 1";
String tdesc_acct_no = DestAccountNumber.getText();
try {
ps = conn.prepareStatement(select);
ps.setString(1, tdesc_acct_no);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
getDestAcctName(rs, "acct_no");
} else {
int choice = JOptionPane.showConfirmDialog(null, "Account Not Found. Enter another?",
"Error", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) { //THIS BLOCK OF CODE DOESN'T WORK. COMPARE NOW AS IT IS COMMENTED
DestAccountNumber.setText(""); // OUT WITH THE BLOCK OF CODE AT ... COMMENTED OUT!
destAcctName.setText("");
} else {
new Transfer_Panel().setVisible(false);
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_DestAccountNumberFocusLost
private void AccountNumberFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_AccountNumberFocusLost
// TODO add your handling code here:
String select = "select acct_no, acct_name, acct_balance from account where acct_no = ? "
+ "order by trans_date desc limit 1";
String tacct_no = AccountNumber.getText();
try {
ps = conn.prepareStatement(select);
ps.setString(1, tacct_no);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
getAcctName(rs, "acct_no");
} else {
int choice = JOptionPane.showConfirmDialog(null, "Account Not Found. Enter another?",
"Error", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
AccountNumber.setText(""); //THIS BLOCK OF CODE DOESN'T WORK. COMPARE NOW AS IT IS COMMENTED
AcctName.setText(""); //OUT WITH THE BLOCK OF CODE AT ... COMMENTED OUT!
currentAcctBal.setText("");
} else {
new Transfer_Panel().setVisible(false); }
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_AccountNumberFocusLost
public void getAcctName(ResultSet rst, String str) {
try {
String AcctNo = rst.getString(str);
if (AccountNumber.getText().equals(AcctNo)) {
AcctName.setText(rst.getString("acct_name"));
currentAcctBal.setText(Double.toString(rst.getDouble("acct_balance")));
} else {
/* int choice = JOptionPane.showConfirmDialog(null, "Account Not Found. Enter another?",
"Error", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
AccountNumber.setText(""); //THIS BLOCK OF CODE DOESN'T WORK. COMPARE NOW AS IT IS COMMENTED
AcctName.setText(""); //OUT WITH THE BLOCK OF CODE AT ... COMMENTED OUT!
currentAcctBal.setText("");
} else {
new Transfer_Panel().setVisible(false); }
*/ }
} catch (SQLException EX) {
EX.printStackTrace();
}
}
public void getDestAcctName(ResultSet rst, String str) {
try {
String AcctNo = rst.getString(str);
if (DestAccountNumber.getText().equals(AcctNo)) {
destAcctName.setText(rst.getString("acct_name"));
} else {
/* int choice = JOptionPane.showConfirmDialog(null, "Account Not Found. Enter another?",
"Error", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) { //THIS BLOCK OF CODE DOESN'T WORK. COMPARE NOW AS IT IS COMMENTED
DestAccountNumber.setText(""); //OUT WITH THE BLOCK OF CODE AT ... COMMENTED OUT!
destAcctName.setText("");
} else {
new Transfer_Panel().setVisible(false); }
*/
}
} catch (SQLException EX) {
EX.printStackTrace();
}
}
public double checkForNullOrString(String value) {
if (value.equals(null) && value.length() == 0) {
return 0;
} else {
try {
double dValue = Double.parseDouble(value);
return dValue;
} catch (NumberFormatException nf) {
JOptionPane.showMessageDialog(null, "Invalid Entry! Check the Transfer Amount");
double dvalue = 0;
return dvalue;
}
}
}
public double checkForNullOrString2(String value) {
if (value.equals(null) && 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 Bal Amount");
double dvalue = 0;
return dvalue;
}
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField AccountNumber;
private javax.swing.JLabel AcctName;
private javax.swing.JTextField DestAccountNumber;
private javax.swing.JTextField Transfer_Amount;
private javax.swing.JTextField currentAcctBal;
private javax.swing.JLabel currentBalance;
private javax.swing.JLabel destAcctName;
private javax.swing.JButton jButton1;
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;
// End of variables declaration//GEN-END:variables
}