      1  /**

      2     A bank account has a balance that can be changed by

      3     deposits and withdrawals.

      4  */

      5  public class BankAccount

      6  {

      7     private double balance;

      8

      9     /**

     10        Constructs a bank account with a zero balance.

     11     */

     12     public BankAccount()

     13     {

     14        balance = 0;

     15     }

     16

     17     /**

     18        Constructs a bank account with a given balance.

     19        @param initialBalance the initial balance

     20     */

     21     public BankAccount(double initialBalance)

     22     {

     23        balance = initialBalance;

     24     }

     25

     26     /**

     27        Deposits money into this account.

     28        @param amount the amount to deposit

     29     */

     30     public void deposit(double amount)

     31     {

     32        balance = balance + amount;

     33     }

     34

     35     /**

     36        Makes a withdrawal from this account, or charges a penalty if

     37        sufficient funds are not available.

     38        @param amount the amount of the withdrawal

     39     */

     40     public void withdraw(double amount)

     41     {

     42        final double PENALTY = 10;

     43        if (amount > balance)

     44        {

     45           balance = balance - PENALTY;

     46        }

     47        else

     48        {

     49           balance = balance - amount;

     50        }

     51     }

     52

     53     /**

     54        Adds interest to this account.

     55        @param rate the interest rate in percent

     56     */

     57     public void addInterest(double rate)

     58     {

     59        double amount = balance * rate / 100;

     60        balance = balance + amount;

     61     }

     62

     63     /**

     64        Gets the current balance of this account.

     65        @return the current balance

     66     */

     67     public double getBalance()

     68     {

     69        return balance;

     70     }

     71  }