Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / WPF

Loan Amortization Application in WPF using VC++

Rate me:
Please Sign up or sign in to vote.
4.75/5 (6 votes)
10 Jul 2009CPOL22 min read 61.5K   2.1K   27  
This article discusses how to do WPF programming in VC++ and one working example of Loan Amortization with some background.
#include "PaymentWindow.h"

PaymentWindow::PaymentWindow()
{
	totalpayments = 0;
	payment = 0.0;

	Title = L"Amortization";
	Height = 480;
	Width = 640;
	WindowStyle = Windows::WindowStyle::ToolWindow;
	ResizeMode = Windows::ResizeMode::NoResize;
	WindowStartupLocation = Windows::WindowStartupLocation::CenterOwner;

	Grid^ grid = gcnew Grid();
	grid->Background::set(Media::Brushes::LightSalmon);

	// Create Rows in grid layout
	Windows::Controls::RowDefinition^ row1 = gcnew Windows::Controls::RowDefinition();
	Windows::Controls::RowDefinition^ row2 = gcnew Windows::Controls::RowDefinition();
	Windows::Controls::RowDefinition^ row3 = gcnew Windows::Controls::RowDefinition();
	Windows::Controls::RowDefinition^ row4 = gcnew Windows::Controls::RowDefinition();
	Windows::Controls::RowDefinition^ row5 = gcnew Windows::Controls::RowDefinition();

	// Create Columns in grid layout
	Windows::Controls::ColumnDefinition^ col1 = gcnew Windows::Controls::ColumnDefinition();
	Windows::Controls::ColumnDefinition^ col2 = gcnew Windows::Controls::ColumnDefinition();

	row1->Height = Windows::GridLength(45);
	row2->Height = Windows::GridLength(45);;
	row3->Height = Windows::GridLength(45);;
	row5->Height = Windows::GridLength(45);;

	// Add all the rows into grid
	grid->RowDefinitions->Add(row1);
	grid->RowDefinitions->Add(row2);
	grid->RowDefinitions->Add(row3);
	grid->RowDefinitions->Add(row4);
	grid->RowDefinitions->Add(row5);

	// Add all the columns into grid
	grid->ColumnDefinitions->Add(col1);
	grid->ColumnDefinitions->Add(col2);

	// Title of the Window
	lblTitle = gcnew Label();
	lblTitle->Content = L"Amortization Schedule";
	lblTitle->VerticalAlignment = Windows::VerticalAlignment::Center;
	lblTitle->HorizontalAlignment = Windows::HorizontalAlignment::Center;
	lblTitle->FontSize = 16;
	grid->SetRow(lblTitle, 0);
	grid->SetColumn(lblTitle, 0);
	grid->SetColumnSpan(lblTitle, 2);

	// Label of Monthly payment
	lblMonthlyPayment = gcnew Label();
	lblMonthlyPayment->Margin = Thickness(5);
	lblMonthlyPayment->Content = L"Monthly Payment";
	lblMonthlyPayment->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(lblMonthlyPayment, 1);
	grid->SetColumn(lblMonthlyPayment, 0);

	// Text Box of Monthly Payment
	txtMonthlyPayment = gcnew TextBox();
	txtMonthlyPayment->Margin = Thickness(5);
	txtMonthlyPayment->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(txtMonthlyPayment, 1);
	grid->SetColumn(txtMonthlyPayment, 1);

	// Label of Total
	lblTotal = gcnew Label();
	lblTotal->Margin = Thickness(5);
	lblTotal->Content = L"Total Amount Paid";
	lblTotal->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(lblTotal, 2);
	grid->SetColumn(lblTotal, 0);

	// Text Box of Total
	txtTotal = gcnew TextBox();
	txtTotal->Margin = Thickness(5);
	txtTotal->VerticalAlignment = Windows::VerticalAlignment::Center;
	grid->SetRow(txtTotal, 2);
	grid->SetColumn(txtTotal, 1);

	// List View
	grdView = gcnew GridView();
	grdView->ColumnHeaderToolTip = L"Amortization Schedule";

	// Create payment no columnd and bind it
	GridViewColumn^ paymentNo = gcnew GridViewColumn();
	paymentNo->DisplayMemberBinding = gcnew Data::Binding("PaymentNo");
	paymentNo->Header = L"Payment No";
	paymentNo->Width = 100;
	grdView->Columns->Add(paymentNo);

	// Create Payment column and bind it
	GridViewColumn^ payment = gcnew GridViewColumn();
	payment->DisplayMemberBinding = gcnew Data::Binding("Payment");
	payment->Header = L"Payment";
	payment->Width = 100;
	grdView->Columns->Add(payment);

	// Create Principle column and bind it
	GridViewColumn^ principle = gcnew GridViewColumn();
	principle->DisplayMemberBinding = gcnew Data::Binding("Principle");
	principle->Header = L"Principle";
	principle->Width = 100;
	grdView->Columns->Add(principle);

	// Create Interest column and bind it
	GridViewColumn^ interest = gcnew GridViewColumn();
	interest->DisplayMemberBinding = gcnew Data::Binding("Interest");
	interest->Header = L"Interest";
	interest->Width = 100;
	grdView->Columns->Add(interest);

	// Create Balance column and bind it
	GridViewColumn^ balance = gcnew GridViewColumn();
	balance->DisplayMemberBinding = gcnew Data::Binding("Balance");
	balance->Header = L"Balance";
	balance->Width = 100;
	grdView->Columns->Add(balance);

	lstView = gcnew ListView();
	lstView->Margin = Thickness(5);
	lstView->View = grdView;

	//lstView->View::set(Windows::Controls::ViewBase::ViewBase);
	grid->SetRow(lstView, 3);
	grid->SetColumn(lstView, 0);
	grid->SetColumnSpan(lstView, 2);

	// Button to exit
	btnExit = gcnew Button();
	btnExit->Content = "Exit";
	btnExit->Width = 120;
	btnExit->Margin = Thickness(10);
	btnExit->Click += gcnew RoutedEventHandler(this, &PaymentWindow::OnBtnExit);
	grid->SetRow(btnExit, 4);
	grid->SetColumn(btnExit, 0);
	grid->SetColumnSpan(btnExit, 2);

	// add all the controls into grid
	grid->Children->Add(lblTitle);
	grid->Children->Add(lblMonthlyPayment);
	grid->Children->Add(txtMonthlyPayment);
	grid->Children->Add(lblTotal);
	grid->Children->Add(txtTotal);
	grid->Children->Add(lstView);
	grid->Children->Add(btnExit);

	Content = grid;

	this->Loaded += gcnew RoutedEventHandler(this, &PaymentWindow::OnLoad);	
}

