In: Computer Science
Information:
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 BankAccount. Note, no driver is required.
Note:
As mentioned no need of Driver class.I am not including it..
Could you plz go through this code and let me know if u
need any changes in this.Thank You
=================================
// BankAccount.java
public abstract class BankAccount {
private int accountNumber;
private double balance;
/**
* @param balance
*/
public BankAccount(double balance) {
this.accountNumber = (int)
(Math.random() * ((99999 - 10000) + 1)) + 10000;
this.balance = balance;
}
public void deposit(double amount) {
if (balance > 0) {
this.balance +=
amount;
}
}
/**
* @return the balance
*/
public double getBalance() {
return balance;
}
/**
* @param balance
* the balance to set
*/
protected void setBalance(double balance) {
this.balance = balance;
}
public abstract void withdraw(double amt);
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "AccountNumber = " +
accountNumber + ", Balance = " + balance;
}
}
========================================
==========================================
// CheckingAccount.java
public class CheckingAccount extends BankAccount
{
private final double OVERDRAFT_FEE = 25;
private double minBalance;
/**
* @param balance
* @param minBalance
*/
public CheckingAccount(double balance, double
minBalance) {
super(balance);
this.minBalance = minBalance;
}
@Override
public void withdraw(double amt) {
if ((getBalance() - amt) <
minBalance) {
setBalance(getBalance() - (OVERDRAFT_FEE + amt));
} else {
setBalance(getBalance() - (amt));
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return super.toString() + ",
Minimum Balance = " + minBalance;
}
}
=========================================
=========================================
// SavingsAccount.java
public class SavingsAccount extends BankAccount
{
private double annualInterestRate;
/**
* @param balance
* @param annualInterestRate
*/
public SavingsAccount(double balance, double
annualInterestRate) {
super(balance);
this.annualInterestRate =
annualInterestRate;
}
public void calculateMonthlyInterest()
{
double monthlyInterest =
getBalance() * (annualInterestRate / 12);
deposit(monthlyInterest);
}
@Override
public void withdraw(double amt) {
if ((getBalance() - amt) >= 0)
{
setBalance(getBalance() - amt);
}
}
//toString() method is used to display the contents of
an object
@Override
public String toString() {
return super.toString()+", Monthly
Interest rate = %"+(annualInterestRate/12);
}
}
=========================================
=========================================