In: Computer Science
Implementing Comparable and Inheritance
You will use what you have learned from Chapters 9 in Big Java: Late Objects to complete an Inheritance SavingsAccount program. Use what you have learned about the classes, Inheritance, and Comparable to complete the project.
Please comment your program correctly. **Please do not use any advanced material that is not in Chapter8 and 9.
Download the attached InheritanceTester.java file and make your modifications. The program includes testing code in main().
:
This is the code!
1) add toString method to BankAccount and SavingsAccount
classes
2) Override the withdraw() in SavingsAccount so that it will not
withdraw more money than is
currently in the account.
3) Provide constructors for SavingsAccount
4) Add this feature to SavingsAccount: If you withdraw more than 3
times you are charged $10 fee
and the fee is immediately withdrawn from your account.once a fee
is deducted you get another 3 free withdrawals.
5) Implement the Comparable Interface for SavingsAccount based on
balance.
Once you finish adding/modifying the code make sure it compiles and
run.
Submit your modified InheritanceTester.java file to the assignment
folder
Warning: Notice that there are 3 classes in one file, and only one
of them is public,
and this public class name should be the filename. Be careful about
the { } braces.
*/
public class InheritanceTester{
public static void main(String[] args){
SavingsAccount tom = new SavingsAccount(5000);
SavingsAccount kim = new SavingsAccount();
tom.withdraw(100);tom.withdraw(1000);tom.withdraw(200);
kim.withdraw(100);//should print error message: Insufficient
balance
System.out.println(tom);//should print $3700 as balance
tom.withdraw(1000);
System.out.println(tom);//should print $2690 as balance, and fee
charged
tom.withdraw(1000);
System.out.println(tom);//should print $1690 as balance
}
}
class BankAccount
{
private double balance;
public BankAccount()
{ balance = 0; }
public BankAccount(double initialBalance)
{ balance = initialBalance; }
public void deposit(double amount)
{ balance = balance + amount; }
public void withdraw(double amount)
{ balance = balance - amount; }
public double getBalance()
{ return balance; }
}
class SavingsAccount extends BankAccount{
//add code here.
Here are the updates to the BankAccount.java and SavingsAccount.java classes with comments. Thanks. =========================================================================== class BankAccount { private double balance; public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; } public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance - amount; } public double getBalance() { return balance; } //1) add toString method to BankAccount public String toString() { return "Current Balance: $" + balance; } }
==================================================================
//5) Implement the Comparable Interface for SavingsAccount based on balance. class SavingsAccount extends BankAccount implements Comparable<BankAccount>{ private int withdrawlCount; //3) Provide constructors for SavingsAccount public SavingsAccount(double balance) { super(balance); withdrawlCount = 0; } public SavingsAccount() { super(0); withdrawlCount = 0; } //2) Override the withdraw() in SavingsAccount so that it will not // withdraw more money than is currently in the account. public void withdraw(double amount) { //// will not withdraw more money than is currently in the account. if (amount > getBalance()) { System.out.println("Insufficient balance"); } else { withdrawlCount += 1; super.withdraw(amount); } //4) Add this feature to SavingsAccount: If you withdraw // more than 3 times you are charged $10 fee if (withdrawlCount > 3) { super.withdraw(10); // subtract $10 //deducted you get another 3 free withdrawals. withdrawlCount = 0; //r eset to zero } } //1) SavingsAccount classes public String toString() { return "$" + getBalance(); } //5) Implement the Comparable Interface for SavingsAccount based on balance. public int compareTo(BankAccount o) { return Double.compare(getBalance(),o.getBalance()); } }
==================================================================