Investment.java.html

  1  /**
  2     A class to monitor the growth of an investment that 
  3     accumulates interest at a fixed annual rate.
  4  */
  5  public class Investment
  6  {
  7     private double balance;
  8     private double rate;
  9     private int year;
 10  
 11     /**
 12        Constructs an Investment object from a starting balance and
 13        interest rate.
 14        @param aBalance the starting balance
 15        @param aRate the interest rate in percent
 16     */
 17     public Investment(double aBalance, double aRate)
 18     {
 19        balance = aBalance;
 20        rate = aRate;
 21        year = 0;
 22     }
 23  
 24     /**
 25        Keeps accumulating interest until a target balance has
 26        been reached.
 27        @param targetBalance the desired balance
 28     */
 29     public void waitForBalance(double targetBalance)
 30     {
 31        while (balance < targetBalance)
 32        {
 33           year++;   
 34           double interest = balance * rate / 100;
 35           balance = balance + interest;
 36        }
 37     }
 38  
 39     /**
 40        Gets the current investment balance.
 41        @return the current balance
 42     */
 43     public double getBalance()
 44     {
 45        return balance;
 46     }
 47  
 48     /**
 49        Gets the number of years this investment has accumulated
 50        interest.
 51        @return the number of years since the start of the investment
 52     */
 53     public int getYears()
 54     {
 55        return year;
 56     }
 57  }