In: Computer Science
Information. in java Consider a banking system with 3 classes: BankAccount, CheckingAccount, and SavingsAccount, that you are to design and write. Have CheckingAccount and SavingsAccount inherit from BankAccount. In BankAccount, you should include an account number, an account balance, a deposit method, a toString method, and an abstract withdraw method. Since deposits work the same way in both child classes, make sure they cannot override it. The constructor should only take an initial deposit and generate a random 5 digit account number. The toString should display the account number and the balance.
In the CheckingAccount class add a minimum balance and a standard overdraft fee of $25. Implement the withdraw method so that overdrafts are allowed, but the overdraft fee is incurred if the balance drops below the minimum balance. Override the toString method to display everything the BankAccount toString displays plus the minimum balance. Use the parent class toString to do most of the work.
In the SavingsAccount class add an annual interest rate (with a default value of 1.5%) and a method to recalculate the balance every month. Since the interest rate is annual, make sure to calculate the interest accordingly. Override the toString method to display everything the BankAccount toString displays plus the interest rate. Like the CheckingAccount toString, you should use the parent class to do most of the work.
question. Write the Java implementation of the SavingsAccount. Note: no driver is required.
BankAccount.java
import java.util.Random;
public abstract class BankAccount {
private int accountNumber;
private double accountBalance;
public BankAccount(double initialDeposit) {
this.accountBalance = initialDeposit;
Random rand = new Random();
this.accountNumber = rand.nextInt(99999 - 10000 + 1) + 10000;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public double getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(double accountBalance) {
this.accountBalance = accountBalance;
}
public final void deposit(double amount) {
this.accountBalance = this.accountBalance + amount;
}
public abstract boolean withdraw(double amount);
public String toString() {
return "Account Number: " + this.accountNumber + ", Balance: "
+ this.accountBalance;
}
}
CheckingAccount.java
public class CheckingAccount extends BankAccount {
private double minimumBalance;
private static int OVERDRAFT_FEE = 25;
public CheckingAccount(double initialDeposit, double minimum) {
super(initialDeposit);
this.minimumBalance = minimum;
}
@Override
public boolean withdraw(double amount) {
this.setAccountBalance(this.getAccountBalance() - amount);
if (this.getAccountBalance() < minimumBalance) {
this.setAccountBalance(this.getAccountBalance() - OVERDRAFT_FEE);
}
return true;
}
public double getMinimumBalance() {
return minimumBalance;
}
public void setMinimumBalance(double minimumBalance) {
this.minimumBalance = minimumBalance;
}
public String toString() {
return super.toString() + ", Minimum Balance: " + this.minimumBalance;
}
}
SavingsAccount.java
public class SavingsAccount extends BankAccount {
public double annualInterestRate;
public SavingsAccount(double initialDeposit) {
super(initialDeposit);
this.annualInterestRate = 1.5;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
@Override
public boolean withdraw(double amount) {
if ((this.getAccountBalance() - amount) < 0) {
return false;
} else {
this.setAccountBalance(this.getAccountBalance() - amount);
return true;
}
}
public void recalculateMonthlyBalance() {
double monthlyInterest = this.getAccountBalance()
* (this.annualInterestRate / 1200.0);
this.setAccountBalance(this.getAccountBalance() + monthlyInterest);
}
public String toString() {
return super.toString() + ", Annual Interest Rate: "
+ this.annualInterestRate;
}
}