Sie sind auf Seite 1von 13

// bank Simulator

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.*;

class BankSimulator implements ActionListener


{
JFrame frame;
JMenuBar menubar;
JMenu mAccount,mTransaction;
JMenuItem miNew,miClose,miExit,miDeposit,miWithdraw;
JDesktopPane desktopPane;
JInternalFrame newAccount,closeAccount,depositAccount,withdrawAccount;
Toolkit toolKit;
Dimension dimension;

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

BankSimulator()
{
frame = new JFrame();
menubar = new JMenuBar();
frame.add(menubar);

mAccount = new JMenu("Account");


mAccount.setMnemonic(KeyEvent.VK_A);
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);

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-----------------------------

miNew.addActionListener(this);
miClose.addActionListener(this);
miExit.addActionListener(this);
miDeposit.addActionListener(this);
miWithdraw.addActionListener(this);

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

}
public void actionPerformed(ActionEvent ae)
{
JMenuItem jmi = null;

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

jmi = (JMenuItem)ae.getSource();

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);
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");
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)
{
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.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");

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)
{
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_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");
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);

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");

JTextField withdraw_tfAccNo = new JTextField();


withdraw_tfAccNo.setColumns(20);
JTextField withdraw_tfName = new JTextField();
withdraw_tfName.setColumns(20);
withdraw_tfName.setEditable(false);
JTextField withdraw_tfBal = new JTextField();
withdraw_tfBal.setColumns(20);
withdraw_tfBal.setEditable(false);
JTextField 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);

desktopPane.add(withdrawAccount);
withdrawAccount.setVisible(true);
}

}
// 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();
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
{
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.OK_OPTION);
System.out.println (j);
if(j==0)
frame.dispose();

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"))
newAccount.dispose();
if(button.getText().equals("CLOSE"))
closeAccount.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+"");

}
catch(Exception ex)
{
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(textField!=null)
{
System.out.println("");

close_tfName.setEditable(false);

System.out.println(textField);
try
{
Connection con = ConnectionClass.getConnection();
System.out.println ("Conn create ");
Statement stmt = con.createStatement();
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();
}
finally
{
try{ConnectionClass.closed();}
catch(Exception es){es.printStackTrace();}
}

}
}

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)
{
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


{
con.close();
}
}

Das könnte Ihnen auch gefallen