a) Define the class bankAccount to store a bank customer’s account number and ba
ID: 669098 • Letter: A
Question
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).
Explanation / Answer
import java.util.Date;
public class AccountsTest {
public static void main(String[] args) throws Exception{
CheckingAccount ca = new CheckingAccount(1, 1000.0, 5, 500, 10 );
ca.displayAcInfo();
ca.deposit(110);
ca.withdraw(700);
ca.displayAcInfo();
System.out.println(" Interest for 4 months with current Balance: "+ ca.calculateInterest(4));
SavingsAccount sa = new SavingsAccount(2, 5000.0, 5);
sa.withdraw(1000);
sa.deposit(100);
sa.displayAcInfo();
sa.withdraw(7000);
}
}
class BankAccount {
protected int accountNo =0 ;
protected double balance = 0;
public BankAccount() {};
public BankAccount(int actNo, double balance) {
this.accountNo = actNo;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if((balance-amount) < 0)
System.out.println(" Not enough balance to withdraw");
else
balance -=amount;
}
public void displayAcInfo() {
System.out.println(" Account No: "+accountNo+" Balance: "+ balance+ " Cur Date : "+ new Date());
}
public int getAccountNo() {
return accountNo;
}
public void setAccountNo(int accountNo) {
this.accountNo = accountNo;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
class CheckingAccount extends BankAccount {
private double interestRate = 0;
private double minBalance = 0;
private double serviceCharge = 0;
private double serivceChargesPaid =0;
public CheckingAccount() {
super();
}
public CheckingAccount(int actNo, double balance, double interestRate, double minBalance, double serviceCharge) {
super(actNo, balance);
this.interestRate = interestRate; //per month
this.minBalance = minBalance;
this.serviceCharge = serviceCharge;
}
public double calculateInterest( int noOfmonths) {
return (this.balance * noOfmonths / 100);
}
public void withdraw(double amount) {
super.withdraw(amount);
if( (this.balance - minBalance) < 0 ) {
this.balance -= this.serviceCharge;
serivceChargesPaid += serviceCharge;
}
}
public void displayAcInfo() {
System.out.println(" Account No: "+accountNo+" Account Type: Checking Balance: "+ balance+ " Cur Date : "+ new Date());
System.out.println(" Interest rate : "+interestRate+" minimum Balance : "+minBalance+" Service charge paid : "+serivceChargesPaid);
}
public double getSerivceChargesPaid() {
return serivceChargesPaid;
}
public void setSerivceChargesPaid(double serivceChargesPaid) {
this.serivceChargesPaid = serivceChargesPaid;
}
public boolean isMinBalanceThere() {
return this.balance > minBalance;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
public double getMinBalance() {
return minBalance;
}
public void setMinBalance(double minBalance) {
this.minBalance = minBalance;
}
public double getServiceCharge() {
return serviceCharge;
}
public void setServiceCharge(double serviceCharge) {
this.serviceCharge = serviceCharge;
}
}
class SavingsAccount extends BankAccount {
private double interestRate; //per month
public SavingsAccount() {
super();
interestRate = 0;
}
public SavingsAccount(int actNo, double balance, double interestRate) {
super(actNo, balance);
this.interestRate = interestRate; //per month
}
public double calculateInterest( int noOfmonths) {
return (this.balance * noOfmonths / 100);
}
public void withdraw(double amount) {
super.withdraw(amount);
}
public void displayAcInfo() {
System.out.print(" Account No: "+accountNo+" Account Type: Savings Balance: "+ balance+ " Cur Date : "+ new Date());
System.out.println(" Interest rate : "+interestRate);
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.