[Java] Please fix what I did wrong We can only change FeeBasedAccount.java nothi
ID: 3779937 • Letter: #
Question
[Java] Please fix what I did wrong
We can only change FeeBasedAccount.java nothing else.
Output should've looked like this
AccountTester.java
BankAccount.java
FIX THIS PART PLEASE
FeeBasedAccount.java
/**
* Write a description of class FeeBasedAccount here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class FeeBasedAccount extends BankAccount
{
private int transactions;
public static final double TRANSACTION_FEE = 2.00;
public static final double FREE_TRANSACTIONS = 3;
/**
* Constructor for objects of class FeeBasedAccount
*/
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 noFree = transactions - FREE_TRANSACTIONS;
double totalFees = noFree * TRANSACTION_FEE;
if (transactions > FREE_TRANSACTIONS)
{
double balance = super.getBalance() - totalFees;
}
}
}
Explanation / Answer
Please read it completely to understand answer of your question.
I appreciate your coding skills. You have written a very good program. As this program is having 3 classes.
1)Account Tester
2)Bank Account
3)Fee Based Account (It extends Bank Account class).
In your program we created one object of BankAccount class and passing reference of FeeBasedAccount class. So whenever you call that object it will check method in the class whose reference has been given at object initialization. But in your program we are calling parent class method by using "Super" keyword.
In your program endOfMonth() function of FeeBasedClass you did one mistake at end of that function and that is the you declared new balance variable of double type. Instead of that you have to type only
balance=balance-totalFees;
OR
balance=super.getBalance() - totalFees;
Just change endOfMonth() function with this one and you will get your expected answer:-
@Override
public void endOfMonth()
{
double noFree = transactions - FREE_TRANSACTIONS;
double totalFees = noFree * TRANSACTION_FEE;
if (transactions > FREE_TRANSACTIONS)
{
balance = balance - totalFees;
}
}
************************************************************************************************************************************
I manually tested each loop. And for convinience putted print statements. Please have a look on my program. I am attaching the program which I changed :-
1)AccountTester class:-
//AccountTester.java
/**
* Tester for BankAccount and its subclasses
*/
public class AccountTester
{
public static void main(String[] args)
{
BankAccount account = new FeeBasedAccount(5000, "abc123");
account.endOfMonth();
System.out.println("No transactions: " + account.getBalance());
System.out.println("Expected: 5000.0");
for (int i = 0; i < FeeBasedAccount.FREE_TRANSACTIONS; i++)
{
account.withdraw(1);
}
account.endOfMonth();
System.out.println("Three Transaction: " + account.getBalance());
System.out.println("Expected: 4997.0");
//make new account
System.out.println("*****************************************");
account = new FeeBasedAccount(5000, "xyz789");
if(account==account){System.out.println("These are same");}
else{System.out.println("These are not same");}
for (int i = 0; i < FeeBasedAccount.FREE_TRANSACTIONS ; i++)
{
account.withdraw(1);
}
account.endOfMonth();
System.out.println("Three Transaction: " + account.getBalance());
System.out.println("Expected: 4997.0");
System.out.println("*******************************************");
for (int i = 0; i < 10; i++)
{System.out.println("------------------");
System.out.println("i : "+i);
account.deposit(1);
}
account.endOfMonth();
System.out.println("Many Transactions: " + account.getBalance());
System.out.println("Expected: 4987.0");
}
}
2)BankAccount class:-
//BankAccount.java
/**
* A bank account has a balance that can be changed by
* deposits and withdrawals and that earn 1% interest per month
*
*/
public class BankAccount
{
public double balance;
public 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;
}
}
}
3) FeeBasedAccount class:-
//FeeBasedAccount.java
/**
* Write a description of class FeeBasedAccount here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class FeeBasedAccount extends BankAccount
{
private int transactions;
public static final double TRANSACTION_FEE = 2.00;
public static final double FREE_TRANSACTIONS = 3;
/**
* Constructor for objects of class FeeBasedAccount
*/
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++;
System.out.println("No. of Transaction : "+transactions);
System.out.println("Balance : "+balance);
}
@Override
public void withdraw(double amount)
{
super.withdraw(amount);
System.out.println("------------------");
transactions++;
System.out.println("No. of Transaction : "+transactions);
System.out.println("Balance : "+balance);
}
@Override
public double getBalance()
{
if(1==1){System.out.println("the user id : "+super.accountId);}
return super.getBalance();
}
@Override
public void endOfMonth()
{
double noFree = transactions - FREE_TRANSACTIONS;
double totalFees = noFree * TRANSACTION_FEE;
System.out.println("At end of the month No. of Transaction : "+transactions+" And NoFree are : "+noFree+" And total fees are "+totalFees);
if (transactions > FREE_TRANSACTIONS)
{
balance = balance - totalFees;
}
}
}
****Output:-
run:
At end of the month No. of Transaction : 0 And NoFree are : -3.0 And total fees are -6.0
the user id : abc123
No transactions: 5000.0
Expected: 5000.0
------------------
No. of Transaction : 1
Balance : 4999.0
------------------
No. of Transaction : 2
Balance : 4998.0
------------------
No. of Transaction : 3
Balance : 4997.0
At end of the month No. of Transaction : 3 And NoFree are : 0.0 And total fees are 0.0
the user id : abc123
Three Transaction: 4997.0
Expected: 4997.0
*****************************************
These are same
------------------
No. of Transaction : 1
Balance : 4999.0
------------------
No. of Transaction : 2
Balance : 4998.0
------------------
No. of Transaction : 3
Balance : 4997.0
At end of the month No. of Transaction : 3 And NoFree are : 0.0 And total fees are 0.0
the user id : xyz789
Three Transaction: 4997.0
Expected: 4997.0
*******************************************
------------------
i : 0
No. of Transaction : 4
Balance : 4998.0
------------------
i : 1
No. of Transaction : 5
Balance : 4999.0
------------------
i : 2
No. of Transaction : 6
Balance : 5000.0
------------------
i : 3
No. of Transaction : 7
Balance : 5001.0
------------------
i : 4
No. of Transaction : 8
Balance : 5002.0
------------------
i : 5
No. of Transaction : 9
Balance : 5003.0
------------------
i : 6
No. of Transaction : 10
Balance : 5004.0
------------------
i : 7
No. of Transaction : 11
Balance : 5005.0
------------------
i : 8
No. of Transaction : 12
Balance : 5006.0
------------------
i : 9
No. of Transaction : 13
Balance : 5007.0
At end of the month No. of Transaction : 13 And NoFree are : 10.0 And total fees are 20.0
the user id : xyz789
Many Transactions: 4987.0
Expected: 4987.0
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.