Bank Simulator in JAVA(swings)

35
// bank Simulator in JAVA import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JTextArea; import java.awt.event.KeyEvent; import javax.swing.JDesktopPane; import javax.swing.JInternalFrame; import java.awt.Color; import java.awt.Toolkit; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.FlowLayout; import java.sql.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; class BankSimulator extends WindowAdapter implements ActionListener {

description

This is a Bank Simulator(Demo) in JAVA.Using swings and awt.back end - mysql

Transcript of Bank Simulator in JAVA(swings)

Page 1: Bank Simulator in JAVA(swings)

// bank Simulator in JAVA

import javax.swing.JFrame;

import javax.swing.JMenuBar;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.JTextArea;

import java.awt.event.KeyEvent;

import javax.swing.JDesktopPane;

import javax.swing.JInternalFrame;

import java.awt.Color;

import java.awt.Toolkit;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import java.awt.FlowLayout;

import java.sql.*;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

class BankSimulator extends WindowAdapter implements ActionListener

{

JFrame frame;

Page 2: Bank Simulator in JAVA(swings)

JMenuBar menubar;

JMenu mAccount,mTransaction;

JMenuItem miNew,miClose,miExit,miDeposit,miWithdraw;

JDesktopPane desktopPane;

JInternalFrame newAccount,closeAccount,depositAccount,withdrawAccount;

Toolkit toolKit;

Dimension dimension;

static boolean flag = false;

static boolean flag2 = false;

JLabel deposit_lbAccNo=null;

JTextField tfAccNo,tfName,tfAmmount,close_tfAccNo,close_tfName,close_tfAmt,deposit_tfAccNo,deposit_tfName,deposit_tfBal,deposit_tfAmt;

JTextField withdraw_tfAccNo,withdraw_tfName,withdraw_tfBal,withdraw_tfAmt;

BankSimulator()

{

frame = new JFrame("Bank Simulator in JAVA");

menubar = new JMenuBar();

frame.add(menubar);

mAccount = new JMenu("Account");

mAccount.setMnemonic(KeyEvent.VK_A);

Page 3: Bank Simulator in JAVA(swings)

mTransaction = new JMenu("Transaction");

mTransaction.setMnemonic(KeyEvent.VK_T);

miNew = new JMenuItem("New Account");

miNew.setMnemonic(KeyEvent.VK_N);

miClose = new JMenuItem("Close Account");

miClose.setMnemonic(KeyEvent.VK_O);

miExit = new JMenuItem("Exit");

miExit.setMnemonic(KeyEvent.VK_F4);

miDeposit = new JMenuItem("Deposit");

miWithdraw = new JMenuItem("WithDraw");

mAccount.add(miNew);

mAccount.add(miClose);

mAccount.add(miExit);

mTransaction.add(miDeposit);

mTransaction.add(miWithdraw);

menubar.add(mAccount);

menubar.add(mTransaction);

frame.setJMenuBar(menubar);

desktopPane = new JDesktopPane(); // create DESKTOP PANE

desktopPane.setBackground(Color.RED);

Page 4: Bank Simulator in JAVA(swings)

toolKit = Toolkit.getDefaultToolkit(); // Toolkit is a abstract class in awt

dimension = toolKit.getScreenSize();

frame.add(desktopPane);

frame.setVisible(true);

frame.setBounds(0,0,800,600);

// ---------------------- Add Action Listner-----------------------------

frame.addWindowListener(this);

miNew.addActionListener(this);

miClose.addActionListener(this);

miExit.addActionListener(this);

miDeposit.addActionListener(this);

miWithdraw.addActionListener(this);

//--------------------------------------------------------------------------

}

public void windowClosing(WindowEvent we)

Page 5: Bank Simulator in JAVA(swings)

{

System.out.println("------------------------- Windows Closed------------------");

JFrame jFrame = (JFrame)we.getWindow();

if(jFrame == frame)

{

frame.dispose();

}

}

public void actionPerformed(ActionEvent ae)

{

JMenuItem jmi = null;

// String className = ae.getSource().getClass().toString();

jmi = (JMenuItem)ae.getSource();

Page 6: Bank Simulator in JAVA(swings)

if(jmi == miNew)

{

newAccount = new JInternalFrame("New Account",false,true,false,true); // create JINTERNAL FRAME

newAccount.setBounds(200,200,320,200);

// newAccount.setBounds(30,30,dimension.width - 300, dimension.height - 300);

newAccount.setLayout(new FlowLayout());

JLabel lbAccNo = new JLabel("Account No");

JLabel lbName = new JLabel("Name");

JLabel lbAmount = new JLabel("Amount");

tfAccNo = new JTextField();

tfAccNo.setEditable(false);

tfAccNo.setColumns(20);

tfName = new JTextField();

tfName.setColumns(20);

tfAmmount = new JTextField();

tfAmmount.setColumns(20);

JButton create = new JButton("Create");

JButton cancel = new JButton("Cancel");

JButton close = new JButton("Close");

newAccount.add(lbAccNo);

Page 7: Bank Simulator in JAVA(swings)

newAccount.add(tfAccNo);

newAccount.add(lbName);

newAccount.add(tfName);

newAccount.add(lbAmount);

newAccount.add(tfAmmount);

newAccount.add(create);

newAccount.add(cancel);

newAccount.add(close);

// ----------------------- Adding Listner to Internal Frame components------------

create.addActionListener(new InternalActionListener());

close.addActionListener(new InternalActionListener());

cancel.addActionListener(new InternalActionListener());

//---------------------------------------------------------------------------------

desktopPane.add(newAccount);

newAccount.setVisible(true);

//----------------------- GET Account no from database ------------

try

{

Connection con = ConnectionClass.getConnection();

System.out.println ("Conn create ");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT max(accountNo) FROM banktable");

Page 8: Bank Simulator in JAVA(swings)

if(rs.next())

{

int a = rs.getInt(1);

a = a+1;

tfAccNo.setText(""+a);

ConnectionClass.closed();

}

}

catch(Exception ex)

{

System.out.println ("-------------------------------"+ex);

ex.printStackTrace();

}

finally

{

try{ConnectionClass.closed();}

catch(Exception es){es.printStackTrace();}

}

//--------------------------------------------------------------------

}

if(jmi == miClose)

Page 9: Bank Simulator in JAVA(swings)

{

flag = true;

System.out.println ("flag = "+flag +"and flag2 ="+flag2);

closeAccount = new JInternalFrame("Close Accout",false,true,false,true);

closeAccount.setBounds(200,200,320,200);

closeAccount.setLayout(new FlowLayout());

JLabel lbAccNo = new JLabel("Account No");

JLabel lbName = new JLabel("Name");

JLabel lbAmt = new JLabel("Amount");

close_tfAccNo = new JTextField();

close_tfAccNo.setName("close_tfAccNo");

close_tfAccNo.setColumns(20);

close_tfName = new JTextField();

// close_tfName.setEditable(false);

close_tfName.setColumns(20);

close_tfAmt = new JTextField();

close_tfAmt.setEditable(false);

close_tfAmt.setColumns(20);

JButton close_button_yes = new JButton("YES");

JButton close_button_no = new JButton("NO");

JButton close_button_close = new JButton("CLOSE");

Page 10: Bank Simulator in JAVA(swings)

closeAccount.add(lbAccNo);

closeAccount.add(close_tfAccNo);

closeAccount.add(lbName);

closeAccount.add(close_tfName);

closeAccount.add(lbAmt);

closeAccount.add(close_tfAmt);

closeAccount.add(close_button_yes);

closeAccount.add(close_button_no);

closeAccount.add(close_button_close);

// ----------------------- Adding Listner to Internal Frame components------------

close_tfAccNo.addActionListener(new InternalActionListener());

close_button_yes.addActionListener(new InternalActionListener());

close_button_no.addActionListener(new InternalActionListener());

close_button_close.addActionListener(new InternalActionListener());

//- --------------------------------------------------------------------------------

desktopPane.add(closeAccount);

closeAccount.setVisible(true);

}

if(jmi == miDeposit)

{

Page 11: Bank Simulator in JAVA(swings)

flag2 = true;

System.out.println ("flag = "+flag +"and flag2 ="+flag2);

depositAccount = new JInternalFrame("Deposit",false,true,false,true);

depositAccount.setBounds(200,200,320,200);

depositAccount.setLayout(new FlowLayout());

JLabel lbAccNo = new JLabel("Account No");

JLabel lbName = new JLabel("Name");

JLabel lbBal = new JLabel("Balance");

JLabel lbAmt = new JLabel("Amount");

deposit_tfAccNo = new JTextField();

deposit_tfAccNo.setColumns(20);

deposit_tfAccNo.setName("deposit_tfAccNo");

deposit_tfName = new JTextField();

deposit_tfName.setColumns(20);

deposit_tfName.setEditable(false);

deposit_tfBal = new JTextField();

deposit_tfBal.setColumns(20);

deposit_tfBal.setEditable(false);

deposit_tfAmt = new JTextField();

deposit_tfAmt.setColumns(20);

JButton deposit_button_update = new JButton("Update");

JButton deposit_button_cancel = new JButton("Cancel");

Page 12: Bank Simulator in JAVA(swings)

JButton deposit_button_close = new JButton("Close");

depositAccount.add(lbAccNo);

depositAccount.add(deposit_tfAccNo);

depositAccount.add(lbName);

depositAccount.add(deposit_tfName);

depositAccount.add(lbBal);

depositAccount.add(deposit_tfBal);

depositAccount.add(lbAmt);

depositAccount.add(deposit_tfAmt);

depositAccount.add(deposit_button_update);

depositAccount.add(deposit_button_cancel);

depositAccount.add(deposit_button_close);

// ----------------------- Adding Listner to Internal Frame components------------

deposit_tfAccNo.addActionListener(new InternalActionListener());

deposit_button_update.addActionListener(new InternalActionListener());

deposit_button_cancel.addActionListener(new InternalActionListener());

deposit_button_close.addActionListener(new InternalActionListener());

//- --------------------------------------------------------------------------------

desktopPane.add(depositAccount);

depositAccount.setVisible(true);

Page 13: Bank Simulator in JAVA(swings)

}

if(jmi == miWithdraw)

{

withdrawAccount = new JInternalFrame("Withdraw",false,true,false,true);

withdrawAccount.setBounds(200,200,320,200);

withdrawAccount.setLayout(new FlowLayout());

JLabel lbAccNo = new JLabel("Account No");

JLabel lbName = new JLabel("Name");

JLabel lbBal = new JLabel("Balance");

JLabel lbAmt = new JLabel("Amount");

withdraw_tfAccNo = new JTextField();

withdraw_tfAccNo.setColumns(20);

withdraw_tfName = new JTextField();

withdraw_tfName.setColumns(20);

withdraw_tfName.setEditable(false);

withdraw_tfBal = new JTextField();

withdraw_tfBal.setColumns(20);

Page 14: Bank Simulator in JAVA(swings)

withdraw_tfBal.setEditable(false);

withdraw_tfAmt = new JTextField();

withdraw_tfAmt.setColumns(20);

JButton withdraw_button_update = new JButton("Update.");

JButton withdraw_button_cancel = new JButton("Cancel.");

JButton withdraw_button_close = new JButton("Close.");

withdrawAccount.add(lbAccNo);

withdrawAccount.add(withdraw_tfAccNo);

withdrawAccount.add(lbName);

withdrawAccount.add(withdraw_tfName);

withdrawAccount.add(lbBal);

withdrawAccount.add(withdraw_tfBal);

withdrawAccount.add(lbAmt);

withdrawAccount.add(withdraw_tfAmt);

withdrawAccount.add(withdraw_button_update);

withdrawAccount.add(withdraw_button_cancel);

withdrawAccount.add(withdraw_button_close);

//-------------------- add listener to internal ------------------------

withdraw_tfAccNo.addActionListener(new InternalActionListener());

withdraw_button_update.addActionListener(new InternalActionListener());

Page 15: Bank Simulator in JAVA(swings)

withdraw_button_cancel.addActionListener(new InternalActionListener());

withdraw_button_close.addActionListener(new InternalActionListener());

//--------------------------------------------------------------------------

desktopPane.add(withdrawAccount);

withdrawAccount.setVisible(true);

}

if(jmi == miExit)

{

frame.dispose();

}

}

// Internal class for maintain internal action listener------------------------

//------------------------------------------------------------------------

public class InternalActionListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

JButton button =null;

JTextField textField = null;

// String string = e.getActionCommand();

Page 16: Bank Simulator in JAVA(swings)

System.out.println ("===="+e.getSource().getClass()+"====");

// int id = e.getID();

if(e.getSource().getClass().toString().equals("class javax.swing.JButton"))

{

button = (JButton)e.getSource();

}

if(e.getSource().getClass().toString().equals("class javax.swing.JTextField"))

{

textField = (JTextField)e.getSource();

}

if(button!=null)

{

if(button.getText().equals("Create"))

{

System.out.println ("I m in create");

int account = Integer.parseInt(tfAccNo.getText());

String name = tfName.getText();

int ammount = Integer.parseInt(tfAmmount.getText());

try

Page 17: Bank Simulator in JAVA(swings)

{

Connection con = ConnectionClass.getConnection();

Statement stmt = con.createStatement();

String query = "insert into bank.banktable(Name,accountNo,amount) values('"+name+"','"+

account+"','"+ammount+"')";

stmt.execute(query);

}

catch(Exception ex)

{

System.out.println ("hello"+ex);

}

finally

{

try{ConnectionClass.closed();}

catch(Exception ex){}

}

int j = JOptionPane.showConfirmDialog(null,"Account has been created","",JOptionPane.PLAIN_MESSAGE);

System.out.println (j);

if(j==0)

newAccount.dispose();

}

Page 18: Bank Simulator in JAVA(swings)

if(button.getText().equals("Cancel"))

{

System.out.println("I m in Cancle");

tfAmmount.setText("");

tfName.setText("");

}

if(button.getText().equals("Close") || button.getText().equals("CLOSE"))

{

System.out.println("I m in Close");

if(button.getText().equals("Close"))

{

if(newAccount != null)

newAccount.dispose();

else if(depositAccount != null)

depositAccount.dispose();

}

if(button.getText().equals("CLOSE"))

closeAccount.dispose();

}

Page 19: Bank Simulator in JAVA(swings)

if(button.getText().equals("Close."))

withdrawAccount.dispose();

if(button.getText().equals("YES"))

{

try

{

Connection con = ConnectionClass.getConnection();

System.out.println ("Conn create ");

Statement stmt = con.createStatement();

String accno = close_tfAccNo.getText();

System.out.println (accno);

stmt.execute("delete from bank.banktable where accountNo = "+accno+"");

int j = JOptionPane.showConfirmDialog(null,"Account Closed","",JOptionPane.PLAIN_MESSAGE);

if(j==0)

closeAccount.dispose();

}

catch(Exception ex)

Page 20: Bank Simulator in JAVA(swings)

{

System.out.println ("-------------------------------"+ex);

ex.printStackTrace();

}

finally

{

try{ConnectionClass.closed();}

catch(Exception es){es.printStackTrace();}

}

}

if(button.getText().equals("NO"))

{

JOptionPane.showMessageDialog(null,"dfdf");

closeAccount.dispose();

}

if(button.getText().equals("Update"))

{

System.out.println ("I m in Update");

try

{

int bal = Integer.parseInt(deposit_tfBal.getText());

Page 21: Bank Simulator in JAVA(swings)

int total = bal + Integer.parseInt(deposit_tfAmt.getText());

String str = deposit_tfAccNo.getText();

Connection con = ConnectionClass.getConnection();

Statement stmt = con.createStatement();

boolean b = stmt.execute("update bank.banktable set amount ="+total+" where accountNo='"+str+"'");

System.out.println (b);

if(b==false)

{

System.out.println ("");

int jop = JOptionPane.showConfirmDialog(null,"Your Account has been updated and your total ammount is "+total,"",JOptionPane.PLAIN_MESSAGE);

depositAccount.dispose();

}

ConnectionClass.closed();

}

catch(SQLException sqe)

{

sqe.printStackTrace();

// ConnectionClass.closed();

}

}

if(button.getText().equals("Update."))

Page 22: Bank Simulator in JAVA(swings)

{

System.out.println ("I m in Update");

try

{

int bal = Integer.parseInt(withdraw_tfBal.getText());

int total = bal - Integer.parseInt(withdraw_tfAmt.getText());

if(total<0)

total=0;

String str = withdraw_tfAccNo.getText();

Connection con = ConnectionClass.getConnection();

Statement stmt = con.createStatement();

boolean b = stmt.execute("update bank.banktable set amount ="+total+" where accountNo='"+str+"'");

System.out.println (b);

if(b==false)

{

System.out.println ("");

int jop = JOptionPane.showConfirmDialog(null,"Your Account has been updated and your total amount is "+total,"",JOptionPane.PLAIN_MESSAGE);

withdrawAccount.dispose();

}

ConnectionClass.closed();

}

catch(SQLException sqe)

Page 23: Bank Simulator in JAVA(swings)

{

sqe.printStackTrace();

// ConnectionClass.closed();

}

}

}

if(textField!=null)

{

// System.out.println(close_tfAccNo);

// System.out.println(deposit_tfAmt);

if(close_tfAccNo != null)

{

System.out.println ("i m in closetf_Name and flag = "+close_tfAccNo);

// close_tfName.setEditable(false);

System.out.println(" @@@@ "+textField.getName().toString());

try

{

Connection con = ConnectionClass.getConnection();

System.out.println ("Conn create ");

Statement stmt = con.createStatement();

Page 24: Bank Simulator in JAVA(swings)

String accno = close_tfAccNo.getText();

System.out.println (accno);

ResultSet rs = stmt.executeQuery("SELECT Name,amount FROM banktable where accountNo='"+accno+"'");

close_tfName.setText("");

close_tfAmt.setText("");

if(rs.next())

{

String name = rs.getString("Name");

System.out.println (name);

close_tfName.setText(name);

System.out.println (rs.getInt("amount"));

int amount = rs.getInt("amount");

close_tfAmt.setText(Integer.toString(amount));

ConnectionClass.closed();

}

}

catch(Exception ex)

{

System.out.println ("-------------------------------"+ex);

ex.printStackTrace();

Page 25: Bank Simulator in JAVA(swings)

}

finally

{

try{ConnectionClass.closed();}

catch(Exception es){es.printStackTrace();}

}

// flag2 false;

}

if(deposit_tfAccNo!=null)

{

try

{

System.out.println ("I m in deposite");

Connection con = ConnectionClass.getConnection();

System.out.println ("Conn create ");

Statement stmt = con.createStatement();

String accno = deposit_tfAccNo.getText();

System.out.println (accno);

ResultSet rs = stmt.executeQuery("SELECT Name,amount FROM banktable where accountNo='"+accno+"'");

deposit_tfName.setText("");

deposit_tfAmt.setText("");

deposit_tfBal.setText("");

Page 26: Bank Simulator in JAVA(swings)

if(rs.next())

{

String name = rs.getString("Name");

System.out.println (name);

deposit_tfName.setText(name);

System.out.println (rs.getInt("amount"));

int amount = rs.getInt("amount");

deposit_tfBal.setText(Integer.toString(amount));

ConnectionClass.closed();

}

}

catch(Exception ex)

{

System.out.println ("-------------------------------"+ex);

ex.printStackTrace();

}

finally

{

try{ConnectionClass.closed();}

catch(Exception es){es.printStackTrace();}

}

Page 27: Bank Simulator in JAVA(swings)

}

if(withdrawAccount != null)

{

try

{

System.out.println ("I m in withdraw");

Connection con = ConnectionClass.getConnection();

System.out.println ("Conn create ");

Statement stmt = con.createStatement();

String accno = withdraw_tfAccNo.getText();

System.out.println (accno);

ResultSet rs = stmt.executeQuery("SELECT Name,amount FROM banktable where accountNo='"+accno+"'");

withdraw_tfName.setText("");

withdraw_tfBal.setText("");

withdraw_tfAmt.setText("");

if(rs.next())

{

String name = rs.getString("Name");

System.out.println (name);

withdraw_tfName.setText(name);

Page 28: Bank Simulator in JAVA(swings)

System.out.println (rs.getInt("amount"));

int amount = rs.getInt("amount");

withdraw_tfBal.setText(Integer.toString(amount));

ConnectionClass.closed();

}

}

catch(Exception ex)

{

System.out.println ("-------------------------------"+ex);

ex.printStackTrace();

}

finally

{

try{ConnectionClass.closed();}

catch(Exception es){es.printStackTrace();}

}

}

}

}

Page 29: Bank Simulator in JAVA(swings)

}

public static void main(String[] args)

{

new BankSimulator();

}

}

class ConnectionClass

{

static Connection con =null;

static

{

try

{

Class.forName("com.mysql.jdbc.Driver");

}

catch(Exception e)

Page 30: Bank Simulator in JAVA(swings)

{

e.printStackTrace();

}

}

public static Connection getConnection()

{

try

{

String url="jdbc:mysql://localhost:3306/bank";

con=DriverManager.getConnection(url,"root","root");

System.out.println("connection estbalish");

return con;

}

catch(Exception e)

{

System.out.println ("I am in -- ConnectionClass/getConnection()/catch ");

e.printStackTrace();

return con;

}

}

public static void closed() throws SQLException

{

Page 31: Bank Simulator in JAVA(swings)

con.close();

}

}