Person p1 = new Person(); p1.addAccount(new RegularAccount(2000)); p1.addAccount
ID: 3808107 • Letter: P
Question
Person p1 = new Person();
p1.addAccount(new RegularAccount(2000));
p1.addAccount(new PremiumAccount(3000));
p1.addAccount(new RegularAccount(200));
p1.addAccount(new RegularAccount(1000));
p1.addAccount(new PremiumAccount(500));
System.out.println("***Call toString() on Person after 5 accounts are added:");
System.out.println(p1);
System.out.println();
System.out.println("***Call getNumAccounts(): " + p1.getNumAccounts() + " " );
System.out.println("***Call getAccount(1): " + p1.getAccount(1) + " " );
System.out.println("***Call getTotalBalance(): " + p1.getTotalBalance() + " " );
System.out.println("***Call getPremiumAccounts()");
System.out.println(" Loop over each PremiumAccount and print:");
ArrayList<PremiumAccount> pAccounts = p1.getPremiumAccounts();
for( PremiumAccount pa : pAccounts )
System.out.println(pa);
System.out.println();
System.out.println("***Call applyInterest(), then print Person:");
p1.applyInterest();
System.out.println(p1);
(Use material from Ch. 13) A bank offers two types of accounts to it customer: RegularAccount and PremiumAccount. s 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 apply/nterest 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 Do the following: a. Model this situation with a class dia ram. You will add to this diagram for Problem 3. Notice: Design "Account" as an abstract class. Define its balance as a protected data field rather than private b. Write the required classes.Explanation / Answer
Hi Please find my implementation.
Please let me know in case of any issue.
public abstract class Account{
protected double balance;
public void withdraw(double amount){
if(balance-amount < 0){
System.out.println("Balance is low");
}else
balance = balance - amount;
}
public void deposit(double amount){
balance = balance + amount;
}
public abstract void applyInterest();
@Override
public String toString() {
return "The balance is $"+String.format("%.2f", balance);
}
}
class PremiumAccount extends Account{
public PremiumAccount(double amount) {
balance = amount;
}
@Override
public void applyInterest() {
balance = balance + balance*0.015;
}
}
class RegularAccount extends Account{
public RegularAccount(double amount) {
balance = amount;
}
@Override
public void applyInterest() {
if(balance > 1000)
balance = balance + (balance - 1000)*0.01;
}
}
####################
import java.util.ArrayList;
public class Person {
private ArrayList<Account> accounts;
public Person() {
accounts = new ArrayList<>();
}
public void addAccount(Account a){
accounts.add(a);
}
public Account getAccount(int i){
return accounts.get(i);
}
public int getNumAccounts(){
return accounts.size();
}
public double getTotalBalance(){
double sum = 0;
for(Account account : accounts)
sum = sum + account.balance;
return sum;
}
public void applyInterest(){
for(Account account : accounts)
account.applyInterest();
}
public ArrayList<PremiumAccount> getPremiumAccounts(){
ArrayList<PremiumAccount> list = new ArrayList<>();
for(Account account : accounts){
if(account instanceof PremiumAccount){
list.add((PremiumAccount)account);
}
}
return list;
}
@Override
public String toString() {
String res = "Num Accounts: "+getNumAccounts()+" ";
for(Account account : accounts)
res = res + "bal = "+account.balance+" ";
res = " Total Balance: "+getTotalBalance();
return res;
}
}
#####################
import java.util.ArrayList;
public class PersonTester {
public static void main(String[] args) {
Person p1 = new Person();
p1.addAccount(new RegularAccount(2000));
p1.addAccount(new PremiumAccount(3000));
p1.addAccount(new RegularAccount(200));
p1.addAccount(new RegularAccount(1000));
p1.addAccount(new PremiumAccount(500));
System.out.println("***Call toString() on Person after 5 accounts are added:");
System.out.println(p1);
System.out.println();
System.out.println("***Call getNumAccounts(): " + p1.getNumAccounts() + " " );
System.out.println("***Call getAccount(1): " + p1.getAccount(1) + " " );
System.out.println("***Call getTotalBalance(): " + p1.getTotalBalance() + " " );
System.out.println("***Call getPremiumAccounts()");
System.out.println(" Loop over each PremiumAccount and print:");
ArrayList<PremiumAccount> pAccounts = p1.getPremiumAccounts();
for( PremiumAccount pa : pAccounts )
System.out.println(pa);
System.out.println();
System.out.println("***Call applyInterest(), then print Person:");
p1.applyInterest();
System.out.println(p1);
}
}
/*
Sample run:
***Call toString() on Person after 5 accounts are added:
Total Balance: 6700.0
***Call getNumAccounts(): 5
***Call getAccount(1): The balance is $3000.00
***Call getTotalBalance(): 6700.0
***Call getPremiumAccounts()
Loop over each PremiumAccount and print:
The balance is $3000.00
The balance is $500.00
***Call applyInterest(), then print Person:
Total Balance: 6762.5
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.