Click here to Skip to main content
15,884,986 members
Articles / Programming Languages / C#

Switching to MVVM

Rate me:
Please Sign up or sign in to vote.
4.93/5 (12 votes)
19 May 2011CPOL11 min read 54.4K   1.2K   36  
How to incorporate MVVM in your existing codebase, step by step, and the risks involved in each step.
using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace MVVM
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private double principle;
        private double interestRate;
        private int duration;
        private double payment;
        private ObservableCollection<PaymentInfo> payments = new ObservableCollection<PaymentInfo>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnCalculate_Click(object sender, RoutedEventArgs e)
        {
            if (txtPrincipleAmount.Text.Length > 0)
            {
                principle = Convert.ToDouble(txtPrincipleAmount.Text);
            }
            else
            {
                MessageBox.Show("Please enter principle amount", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (txtInterestRate.Text.Length > 0)
            {
                interestRate = Convert.ToDouble(txtInterestRate.Text);
                interestRate /= 100;
                interestRate /= 12;
            }
            else
            {
                MessageBox.Show("Please enter interest", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (txtDuration.Text.Length > 0)
            {
                duration = Convert.ToInt32(txtDuration.Text);
            }
            else
            {
                MessageBox.Show("Please enter duration", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            CalculatePayment();
        }

        // Calculate the complete amortization schedule and fill the data grid control
        private void CalculatePayment()
        {
            int totalpayments = duration * 12;

            Title = "Amortization Schedule for " + 
                Convert.ToString(totalpayments) + " Payments";

            // calculate interest term
            double interestTerm = Math.Pow((1 + interestRate), totalpayments);

            // calculate payment
            payment = (principle * interestRate) / (1 - (1 / interestTerm));

            payments.Clear();

            for (int iIndex = 1; iIndex <= totalpayments; ++iIndex)
            {
                PaymentInfo paymentInfo = new PaymentInfo();
                paymentInfo.PaymentNo = iIndex;
                paymentInfo.Balance = CalculateBalance(iIndex);
                paymentInfo.Payment = payment;
                paymentInfo.Interest = CalculateInterestPart(iIndex);
                paymentInfo.Principle = CalculatePrinciple(iIndex);

                payments.Add(paymentInfo);
            }

            lstAmortization.ItemsSource = payments;
        }

        // Calculate the remaining balance at particular payment
        private double CalculateBalance(int month)
        {
            double interestTerm = Math.Pow((1 + interestRate), month);
            double totalInterest = principle * interestTerm;
            double totalPaid = payment * (interestTerm - 1) / interestRate;
            return totalInterest - totalPaid;
        }

        // Calculate the Interest part of any particular payment
        private double CalculateInterestPart(int month)
        {
            double interestTerm = Math.Pow((1 + interestRate), (month - 1));
            double totalInterest = principle * interestTerm;
            double totalPaid = payment * (interestTerm - 1) / interestRate;
            return (totalInterest - totalPaid) * interestRate;
        }

        // Calculate the principle part of any particular payment
        private double CalculatePrinciple(int month)
        {
            return payment - CalculateInterestPart(month);
        }
    }

    public class PaymentInfo
    {
        public int PaymentNo
        { get; set; }

        public double Payment
        { get; set; }

        public double Principle
        { get; set; }

        public double Interest
        { get; set; }

        public double Balance
        { get; set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader American Institute for Research
United States United States
Working as a Team leader in American Institute for Research

Comments and Discussions