In: Computer Science
Object Design Example: Bank Account Object
Implement the bank account class that stores account number and the balance. It has methods to deposit and withdraw money. In addition, the default constructor sets account number to 0000 and balance 0. Other constructor should take the account number and the initial balance as parameters. Finally, the account class should have a static data member that stores how many accounts are created for a given application.
a) Draw the UML diagram
b) Write Java code to implement the bank account class
UML:
Account.java:
public class Account {
private String accNo;
private int balance;
static int count;
public Account(){
this.accNo="0000";
this.balance=0;
count=count+1;
}
public Account(String accNo,int balance){
this.accNo=accNo;
this.balance=balance;
count=count+1;
}
public String getAccNo() {
return accNo;
}
public void setAccNo(String accNo) {
this.accNo = accNo;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public void withdraw(int amount){
this.balance=this.balance-amount;
}
public void deposit(int amount){
this.balance=this.balance+amount;
}
public String toString(){
return "Account No:
"+this.accNo+"\nBalance: "+this.balance;
}
}
Tester:
public class AccountTester {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Account acc=new Account();
acc.deposit(100);
System.out.println(acc.toString());
acc.withdraw(20);
System.out.println(acc.toString());
Account acc1=new Account("1231",
200);
System.out.println(acc1.toString());
System.out.println("No of Accounts:
"+Account.count);
}
}
Expected output: