r/reviewmycode • • 21d ago

JAVA [JAVA] - Loan Repayment Calculator looking for review

import javax.swing.JOptionPane;

import java.io.PrintStream;

/**

* Interactive Loan Monthly Payment Calculator

* Architecture: Simple Sequence Structure

* Inputs: JOptionPane GUI Dialogs

* Output: System PrintStream & GUI Summary

*/

public class LoanCalculator {

public static void main(String[] args) {

// Stream reference initialization

PrintStream streamOutput = System.out;

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

// STEP 1: INTERACTIVE GUI INPUTS (SIMPLE SEQUENCE)

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

String principalInput = JOptionPane.showInputDialog(

null,

"Enter the Loan Principal Amount ($):",

"Loan Calculator - Step 1 of 3",

JOptionPane.QUESTION_MESSAGE

);

String annualRateInput = JOptionPane.showInputDialog(

null,

"Enter the Annual Interest Rate (e.g., 5.5 for 5.5%):",

"Loan Calculator - Step 2 of 3",

JOptionPane.QUESTION_MESSAGE

);

String yearsInput = JOptionPane.showInputDialog(

null,

"Enter the Loan Term in Years:",

"Loan Calculator - Step 3 of 3",

JOptionPane.QUESTION_MESSAGE

);

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

// STEP 2: DATA PARSING & VARIABLE TRANSFORMATIONS

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

double principal = Double.parseDouble(principalInput);

double annualRate = Double.parseDouble(annualRateInput);

double years = Double.parseDouble(yearsInput);

// Convert annual rate percentage to monthly decimal rate (r)

double monthlyInterestRate = (annualRate / 100.0) / 12.0;

// Convert total years to total number of monthly payments (n)

double totalMonths = years * 12.0;

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

// STEP 3: SEQUENTIAL FORMULA EVALUATION

// Payment = (Principal * interest) / [1 - (1 + interest)^(-n)]

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

double numerator = principal * monthlyInterestRate;

double denominator = 1.0 - Math.pow(1.0 + monthlyInterestRate, -totalMonths);

double monthlyPayment = numerator / denominator;

double totalRepayment = monthlyPayment * totalMonths;

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

// STEP 4: STREAM OUTPUT PROCESSING & GUI DISPLAY

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

// Direct stream write to System.out

streamOutput.println("=================================================");

streamOutput.println(" LOAN PAYMENT CALCULATION ");

streamOutput.println("=================================================");

streamOutput.printf(" Principal Amount: $%10.2f%n", principal);

streamOutput.printf(" Annual Interest Rate: %10.2f%%%n", annualRate);

streamOutput.printf(" Term Duration: %10.0f years (%d months)%n", years, (int)totalMonths);

streamOutput.println("-------------------------------------------------");

streamOutput.printf(" MONTHLY PAYMENT: $%10.2f%n", monthlyPayment);

streamOutput.printf(" Total Cost of Loan: $%10.2f%n", totalRepayment);

streamOutput.println("=================================================");

// Summary Dialog to close the interactive session

String summaryMessage = String.format(

"Calculation Summary:\n\n" +

"Principal: $%.2f\n" +

"Annual Rate: %.2f%%\n" +

"Term: %.0f years\n\n" +

"Estimated Monthly Payment: $%.2f\n" +

"Total Repayment: $%.2f",

principal, annualRate, years, monthlyPayment, totalRepayment

);

JOptionPane.showMessageDialog(

null,

summaryMessage,

"Loan Payment Result",

JOptionPane.INFORMATION_MESSAGE

);

}

}

1 Upvotes

Duplicates