[Java] First of all, please check your code using the link below. The output has
ID: 3809990 • Letter: #
Question
[Java]
First of all, please check your code using the link below. The output has to be exactly same with the tester in that link.
http://www.codecheck.it/files/16112819477e2pyol56ybmy49gsvrvq4gm3
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
//BankAccount.java
/**
* A bank account has a balance that can be changed by
* deposits and withdrawals
*
*/
public 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
* The java class InterestAccount that inherits from BankAccount
* and the class sets a static final field for number
* of free transactions, minimum balance and below minimum
* balance values
* */
public class TransactionAccount extends BankAccount {
public static final int FREE_TRANSACTIONS = 4;
public final static double MINIMUM_BALANCE = 1500;
public final static double BELOW_MINIMUM_FEE = 5.0;
private int numTransactions;
public TransactionAccount() {
super(0, "");
//set numTransactions=0
numTransactions=0;
}
//constructor
public TransactionAccount(double initialBalance, String id) {
super(initialBalance, id);
}
public void deposit(double amount)
{
//call deposit method of bank account class
super.deposit(amount);
numTransactions++;
}
public void withdraw(double amount) {
//call deposit method of bank account class
super.withdraw(amount);
numTransactions++;
}
public void endOfMonth()
{
//deduct fee if over free transations
if (numTransactions>FREE_TRANSACTIONS)
{
super.withdraw((numTransactions-FREE_TRANSACTIONS)*BELOW_MINIMUM_FEE);
//reset
numTransactions=0;
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
/*
* Class Name:InterestAccount
* The java class InterestAccount that inherits from BankAccount
* and the class sets a static final field for interest rate
* that is (0.09/100)/12=0.00075 for each month interest rate.
* */
public class InterestAccount extends BankAccount {
public static final double INTEREST_RATE=0.00075;
public InterestAccount(double initialBalance, String id)
{
super(initialBalance, id);
}
//calculate interest rate and add to deposit method
public void endOfMonth() {
double bal=super.getBalance();
double interest=bal*INTEREST_RATE;
super.deposit(interest);
}
}
---------------------------------------------------------------------------------------------------------------------------------------
//AccountTester.java
/**
* Tester for BankAccount and its subclasses
*/
public class AccountTester
{
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("Four 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: %5.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("%5.2f%n" , account2.getBalance());
System.out.println("Expected: 1008.13");
}
}
---------------------------------------------------------------------------------------------------------------------------------------
Result output:
No transactions: 1500.0
Expected: 1500.0
Four 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.