Пример #1
0
        private InterestResult CreateResult(decimal lumpSumInvestment, decimal monthlyInvestment, int timescaleInYears,
                                            decimal wideBoundPercentageUpper, decimal wideBoundPercentageLower, decimal narrowBoundPercentageUpper,
                                            decimal narrowBoundPercentageLower, decimal targetValue)
        {
            var totalInvested = _interestCalculator.Calculate(lumpSumInvestment, monthlyInvestment, 0)
                                .Take(timescaleInYears + 1).Last();

            var wideBoundUpperSeries = _interestCalculator.Calculate(lumpSumInvestment, monthlyInvestment, wideBoundPercentageUpper)
                                       .Take(timescaleInYears + 1);

            var wideBoundLowerSeries = _interestCalculator.Calculate(lumpSumInvestment, monthlyInvestment, wideBoundPercentageLower)
                                       .Take(timescaleInYears + 1);

            var narrowBoundLowerSeries = _interestCalculator.Calculate(lumpSumInvestment, monthlyInvestment, narrowBoundPercentageLower)
                                         .Take(timescaleInYears + 1);

            var narrowBoundUpperSeries = _interestCalculator.Calculate(lumpSumInvestment, monthlyInvestment, narrowBoundPercentageUpper)
                                         .Take(timescaleInYears + 1);

            var years = Enumerable.Range(0, timescaleInYears + 1);

            var targetYears = Enumerable.Repeat(targetValue, timescaleInYears + 1);

            var response = new InterestResult(totalInvested, wideBoundUpperSeries, wideBoundLowerSeries, narrowBoundLowerSeries, narrowBoundUpperSeries, years, targetYears);

            return(response);
        }
Пример #2
0
        /// <summary>
        /// Calcula el total de interés ganado en la cuenta.
        /// </summary>
        /// <returns>
        /// Interés ganado.
        /// </returns>
        public double GetInterestEarned()
        {
            double interestEarned;

            switch (this.Type)
            {
            case AccountType.Checking:
                _interestCalculator = new InterestCalculatorChecking();
                break;

            case AccountType.Savings:
                _interestCalculator = new InterestCalculatorSaving();
                break;

            case AccountType.MaxiSavings:
                _interestCalculator = new InterestCalculatorMaxiSaving();
                break;

            case AccountType.NewMaxiSavings:
                _interestCalculator = new InterestCalculatorNewMaxiSaving(this.LastWithdraw);
                break;
            }

            interestEarned = _interestCalculator.Calculate(Balance, this.DateCreate);
            return(interestEarned);
        }
Пример #3
0
        public void ApplyInterestUpdatesTheBalance()
        {
            // Arange
            IInterestCalculator interestCalculator = Substitute.For <IInterestCalculator>();

            interestCalculator.Calculate().Returns(123);
            SavingsAccount savingsAccount = new SavingsAccount(interestCalculator);

            // Act
            savingsAccount.ApplyInterest();

            // Assert
            Assert.AreEqual(123, savingsAccount.Balance);
        }
Пример #4
0
 public void ApplyInterest()
 {
     Balance += _interestCalculator.Calculate();
 }
Пример #5
0
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime startDate    = ctlStartDate.Value.Date;
                DateTime endDate      = ctlEndDate.Value.Date;
                int      interestDays = endDate.Subtract(startDate).Days + 1;
                if (interestDays < 1)
                {
                    HostUI.ErrorMessageBox("Start date must be before end date.");
                    return;
                }
                IInterestCalculator calculator = (IInterestCalculator)cboInterestType.SelectedItem;
                if (calculator == null)
                {
                    HostUI.ErrorMessageBox("Please select an interest type.");
                    return;
                }
                string interestCatKey = GetInterestCategoryKey();
                if (interestCatKey == null)
                {
                    HostUI.ErrorMessageBox("Please select an interest category.");
                    return;
                }
                decimal annualRate;
                if (!decimal.TryParse(txtAnnualRate.Text, out annualRate))
                {
                    HostUI.ErrorMessageBox("Please enter a valid annual interest rate, like \"9.5\" for 9.5%.");
                    return;
                }

                // Compute daily balances from all selected registers
                decimal   startingBalance = 0M;
                decimal[] dailyTotals     = new decimal[interestDays];
                foreach (ListViewItem itm in lvwRegisters.CheckedItems)
                {
                    Register reg = (Register)itm.Tag;
                    foreach (BaseTrx baseTrx in new RegIterator <BaseTrx>(reg))
                    {
                        if (!baseTrx.IsFake && ((baseTrx is BankTrx) || (baseTrx is ReplicaTrx)))
                        {
                            if (baseTrx.TrxDate < startDate)
                            {
                                startingBalance += baseTrx.Amount;
                            }
                            else if (baseTrx.TrxDate <= endDate)
                            {
                                dailyTotals[baseTrx.TrxDate.Subtract(startDate).Days] += baseTrx.Amount;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                decimal[] dailyBalances    = new decimal[interestDays];
                decimal   prevBalance      = startingBalance;
                decimal   sumDailyBalances = 0M;
                for (int i = 0; i < interestDays; i++)
                {
                    dailyBalances[i]  = prevBalance + dailyTotals[i];
                    sumDailyBalances += dailyBalances[i];
                    prevBalance       = dailyBalances[i];
                }

                // Calculate interest
                decimal annualRateFraction = annualRate / 100M;
                decimal avgDailyBal        = Math.Round(sumDailyBalances / interestDays, 2);
                decimal totalInterest      = Math.Round(calculator.Calculate(startDate, dailyBalances, annualRateFraction), 2);
                string  memo = calculator.Memo(annualRateFraction, avgDailyBal);

                // Create bank trx in current register
                BankTrx  interestTrx    = new BankTrx(HostUI.GetCurrentRegister());
                DateTime dummy          = DateTime.MinValue;
                string   trxDescription = PersonalAcctExists ? "Interest:DIVIDE" : "Interest";
                interestTrx.NewStartNormal(true, "Inv", endDate, trxDescription, memo, BaseTrx.TrxStatus.Unreconciled,
                                           false, 0M, false, false, 0, "", "");
                interestTrx.AddSplit("", interestCatKey, "", "", Utilities.EmptyDate, Utilities.EmptyDate, "", "", totalInterest);
                if (!HostUI.AddNormalTrx(interestTrx, ref dummy, false, "Calculate Interest"))
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                ErrorHandling.TopException(ex);
            }
        }
Пример #6
0
 /// <summary>
 /// Calculate earned interest
 /// </summary>
 /// <returns></returns>
 public double interestEarned()
 {
     return(interestCalculator.Calculate(Transactions, dateProvider.now()));
 }