// Program LoanCalculator (Step 5 - Final)


import javabook.*;

/**
 * Introduction to OOP with Java 2nd Edition, McGraw-Hill
 *   
 *  Chapter 4
 *
 * <p>
 * This class handles the input, computation, and output of loan
 * calculation.
 *
 * @author Dr. Caffeine 
 */
class LoanCalculator
{

//----------------------------------
//    Data Members
//----------------------------------

    /**
     * Constant for the number of months in a year
     */
    private final int MONTHS_IN_YEAR = 12;
      
    /**
     * The main window of the program
     */
    private MainWindow mainWindow;
         
    /**
     * InputBox for getting three input values
     */
    private InputBox  inputBox;
    
    /**
     * OutputBox for displaying the input values
     * and the payments
     */
    private OutputBox outputBox;

    /**
     * Input to the program - the amount of the loan
     */
    private float    loanAmount;
    
    /**
     * Input to the program - the annual interest rate
     */
    private float  annualInterestRate;
    
    /**
     * The monthly interest rate
     */
    private float  monthlyInterestRate;
    
    /**
     * The monthly loan payment
     */
    private double  monthlyPayment;
    
    /**
     * The total loan payment
     */
    private double totalPayment;

    /**
     * Input to the program - the loan period in years
     */
    private int   loanPeriod;
    
    /**
     * The number of monthly payments
     */
    private int  numberOfPayments;


//----------------------------------
//    Constructors
//----------------------------------

    public LoanCalculator()
    {
        mainWindow  = new MainWindow("L O A N  C A L C U L A T O R");
        inputBox    = new InputBox(mainWindow);
        outputBox   = new OutputBox(mainWindow);
    }

//-------------------------------------------------
//      Public Methods:
// 
//          void start (        )
//
//------------------------------------------------
                                
    /**
     * Top level method that calls other private methods
     * to compute the monthly and total loan payments
     */
    public void start()
    {
        mainWindow.setVisible( true );
        outputBox.setVisible( true );
        
        describeProgram();   //tell waht the program does
        getInput();          //get three input values
        computePayment();    //compute the monthly payment and total
        displayOutput();     //diaply the results
   }

//-------------------------------------------------
//      Private Methods:
// 
//      void    computePayment    (        )
//      void    describeProgram   (        )
//      void    displayOutput     (        )
//      void    getInputs         (        )
//
//------------------------------------------------
                                
    /**
     * Computes the monthly and total loan payments.
     */
    private void computePayment()
    {
        monthlyInterestRate = annualInterestRate /  100.0f / MONTHS_IN_YEAR;
        numberOfPayments    = loanPeriod * MONTHS_IN_YEAR;

        monthlyPayment      = (loanAmount * monthlyInterestRate) /
                                (1 - Math.pow(1/(1 + monthlyInterestRate),
                                                         numberOfPayments ) );

        totalPayment        =  monthlyPayment * numberOfPayments;
    }

    /**
     * Provides a brief explaination of the program to the user.
     */
    private void describeProgram()
    {
        outputBox.printLine("This program computes the monthly and total");
        outputBox.printLine("payments for a given loan amount, annual ");
        outputBox.printLine("interest rate, and loan period (# of years)");
        outputBox.skipLine(2);
    }


    /**
     * Display the input values and monthly and total payments.
     */
    private void displayOutput() 
    {
        outputBox.printLine("For");
        outputBox.printLine("Loan Amount:          $" + loanAmount);
        outputBox.printLine("Annual Interest Rate:  " + annualInterestRate + "%");
        outputBox.printLine("Loan Period (years):   " + loanPeriod);
        outputBox.skipLine(1);

        outputBox.printLine("Monthly payment is    $ " + monthlyPayment);
        outputBox.printLine("  TOTAL payment is    $ " + totalPayment);
    }   


    /**
     * Gets three input values--loan amount, interest rate, and
     * loan period--using an InputBox object
     */
    private void getInput() 
    {
        loanAmount          = inputBox.getFloat("Loan Amount (Dollars&Cents):");
        annualInterestRate  = inputBox.getFloat("Annual Interest Rate (e.g. 9.5):");
        loanPeriod          = inputBox.getInteger("Loan Period - # of years:");
    
    }
    
}