[Java] Please fix what I did wrong The only class we can modify is TimeDepositAc
ID: 3573285 • Letter: #
Question
[Java] Please fix what I did wrong
The only class we can modify is TimeDepositAccount.
Any other classses, we can't.
The output should look like below:
The bold part is what I'm getting wrong.
Use the following files:
BankAccount.java
AccountTester.java
FeeBasedAccount.java
public class FeeBasedAccount extends BankAccount
{
public static final double TRANSACTION_FEE = 2.00;
public static final double FREE_TRANSACTIONS = 3;
private int transactions;
public FeeBasedAccount(double money, String idNumber)
{
super(money, idNumber);
transactions = 0;
}
@Override
public String getAccountId()
{
return super.getAccountId();
}
@Override
public void deposit(double amount)
{
super.deposit(amount);
transactions++;
}
@Override
public void withdraw(double amount)
{
super.withdraw(amount);
transactions++;
}
@Override
public double getBalance()
{
return super.getBalance();
}
@Override
public void endOfMonth()
{
double chargedTransactions = transactions - FREE_TRANSACTIONS;
double total = chargedTransactions * TRANSACTION_FEE;
if (transactions > FREE_TRANSACTIONS)
{
double balance = super.getBalance() - total;
this.withdraw(total);
}
}
}
Complete the following files:
TimeDepositAccount.java
public class TimeDepositAccount extends BankAccount //This account is a subclass of BankAccount.
{
public TimeDepositAccount(double initialBalance, String id, int months)
{
super(initialBalance, id);
maturity = months;
}
@Override
/**
* Deposits money into the bank account.
* @param in the amount to deposit
*/
public void deposit(double in)
{
super.deposit(in);
}
@Override
/**
* Withdraws money from the bank account.
* There is a penalty for early withdrawal.
* @param out the amount to withdraw
*/
public void withdraw(double out)
{
double fee = out * PENALTY;
super.withdraw(out);
if (maturity < 12)
{
super.withdraw(fee);
}
}
@Override
/**
* Do end of month processing for the account
*/
public void endOfMonth()
{
super.deposit(super.getBalance() * INTEREST);
}
/**
* a method that returns the amount of time the money must be left on deposit.
* @return maturity the amount of time the money must be left on deposit.
*/
public int getTimeToMaturity()
{
return maturity;
}
}
Explanation / Answer
/**
* A bank account has a balance that can be changed by deposits and withdrawals
* and that earn 1% interest per month
*
*/
public class BankAccount {
private double balance;
private String accountId;
public final static double MINIMUM_BALANCE = 5000;
public final static double BELOW_MINIMUM_FEE = 15.0;
/**
* Constructs a bank account with a given balance.
*
* @param initialBalance
* the initial balance
* @param id
* the id for this account
*/
public BankAccount(double initialBalance, String id) {
balance = initialBalance;
accountId = id;
}
/**
* Gets the id for this account
*
* @returns the id for this account
*/
public String getAccountId() {
return accountId;
}
/**
* Deposits money into the bank account.
*
* @param amount
* the amount to deposit
*/
public void deposit(double amount) {
balance = balance + amount;
}
/**
* Withdraws money from the bank account.
*
* @param amount
* the amount to withdraw
*/
public void withdraw(double amount) {
balance = balance - amount;
}
/**
* Gets the current balance of the bank account.
*
* @return the current balance
*/
public double getBalance() {
return balance;
}
/**
* Do end of month processing for the account
*/
public void endOfMonth() {
if (balance < MINIMUM_BALANCE) {
balance = balance - BELOW_MINIMUM_FEE;
}
}
}
/**
* Tester for BankAccount and its subclasses
*/
public class AccountTester {
public static void main(String[] args) {
BankAccount account = new TimeDepositAccount(1000, "abc123", 12);
for (int i = 0; i < 12; i++) {
account.endOfMonth();
}
System.out.printf("Amount at maturity: %.2f%n", account.getBalance());
System.out.println("Expected: 1011.06");
account.withdraw(100);
System.out.printf("Withdraw after maturity: %.2f%n",
account.getBalance());
System.out.println("Expected: 911.06");
// new account
account = new TimeDepositAccount(1000, "xyz789", 12);
account.endOfMonth();
account.withdraw(100);
System.out.printf("Withdraw before maturity: %.2f%n",
account.getBalance());
System.out.println("Expected: 880.92");
}
}
public class FeeBasedAccount extends BankAccount {
public static final double TRANSACTION_FEE = 2.00;
public static final double FREE_TRANSACTIONS = 3;
private int transactions;
public FeeBasedAccount(double money, String idNumber) {
super(money, idNumber);
transactions = 0;
}
@Override
public String getAccountId() {
return super.getAccountId();
}
@Override
public void deposit(double amount) {
super.deposit(amount);
transactions++;
}
@Override
public void withdraw(double amount) {
super.withdraw(amount);
transactions++;
}
@Override
public double getBalance() {
return super.getBalance();
}
@Override
public void endOfMonth() {
double chargedTransactions = transactions - FREE_TRANSACTIONS;
double total = chargedTransactions * TRANSACTION_FEE;
if (transactions > FREE_TRANSACTIONS) {
double balance = super.getBalance() - total;
this.withdraw(total);
}
}
}
public class TimeDepositAccount extends BankAccount // This account is a
// subclass of BankAccount.
{
private int maturity;
public final static double PENALTY = 0.02;
public final static double INTEREST = 0.01;
public TimeDepositAccount(double initialBalance, String id, int months) {
super(initialBalance, id);
maturity = months;
}
@Override
/**
* Deposits money into the bank account.
* @param in the amount to deposit
*/
public void deposit(double in) {
super.deposit(in);
}
@Override
/**
* Withdraws money from the bank account.
* There is a penalty for early withdrawal.
* @param out the amount to withdraw
*/
public void withdraw(double out) {
double fee = out * PENALTY;
super.withdraw(out);
if (maturity < 12) {
super.withdraw(fee);
}
}
@Override
/**
* Do end of month processing for the account
*/
public void endOfMonth() {
super.deposit(super.getBalance() * INTEREST);
}
/**
* a method that returns the amount of time the money must be left on
* deposit.
*
* @return maturity the amount of time the money must be left on deposit.
*/
public int getTimeToMaturity() {
return maturity;
}
}
OUTPUT:
Amount at maturity: 1126.83
Expected: 1011.06
Withdraw after maturity: 1026.83
Expected: 911.06
Withdraw before maturity: 910.00
Expected: 880.92
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.