Readplace

(Medium) - How TDD Can Prevent Over-Engineering

Gist 1 min read
View original
  • current
Summary (TL;DR)
The function calculates interest on a loan amount using tiered rates. Up to $2000, interest is $0. For $2001-$5000, interest is $0.09 per dollar above $2000. For $5001-$10000, interest includes the previous tier plus $0.05 per dollar above $5000 ($0.14-$0.09). Above $10000, interest includes previous tiers plus $0.07 per dollar above $10000 ($0.21-$0.14).

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

const interestToPayFor = (loanAmount) => {
const END_OF_FIRST_RANGE = Money('$2000.00');
const END_OF_SECOND_RANGE = Money('$5000.00');
const END_OF_THIRD_RANGE = Money('$10000.00');
const CENTS_FOR_FIRST_RANGE = Money('$0.00');
const CENTS_FOR_SECOND_RANGE = Money('$0.09');
const CENTS_FOR_THIRD_RANGE = Money('$0.14');
const CENTS_FOR_FOURTH_RANGE = Money('$0.21');
if (loanAmount.greaterThan(END_OF_FIRST_RANGE) && loanAmount.lessThan(Money('$5001.00'))) {
const dollarsAboveThreshold = loanAmount.minus(END_OF_FIRST_RANGE);
const interestAmount = CENTS_FOR_SECOND_RANGE.multipliedBy(dollarsAboveThreshold);
return interestAmount;
}
if (loanAmount.greaterThan(END_OF_SECOND_RANGE) && loanAmount.lessThan(Money('$10001.00'))) {
let interestAmount = Money('$0.00');
const dollarsAboveThresholdForSecondRange = loanAmount.minus(END_OF_FIRST_RANGE);
const interestAmountForSecondRange = CENTS_FOR_SECOND_RANGE.multipliedBy(dollarsAboveThresholdForSecondRange);
interestAmount = interestAmount.plus(interestAmountForSecondRange);
const dollarsAboveThresholdForThirdRange = loanAmount.minus(END_OF_SECOND_RANGE);
const interestAmountForThirdRange = CENTS_FOR_THIRD_RANGE.minus(CENTS_FOR_SECOND_RANGE).multipliedBy(dollarsAboveThresholdForThirdRange);
interestAmount = interestAmount.plus(interestAmountForThirdRange);
return interestAmount;
}
if (loanAmount.greaterThan(END_OF_THIRD_RANGE)) {
let interestAmount = Money('$0.00');
const dollarsAboveThresholdForSecondRange = loanAmount.minus(END_OF_FIRST_RANGE);
const interestAmountForSecondRange = CENTS_FOR_SECOND_RANGE.multipliedBy(dollarsAboveThresholdForSecondRange);
interestAmount = interestAmount.plus(interestAmountForSecondRange);
const dollarsAboveThresholdForThirdRange = loanAmount.minus(END_OF_SECOND_RANGE);
const interestAmountForThirdRange = CENTS_FOR_THIRD_RANGE.minus(CENTS_FOR_SECOND_RANGE).multipliedBy(dollarsAboveThresholdForThirdRange);
interestAmount = interestAmount.plus(interestAmountForThirdRange);
const dollarsAboveThresholdForFourthRange = loanAmount.minus(END_OF_THIRD_RANGE);
const interestAmountForFourthRange = CENTS_FOR_FOURTH_RANGE.minus(CENTS_FOR_THIRD_RANGE).multipliedBy(dollarsAboveThresholdForFourthRange);
interestAmount = interestAmount.plus(interestAmountForFourthRange);
return interestAmount;
}
return CENTS_FOR_FIRST_RANGE;
};