a Define the class bankAccount to store a bank customers acc
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class inherits members to store the account number and the balance from the base class. A customer with a checking account typically receives interest, maintains a minimum balance, and pays service charges if the balance falls below the minimum balance. Add member variables to store this additional information. In addition to the operations inherited from the base class, this class should provide the following operations: set interest rate, retrieve interest rate, set minimum balance, retrieve minimum balance, set service charges, retrieve service charges, post interest, verify if the balance is less than the minimum balance, write a check, withdraw (override the method of the base class), and print account information. Add appropriate constructors. c. Every bank offers a savings account. Derive the class savingsAccount from the class bankAccount (designed in part (a)). This class inherits members to store the account number and the balance from the base class. A customer with a savings account typically receives interest, makes deposits, and withdraws money. In addition to the operations inherited from the base class, this class should provide the following operations: set interest rate, retrieve interest rate, post interest, withdraw (override the method of the base class), and print account information. Add appropriate constructors. d. Write a program to test your classes designed in parts (b) and (c).
Solution
package org.jay.sample;
class BankAccount{
private int accountNumber;
private double balance;
/**
* @param accountNumber
* @param balance
*/
public BankAccount(int accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
/**
* @return the accountNumber
*/
public int getAccountNumber() {
return accountNumber;
}
/**
* @param accountNumber the accountNumber to set
*/
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
/**
* @return the balance
*/
public double getBalance() {
return balance;
}
/**
* @param balance the balance to set
*/
public void setBalance(double balance) {
this.balance = balance;
}
/**
* deposit money
*/
public void deposit(double money){
balance+=money;
}
/**
* withdraw money
*/
public void withdraw(double money){
balance-=money;
}
/*
* account information
*/
@Override
public String toString() {
return \"BankAccount [accountNumber=\" + accountNumber + \", balance=\" + balance + \"]\";
}
public void printAccountInformation(){
System.out.println(\"Account Number : \"+accountNumber);
System.out.println(\"Available Balance : \"+balance);
}
}
class CheckingAccount extends BankAccount{
private double minBalance;
private double interestRate;
private double serviceCharge;
/**
* @param accountNumber
* @param balance
* @param minBalance
* @param interestRate
* @param serviceCharge
*/
public CheckingAccount(int accountNumber, double balance, double minBalance, double interestRate,
double serviceCharge) {
super(accountNumber, balance);
this.minBalance = minBalance;
this.interestRate = interestRate;
this.serviceCharge = serviceCharge;
}
@Override
public void withdraw(double money) {
if(money<=getBalance()){
super.withdraw(money);
}else{
System.out.println(\"You do not have suffiecient balance!!\");
}
}
/**
* @return the minBalance
*/
public double getMinBalance() {
return minBalance;
}
/**
* @param minBalance the minBalance to set
*/
public void setMinBalance(double minBalance) {
this.minBalance = minBalance;
}
/**
* @return the interestRate
*/
public double getInterestRate() {
return interestRate;
}
/**
* @param interestRate the interestRate to set
*/
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
/**
* @return the serviceCharge
*/
public double getServiceCharge() {
return serviceCharge;
}
/**
* @param serviceCharge the serviceCharge to set
*/
public void setServiceCharge(double serviceCharge) {
this.serviceCharge = serviceCharge;
}
public boolean verifyMinimumBalanceMaintained(){
return getBalance()>minBalance;
}
public double postInterest(){
double balance=0.0;
balance=getBalance()+((getBalance()*interestRate)/100);
return balance;
}
public void printAccountInformation(){
System.out.println(\"Account Number : \"+getAccountNumber());
System.out.println(\"Available Balance : \"+getBalance());
System.out.println(\"Minimum Balance : \"+minBalance);
System.out.println(\"Interest Rate : \"+interestRate+\" %\");
System.out.println(\"Post interest : \"+postInterest());
System.out.println(\"Service charge : \"+serviceCharge+\" %\");
System.out.println(\"Minimum balance maintained : \"+verifyMinimumBalanceMaintained());
}
}
class SavingsAccount extends BankAccount{
private double interestRate;
/**
* @param accountNumber
* @param balance
* @param interestRate
*/
public SavingsAccount(int accountNumber, double balance, double interestRate) {
super(accountNumber, balance);
this.interestRate = interestRate;
}
/**
* @return the interestRate
*/
public double getInterestRate() {
return interestRate;
}
/**
* @param interestRate the interestRate to set
*/
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
public double postInterest(){
double balance=0.0;
balance=getBalance()+((getBalance()*interestRate)/100);
return balance;
}
@Override
public void withdraw(double money) {
if(money<=getBalance()){
super.withdraw(money);
}else{
System.out.println(\"You do not have suffiecient balance!!\");
}
}
public void writeCheck(double amount){
if(getBalance()>=amount){
setBalance(getBalance()-amount);
}else{
System.out.println(\"Check bounced\");
}
}
public void printAccountInformation(){
System.out.println(\"Account Number : \"+getAccountNumber());
System.out.println(\"Available Balance : \"+getBalance());
System.out.println(\"Interest Rate : \"+interestRate+\" %\");
System.out.println(\"Post interest : \"+postInterest());
}
}
public class BankDemo {
public static void main(String[] args) {
CheckingAccount account=new CheckingAccount(786, 10000, 1000, 10, 2);
account.printAccountInformation();
account.withdraw(9500);
System.out.println(\"------------------------------------------------------\");
account.printAccountInformation();
System.out.println(\"-----------------------------Savings account---------------------------\");
SavingsAccount savings=new SavingsAccount(890, 5000, 8);
savings.printAccountInformation();
}
}
--------------------------------------------------------------output-----------------------------------------------------------------------------------
Account Number : 786
Available Balance : 10000.0
Minimum Balance : 1000.0
Interest Rate : 10.0 %
Post interest : 11000.0
Service charge : 2.0 %
Minimum balance maintained : true
------------------------------------------------------
Account Number : 786
Available Balance : 500.0
Minimum Balance : 1000.0
Interest Rate : 10.0 %
Post interest : 550.0
Service charge : 2.0 %
Minimum balance maintained : false
-----------------------------Savings account---------------------------
Account Number : 890
Available Balance : 5000.0
Interest Rate : 8.0 %
Post interest : 5400.0




