Wednesday, April 8, 2015

Sample: LoanCalculator

LoanCalculator.java, Loan.java
  /*
   Introduction to OOP with Java (5th Ed), McGraw-Hill
 
   Wu/Otani
 
   Chapter 4 Sample Development: Loan Calculation (Step 5)
 
   File: Step5/LoanCalculator.java
 
   This class controls the input, computation, and output of loan
 */

 import java.text.DecimalFormat;
import java.util.*;

 class LoanCalculator {

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

      // This object does the actual loan computation
     private Loan loan;
    
     private Scanner scanner;

// ----------------------------------
 //
//   Main Method
 //
// ----------------------------------

     public static void main(String[] arg) {
        
         LoanCalculator calculator = new LoanCalculator();
         calculator.start();
     }

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

     public LoanCalculator() {
        
         scanner = new Scanner(System.in);
        
     }

// -------------------------------------------------
//       Public Methods:
 //
//           void start (        )
 //
// ------------------------------------------------

     //Top-level method that calls other private methods
     public void start() {

         describeProgram();   //tell what the program does
         getInput();          //get three input values
         displayOutput();     //display the results
     }


// -------------------------------------------------
//       Private Methods:
 //
//       void    describeProgram   (        )
//       void    displayOutput     (        )
//       void    getInputs         (        )
 //
// ------------------------------------------------


     // Provides a brief explanation of the program to the user.
     private void describeProgram() {
         System.out.println("This program computes the monthly and total");
         System.out.println("payments for a given loan amount, annual ");
         System.out.println("interest rate, and loan period (# of years).");
         System.out.println("\n");
     }


     //Displays the input values and monthly and total payments.
     private void displayOutput() {
        
         DecimalFormat df = new DecimalFormat("0.00");
        
         System.out.println("Loan Amount: $" + loan.getAmount());
         System.out.println("Annual Interest Rate:"
                             + loan.getRate() + "%");
         System.out.println("Loan Period (years): " + loan.getPeriod());

         System.out.println("Monthly payment is $ " +
                                        df.format(loan.getMonthlyPayment()));
         System.out.println("  TOTAL payment is $ " +
                                        df.format(loan.getTotalPayment()));
     }


     // Gets three input values--loan amount, interest rate, and
     // loan period
     private void getInput() {
         double  loanAmount, annualInterestRate;

         int     loanPeriod;

         System.out.print("Loan Amount (Dollars+Cents):");
         loanAmount  = scanner.nextDouble();

         System.out.print("Annual Interest Rate (e.g., 9.5):");
         annualInterestRate = scanner.nextDouble();

         System.out.print("Loan Period - # of years:");
         loanPeriod  = scanner.nextInt();

         //create a new loan with the input values
         loan = new Loan(loanAmount, annualInterestRate,loanPeriod);

     }

 }

//---------------------

// Loan Class (Step 5)

/*
   Introduction to OOP with Java (5th Ed), McGraw-Hill
 
   Wu/Otani
 
   Chapter 4 Sample Development: Loan Calculation (Step 5)
 
   File: Step5/Loan.java
 
   NO CHANGES from STEP 4 CODE
  
   This class handles the loan computation.

*/

class Loan {

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

    // Constant for the number of months in a year
    private final int MONTHS_IN_YEAR = 12;

    // The amount of the loan
    private double    loanAmount;

    //The monthly interest rate
    private double    monthlyInterestRate;

    // The number of monthly payments
    private int       numberOfPayments;



//----------------------------------
//    Constructor
//----------------------------------

    //Creates a new Loan object with passed values.
    public Loan(double amount, double rate, int period) {
        setAmount(amount);
        setRate  (rate  );
        setPeriod(period);
    }

//-------------------------------------------------
//      Public Methods:
//
//          double  getAmount   (           )
//          double  getPeriod   (           )
//          int     getRate     (           )
//
//          double  getMontlyPayment(       )
//          double  getTotalPayment (       )
//
//          void    setAmount   ( double    )
//          void    setPeriod   ( int       )
//          void    setRate     ( double    )
//
//------------------------------------------------

    //Returns the loan amount.
    public double getAmount( ) {
        return loanAmount;
    }

    //Returns the loan period in the number of years.
    public int getPeriod( ) {
        return numberOfPayments / MONTHS_IN_YEAR;
    }

    //Returns the annual interest rate.
    public double getRate( ) {
        return monthlyInterestRate * 100.0 * MONTHS_IN_YEAR;
    }
   
    //Returns the monthly payment
    public double getMonthlyPayment( ) {
        double monthlyPayment;

        monthlyPayment = (loanAmount * monthlyInterestRate)
                           /
                         (1 - Math.pow(1/(1 + monthlyInterestRate),
                                          numberOfPayments ) );
        return monthlyPayment;
    }
   
    //Returns the total payment
    public double getTotalPayment( ) {
        double totalPayment;

        totalPayment = getMonthlyPayment( ) * numberOfPayments;

        return totalPayment;
    }

    //Sets the loan amount of this loan.
    public void setAmount(double amount) {
        loanAmount = amount;
    }

    //Sets the interest rate of this loan.
    public void setRate(double annualRate) {
        monthlyInterestRate = annualRate / 100.0 / MONTHS_IN_YEAR;
    }

    //Sets the loan period of this loan.
    public void setPeriod(int periodInYears) {
        numberOfPayments = periodInYears * MONTHS_IN_YEAR;
    }

}

No comments:

Post a Comment