-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeposit_Panel.java
361 lines (311 loc) · 15.2 KB
/
Deposit_Panel.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
/*
* 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 com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
import java.awt.event.KeyEvent;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
//import java.util.Date;
import javax.swing.JOptionPane;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
/**
*
* @author Patricia Osifo
*/
public class Deposit_Panel extends javax.swing.JPanel {
Connection conn;
PreparedStatement ps;
final String url = "jdbc:mysql://localhost:3306/banking_app";
final String user = "root";
final String pass = "";
PreparedStatement stat;
ResultSet res;
/**
* Creates new form Deposit_Panel
*/
public Deposit_Panel() {
initComponents();
doConnect();
}
public final void doConnect() {
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();
Depo_Amount = new javax.swing.JTextField();
Narration = new javax.swing.JTextField();
current_bal = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
depositButton = new javax.swing.JButton();
AcctName = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setFont(new java.awt.Font("Tw Cen MT Condensed", 1, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(204, 204, 204));
jLabel1.setText("Deposit");
add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 30, -1, -1));
jLabel2.setText("Account Number");
add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(44, 97, -1, -1));
jLabel3.setText("Deposit Amount");
add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(44, 165, -1, -1));
jLabel4.setText("Narration");
add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(44, 223, -1, -1));
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(146, 90, 102, 29));
Depo_Amount.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
Depo_AmountKeyPressed(evt);
}
});
add(Depo_Amount, new org.netbeans.lib.awtextra.AbsoluteConstraints(146, 162, 102, -1));
add(Narration, new org.netbeans.lib.awtextra.AbsoluteConstraints(146, 214, 102, 33));
current_bal.setEditable(false);
current_bal.setBackground(new java.awt.Color(102, 102, 102));
current_bal.setForeground(new java.awt.Color(255, 255, 255));
current_bal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
current_balActionPerformed(evt);
}
});
add(current_bal, new org.netbeans.lib.awtextra.AbsoluteConstraints(146, 298, 102, -1));
jLabel5.setText("Account Balance");
add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(44, 298, 84, 20));
depositButton.setBackground(new java.awt.Color(102, 0, 255));
depositButton.setFont(new java.awt.Font("Sylfaen", 1, 14)); // NOI18N
depositButton.setForeground(new java.awt.Color(153, 153, 255));
depositButton.setText("Deposit");
depositButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
depositButtonActionPerformed(evt);
}
});
add(depositButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 350, -1, -1));
AcctName.setBackground(new java.awt.Color(153, 153, 153));
AcctName.setFont(new java.awt.Font("Kristen ITC", 0, 14)); // NOI18N
AcctName.setForeground(new java.awt.Color(255, 255, 255));
AcctName.setEnabled(false);
add(AcctName, new org.netbeans.lib.awtextra.AbsoluteConstraints(266, 90, 190, 44));
jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/banking_app/images/20191222161248.jpg"))); // NOI18N
add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 530, 460));
}// </editor-fold>//GEN-END:initComponents
private void current_balActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_current_balActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_current_balActionPerformed
private void AccountNumberKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_AccountNumberKeyReleased
}//GEN-LAST:event_AccountNumberKeyReleased
private void AccountNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AccountNumberActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_AccountNumberActionPerformed
private void depositButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_depositButtonActionPerformed
// TODO add your handling code here:
String acct_no = AccountNumber.getText();
String select = "Select * from account where acct_no = ? order by trans_date desc limit 1";
try {
if (acct_no.equals("")) {
JOptionPane.showMessageDialog(this, "Account Number Field is Empty");
} else {
ps = conn.prepareStatement(select);
ps.setString(1, acct_no);
ResultSet rst = ps.executeQuery();
if (rst.next()) {
Double deposit = checkForNullOrString(Depo_Amount.getText());
Double cur_bal = checkForNullOrString2(current_bal.getText());//NOTE this line still possesses a problem: NumberFormatException
//for when a string is entered in the Acct Balance textfield, I would
//love to back to the original way where thr checkForString method caught it
//but there would be a redundant pop-up message. Should I use the method but forfeit the pop-up message in the method?
//Then let another pop-up message handle the information passage to the use?
Double newAmount = 0.0;//Well, thank God the assigned value is never used...(For you who doesn't get this, look to the yellow lantern besdie the line number)
// Damn, used it now in line 219 where I changed newAmount = cur_bal to newAmount += cur_bal
if (deposit > 0) {
newAmount = cur_bal + deposit;
} else {
JOptionPane.showMessageDialog(this, "Entered Deposit Amount is not allowed");
newAmount += cur_bal;
}
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 = AcctName.getText();
String trans_type = "D - Deposit";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-DD HH-MM-SS");
LocalDateTime trans_date = LocalDateTime.now();
try {
if (tacct_no.equals("") || tacct_name.equals("") || Depo_Amount.getText().equals("")
|| current_bal.getText().equals("") || deposit < 0) {
JOptionPane.showMessageDialog(this, "Invalid or Empty Entry/ies! Deposit was not made");
} 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, deposit);
ps.setString(5, trans_type);
ps.setDouble(6, newAmount);
ps.setString(7, trans_date.toString());
ps.executeUpdate();
JOptionPane.showMessageDialog(this, "Deposit has been made to the Account " + tacct_no);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else {
int choice = JOptionPane.showConfirmDialog(null, " Do you wish to enter another?", "Account", JOptionPane.OK_CANCEL_OPTION);
if (choice == JOptionPane.OK_OPTION) {
AccountNumber.setText("");
AcctName.setText("");
current_bal.setText("");
} else {
new Deposit_Panel().setVisible(false);
}
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_depositButtonActionPerformed
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("");
current_bal.setText("");
JOptionPane.showMessageDialog(null, "Account Not Found");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_AccountNumberFocusLost
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 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("");
current_bal.setText("");
JOptionPane.showMessageDialog(null, "Account Not Found");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}//GEN-LAST:event_AccountNumberKeyPressed
private void Depo_AmountKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Depo_AmountKeyPressed
// TODO add your handling code here:
}//GEN-LAST:event_Depo_AmountKeyPressed
//Depo_Amount.getText().equals(null) ||
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 Deposit 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;
}
}
}
public void getAcctName(ResultSet rst, String str) {
try {
String AcctNo = rst.getString(str);
if (AccountNumber.getText().equals(AcctNo)) {
AcctName.setText(rst.getString("acct_name"));
current_bal.setText(Double.toString(rst.getDouble("acct_balance")));
}
} catch (SQLException EX) {
EX.printStackTrace();
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField AccountNumber;
private javax.swing.JLabel AcctName;
private javax.swing.JTextField Depo_Amount;
private javax.swing.JTextField Narration;
private javax.swing.JTextField current_bal;
private javax.swing.JButton depositButton;
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 jLabel6;
// End of variables declaration//GEN-END:variables
}