void PaymentWindow::OnLoad(Object^ sender, RoutedEventArgs^ e)
{
	DisplayAmortization();
}

void PaymentWindow::OnBtnExit(Object^ sender, RoutedEventArgs^ e)
{
	Close();
}

void PaymentWindow::DisplayAmortization()
{
	CalculatePayment();

	// total amount paid
	double totalAmount = payment * totalpayments;

	txtTotal->Text = Convert::ToString(totalAmount);
	txtMonthlyPayment->Text = Convert::ToString(payment);

}

// Calculate the complete amortization schedule and fill the list control
void PaymentWindow::CalculatePayment()
{
	Title = L"Amortizatin Schedule for " + Convert::ToString(totalpayments) + " Payments";

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

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

	for (int iIndex = 1; iIndex <= totalpayments; ++iIndex)
	{
		PaymentInfo^ paymentInfo = gcnew PaymentInfo();
		paymentInfo->PaymentNo = iIndex;
		paymentInfo->Balance = CalculateBalance(iIndex);
		paymentInfo->Payment = payment;
		paymentInfo->Interest = CalculateInterestPart(iIndex);
		paymentInfo->Principle = CalculatePrinciple(iIndex);

		lstView->Items->Add(paymentInfo);
	}
}

// Calculate the remaining balance at particular payment
double PaymentWindow::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
double PaymentWindow::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
double PaymentWindow::CalculatePrinciple(int month)
{
    return payment - CalculateInterestPart(month);
}

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