コード例 #1
0
        public IActionResult OnGet(int npvId)
        {
            dto           = new NpvDTO();
            dto.CashFlows = new List <CashFlowDTO>();

            return(Page());
        }
コード例 #2
0
        public void Should_Not_Accept_Lower_Rate_Higher_Than_Upper_Rate()
        {
            var dto = new NpvDTO
            {
                Name           = "Cashflow 1",
                IncrementRate  = 25,
                LowerRate      = 2,
                UpperRate      = 1,
                InitialValue   = 10000,
                TotalNpvAmount = null,
                CashFlows      = new List <CashFlowDTO> {
                    new CashFlowDTO {
                        Amount = 1000
                    },
                    new CashFlowDTO {
                        Amount = 2000
                    }
                }
            };

            Mock <INpvValidate> mockValidator = new Mock <INpvValidate>();

            mockValidator.Setup(x => x.Validate(It.Is <NpvDTO>(n => n.LowerRate > n.UpperRate))).Throws <Exception>();

            var sut = new NpvCalculator(mockValidator.Object);

            Assert.Throws <Exception>(() => sut.Compute(dto));
        }
コード例 #3
0
        public IActionResult OnPost()
        {
            dto.CashFlows = new List <CashFlowDTO>();

            for (int i = 0; i < Request.Form["y.Id"].Count; i++)
            {
                dto.CashFlows.Add(new CashFlowDTO
                {
                    Id     = Convert.ToInt32(Request.Form["y.Id"][i]),
                    Amount = string.IsNullOrEmpty(Request.Form["y.Amount"][i]) ? 0 : Convert.ToDouble(Request.Form["y.Amount"][i])
                });
            }
            NpvValidate   validate = new NpvValidate();
            NpvCalculator calc     = new NpvCalculator(validate);

            dto = calc.Compute(dto);

            if (ModelState.IsValid)
            {
                var npv = mapper.Map <Npv>(dto);
                npvData.Update(npv);

                return(RedirectToPage("./Detail", new { npvId = dto.NpvId }));
            }
            else
            {
                ModelState.AddModelError("error", "An error occured");
            }

            return(Page());
        }
コード例 #4
0
        public void Compute_NPV()
        {
            var dto = new NpvDTO
            {
                Name           = "Cashflow 1",
                IncrementRate  = 25,
                LowerRate      = 1,
                UpperRate      = 2,
                InitialValue   = 10000,
                TotalNpvAmount = null,
                CashFlows      = new List <CashFlowDTO> {
                    new CashFlowDTO {
                        Amount = 1000
                    },
                    new CashFlowDTO {
                        Amount = 2000
                    }
                }
            };

            Mock <INpvValidate> mockValidator = new Mock <INpvValidate>();

            mockValidator.Setup(x => x.Validate(It.IsAny <NpvDTO>())).Returns(true);

            var sut = new NpvCalculator(mockValidator.Object);

            var n = sut.Compute(dto);

            Assert.Equal("-7750.14", String.Format("{0:0.##}", n.TotalNpvAmount));
        }
コード例 #5
0
        public IActionResult OnGet(int npvId)
        {
            var result = npvData.GetByNpvId(npvId);

            dto = mapper.Map <NpvDTO>(result);

            if (dto == null)
            {
                return(RedirectToPage("./NotFound"));
            }
            return(Page());
        }
コード例 #6
0
        public bool Validate(NpvDTO npv)
        {
            if (npv.LowerRate > npv.UpperRate)
            {
                throw new Exception("Lower rate should be lower than upper rate");
            }
            if (npv.CashFlows.Count == 0)
            {
                throw new Exception("Cash flow(s) are required.");
            }

            return(true);
        }
コード例 #7
0
        public void Should_Not_Accept_If_Empty_Cashflow()
        {
            var dto = new NpvDTO
            {
                Name           = "Cashflow 1",
                IncrementRate  = 25,
                LowerRate      = 2,
                UpperRate      = 1,
                InitialValue   = 10000,
                TotalNpvAmount = null,
                CashFlows      = null
            };

            Mock <INpvValidate> mockValidator = new Mock <INpvValidate>();

            mockValidator.Setup(x => x.Validate(It.Is <NpvDTO>(n => n.CashFlows == null))).Throws <Exception>();

            var sut = new NpvCalculator(mockValidator.Object);

            Assert.Throws <Exception>(() => sut.Compute(dto));
        }
コード例 #8
0
ファイル: Compute.cshtml.cs プロジェクト: toastedcoke/Finance
        public IActionResult OnGet()
        {
            try
            {
                dto = TempData.Get <NpvDTO>("npvDTO");

                if (dto == null)
                {
                    return(RedirectToPage("./NotFound"));
                }

                NpvValidate   validate = new NpvValidate();
                NpvCalculator calc     = new NpvCalculator(validate);
                dto = calc.Compute(dto);

                return(Page());
            }
            catch (Exception e)
            {
                return(RedirectToPage("./Error"));
            }
        }
コード例 #9
0
ファイル: NpvCalculator.cs プロジェクト: toastedcoke/Finance
        public NpvDTO Compute(NpvDTO npv)
        {
            try
            {
                if (validator.Validate(npv))
                {
                    int n = npv.CashFlows.Count;

                    var rate = npv.LowerRate;
                    int i    = 0; //year

                    double total = npv.InitialValue * -1;

                    foreach (var item in npv.CashFlows)
                    {
                        i++;
                        item.NpvAmount = total + (item.Amount / Math.Pow(1 + Convert.ToDouble(rate) / 100, i));

                        rate += npv.IncrementRate;
                        total = item.NpvAmount;
                    }

                    npv.TotalNpvAmount = total;
                }
                else
                {
                    npv.TotalNpvAmount = null;
                    npv.CashFlows.ForEach(x => x.NpvAmount = 0);
                }

                return(npv);
            }
            catch (Exception e)
            {
                throw e;
            }
        }