[Java] You are given a BankAccount class. You are to write two subclasses: Trans
ID: 3809844 • Letter: #
Question
[Java]
You are given a BankAccount class. You are to write two subclasses: TransactionAccount and 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
Use the following files:
AccountTester.java
BankAccount.java
Please check your code using the link below. It has to exact same with the tester. Sorry to bug you.
http://www.codecheck.it/files/16112819477e2pyol56ybmy49gsvrvq4gm3
Explanation / Answer
This is the 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{
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;
public TransactionAccount(double balance, String accId){
super(balance,accId);
this.transactions = 0;
}
@Override
public void endOfMonth()
{
if (transactions>FREE_TRANSACTIONS)
{
super.withdraw((transactions-FREE_TRANSACTIONS)*FEE_EXTRA_TRANSACTION);
}
}
@Override
public void deposit(double amount)
{
super.deposit(amount);
transactions++;
}
@Override
public void withdraw(double amount)
{
super.withdraw(amount);
transactions++;
}
}
class InterestAccount extends BankAccount{
private static final double ANNUAL_INTEREST_RATE = 0.9d;
public InterestAccount(double balance, String accId){
super(balance,accId);
}
@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 :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.