public IActionResult OnGet(int npvId) { dto = new NpvDTO(); dto.CashFlows = new List <CashFlowDTO>(); return(Page()); }
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)); }
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()); }
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)); }
public IActionResult OnGet(int npvId) { var result = npvData.GetByNpvId(npvId); dto = mapper.Map <NpvDTO>(result); if (dto == null) { return(RedirectToPage("./NotFound")); } return(Page()); }
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); }
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)); }
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")); } }
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; } }