This assignment will build upon Assignment Two. You will no longer use an array
ID: 3571997 • Letter: T
Question
This assignment will build upon Assignment Two. You will no longer use an array to verify data rather you will need to create a MySql database to hold customer information. You will use the GUI you created in Assignment Two to allow Customers to login to their Bank accounts. The customer’s data will be stored in a database table. It will contain the following fields: customer name, PIN, customer email, type of account (Checking, savings, etc) and account balance. Note customers can have more than one type of an account. For example they can have both a checking and a savings account. Assignment Two allowed for the login verification to take place. In Assignment Three will build upon this system and allow the user to access their account and let them withdraw and deposit into an account, or logout of the system. Once the verification process is completed the user should hit continue which will allow another GUI to popup (please see attached file). This GUI will allow the user to choose a transaction (deposit, withdraw or logout). As you can see from the GUI the balance is displayed before a transaction takes place as is the customer's name. If the user chooses to deposit or withdraw from an account the GUI must then display the new account balance after completing the transaction. You will also need to display the entire database table before and after each transaction so I can actually see that the DB that data has been inserted and deleted into the table. I will leave it up to you how you wish to do this.
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class createBankaccount extends Frame implements ActionListener
{
Label labone=new Label(" ");
Label labtwo=new Label(" ");
TextField t[]=new TextField [4];
Label l[]=new Label [4];
Button butone=new Button("First Create Account");
Button buttwo=new Button("next Test Account");
createBankAccount b;
createBankaccount()
{
addWindowListener(new NewWindowAdapter());
setLayout(new GridLayout(2,0));
Panel pone=new Panel();
Panel ptwo=new Panel();
butone.addActionListener(this);
buttwo.addActionListener(this);
p.setLayout(new GridLayout(5,2));
ptwo.add(labtwo);
ptwo.add(labone);
l[0]=new Label("Enter Account Number");
l[1]=new Label("Enter 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);
pone.add(l[i]);
pone.add(t[i]);
}
pone.add(butone);
pone.add(buttwo);
buttwo.setVisible(false);
l[2].setVisible(false);
l[3].setVisible(false);
t[2].setVisible(false);
t[3].setVisible(false);
add(pone);
add(ptwo);
}
String testAccount(int deposit_amt,int withdraw_amt)
{
String message;
b.deposit(deposit_amt);
message="This Transaction is Succesful";
try
{
b.withdraw(withdraw_amt);
}catch(FundsInsufficientException z)
{
z=new FundsInsufficientException(b.amount,withdraw_amt);
message=String.valueOf(z);
}
return message;
}
public void actionPerformed(ActionEvent ae)
{
String stringeone=ae.getActionCommand();
if(stringone.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("The Account : "+b.accnum+",The Current Balance : "+b.amount);
return;
}
else
{
lab.setText(testAccount(Integer.parseInt(t[2].getText()),Integer.parseInt(t[3].getText())));
lab1.setText("The Account : "+b.accnum+", The Current Balance : "+b.amount);
}
}
public static void main(String arg[])
{
Bankaccount p=new Bankaccount();
p.setTitle("Create a Bank Account ");
p.setSize(600,200);
p.setVisible(true);
}
}
class NewWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
class BankAccount
{
int accountnum;
int newamount;
BankAccount(int num,int amt)
{
accountnum=num;
newamount=amt;
}
public void deposit(int amt)
{
newamount=newamount+amt;
}
public void withdraw(int amt) throws FundsInsufficientException
{
if(amt>newamount)
throw new FundsInsufficientException(amount,amt);
else
newamount=newamount-amt;
}
}
class FundsInsufficientException extends Exception
{
int balance;
int withdraw_amount;
FundsInsufficientException(int bal,int withdraw_amt)
{
balance=bal;
withdraw_amount=withdraw_amt;
}
public String toString()
{
return "Your withdraw amount ("+withdraw_amount+") is less than the balance ("+balance+"). No withdrawal was recorded.";
}
}