Advertisement
Guest User

Untitled

a guest
May 26th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. public class CapitalCalculator : CompareModel
  2.     {
  3.         public DateTime StartDate { get; set; }
  4.         public DateTime StopDate { get; set; }
  5.  
  6.         private const int Capitalisation = 12;
  7.         public Tuple<Decimal, Decimal> GetIncome(List<FundValue> fundList)
  8.         {
  9.             var startandstop = GetValidDate(fundList);
  10.             Tuple<FundValue,FundValue> dates = new Tuple<FundValue, FundValue>(startandstop.ElementAt(0),
  11.                 startandstop.ElementAt(startandstop.Count - 1));
  12.             var fundIncome = GetFundIncome(dates);
  13.             var depositIncome = GetDepositIncome(dates);
  14.             return new Tuple<decimal, decimal>(fundIncome, decimal.Round(depositIncome,2));
  15.         }
  16.  
  17.         private decimal GetDepositIncome(Tuple<FundValue, FundValue> dates)
  18.         {
  19.             int numberOfMonths;
  20.             if (dates.Item2.fundDate.Day < dates.Item1.fundDate.Day)
  21.                 numberOfMonths = CountMonths(dates.Item1.fundDate,dates.Item2.fundDate)- 1;
  22.             else
  23.                 numberOfMonths = CountMonths(dates.Item1.fundDate, dates.Item2.fundDate);
  24.             var income = GetPartialDepositIncome(numberOfMonths);
  25.             return income;
  26.         }
  27.  
  28.         private decimal GetPartialDepositIncome(int numberOfMonths)
  29.         {
  30.             decimal totalIncome = Money;
  31.             for (int counter = 0; counter < numberOfMonths; counter++)
  32.             {
  33.                 totalIncome += (decimal)(decimal.ToDouble(totalIncome)*(Percent/Capitalisation));
  34.             }
  35.             return totalIncome-Money;
  36.         }
  37.  
  38.         private static int CountMonths(DateTime start, DateTime end)
  39.         {
  40.             return (end.Year * 12 + end.Month) - (start.Year * 12 + start.Month);
  41.         }
  42.  
  43.         private List<FundValue> GetValidDate(List<FundValue> fundList)
  44.         {
  45.             var starttrim = from fundval in fundList where fundval.fundDate >= StartDate select fundval;
  46.             var stoptrim = (from fundval in starttrim where fundval.fundDate <= StopDate select fundval).ToList();
  47.  
  48.             return stoptrim;
  49.         }
  50.  
  51.         private decimal GetFundIncome(Tuple<FundValue, FundValue> dates)
  52.         {
  53.             double unitcount =decimal.ToDouble(this.Money)/dates.Item1.value;
  54.             decimal income = (decimal)(unitcount*dates.Item2.value);
  55.             return income-Money;
  56.         }
  57.  
  58.         private List<decimal> GetFundPartialIncomeList(List<FundValue> fundList)
  59.         {
  60.             List<decimal> partialIncome = new List<decimal>();
  61.             foreach (var fund in fundList)
  62.             {
  63.                 partialIncome.Add((decimal)(fund.value * decimal.ToDouble(Money)));
  64.             }
  65.             return partialIncome;
  66.         }
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement