Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Beginners Lab Assignments Code Examples

Program to create GUI for Bank Account Simulation

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
Program to create GUI for Bank Account Simulation import java.awt.*; import java.awt.event.*; import javax.swing.*; class GuiAccTest extends Frame implements ActionListener { 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; GuiAccTest() { addWindowListener(new NewWindowAdapter()); setLayout(new GridLayout(2,0)); Panel p=new Panel(); Panel p1=new Panel(); but.addActionListener(this); but1.addActionListener(this); p.setLayout(new GridLayout(5,2)); p1.add(lab1); p1.add(lab); l[0]=new Label("Account Number"); l[1]=new Label("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); } String testAccount(int d_amt,int w_amt) { String msg; b.deposit(d_amt); 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 void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); if(str.equals("Create Account")) { b=new BankAccount(Integer.parseInt(t[0].getText()),Integer.parseInt(t[1].getText())); but1.setVisible(true); l[2].setVisible(true); l[3].setVisible(true); t[2].setVisible(true); 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); return; } else { lab.setText(testAccount(Integer.parseInt(t[2].getText()),Integer.parseInt(t[3].getText()))); lab1.setText("Account : "+b.accnum+", Current Balance : "+b.amount); } } public static void main(String arg[]) { GuiAccTest at=new GuiAccTest(); at.setTitle("Bank Account Tester"); at.setSize(600,200); at.setVisible(true); } } class NewWindowAdapter extends WindowAdapter { public void windowClosing(WindowEvent we) { System.exit(0); } } 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 less than the balance ("+balance+"). No withdrawal was recorded."; } }