Please enable Javascript to view the contents

## Bottom-up to development a Loan Calculator

·  ☕ 2 分钟

Bottom-up development is a style of developing programs. With the bottom-up development, we develop in the reverse inside-out fashion; that is, we develop the service classes first. To test the service classes, we write a temporary dummy main class. After the service classes are done, we complete the top-level class that uses these service classes. The bottom-up development for this program implements the Loanclass first fully and then the LoanCalculatorclass.

# Development steps

In this example, we write the service class (Loanclass) and write the main function in the Loanclass to test the Loanclass. Next, we create a LoanCalculatorclass to handle the user’s input and output, and compute the monthly and total payments by calling the member method (getLoan()) of Loanclass.

# Loan class

## Step 1

First, we write a constructor (Loan()) to receive three form parameters (double amount, double rate and int period), and assign them to member variables (double amount, double rate and int period).

Second, we write two dummy function (double getMonPay() and double getTotalPay()) .

Third, we write a main function to test the Loanclass.

Source code:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41  class Loan { private final int MONTHS_IN_YEAR = 12; private double amount; // Loan amount private double rate; // Lnterest rate private int period; // Loan period // Constructor public Loan(double amount, double rate, int period){ this.amount = amount; this.rate = rate; this.period = period; } // Calculating monthly payments public double getMonPay(){ return 100.1; } // Calculating the total payments public double getTotalPay(){ return 112312.1212; } // TEST public static void main(String[] args) { double amount = 100.3; double rate = 22; int period = 12; Loan loan = new Loan(amount, rate, period); System.out.println("Expected value: " + amount + " Actual value： " + loan.amount); System.out.println("Expected value: " + rate + " Actual value： " + loan.rate); System.out.println("Expected value: " + period + " Actual value： " + loan.period); System.out.println("Expected value: " + 100.1 + " Actual value： " + loan.getMonPay()); System.out.println("Expected value: " + 112312.1212 + " Actual value： " + loan.getTotalPay()); } } 

operation result:

 1 2 3 4 5  Expected value: 100.3 Actual value： 100.3 Expected value: 22.0 Actual value： 22.0 Expected value: 12 Actual value： 12 Expected value: 100.1 Actual value： 100.1 Expected value: 112312.1212 Actual value： 112312.1212 

## Step 2

Complete the logic in the dummy function to make it a true member method.

Use the real value in the main function to test whether the logic of the member method (double getMonPay() and double getTotalPay()) is correct.

Source code:

 1 2 3 4 5 6 7 8 9  // Calculating monthly payments public double getMonPay(){ return (amount * (rate / 100.0)) / MONTHS_IN_YEAR; } // Calculating the total payments public double getTotalPay(){ return amount + ((amount * (rate / 100.0)) * period); } 

operation result:

 1 2 3 4 5  Expected value: 100.3 Actual value： 100.3 Expected value: 22.0 Actual value： 22.0 Expected value: 12 Actual value： 12 Expected value: 1.838833 Actual value： 1.8388333333333333 Expected value: 365.092 Actual value： 365.092 

So far, the Loanclass has been completed.

# Loan Calculator class

## Step 1

First, we write a member method (void getInput()) to handle input and another member method (void getOutput()) to handle output.

Sexond, we write a fake main function to test the input and output.

Source code:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42  import java.util.Scanner; class LoanCalculator { private double amount; // Loan amount private double rate; // Lnterest rate private int period; // Loan period private double monPayment; // Monthly payment private double totalPayment; // Total payment public static void main(String[] args) { LoanCalculator loanCalculator = new LoanCalculator(); loanCalculator.getInput(); System.out.println("Input: " + loanCalculator.amount + " " + loanCalculator.rate + " " + loanCalculator.period); System.out.println(); loanCalculator.monPayment = 100.1; loanCalculator.totalPayment = 200.2; loanCalculator.getOutput(); } // Processing input private void getInput(){ Scanner sc = new Scanner(System.in); System.out.print("Your loan amount: $"); amount = sc.nextDouble(); System.out.print("Your loan interest rate (%): "); rate = sc.nextDouble(); System.out.print("Your loan period (year): "); period = sc.nextInt(); sc.close(); } // Processing output private void getOutput(){ System.out.println("Your monthly payment is$" + monPayment); System.out.println("Your total payment is $" + totalPayment); } }  operation result:  1 2 3 4 5 6 7  Your loan amount:$100.1 Your loan interest rate (%): 33.2 Your loan period (year): 12 Input: 100.1 33.2 12 Your monthly payment is $100.1 Your total payment is$200.2 

## Step 2

Rewrite the main function, call the getMonPay() method of the Loan class to calculating the monthly payments, and call the getTotalPay() method of the Loan class to calculating the total payments.

Source code:

  1 2 3 4 5 6 7 8 9 10 11 12  public static void main(String[] args) { LoanCalculator loanCalculator = new LoanCalculator(); loanCalculator.getInput(); Loan loan = new Loan(loanCalculator.amount, loanCalculator.rate, loanCalculator.period); System.out.println(); loanCalculator.monPayment = loan.getMonPay(); loanCalculator.totalPayment = loan.getTotalPay(); loanCalculator.getOutput(); } 

operation result:

 1 2 3 4 5 6  Your loan amount: $100 Your loan interest rate (%): 22 Your loan period (year): 10 Your monthly payment is$1.8333333333333333 Your total payment is \$320.0 

Completed!!! ヾ(´▽’)ﾉ.

Mogeko
I am a student of Debrecen University, Hungary, majoring in Computer Science. I hope to be a Full-stack in the future.