Design a class named Bank Account to hold the following data for a bank account:
ID: 3555664 • Letter: D
Question
Design a class named Bank Account to hold the following data for a bank account:
The class should have the following methods:
Next, design a SavingsAccount class that extends the BankAccount class. The SavingsAccount class should have a status field to represent an active or inactive account. If the balance of a savings account falls below $25, it becomes inactive. (The status field could be a boolean variable.) No more withdrawals may be made until the balance is raised above $25, at which time the account becomes active again. The savings account class should have the following methods:
In your main, create a SavingsAccount object by making up a starting balance and rate and write a loop that iterates 5 times and does a random deposit and withdrawal (values 1 - 100) in each iteration on the account using your methods. Print out the status of the account(balance, charges, number of transactions) after each transaction.
Explanation / Answer
BankAccount Class
public class BankAccount {
protected float balance;
protected float numDeposits;
protected float numWithdrawals;
protected float annRate;
protected float monSrvCrg;
public BankAccount() {
balance = 0;
numDeposits = 0;
numWithdrawals = 0;
annRate = 0;
monSrvCrg = 0;
}
public BankAccount(float bal, float rate) {
this();
this.balance = bal;
this.annRate = rate;
}
public void deposit(float amount) {
balance += amount;
numDeposits++;
}
public void withdraw(float amount) {
balance -= amount;
numDeposits++;
}
public void calcInterest() {
//Monthly Interest Rate = (Annual Interest Rate / 12)
//Monthly Interest = Balance * Monthly Interest Rate
//Balance = Balance + Monthly Interest
float monRate = annRate/12;
float monInt = balance*monRate;
balance += monInt;
}
public void monthlyProcess() {
balance -= monSrvCrg;
calcInterest();
numWithdrawals = 0;
numDeposits = 0;
monSrvCrg = 0;
}
}
SavingAccount Class
public class SavingAccount extends BankAccount{
public boolean active;
public SavingAccount(float bal, float rate) {
super(bal, rate);
if(bal < 25) {
active = false;
} else {
active = true;
}
}
public void withdraw(float amount) {
if(active) {
super.withdraw(amount);
}
}
public void deposit(float amount) {
if(!active) {
if(amount + balance < 25) {
return;
}
}
super.deposit(amount);
}
public void monthlyProcess() {
if(numWithdrawals > 4) {
monSrvCrg += numWithdrawals - 4;
}
super.monthlyProcess();
if(balance < 25) {
active = false;
}
}
public void printStatus() {
System.out.println("balance = "+balance+", charges = "+monSrvCrg+", number of transactions = "+(numDeposits + numWithdrawals));
}
}
Main Class
package chegg;
public class AccountMain {
public static void main(String[] args) {
float bal = (float) (Math.random()*5000 + 1000); //Intial balance in range [1000, 6000]
float rate = 12;
SavingAccount sv = new SavingAccount(bal, rate);
for(int i = 0; i < 5; i++) {
float dep = (float) (Math.random()*99 + 1);
sv.deposit(dep);
sv.printStatus();
float wit = (float) (Math.random()*99 + 1);
sv.withdraw(wit);
sv.printStatus();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.