Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tasks;
- import java.sql.*;
- import javax.swing.JOptionPane;
- public class Banking extends javax.swing.JFrame
- {
- //Database details
- static final String DB_URL = "jdbc:derby://localhost/db_main";
- static final String USER = "db_admin";
- static final String PASS = "MNMN36F9"; //zAfevq4I1Fpy MNMN36F9
- //Variables
- String pin;
- String account;
- Integer intBal;
- public Banking()
- {
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jButton2 = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTextArea1 = new javax.swing.JTextArea();
- lblHeader = new javax.swing.JLabel();
- lblBalance = new javax.swing.JLabel();
- textAccount = new javax.swing.JTextField();
- btnConfirm = new javax.swing.JButton();
- textWithdraw = new javax.swing.JTextField();
- btnWithdraw = new javax.swing.JButton();
- textDeposit = new javax.swing.JTextField();
- btnDeposit = new javax.swing.JButton();
- btnPINChange = new javax.swing.JButton();
- textPIN = new javax.swing.JPasswordField();
- jButton2.setText("jButton2");
- jTextArea1.setColumns(20);
- jTextArea1.setRows(5);
- jScrollPane1.setViewportView(jTextArea1);
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- lblHeader.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- lblHeader.setText("Account Overview");
- lblBalance.setText("Balance | ");
- textAccount.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- textAccountActionPerformed(evt);
- }
- });
- btnConfirm.setText("Update");
- btnConfirm.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnConfirmActionPerformed(evt);
- }
- });
- btnWithdraw.setText("Withdraw");
- btnWithdraw.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnWithdrawActionPerformed(evt);
- }
- });
- btnDeposit.setText("Deposit");
- btnDeposit.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnDepositActionPerformed(evt);
- }
- });
- btnPINChange.setText("Change PIN");
- btnPINChange.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnPINChangeActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(textWithdraw, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(textDeposit, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(47, 47, 47)
- .addComponent(lblHeader))
- .addGroup(layout.createSequentialGroup()
- .addGap(92, 92, 92)
- .addComponent(lblBalance))
- .addGroup(layout.createSequentialGroup()
- .addGap(38, 38, 38)
- .addComponent(textAccount, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(btnConfirm))
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(btnWithdraw, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(btnDeposit, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(textPIN)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(btnPINChange, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(lblHeader)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(textAccount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addComponent(lblBalance)
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(textWithdraw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(textDeposit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(btnWithdraw)
- .addComponent(btnDeposit))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(btnPINChange)
- .addComponent(textPIN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void textAccountActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- //Update balance from inputted account number
- private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {
- java.sql.Connection conn = null;
- Statement stmt1 = null;
- account = textAccount.getText();
- try
- {
- Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- stmt1 = conn.createStatement();
- //Query
- String sql1 = "SELECT balance FROM tbl_test_users_alt WHERE account ='"+ account +"'";
- ResultSet result1 = stmt1.executeQuery(sql1);
- //Gathering results
- while(result1.next())
- {
- String balance = result1.getString("balance");
- Integer temp = Integer.parseInt(balance);
- intBal = temp;
- lblBalance.setText("Balance | £" + balance);
- }
- result1.close();
- stmt1.close();
- conn.close();
- }
- //Error Catching
- catch(SQLException se)
- {
- se.printStackTrace();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if(stmt1!=null)
- {
- stmt1.close();
- }
- }
- catch(SQLException se2)
- {
- }
- try
- {
- if(conn!=null)
- {
- conn.close();
- }
- }
- catch(SQLException se)
- {
- se.printStackTrace();
- }
- }
- }
- private void btnWithdrawActionPerformed(java.awt.event.ActionEvent evt) {
- java.sql.Connection conn = null;
- Statement stmt1 = null;
- //Getting text and converting to an Integer
- Integer withdrawVal = Integer.parseInt(textWithdraw.getText());
- //Subtracting withdraw value from balance
- intBal -= withdrawVal;
- //Checking if the withdraw value is less than the balance of the account
- if(withdrawVal <= intBal)
- {
- try
- {
- Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- stmt1 = conn.createStatement();
- //Query
- String sql1 = "UPDATE tbl_test_users_alt " + "SET balance=? WHERE account=?";
- //Assigning values to the '?' in query with PreparedStatement
- PreparedStatement pstmt = conn.prepareStatement(sql1);
- pstmt.setString(1, intBal.toString());
- pstmt.setString(2, account);
- pstmt.execute();
- //Query
- String sql2 = "SELECT balance FROM tbl_test_users_alt WHERE account ='"+ account +"'";
- ResultSet result1 = stmt1.executeQuery(sql2);
- //Gathering results
- while(result1.next())
- {
- String balance = result1.getString("balance");
- lblBalance.setText("Balance | " + balance);
- }
- result1.close();
- stmt1.close();
- conn.close();
- }
- //Error Catching
- catch(SQLException se)
- {
- se.printStackTrace();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if(stmt1!=null)
- {
- stmt1.close();
- }
- }
- catch(SQLException se2)
- {
- }
- try
- {
- if(conn!=null)
- {
- conn.close();
- }
- }
- catch(SQLException se)
- {
- se.printStackTrace();
- }
- //Popup message - Success
- JOptionPane.showMessageDialog(null,
- "You have successfully withdrawed money, please collect it.",
- "Success: You have withdrawed money", JOptionPane.INFORMATION_MESSAGE);
- textWithdraw.setText("");
- }
- }
- else
- {
- //Popup message - Error
- JOptionPane.showMessageDialog(null,
- "The amount you tried to withdraw exceeds your current account balance, please try again.",
- "Error: Insufficient Funds", JOptionPane.WARNING_MESSAGE);
- textWithdraw.setText("");
- }
- }
- private void btnDepositActionPerformed(java.awt.event.ActionEvent evt) {
- java.sql.Connection conn = null;
- Statement stmt1 = null;
- //Getting text and converting to an Integer
- Integer depositVal = Integer.parseInt(textDeposit.getText());
- //Adding deposit value to the balance
- intBal += depositVal;
- try
- {
- Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- stmt1 = conn.createStatement();
- //Query
- String sql1 = "UPDATE tbl_test_users_alt " + "SET balance=? WHERE account=?";
- //Assigning values to the '?' in query with PreparedStatement
- PreparedStatement pstmt = conn.prepareStatement(sql1);
- pstmt.setString(1, intBal.toString());
- pstmt.setString(2, account);
- pstmt.execute();
- //Query
- String sql2 = "SELECT balance FROM tbl_test_users_alt WHERE account ='"+ account +"'";
- ResultSet result1 = stmt1.executeQuery(sql2);
- //Gathering results
- while(result1.next())
- {
- String balance = result1.getString("balance");
- lblBalance.setText("Balance | " + balance);
- }
- result1.close();
- stmt1.close();
- conn.close();
- }
- //Error catching
- catch(SQLException se)
- {
- se.printStackTrace();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if(stmt1!=null)
- {
- stmt1.close();
- }
- }
- catch(SQLException se2)
- {
- }
- try
- {
- if(conn!=null)
- {
- conn.close();
- }
- }
- catch(SQLException se)
- {
- se.printStackTrace();
- }
- //Popup message - Success
- JOptionPane.showMessageDialog(null,
- "You have successfully deposited money, please confirm your balance.",
- "Success: You have deposited money", JOptionPane.INFORMATION_MESSAGE);
- textDeposit.setText("");
- }
- }
- private void btnPINChangeActionPerformed(java.awt.event.ActionEvent evt) {
- java.sql.Connection conn = null;
- Statement stmt1 = null;
- char []passChars=textPIN.getPassword();
- //Checking that the pin length is 4
- if(textPIN.getText().length() != 4)
- {
- JOptionPane.showMessageDialog(null,"Your PIN needs to be 4 digits.", "Error: PIN wrong length", JOptionPane.WARNING_MESSAGE);
- textPIN.setText("");
- }
- else
- {
- try
- {
- Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- stmt1 = conn.createStatement();
- //Query
- String sql1 = "UPDATE tbl_test_users_alt " + "SET pin=? WHERE account=?";
- String pin = new String(passChars);
- //Assigning values to the '?' in query with PreparedStatement
- PreparedStatement pstmt = conn.prepareStatement(sql1);
- pstmt.setString(1, pin);
- pstmt.setString(2, account);
- pstmt.execute();
- stmt1.close();
- conn.close();
- }
- //Error catching
- catch(SQLException se)
- {
- se.printStackTrace();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if(stmt1!=null)
- {
- stmt1.close();
- }
- }
- catch(SQLException se2)
- {
- }
- try
- {
- if(conn!=null)
- {
- conn.close();
- }
- }
- catch(SQLException se)
- {
- se.printStackTrace();
- }
- //Popup message - Success
- JOptionPane.showMessageDialog(null,"Your PIN has been changed", "Success: PIN Changed", JOptionPane.WARNING_MESSAGE);
- textPIN.setText("");
- }
- }
- }
- public static void main(String args[])
- {
- java.awt.EventQueue.invokeLater(new Runnable()
- {
- public void run() {
- new Banking().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btnConfirm;
- private javax.swing.JButton btnDeposit;
- private javax.swing.JButton btnPINChange;
- private javax.swing.JButton btnWithdraw;
- private javax.swing.JButton jButton2;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTextArea jTextArea1;
- private javax.swing.JLabel lblBalance;
- private javax.swing.JLabel lblHeader;
- private javax.swing.JTextField textAccount;
- private javax.swing.JTextField textDeposit;
- private javax.swing.JPasswordField textPIN;
- private javax.swing.JTextField textWithdraw;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement