Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Switching to MVVM

, 19 May 2011 CPOL
How to incorporate MVVM in your existing codebase, step by step, and the risks involved in each step.
MVVM_Src.zip
MVVM
MVVM
Properties
Settings.settings
Step1
Properties
Settings.settings
Step10
Properties
Settings.settings
Step2
Properties
Settings.settings
Step3
Properties
Settings.settings
Step4
Properties
Settings.settings
Step5
Properties
Settings.settings
Step6
Properties
Settings.settings
Step7
Properties
Settings.settings
Step8
Properties
Settings.settings
Step9
Properties
Settings.settings
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)

Share

About the Author

Zeeshan Amjad
Software Developer (Senior) Bloomberg LP
United States United States
Working as a Sr C++ Developer at Bloomberg LP

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 19 May 2011
Article Copyright 2011 by Zeeshan Amjad
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid