private void Form_Load(object sender, EventArgs e)
        {
            this.calc = new Calc(1000.00, 0.00, 0.0265, 12, 0.00);
            this.targetField = 3;

            this.targetService = 1;
            this.services = new List<CalcService>();
            this.services.Add(new SimpleCalcService());
            this.services.Add(new CompoundCalcService());

            this.calcTarget(this.calc);
            this.modelToView(this.calc);
        }
 private void calcTarget(Calc calc)
 {
     this.service = this.services.ElementAt(this.targetService);
     switch (targetField)
     {
         case 0:
             this.service.InitialAmount(calc);
             break;
         case 1:
             this.service.MonthQtd(calc);
             break;
         case 2:
             this.service.InterestRate(calc);
             break;
         case 3:
             this.service.TotalAmount(calc);
             break;
     }
 }
 public void TotalAmount(Calc calc)
 {
     // M = C * (1 + i) ^ t
     calc.TotalAmount = calc.InitialAmount * Math.Pow(1 + calc.InterestRate, calc.MonthQtd);
     calc.InstallmentAmount = calc.TotalAmount / calc.MonthQtd;
 }
 public void MonthQtd(Calc calc)
 {
     // t = ln(M / C) / ln(1 + i)
     calc.MonthQtd = Convert.ToInt32(Math.Log(calc.TotalAmount / calc.InitialAmount) / Math.Log(1 + calc.InterestRate));
     calc.InstallmentAmount = calc.TotalAmount / calc.MonthQtd;
 }
 public void InterestRate(Calc calc)
 {
     // i = exp(ln(M / C) / t) - 1
     calc.InterestRate = Math.Exp(Math.Log(calc.TotalAmount / calc.InitialAmount) / calc.MonthQtd) - 1;
     calc.InstallmentAmount = calc.TotalAmount / calc.MonthQtd;
 }
 public void InitialAmount(Calc calc)
 {
     // C = M / (1 + i) ^ t
     calc.InitialAmount = (calc.TotalAmount / Math.Pow(1 + calc.InterestRate, calc.MonthQtd));
     calc.InstallmentAmount = calc.TotalAmount / calc.MonthQtd;
 }
 public void TotalAmount(Calc calc)
 {
     // M = C * ( 1 + ( i * t ))
     calc.TotalAmount = calc.InitialAmount * (1 + (calc.InterestRate * calc.MonthQtd));
     calc.InstallmentAmount = calc.TotalAmount / calc.MonthQtd;
 }
 public void MonthQtd(Calc calc)
 {
     // t = ( M / C - 1 ) / i
     calc.MonthQtd = Convert.ToInt32((calc.TotalAmount / calc.InitialAmount - 1) / calc.InterestRate);
     calc.InstallmentAmount = calc.TotalAmount / calc.MonthQtd;
 }
 public void InterestRate(Calc calc)
 {
     // i = ( M / C - 1 ) / t
     calc.InterestRate = (calc.TotalAmount / calc.InitialAmount - 1) / calc.MonthQtd;
     calc.InstallmentAmount = calc.TotalAmount / calc.MonthQtd;
 }
 private void viewToModel(Calc calc)
 {
     calc.InitialAmount = txtInitialAmount.Text != "" ? Double.Parse(txtInitialAmount.Text, NumberStyles.Currency) : 0.00;
     calc.InterestRate = txtInterestRate.Text != "" ? Double.Parse(txtInterestRate.Text, NumberStyles.Float) / 100 : 0.00;
     calc.MonthQtd = txtMonthQtd.Text != "" ? Int32.Parse(txtMonthQtd.Text) : 0;
     calc.TotalAmount = txtTotalAmount.Text != "" ? Double.Parse(txtTotalAmount.Text, NumberStyles.Currency) : 0.00;
     calc.InstallmentAmount = txtInitialAmount.Text != "" ? Double.Parse(txtInstallmentAmount.Text, NumberStyles.Currency) : 0.00;
     this.radioToTargetField();
     this.radioToTargetService();
 }
 private void modelToView(Calc calc)
 {
     txtInitialAmount.Text = calc.InitialAmount.ToString("C2");
     txtInterestRate.Text = Convert.ToString(calc.InterestRate * 100);
     txtMonthQtd.Text = Convert.ToString(calc.MonthQtd);
     txtTotalAmount.Text = calc.TotalAmount.ToString("C2");
     txtInstallmentAmount.Text = calc.InstallmentAmount.ToString("C2");
     this.targetFieldToRadio();
     this.targetServiceToRadio();
     this.updateDataGrid();
 }