Add event handling to the AccountUI class which will allow you to: 1. Create new account, when the ââ?¬Ë?Createââ?¬â?¢ button is pressed a) Make sure the user has entered an ID and the starting balance b) Create an account using the Account class constructor c) Update the ââ?¬Ë?Current Balanceââ?¬Â? Text field in the Transaction report section d) Update the ââ?¬Ë?Account Date Createdââ?¬â?¢ Text Field e) Hint: declare a global class variable of the type Account so it could be used in the AccountUI inner classes (ActionListener classes). 2. Execute a ââ?¬Ë?Depositââ?¬â?¢ or ââ?¬Ë?Withdrawalââ?¬â?¢ when the ââ?¬Ë?Executeââ?¬â?¢ button is pressed a) Use the Account correct method for the specific transaction b) Make sure that an amount is entered and one of the Radio buttons is selected. c) Update the ââ?¬Ë?Current Balanceââ?¬Â? Text field in the Transaction report section d) Add the transaction to the list of transactions defined in the Account class 3. Show all transactions in the TextArea when the ââ?¬Å?Reportââ?¬Â? button is pressed a) Get the list of transactions using the getTrasactions() method of the class Account. b) For each transactions print: date of transaction, type of transaction, amount of transaction and balance after transaction execution. c) All of the methods are provided by the interface Transaction. 4. Add the interface transaction to your project 5. Add the inner class ââ?¬Å?TransactionObjectââ?¬Â? in the Account class. 6. Add the object transactionList to the Account class as follow: a) List<Transaction> transactionList = new ArrayList< Transaction>(); See the figure below.
Here is an example of BankAccount using swing components which allow the user to withdraw and deposit amount and display the current balance.
import java.awt.*; import java.awt.event.*; import javax.swing.*; class Account extends Frame { Label lab=new Label(" "); Label lab1=new Label(" "); TextField t[]=new TextField [4]; Label l[]=new Label [4]; Button but=new Button("Create Account"); Button but1=new Button("Test Account"); BankAccount b; Account(){ addWindowListener(new NewWindowAdapter()); setLayout(new GridLayout(2,0)); Panel p=new Panel(); Panel p1=new Panel(); p.setLayout(new GridLayout(5,2)); p.setBackground(Color.pink); p1.add(lab1); p1.add(lab); l[0]=new Label("Enter Your Account Number"); l[1]=new Label("Enter Your Initial Balance"); l[2]=new Label("Deposit Amount"); l[3]=new Label("Withdraw Amount"); for(int i=0;i<4;i++){ t[i]=new TextField(10); p.add(l[i]); p.add(t[i]); } p.add(but); p.add(but1); but1.setVisible(false); l[2].setVisible(false); l[3].setVisible(false); t[2].setVisible(false); t[3].setVisible(false); add(p); add(p1); p1.setBackground(Color.pink); but.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String des=JOptionPane.showInputDialog(null,"Deposit or Withdraw"); if(des.equals("Deposit")){ b=new BankAccount(Integer.parseInt(t[0].getText()),Integer.parseInt(t[1].getText())); but1.setVisible(true); l[2].setVisible(true); l[3].setVisible(false); t[2].setVisible(true); t[3].setVisible(false); but.setVisible(false); l[0].setVisible(false); l[1].setVisible(false); t[0].setVisible(false); t[1].setVisible(false); lab1.setText("Account : "+b.accnum+", Current Balance : "+b.amount); but1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ lab.setText(testAccount1(Integer.parseInt(t[2].getText()))); lab1.setText("Account : "+b.accnum+", Current Balance : "+b.amount); } }); } else if(des.equals("Withdraw")){ b=new BankAccount(Integer.parseInt(t[0].getText()),Integer.parseInt(t[1].getText())); but1.setVisible(true); l[2].setVisible(false); l[3].setVisible(true); t[2].setVisible(false); t[3].setVisible(true); but.setVisible(false); l[0].setVisible(false); l[1].setVisible(false); t[0].setVisible(false); t[1].setVisible(false); lab1.setText("Account : "+b.accnum+", Current Balance : "+b.amount); but1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ lab.setText(testAccount2(Integer.parseInt(t[3].getText()))); lab1.setText("Account : "+b.accnum+", Current Balance : "+b.amount); } }); } } }); } String testAccount1(int d_amt){ String msg; b.deposit(d_amt); msg="Transaction Succesful"; return msg; } String testAccount2(int w_amt){ String msg; msg="Transaction Succesful"; try{ b.withdraw(w_amt); }catch(FundsInsufficientException fe){ fe=new FundsInsufficientException(b.amount,w_amt); msg=String.valueOf(fe); } return msg; } public static void main(String arg[]){ Account at=new Account (); at.setTitle("Bank Account System"); at.setSize(500,300); at.setVisible(true); } } class NewWindowAdapter extends WindowAdapter{ public void windowClosing(WindowEvent we){ System.exit(0); } }
continue..
class BankAccount{ int accnum; int amount; BankAccount(int num,int amt){ accnum=num; amount=amt; } public void deposit(int amt){ amount=amount+amt; } public void withdraw(int amt) throws FundsInsufficientException{ if(amt>amount) throw new FundsInsufficientException(amount,amt); else amount=amount-amt; } } class FundsInsufficientException extends Exception{ int balance; int withdraw_amount; FundsInsufficientException(int bal,int w_amt){ balance=bal; withdraw_amount=w_amt; } public String toString(){ return "Your withdraw amount ("+withdraw_amount+") is more than the balance ("+balance+"). No withdrawal was recorded."; } }
For more information, visit the following link:
http://www.roseindia.net/tutorial/java/core/bankAccountApplication.html
Ads