Create a GUI app to run the bank account menu from ManageAccount. The checkReque
ID: 3695066 • Letter: C
Question
Create a GUI app to run the bank account menu from ManageAccount. The checkRequest method should call each of the choice from the menu.
public class MngAccount {
public static void main(String[] args) {
Account acct1, acct2;
// create account1 for Sally with $1000
acct1 = new Account(1000, "Sally", 1111);
// create account2 for Joe with $500
acct2 = new Account(500, "Joe", 2222);
// deposit $100 to Joe's account
acct2.deposit(100);
// print Joe's new balance (use getBalance())
System.out.println("Joe's Balance:" + acct2.getBalance());
// withdraw $50 from Sally's account
acct1.withdraw(50);
// print Sally's new balance (use getBalance())
System.out.println("Sally's Balance:" + acct1.getBalance());
// charge fees to both accounts
acct1.chargeFee();
acct2.chargeFee();
// change the name on Joe's account to Joseph
acct2.changeName("Joseph");
// print summary for both accounts
System.out.println(acct1);
System.out.println(acct2);
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Bank 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("Lets Create Account");
Button but1=new Button("now Test this Account");
BankAccounthere b;
bank()
{
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);
z[0]=new Label("Account Number");
z[1]=new Label(" Initial Balance");
z[2]=new Label("To Deposit Amount");
z[3]=new Label("To Withdraw Amount");
for(int i=0;i<4;i++)
{
t[i]=new TextField(10);
p.add(z[i]);
p.add(t[i]);
}
p.add(but);
p.add(but1);
but1.setVisible(false);
z[2].setVisible(false);
[ z]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.depositmoney(d_amt);
msg="Transaction Succesful";
try
{
b.withdrawmoney(w_amt);
}catch(FundsInsufficientException fe)
{
fe=new InsufficientfundsException(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);
z[2].setVisible(true);
z[3].setVisible(true);
t[2].setVisible(true);
t[3].setVisible(true);
but.setVisible(false);
z[0].setVisible(false);
z[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[])
{
Bank t1=new Bank();
t1.setTitle("This is to test Bank Account");
t1.setSize(600,200);
t1.setVisible(true);
}
}
class NewWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
class BankAccounthere
{
int accnum;
int amount;
BankAccounthere(int num,int amt)
{
accnum=num;
amount=amt;
}
public void depositmoney(int amt)
{
amount=amount+amt;
}
public void withdrawmoney(int amt) throws FundsInsufficientException
{
if(amt>amount)
throw new InsufficientfundsException(amount,amt);
else
amount=amount-amt;
}
}
class InsufficientfundsException extends Exception
{
int balance;
int withdraw_amount;
InsufficientfundsException(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+")..";
}
}