From 52e84ccf10dc6d65fdcf0a473690b417173933b8 Mon Sep 17 00:00:00 2001 From: SSShogunn Date: Wed, 9 Oct 2024 18:52:32 +0530 Subject: [PATCH] improved loan calculator --- frontend/src/pages/Loan/Loan.css | 28 ++++++++------ frontend/src/pages/Loan/Loan.js | 65 ++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 27 deletions(-) diff --git a/frontend/src/pages/Loan/Loan.css b/frontend/src/pages/Loan/Loan.css index 62eb848..630fd32 100644 --- a/frontend/src/pages/Loan/Loan.css +++ b/frontend/src/pages/Loan/Loan.css @@ -92,34 +92,38 @@ } .loan-calculator { - margin-top: 40px; + margin-top: 2rem; background-color: #ffffff; - border-radius: 8px; - padding: 25px; + border-radius: var(--border-radius); + padding: 2rem; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); } .calculator-form { - display: flex; - flex-direction: column; /* Stack inputs vertically */ + display: grid; + gap: 1rem; } .calculator-form label { - margin-bottom: 15px; + display: flex; + flex-direction: column; font-size: 1rem; + color: var(--text-color); } -.calculator-form input { - padding: 10px; +.calculator-form input, +.calculator-form select { + padding: 0.75rem; border: 1px solid #ccc; - border-radius: 5px; + border-radius: var(--border-radius); font-size: 1rem; + margin-top: 0.5rem; } .calculator-result { - margin-top: 20px; - font-size: 1.5rem; - color: #2B4B77; + margin-top: 1.5rem; + font-size: 1.2rem; + color: var(--primary-color); } /* Media Queries for Responsiveness */ diff --git a/frontend/src/pages/Loan/Loan.js b/frontend/src/pages/Loan/Loan.js index 8d8cf1c..386abde 100644 --- a/frontend/src/pages/Loan/Loan.js +++ b/frontend/src/pages/Loan/Loan.js @@ -1,4 +1,3 @@ -// src/LoanPage.js import React, { useState } from 'react'; import './Loan.css'; // Import your custom CSS file @@ -6,13 +5,28 @@ const Loan = () => { const [loanAmount, setLoanAmount] = useState(''); const [interestRate, setInterestRate] = useState(''); const [loanTerm, setLoanTerm] = useState(''); - const [totalPayment, setTotalPayment] = useState(null); + const [paymentFrequency, setPaymentFrequency] = useState('monthly'); + const [calculationResult, setCalculationResult] = useState(null); - const calculateTotalPayment = () => { + const calculateLoan = () => { const principal = parseFloat(loanAmount); - const calculatedInterest = (principal * (parseFloat(interestRate) / 100)) * parseFloat(loanTerm); - const total = principal + calculatedInterest; - setTotalPayment(total.toFixed(2)); + const rate = parseFloat(interestRate) / 100 / 12; + const termInMonths = parseFloat(loanTerm) * 12; + + if (isNaN(principal) || isNaN(rate) || isNaN(termInMonths)) { + alert('Please enter valid numbers for all fields.'); + return; + } + + const monthlyPayment = (principal * rate * Math.pow(1 + rate, termInMonths)) / (Math.pow(1 + rate, termInMonths) - 1); + const totalPayment = monthlyPayment * termInMonths; + const totalInterest = totalPayment - principal; + + setCalculationResult({ + monthlyPayment: monthlyPayment.toFixed(2), + totalPayment: totalPayment.toFixed(2), + totalInterest: totalInterest.toFixed(2) + }); }; return ( @@ -68,39 +82,60 @@ const Loan = () => {

Loan Calculator

-
-
- {totalPayment && ( + + + + {calculationResult && (
-

Total Amount to be Paid: ${totalPayment}

+

Loan Summary:

+

Monthly Payment: ${calculationResult.monthlyPayment}

+

Total Payment: ${calculationResult.totalPayment}

+

Total Interest: ${calculationResult.totalInterest}

)}