Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package accounts;
  2.  
  3. public class SavingsAccount extends BankAccount
  4. {
  5.     private int mInterestRate;
  6.  
  7.     public SavingsAccount(int id, String name)              { super(id, name); }
  8.     public SavingsAccount(int id, String name, int balance) { super(id, name, balance); }
  9.  
  10.     public int getInterestRate()                            { return mInterestRate; }
  11.     public void setInterestRate(int newInterestRate)        { mInterestRate = newInterestRate; }
  12.  
  13.     public void applyInterest()
  14.     {
  15.         float interest = mInterestRate / 100f * mBalance;
  16.  
  17.         mBalance += interest;
  18.     }
  19.  
  20.     @Override public void deposit(int amount)
  21.     {
  22.         if (amount < 0)
  23.             throw new IllegalArgumentException("The amount can not be smaller than 0.");
  24.  
  25.         if (amount > 10_000)
  26.             throw new IllegalArgumentException("The amount can not be greater than 10_000 in a SavingsAccount.");
  27.  
  28.         mBalance += amount;
  29.     }
  30.  
  31.     /** @return Json string representation of the class. */
  32.     @Override public String toString()
  33.     {
  34.         return new StringBuilder()
  35.                 .append("{ \"type\": \"SavingsAccount\""    )
  36.                 .append(", \"id\": "                        )
  37.                 .append(getId()                             )
  38.                 .append(", \"accountHolder\": \""           )
  39.                 .append(getName()                           )
  40.                 .append("\", \"currentBalance\": "          )
  41.                 .append(mBalance                            )
  42.                 .append(", \"interestRate\": "              )
  43.                 .append(mInterestRate                       )
  44.                 .append(" }"                                )
  45.                 .toString();
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement