Advertisement
Guest User

Untitled

a guest
Nov 9th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.49 KB | None | 0 0
  1. package tasks;
  2.  
  3. import java.sql.*;
  4. import javax.swing.JOptionPane;
  5.  
  6. public class Banking extends javax.swing.JFrame
  7. {
  8. //Database details
  9. static final String DB_URL = "jdbc:derby://localhost/db_main";
  10. static final String USER = "db_admin";
  11. static final String PASS = "MNMN36F9"; //zAfevq4I1Fpy MNMN36F9
  12.  
  13. //Variables
  14. String pin;
  15. String account;
  16. Integer intBal;
  17.  
  18. public Banking()
  19. {
  20. initComponents();
  21. }
  22.  
  23. @SuppressWarnings("unchecked")
  24. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  25. private void initComponents() {
  26.  
  27. jButton2 = new javax.swing.JButton();
  28. jScrollPane1 = new javax.swing.JScrollPane();
  29. jTextArea1 = new javax.swing.JTextArea();
  30. lblHeader = new javax.swing.JLabel();
  31. lblBalance = new javax.swing.JLabel();
  32. textAccount = new javax.swing.JTextField();
  33. btnConfirm = new javax.swing.JButton();
  34. textWithdraw = new javax.swing.JTextField();
  35. btnWithdraw = new javax.swing.JButton();
  36. textDeposit = new javax.swing.JTextField();
  37. btnDeposit = new javax.swing.JButton();
  38. btnPINChange = new javax.swing.JButton();
  39. textPIN = new javax.swing.JPasswordField();
  40.  
  41. jButton2.setText("jButton2");
  42.  
  43. jTextArea1.setColumns(20);
  44. jTextArea1.setRows(5);
  45. jScrollPane1.setViewportView(jTextArea1);
  46.  
  47. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  48.  
  49. lblHeader.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  50. lblHeader.setText("Account Overview");
  51.  
  52. lblBalance.setText("Balance | ");
  53.  
  54. textAccount.addActionListener(new java.awt.event.ActionListener() {
  55. public void actionPerformed(java.awt.event.ActionEvent evt) {
  56. textAccountActionPerformed(evt);
  57. }
  58. });
  59.  
  60. btnConfirm.setText("Update");
  61. btnConfirm.addActionListener(new java.awt.event.ActionListener() {
  62. public void actionPerformed(java.awt.event.ActionEvent evt) {
  63. btnConfirmActionPerformed(evt);
  64. }
  65. });
  66.  
  67. btnWithdraw.setText("Withdraw");
  68. btnWithdraw.addActionListener(new java.awt.event.ActionListener() {
  69. public void actionPerformed(java.awt.event.ActionEvent evt) {
  70. btnWithdrawActionPerformed(evt);
  71. }
  72. });
  73.  
  74. btnDeposit.setText("Deposit");
  75. btnDeposit.addActionListener(new java.awt.event.ActionListener() {
  76. public void actionPerformed(java.awt.event.ActionEvent evt) {
  77. btnDepositActionPerformed(evt);
  78. }
  79. });
  80.  
  81. btnPINChange.setText("Change PIN");
  82. btnPINChange.addActionListener(new java.awt.event.ActionListener() {
  83. public void actionPerformed(java.awt.event.ActionEvent evt) {
  84. btnPINChangeActionPerformed(evt);
  85. }
  86. });
  87.  
  88. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  89. getContentPane().setLayout(layout);
  90. layout.setHorizontalGroup(
  91. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  92. .addGroup(layout.createSequentialGroup()
  93. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  94. .addGroup(layout.createSequentialGroup()
  95. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  96. .addGroup(layout.createSequentialGroup()
  97. .addContainerGap()
  98. .addComponent(textWithdraw, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
  99. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  100. .addComponent(textDeposit, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
  101. .addGroup(layout.createSequentialGroup()
  102. .addGap(47, 47, 47)
  103. .addComponent(lblHeader))
  104. .addGroup(layout.createSequentialGroup()
  105. .addGap(92, 92, 92)
  106. .addComponent(lblBalance))
  107. .addGroup(layout.createSequentialGroup()
  108. .addGap(38, 38, 38)
  109. .addComponent(textAccount, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
  110. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  111. .addComponent(btnConfirm))
  112. .addGroup(layout.createSequentialGroup()
  113. .addContainerGap()
  114. .addComponent(btnWithdraw, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
  115. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  116. .addComponent(btnDeposit, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
  117. .addGap(0, 0, Short.MAX_VALUE))
  118. .addGroup(layout.createSequentialGroup()
  119. .addContainerGap()
  120. .addComponent(textPIN)
  121. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  122. .addComponent(btnPINChange, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
  123. .addContainerGap())
  124. );
  125. layout.setVerticalGroup(
  126. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  127. .addGroup(layout.createSequentialGroup()
  128. .addContainerGap()
  129. .addComponent(lblHeader)
  130. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  131. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  132. .addComponent(textAccount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  133. .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
  134. .addGap(18, 18, 18)
  135. .addComponent(lblBalance)
  136. .addGap(18, 18, 18)
  137. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  138. .addComponent(textWithdraw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  139. .addComponent(textDeposit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  140. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  141. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  142. .addComponent(btnWithdraw)
  143. .addComponent(btnDeposit))
  144. .addGap(18, 18, 18)
  145. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  146. .addComponent(btnPINChange)
  147. .addComponent(textPIN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  148. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  149. );
  150.  
  151. pack();
  152. }// </editor-fold>
  153.  
  154. private void textAccountActionPerformed(java.awt.event.ActionEvent evt) {
  155. // TODO add your handling code here:
  156. }
  157.  
  158. //Update balance from inputted account number
  159. private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {
  160. java.sql.Connection conn = null;
  161. Statement stmt1 = null;
  162.  
  163. account = textAccount.getText();
  164.  
  165. try
  166. {
  167. Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
  168. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  169. stmt1 = conn.createStatement();
  170.  
  171. //Query
  172. String sql1 = "SELECT balance FROM tbl_test_users_alt WHERE account ='"+ account +"'";
  173.  
  174. ResultSet result1 = stmt1.executeQuery(sql1);
  175.  
  176. //Gathering results
  177. while(result1.next())
  178. {
  179. String balance = result1.getString("balance");
  180.  
  181. Integer temp = Integer.parseInt(balance);
  182. intBal = temp;
  183.  
  184. lblBalance.setText("Balance | £" + balance);
  185. }
  186. result1.close();
  187. stmt1.close();
  188. conn.close();
  189. }
  190. //Error Catching
  191. catch(SQLException se)
  192. {
  193. se.printStackTrace();
  194. }
  195. catch(Exception e)
  196. {
  197. e.printStackTrace();
  198. }
  199. finally
  200. {
  201. try
  202. {
  203. if(stmt1!=null)
  204. {
  205. stmt1.close();
  206. }
  207. }
  208. catch(SQLException se2)
  209. {
  210. }
  211. try
  212. {
  213. if(conn!=null)
  214. {
  215. conn.close();
  216. }
  217. }
  218. catch(SQLException se)
  219. {
  220. se.printStackTrace();
  221. }
  222. }
  223. }
  224.  
  225. private void btnWithdrawActionPerformed(java.awt.event.ActionEvent evt) {
  226. java.sql.Connection conn = null;
  227. Statement stmt1 = null;
  228.  
  229. //Getting text and converting to an Integer
  230. Integer withdrawVal = Integer.parseInt(textWithdraw.getText());
  231.  
  232. //Subtracting withdraw value from balance
  233. intBal -= withdrawVal;
  234.  
  235. //Checking if the withdraw value is less than the balance of the account
  236. if(withdrawVal <= intBal)
  237. {
  238. try
  239. {
  240. Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
  241. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  242. stmt1 = conn.createStatement();
  243.  
  244. //Query
  245. String sql1 = "UPDATE tbl_test_users_alt " + "SET balance=? WHERE account=?";
  246.  
  247. //Assigning values to the '?' in query with PreparedStatement
  248. PreparedStatement pstmt = conn.prepareStatement(sql1);
  249. pstmt.setString(1, intBal.toString());
  250. pstmt.setString(2, account);
  251. pstmt.execute();
  252.  
  253. //Query
  254. String sql2 = "SELECT balance FROM tbl_test_users_alt WHERE account ='"+ account +"'";
  255.  
  256. ResultSet result1 = stmt1.executeQuery(sql2);
  257.  
  258. //Gathering results
  259. while(result1.next())
  260. {
  261. String balance = result1.getString("balance");
  262. lblBalance.setText("Balance | " + balance);
  263. }
  264. result1.close();
  265. stmt1.close();
  266. conn.close();
  267. }
  268. //Error Catching
  269. catch(SQLException se)
  270. {
  271. se.printStackTrace();
  272. }
  273. catch(Exception e)
  274. {
  275. e.printStackTrace();
  276. }
  277. finally
  278. {
  279. try
  280. {
  281. if(stmt1!=null)
  282. {
  283. stmt1.close();
  284. }
  285. }
  286. catch(SQLException se2)
  287. {
  288. }
  289. try
  290. {
  291. if(conn!=null)
  292. {
  293. conn.close();
  294. }
  295. }
  296. catch(SQLException se)
  297. {
  298. se.printStackTrace();
  299. }
  300. //Popup message - Success
  301. JOptionPane.showMessageDialog(null,
  302. "You have successfully withdrawed money, please collect it.",
  303. "Success: You have withdrawed money", JOptionPane.INFORMATION_MESSAGE);
  304. textWithdraw.setText("");
  305. }
  306. }
  307. else
  308. {
  309. //Popup message - Error
  310. JOptionPane.showMessageDialog(null,
  311. "The amount you tried to withdraw exceeds your current account balance, please try again.",
  312. "Error: Insufficient Funds", JOptionPane.WARNING_MESSAGE);
  313. textWithdraw.setText("");
  314. }
  315. }
  316.  
  317. private void btnDepositActionPerformed(java.awt.event.ActionEvent evt) {
  318. java.sql.Connection conn = null;
  319. Statement stmt1 = null;
  320.  
  321. //Getting text and converting to an Integer
  322. Integer depositVal = Integer.parseInt(textDeposit.getText());
  323.  
  324. //Adding deposit value to the balance
  325. intBal += depositVal;
  326.  
  327. try
  328. {
  329. Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
  330. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  331. stmt1 = conn.createStatement();
  332.  
  333. //Query
  334. String sql1 = "UPDATE tbl_test_users_alt " + "SET balance=? WHERE account=?";
  335.  
  336. //Assigning values to the '?' in query with PreparedStatement
  337. PreparedStatement pstmt = conn.prepareStatement(sql1);
  338. pstmt.setString(1, intBal.toString());
  339. pstmt.setString(2, account);
  340. pstmt.execute();
  341.  
  342. //Query
  343. String sql2 = "SELECT balance FROM tbl_test_users_alt WHERE account ='"+ account +"'";
  344.  
  345. ResultSet result1 = stmt1.executeQuery(sql2);
  346.  
  347. //Gathering results
  348. while(result1.next())
  349. {
  350. String balance = result1.getString("balance");
  351. lblBalance.setText("Balance | " + balance);
  352. }
  353. result1.close();
  354. stmt1.close();
  355. conn.close();
  356. }
  357. //Error catching
  358. catch(SQLException se)
  359. {
  360. se.printStackTrace();
  361. }
  362. catch(Exception e)
  363. {
  364. e.printStackTrace();
  365. }
  366. finally
  367. {
  368. try
  369. {
  370. if(stmt1!=null)
  371. {
  372. stmt1.close();
  373. }
  374. }
  375. catch(SQLException se2)
  376. {
  377. }
  378. try
  379. {
  380. if(conn!=null)
  381. {
  382. conn.close();
  383. }
  384. }
  385. catch(SQLException se)
  386. {
  387. se.printStackTrace();
  388. }
  389. //Popup message - Success
  390. JOptionPane.showMessageDialog(null,
  391. "You have successfully deposited money, please confirm your balance.",
  392. "Success: You have deposited money", JOptionPane.INFORMATION_MESSAGE);
  393. textDeposit.setText("");
  394. }
  395. }
  396.  
  397. private void btnPINChangeActionPerformed(java.awt.event.ActionEvent evt) {
  398. java.sql.Connection conn = null;
  399. Statement stmt1 = null;
  400.  
  401. char []passChars=textPIN.getPassword();
  402.  
  403. //Checking that the pin length is 4
  404. if(textPIN.getText().length() != 4)
  405. {
  406. JOptionPane.showMessageDialog(null,"Your PIN needs to be 4 digits.", "Error: PIN wrong length", JOptionPane.WARNING_MESSAGE);
  407. textPIN.setText("");
  408. }
  409. else
  410. {
  411. try
  412. {
  413. Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
  414. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  415. stmt1 = conn.createStatement();
  416.  
  417. //Query
  418. String sql1 = "UPDATE tbl_test_users_alt " + "SET pin=? WHERE account=?";
  419.  
  420. String pin = new String(passChars);
  421.  
  422. //Assigning values to the '?' in query with PreparedStatement
  423. PreparedStatement pstmt = conn.prepareStatement(sql1);
  424. pstmt.setString(1, pin);
  425. pstmt.setString(2, account);
  426. pstmt.execute();
  427.  
  428. stmt1.close();
  429. conn.close();
  430. }
  431. //Error catching
  432. catch(SQLException se)
  433. {
  434. se.printStackTrace();
  435. }
  436. catch(Exception e)
  437. {
  438. e.printStackTrace();
  439. }
  440. finally
  441. {
  442. try
  443. {
  444. if(stmt1!=null)
  445. {
  446. stmt1.close();
  447. }
  448. }
  449. catch(SQLException se2)
  450. {
  451. }
  452. try
  453. {
  454. if(conn!=null)
  455. {
  456. conn.close();
  457. }
  458. }
  459. catch(SQLException se)
  460. {
  461. se.printStackTrace();
  462. }
  463. //Popup message - Success
  464. JOptionPane.showMessageDialog(null,"Your PIN has been changed", "Success: PIN Changed", JOptionPane.WARNING_MESSAGE);
  465. textPIN.setText("");
  466. }
  467. }
  468. }
  469.  
  470.  
  471. public static void main(String args[])
  472. {
  473. java.awt.EventQueue.invokeLater(new Runnable()
  474. {
  475. public void run() {
  476. new Banking().setVisible(true);
  477. }
  478. });
  479. }
  480.  
  481. // Variables declaration - do not modify
  482. private javax.swing.JButton btnConfirm;
  483. private javax.swing.JButton btnDeposit;
  484. private javax.swing.JButton btnPINChange;
  485. private javax.swing.JButton btnWithdraw;
  486. private javax.swing.JButton jButton2;
  487. private javax.swing.JScrollPane jScrollPane1;
  488. private javax.swing.JTextArea jTextArea1;
  489. private javax.swing.JLabel lblBalance;
  490. private javax.swing.JLabel lblHeader;
  491. private javax.swing.JTextField textAccount;
  492. private javax.swing.JTextField textDeposit;
  493. private javax.swing.JPasswordField textPIN;
  494. private javax.swing.JTextField textWithdraw;
  495. // End of variables declaration
  496. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement