Create a JAVA class for Bank Account abstract data type. It should have the attributes account number (integer), account holder (string) and current balance (float). Define methods to get and set account numbers and account holders (total = 4). Add a method, which will return the account holders current balance. Finally, add two methods, which allow deposit and withdrawal of money updating the current balance as appropriate. NB: Dont add any other methods (member functions)
Using the Bank Account class above, write a program to test these methods as follows: -
i) Create three bank accounts, two to start with balances 0.00 and one with 2,000.
ii) Set the account numbers to 101,102 and 103 respectively.
iii) Set names of account holders.
iv) Credit (deposit) account 101 with 2,000.00 and account 102 with 1,750.00.
v) Debit (withdraw) account 103 with 1,250.00
vi) Display the account numbers, account holders and current balance for the three
public class BankAccount {
private int accountNumber;
private String accountHolder;
private float currentBalance;
public int getAccountNumber(){
return this.accountNumber;
}
public String getAccountHolder(){
return this.accountHolder;
}
public void setAccountNumber(int number){
this.accountNumber=number;
}
public void setAccountHolder(String name){
this.accountHolder=name;
}
public float getCurrentBalance(){
return this.currentBalance;
}
public void depositMoney(float money){
this.currentBalance+=money;
}
public void withdrawMoney(float money){
this.currentBalance-=money;
}
}
public class BankAccountTest {
public static void main(String args[]){
//i
BankAccount first = new BankAccount();
BankAccount second = new BankAccount();
BankAccount third = new BankAccount();
third.depositMoney(2000);
//ii
first.setAccountNumber(101);
second.setAccountNumber(102);
third.setAccountNumber(103);
//iii
first.setAccountHolder("Antony");
second.setAccountHolder("John");
third.setAccountHolder("Abdul");
//iv
first.depositMoney(2000);
second.depositMoney(1750);
//v
third.withdrawMoney(1250);
//vi
System.out.println("Account number: "+first.getAccountNumber());
System.out.println("Account holder: "+first.getAccountHolder());
System.out.println("Current balance: "+first.getCurrentBalance());
System.out.println();
System.out.println("Account number: "+second.getAccountNumber());
System.out.println("Account holder: "+second.getAccountHolder());
System.out.println("Current balance: "+second.getCurrentBalance());
System.out.println();
System.out.println("Account number: "+third.getAccountNumber());
System.out.println("Account holder: "+third.getAccountHolder());
System.out.println("Current balance: "+third.getCurrentBalance());
}
}
Comments
Leave a comment