Question

In: Computer Science

/* Work to do: 1) add toString method to BankAccount and SavingsAccount classes 2) Override the...

/* 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.


}

Solutions

Expert Solution

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 :-


Related Solutions

Information: Consider a banking system with 3 classes:  BankAccount, CheckingAccount, and SavingsAccount, that you are to design...
Information: Consider a banking system with 3 classes:  BankAccount, CheckingAccount, and SavingsAccount, that you are to design and write. Have CheckingAccount and SavingsAccount inherit from BankAccount. In BankAccount, you should include an account number, an account balance, a deposit method, a toString method, and an abstract withdraw method. Since deposits work the same way in both child classes, make sure they cannot override it. The constructor should only take an initial deposit and generate a random 5 digit account number. The...
Information. in java Consider a banking system with 3 classes:  BankAccount, CheckingAccount, and SavingsAccount, that you are...
Information. in java Consider a banking system with 3 classes:  BankAccount, CheckingAccount, and SavingsAccount, that you are to design and write. Have CheckingAccount and SavingsAccount inherit from BankAccount. In BankAccount, you should include an account number, an account balance, a deposit method, a toString method, and an abstract withdraw method. Since deposits work the same way in both child classes, make sure they cannot override it. The constructor should only take an initial deposit and generate a random 5 digit account...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a BankAccountconstructed from data input from the keyboard. Override ReadAccount in SavingsAccount to return an account that refers to a SavingsAccount that you construct, again initializing it with data from the keyboard. Similarly, implement ReadAccount in the CheckingAccount class. Use the following code to test your work. public static void testCode()         {             SavingsAccount savings = new SavingsAccount(100.00, 3.5);             SavingsAccount s = (SavingsAccount)savings.ReadAccount();...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000 (the insurance limit of the U.S. government). The method should block until sufficient money has been withdrawn by another thread. Test your program with a large number of deposit threads. (All other classes are provided below) Bank Account.java (This class is the one that needs to be modified) /** A bank account has a balance that can be changed by deposits and withdrawals. */...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000 (the insurance limit of the U.S. government). The method should block until sufficient money has been withdrawn by another thread. Test your program with a large number of deposit threads. Bank account class: (THE ONE THAT NEEDS TO BE EDITED) /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount { private double balance; /**...
Please write a main method that creates 2 pizzas and uses the toString() method to test...
Please write a main method that creates 2 pizzas and uses the toString() method to test this program. public class PizzaOrder { private Pizza[] order; private int numPizzas; public PizzaOrder() { order = new Pizza[1]; order[0] = new Pizza("Cheese", 0); numPizzas = 1; } public PizzaOrder(int count) { order = new Pizza[count]; this.numPizzas = 0; } public void addPizza(Pizza pizza) { for (int i = 0; i < order.length; i++) { if (order[i] == null) { order[i] = pizza; System.out.println("Pizza...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
1. what gets printed if anything after executing the main method of class BankAccount below: public...
1. what gets printed if anything after executing the main method of class BankAccount below: public class BankAccount {      private static int masterKey =1;    private String name;      private int accountNumber;      private double balance = 0;      private int age = 1;      public BankAccount (String myName , double balance) {      name = myName;      this.balance = 5 + 2 * balance ;      accountNumber = masterKey;      age++;    masterKey++;    printMe(); } public void...
1. define the right to work laws? 2. do you live, work, or hope to work...
1. define the right to work laws? 2. do you live, work, or hope to work in the right to work state? 3.what is the impact on unions of the presence or absence of such laws in that state? 4. explain the court decisions in Janus vs American federation of state, county, and municipal employees. 5. in your informed opinion, how will this rulling impact unions in the years to come?
Prob 1: Do not work on problem 1. I would you to work on problem 2...
Prob 1: Do not work on problem 1. I would you to work on problem 2 You have been given the following information on a project: It has a five-year lifetime The initial investment in the project will be $25 million, and the investment will be depreciated straight line, down to a salvage value of $10 million at the end of the fifth year. The revenues are expected to be $20 million next year and to grow 10% a year...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT