A Java question. You are given a BankAccount class. You are to write two subclas
ID: 3810025 • Letter: A
Question
A Java question. You are given a BankAccount class. You are to write two subclasses: TransactionAccountand InterestAccount
BankAccount:
This class is provided for you. It is different from earlier versions in that it requires a minimum balance of $1500. There is a $10.00 fee for any month where the ending balance is below the minimum balance. There is an endOfMonth method to handle end of month processing. Its subclasses will handle end of month processing differently.
TransactionAccount:
This account is a subclass of BankAccount. It has no minimum balance, but there is a charges for every transaction after a certain number of free transactions. At the current time, the number of free transactions is 4 and the charge is a $5.00 for every transaction after that amount. Both deposits and withdrawals are transactions. There is no charge to get the balance. The fees are subtracted during end of month processing. Define and use constants.
InterestAccount:
This is a subclass of BankAccount. The owner of the account can deposit or withdraw money at any point. The account earns 0.9% interest annually compounded monthly. Interest is paid on the ending balance during end of month processing. Use a constant for annual interest rate.
Provide Javadoc. You can use @Override for overridden methods
---------------------------------------------------------------------------------------------------------------------------
The AccountTester and BankAccount have been given as follow:
AccountTester.java
BankAccount.java
plz give me the answer of TransactionAccount and InterestAccount ,Thank you
Explanation / Answer
Please find the below java program
import java.util.*;
import java.lang.*;
import java.io.*;
class BankAccount
{
private double balance;
private String accountId;
public final static double MINIMUM_BALANCE = 1500;
public final static double BELOW_MINIMUM_FEE = 10.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;
}
}
}
class TransactionAccount extends BankAccount{
//Constants
public final static double MINIMUM_BALANCE = 0;
public final static int FREE_TRANSACTIONS = 4;
public final static int FEE_EXTRA_TRANSACTION = 5;
private int transactions;
//This constructor sets balance and account Id and initilzes the number of transactions to 0
public TransactionAccount(double balance, String accId){
super(balance,accId);
this.transactions = 0;
}
//If number of transactions is greater than number of free transactions allowed, extra fees per transaction is applied
@Override
public void endOfMonth()
{
if (transactions>FREE_TRANSACTIONS)
{
super.withdraw((transactions-FREE_TRANSACTIONS)*FEE_EXTRA_TRANSACTION);
}
}
//Deposits the amount and increments the transaction count
@Override
public void deposit(double amount)
{
super.deposit(amount);
transactions++;
}
//Withdraws the amount and increments the transaction count
@Override
public void withdraw(double amount)
{
super.withdraw(amount);
transactions++;
}
}
class InterestAccount extends BankAccount{
//Constant for interest rate
private static final double ANNUAL_INTEREST_RATE = 0.9d;
//This constructor sets the balance and account Id
public InterestAccount(double balance, String accId){
super(balance,accId);
}
//Interest is added at the end of each month and added to the balance
@Override
public void endOfMonth()
{
double balance = super.getBalance();
double interest = (balance*ANNUAL_INTEREST_RATE)/1200;
super.deposit(interest);
}
}
class Ideone
{
public static void main(String[] args)
{
//check that these are subclasses of BankAccount
BankAccount account = new TransactionAccount(1500, "abc123");
account.endOfMonth();
System.out.println("No transactions: " + account.getBalance());
System.out.println("Expected: 1500.0");
//
for (int i = 0; i < TransactionAccount.FREE_TRANSACTIONS; i++)
{
account.withdraw(1);
}
account.endOfMonth();
System.out.println("Three Transaction: " + account.getBalance());
System.out.println("Expected: 1496.0");
//make new account
account = new TransactionAccount(1500, "xyz789");
//withdraw $4 the add $4 using 6 transactions
for (int i = 0; i < TransactionAccount.FREE_TRANSACTIONS ; i++)
{
account.withdraw(1);
}
account.deposit(2.0);
account.deposit(2.0);
System.out.println("Before end of month: " + account.getBalance());
System.out.println("Expected: 1500.0");
account.endOfMonth();
System.out.println("After end of month: " + account.getBalance());
System.out.println("Expected: 1490.0");
//InterestAccount testing
BankAccount account2 = new InterestAccount(1000, "abc123");
account2.endOfMonth();
System.out.printf("after one month: %.2f%n" , account2.getBalance());
System.out.println("Expected: 1000.75");
account2 = new InterestAccount(1000, "qrs123");
account2.withdraw(100);
//do a 12 end of month processings
for(int i = 0; i < 12; i++)
{
account2.endOfMonth();
}
account2.deposit(100);
System.out.printf("%.2f%n" , account2.getBalance());
System.out.println("Expected: 1008.13");
}
}
OUTPUT :
No transactions: 1500.0
Expected: 1500.0
Three Transaction: 1496.0
Expected: 1496.0
Before end of month: 1500.0
Expected: 1500.0
After end of month: 1490.0
Expected: 1490.0
after one month: 1000.75
Expected: 1000.75
1008.13
Expected: 1008.13
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.