private static double calculateAccruedInterest(double lastBalance, double totalInterest, DateTime startingDate, DateTime endingDate, DateTime lastWithdrawlDate, InterestType interestType) { // If there are no wothdraws, set the last withdrawl to starting date if (lastWithdrawlDate == DateTime.MinValue) { lastWithdrawlDate = startingDate; } // Calculate number of days since last withdrawl to the starting transaction date int daysSinceLastWithdrawl = startingDate.Subtract(lastWithdrawlDate).Days; // If there are no withdralws in the last 10 days, use 5% rate if (daysSinceLastWithdrawl > 10) { return(InterestCalculator.calculateAccruedInterest(lastBalance, totalInterest, 5, startingDate, endingDate, interestType)); } // If the span is more, we need to use different rates based on withdrawl date (like amount based rate slabs) DateTime nextInterestRateUpgradeDate = lastWithdrawlDate.AddDays(10); if (nextInterestRateUpgradeDate < endingDate) { // use 0.1% rate until 10-day withdrawl rule breaks, and use 5% after that date double interest = InterestCalculator.calculateAccruedInterest(lastBalance, totalInterest, 0.1, startingDate, nextInterestRateUpgradeDate, interestType); return(interest + InterestCalculator.calculateAccruedInterest(lastBalance, totalInterest + interest, 5, nextInterestRateUpgradeDate, endingDate, interestType)); } else { // Use 0.1% rate if there are no withdrawls in the past 10 days return(InterestCalculator.calculateAccruedInterest(lastBalance, totalInterest, 0.1, startingDate, endingDate, interestType)); } }
/// <summary> /// Caculates interest earned in the checking account /// </summary> /// <returns>amount</returns> public override double InterestEarned() { lock (TransactionLock) { // Use 0.1% rate accrued daily return(InterestCalculator.calculateAccruedInterest(Transactions, 0.1, DateTime.Now, InterestType.COMPOUNDED_DAILY)); } }
private static double calculateAccruedInterest(double lastBalance, double totalInterest, DateTime startingDate, DateTime endingDate, InterestType interestType) { // If the balance is less than $1000, use 0.1% rate if (lastBalance <= 1000) { return(InterestCalculator.calculateAccruedInterest(lastBalance, totalInterest, 0.1, startingDate, endingDate, interestType)); } // Otherwise, apply 0.1% on $1000, and 0.2% on balance over $1000 double interest = InterestCalculator.calculateAccruedInterest(1000, totalInterest, 0.1, startingDate, endingDate, interestType); return(interest + InterestCalculator.calculateAccruedInterest((lastBalance - 1000), (totalInterest + interest), 0.2, startingDate, endingDate, interestType)); }