A bank offers two types of accounts to its customer: RegularAccount and PremiumA
ID: 3670649 • Letter: A
Question
A bank offers two types of accounts to its customer: RegularAccount and PremiumAccount. All accounts have a balance and methods: deposit and withdraw which both accept an amount of money and either adds to the balance (deposit) or subtracts the amount from the balance (withdraw). An account also has a toString method that returns a string like this: The balance is $3,603.35.
Accounts also have an applyInterest method that calculates the interest and then adds this to the balance. However, interest is calculated differently for the two types of accounts. A RegularAccount earns 1% interest for the balance over $1000. The PremiumAccount earns 1.5% interest on the entire balance.
Hint: Design “Account” as an abstract class. You may define its balance as a protected data field b. Write the required classes. c. Write a tester, AccountTester.java and use the following main to test:
Account ac1 = new RegularAccount(500);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
ac1.deposit(1200);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
ac1.withdraw(1000);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
PremiumAccount pacc1 = new PremiumAccount(500);
System.out.println(pacc1);
pacc1.applyInterest();
System.out.println(pacc1);
pacc1.deposit(1200);
System.out.println(pacc1);
pacc1.applyInterest();
System.out.println(pacc1);
The code should output the following:
Regular Account: The balance is $500.00
Regular Account: The balance is $500.00
Regular Account: The balance is $1700.00
Regular Account: The balance is $1707.00
Regular Account: The balance is $707.00
Regular Account: The balance is $707.00
Premium Account: The balance is $500.00
Premium Account: The balance is $507.50
Premium Account: The balance is $1707.50
Premium Account: The balance is $1733.11
Explanation / Answer
Please find the required solution:
//declaration of abstract class Account
abstract class Account {
// declare protected variables named balance and interest rate
protected double balance;
protected final double interestRate;
// one argument constructor that initializes the interest rate
Account(double interestRate) {
this.interestRate = interestRate;
}
// method simulates deposit
void deposit(double amount) {
balance += amount;
}
// method that simulates withdraw
void withdraw(double amount) {
balance -= amount;
}
// abstract method applyInterest
abstract void applyInterest();
// implementation of toString method
@Override
public String toString() {
return "The balance is " + balance;
}
}
/**
* Implementation of class RegularAccount
*
*/
class RegularAccount extends Account {
RegularAccount(double balance) {
super(1);
this.balance = balance;
}
@Override
void applyInterest() {
if (balance > 1000)
balance = balance + (interestRate * (balance - 1000) / 100);
}
}
/**
* Implementation of class PremiumAccount
*
*/
class PremiumAccount extends Account {
PremiumAccount(double balance) {
super(1.5);
this.balance = balance;
}
@Override
void applyInterest() {
balance = balance + (interestRate * balance) / 100;
}
}
/*
* Test Method
*/
public class AccountTester {
public static void main(String[] args) {
Account ac1 = new RegularAccount(500);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
ac1.deposit(1200);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
ac1.withdraw(1000);
System.out.println(ac1);
ac1.applyInterest();
System.out.println(ac1);
PremiumAccount pacc1 = new PremiumAccount(500);
System.out.println(pacc1);
pacc1.applyInterest();
System.out.println(pacc1);
pacc1.deposit(1200);
System.out.println(pacc1);
pacc1.applyInterest();
System.out.println(pacc1);
}
}
Sample screenshot:
The balance is 500.0The balance is 500.0
The balance is 1700.0
The balance is 1707.0
The balance is 707.0
The balance is 707.0
The balance is 500.0
The balance is 507.5
The balance is 1707.5
The balance is 1733.1125
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.