/**

 * Class to claculate compound interest rate based on inputs from user

 * @author Ahmad Mobasseri

 *

 */

class InterestComputer

{

      private static final double DEFAULT_PRINCIPLE = 100;

      private static final double DEFAULT_INTERESTRATE = 5;

      private static final double DEFAULT_TIME = 1;                     // Year

      private double principle;

      private double interestRate;

      private double time;

      public static final long serialVersionUID = 1;

     

      // Default Constructor

      public InterestComputer()

      { 

            principle    = DEFAULT_PRINCIPLE;

            interestRate = DEFAULT_INTERESTRATE;

            time         = DEFAULT_TIME;

      }

 

//  NOTE: Changed Exceptions in this class from checked Exception to RuntimeExceptions so

//    the caller doen't HAVE TO do 'try-catch' when he can check if data valid by simple if

//  statement. But caller also have option to use 'try-catch' as I have done in caller classes    

     

      // Constructor

      public InterestComputer(double inPrinciple, double inInterestRate, double inTime) //throws Exception

      { 

            if (inPrinciple < 0 || inInterestRate < 0 || inTime < 0 )

                  throw new RuntimeException("Negative numbers are invalid");

            principle    = inPrinciple;

            interestRate = inInterestRate;

            time         = inTime;

      }

     

// Setters 

      public void setPrinciple(double setterInPrinciple) //throws Exception

      {    

            if (setterInPrinciple < 0)

                  throw new IllegalArgumentException("Negative number for Principe, is invalid");

            principle = setterInPrinciple;     

      }

      public void setInterestRate(double setterInInterestRate) //throws Exception

      {    

            if (setterInInterestRate < 0)

                  throw new IllegalArgumentException("Negative number for Interest rate, is invalid");

            interestRate = setterInInterestRate;

      }

      public void setTime(double setterInTime) //throws Exception

      {    

            if (setterInTime < 0)

                  throw new IllegalArgumentException("Negative number for Time, is invalid");

            time = setterInTime;

      }

     

// Getters

      public double getPrinciple() { return principle; }

      public double getInterestRate() { return interestRate; }

      public double getTime()             { return time; }

     

/**

 * Compute Interest rate

 * @return double result

 */

      public double interestCompute()

      {

            double interestRateFloat = interestRate / 100;

            double amount = principle * Math.pow(1 + interestRateFloat, time);

            return amount;

      }

     

}