Skip to content

Commit

Permalink
feat: Investment Calculator (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitkumawat1m authored Oct 25, 2023
1 parent 8f26e2d commit 42569eb
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions packages/kitchen-sink/src/examples/investment-calculator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { useState } from 'react';
import { block } from 'million/react';

interface Results {
totalInvestment: number;
totalPayment: number;
totalInterest: number;
}

const InvestmentCalculator = block(() => {
const [investmentAmount, setInvestmentAmount] = useState<number | string>('');
const [interestRate, setInterestRate] = useState<number | string>('');
const [numberOfYears, setNumberOfYears] = useState<number | string>('');
const [results, setResults] = useState<Results | null>(null);


const calculateInterest = ()=>{
const p = parseFloat(investmentAmount as string);
const r = parseFloat(interestRate as string);
const n = parseInt(numberOfYears as string, 10);

if (isNaN(p) || isNaN(r) || isNaN(n) || p <= 0 || r <= 0 || n <= 0) {
alert('Please enter valid numbers.');
return;
}

const totalInterest = (p*r*n)/100;
const totalPayment = totalInterest + p;
const totalInvestment = p;

setResults({
totalInvestment,
totalPayment,
totalInterest,
});
}


return (
<div className="InvestmentCalculator">
<h1>Investment Calculator</h1>
<div>
<label>Total Investment ($): </label>
<input
type="number"
value={investmentAmount}
onChange={(e) => setInvestmentAmount(e.target.value)}
style={{ display: 'block', marginBottom: '15px' }}
/>
</div>
<div>
<label>Interest Rate (Annual %): </label>
<input
type="number"
value={interestRate}
onChange={(e) => setInterestRate(e.target.value)}
style={{ display: 'block', marginBottom: '15px' }}
/>
</div>
<div>
<label>Duration (Years): </label>
<input
type="number"
value={numberOfYears}
onChange={(e) => setNumberOfYears(e.target.value)}
style={{ display: 'block', marginBottom: '15px' }}
/>
</div>
<button onClick={calculateInterest}>Calculate</button>

{results && (
<div className="results">
<h2>Results:</h2>
<div>End Balance: ${results.totalPayment.toFixed(2)}</div>
<div>Total Investment : ${results.totalInvestment.toFixed(2)}</div>
<div>Total Interest: ${results.totalInterest.toFixed(2)}</div>
</div>
)}
</div>
);
});

export default InvestmentCalculator;

2 comments on commit 42569eb

@vercel
Copy link

@vercel vercel bot commented on 42569eb Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

million-kitchen-sink – ./packages/kitchen-sink

million-kitchen-sink.vercel.app
million-kitchen-sink-git-main-millionjs.vercel.app
million-kitchen-sink-millionjs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 42569eb Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sink – ./packages/kitchen-sink

sink-git-main-millionjs.vercel.app
million-kitchen-sink-atit.vercel.app
sink.million.dev
sink-millionjs.vercel.app

Please sign in to comment.