In: Computer Science
/* Work to do:
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.
}
This is the code of your questions which contain all the point that you have asked, I mentioned the below with all the things with comments so that you can easily understand the modifications. Copy and paste the code in java IDE or compiler and run it. I also provide the screenshot of output.
InheritanceTester.java
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; }
// 1. toString method to print balance
public String toString(){
return "Available Balance"+Double.toString(balance);
}
}
// 5. Implements the Comparable Interface for
SavingsAccount based on balance
class SavingsAccount extends BankAccount implements
Comparable{
private final int free_transaction=3;
private final double transaction_fees=10.0;
private int transaction_count=0;
// 3. constructor of SavingsAccount
SavingsAccount(){
super();
}
SavingsAccount(double initialBalance){
super(initialBalance);
}
// 2. method for not to withraw more money than is currently in the
account.
public void withdraw(double amount)
{
if(amount>super.getBalance())
System.out.println("Insufficient balance");
else{
transaction_count++;
super.withdraw(amount);
deduct();
}
}
// 4. method for if anyone withdraw more than 3 times it
will be charged $10 fee
public void deduct(){
if(transaction_count>free_transaction){
double fees = transaction_fees;
super.withdraw(fees);
transaction_count=0;
}
}
// 1.toString method to print balance and fees
charged
public String toString(){
if(transaction_count==0)
return ("Available Balance: "+Double.toString(super.getBalance())+"
and Fess Charged: "+transaction_fees);
return ("Available Balance:
"+Double.toString(super.getBalance()));
}
public int compareTo(Object o) {
throw new UnsupportedOperationException("Not supported
yet.");
}
}
Screenshot of output :-