Posted by: Zeeshan Amjad | September 5, 2009

Loan Amortization Application in WPF


Lets going to make a simple WPF application to calculate the Loan Amortization schedule. Here is a XAML code to crate the user interface of the application.

  1: <Window x:Class="Amortization.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Loan Amortization" Height="600" Width="800" Background="Wheat">
  5:     <Grid>
  6:         <Grid.RowDefinitions>
  7:             <RowDefinition/>
  8:             <RowDefinition/>
  9:             <RowDefinition/>
 10:             <RowDefinition Height="4*" />
 11:             <RowDefinition/>
 12:         </Grid.RowDefinitions>
 13:         
 14:         <Grid.ColumnDefinitions>
 15:             <ColumnDefinition/>
 16:             <ColumnDefinition/>
 17:         </Grid.ColumnDefinitions>
 18:         
 19:         <TextBlock Grid.Column="0" Grid.Row="0" Margin="5" VerticalAlignment="Center">
 20:             Principle Amount
 21:         </TextBlock>
 22: 
 23:         <TextBox Grid.Column="1" Grid.Row="0" Margin="5" Name="txtPrinciple" VerticalAlignment="Center">            
 24:         </TextBox>
 25:         
 26:         <TextBlock Grid.Column="0" Grid.Row="1" Margin="5" VerticalAlignment="Center">
 27:             Interest Rate
 28:         </TextBlock>
 29: 
 30:         <TextBox Grid.Column="1" Grid.Row="1" Margin="5" Name="txtInterest" VerticalAlignment="Center">
 31:         </TextBox>
 32: 
 33:         <TextBlock Grid.Column="0" Grid.Row="2" Margin="5" VerticalAlignment="Center">
 34:             No of years
 35:         </TextBlock>
 36: 
 37:         <TextBox Grid.Column="1" Grid.Row="2" Margin="5" Name="txtYears" VerticalAlignment="Center">
 38:         </TextBox>
 39: 
 40:         <ListBox Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" 
 41:                  Name="lstAmortization" Background="Wheat" HorizontalContentAlignment="Stretch">
 42:             <ListBox.ItemTemplate>
 43:                 <DataTemplate>
 44:                     <Border Margin="5" Background="White" BorderBrush="Brown" BorderThickness="5" CornerRadius="10">
 45:                         <Grid>
 46:                             <Grid.RowDefinitions>
 47:                                 <RowDefinition/>
 48:                                 <RowDefinition/>
 49:                                 <RowDefinition/>
 50:                                 <RowDefinition/>
 51:                                 <RowDefinition/>
 52:                             </Grid.RowDefinitions>
 53:                             
 54:                             <Grid.ColumnDefinitions>
 55:                                 <ColumnDefinition/>
 56:                                 <ColumnDefinition/>
 57:                             </Grid.ColumnDefinitions>
 58:                             
 59:                             <TextBlock Grid.Row="0" Grid.Column="0" Margin="5">
 60:                                 Payment No
 61:                             </TextBlock>
 62:                             
 63:                             <TextBlock Grid.Row="0" Grid.Column="1" Margin="5, 0" Foreground="Blue" FontSize="12" Text="{Binding Path=PaymentNo}"/>
 64: 
 65:                             <TextBlock Grid.Row="1" Grid.Column="0" Margin="5, 0">
 66:                                 Payment
 67:                             </TextBlock>
 68: 
 69:                             <TextBlock Grid.Row="1" Grid.Column="1" Margin="5, 0" Foreground="Blue" FontSize="12" Text="{Binding Path=Payment}"/>
 70: 
 71:                             <TextBlock Grid.Row="2" Grid.Column="0" Margin="5, 0">
 72:                                 Principle
 73:                             </TextBlock>
 74: 
 75:                             <TextBlock Grid.Row="2" Grid.Column="1" Margin="5, 0" Foreground="Blue" FontSize="12" Text="{Binding Path=Principle}"/>
 76: 
 77:                             <TextBlock Grid.Row="3" Grid.Column="0" Margin="5, 0">
 78:                                 Interest
 79:                             </TextBlock>
 80: 
 81:                             <TextBlock Grid.Row="3" Grid.Column="1" Margin="5, 0" Foreground="Blue" FontSize="12" Text="{Binding Path=Interest}"/>
 82: 
 83:                             <TextBlock Grid.Row="4" Grid.Column="0" Margin="5, 0">
 84:                                 Balance
 85:                             </TextBlock>
 86: 
 87:                             <TextBlock Grid.Row="4" Grid.Column="1" Margin="5, 0" Foreground="Blue" FontSize="12" Text="{Binding Path=Balance}"/>
 88:                         </Grid>
 89:                     </Border>
 90:                 </DataTemplate>
 91:             </ListBox.ItemTemplate>        
 92:         </ListBox>
 93:         
 94:         <Button Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="2" Width="75" Height="45" Name="btnCalculate" Click="btnCalculate_Click">
 95:             Calculate
 96:         </Button>
 97:     </Grid>
 98: </Window>
 99: 

This application will display the loan amortization schedule in list box. To display it in more user friendly way we use the data template with list box.

Here is a C# code to perform all the calculation.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: 
 15: namespace Amortization
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         private int years;
 23:         private double interestRate;
 24:         private double principle;
 25:         private int totalPayments;
 26:         private double payment;
 27:         private double monthlyRate;
 28: 
 29:         public Window1()
 30:         {
 31:             InitializeComponent();
 32:         }
 33: 
 34:         private void btnCalculate_Click(object sender, RoutedEventArgs e)
 35:         {
 36:             years = Convert.ToInt32(txtYears.Text);
 37:             interestRate = Convert.ToDouble(txtInterest.Text) / 100;
 38:             principle = Convert.ToDouble(txtPrinciple.Text);
 39:             monthlyRate = interestRate / 12;
 40:             totalPayments = years * 12;
 41: 
 42:             calculatePayment();
 43:         }
 44: 
 45:         private void calculatePayment()
 46:         {
 47:             // calculate interest term
 48:             double interestTerm = Math.Pow((1 + monthlyRate), this.totalPayments);
 49: 
 50:             // calculate payment
 51:             this.payment = (this.principle * monthlyRate) / (1 - (1 / interestTerm));
 52: 
 53:             for (int iIndex = 1; iIndex <= totalPayments; ++iIndex)
 54:             {
 55:                 PaymentInfo paymentInfo = new PaymentInfo();
 56:                 paymentInfo.PaymentNo = iIndex;
 57:                 paymentInfo.Balance = calculateBalance(iIndex);
 58:                 paymentInfo.Payment = payment;
 59:                 paymentInfo.Interest = calculateInterestPart(iIndex);
 60:                 paymentInfo.Principle = calculatePrinciple(iIndex);
 61: 
 62:                 lstAmortization.Items.Add(paymentInfo);
 63:             }
 64:         }
 65: 
 66:         private double calculateBalance(int month)
 67:         {
 68:             double interestTerm = Math.Pow((1 + monthlyRate), month);
 69:             double totalInterest = this.principle * interestTerm;
 70:             double totalPaid = payment * (interestTerm - 1) / monthlyRate;
 71:             return totalInterest - totalPaid;
 72:         }
 73: 
 74:         private double calculateInterestPart(int month)
 75:         {
 76:             double interestTerm = Math.Pow((1 + monthlyRate), (month - 1));
 77:             double totalInterest = this.principle * interestTerm;
 78:             double totalPaid = payment * (interestTerm - 1) / monthlyRate;
 79:             return (totalInterest - totalPaid) * monthlyRate;
 80:         }
 81: 
 82:         private double calculatePrinciple(int month)
 83:         {
 84:             return payment - calculateInterestPart(month);
 85:         }
 86: 
 87: 
 88:     }
 89: 
 90:     class PaymentInfo
 91:     {
 92:         private int paymentNo;
 93:         private double payment;
 94:         private double principle;
 95:         private double interest;
 96:         private double balance;
 97: 
 98:         public int PaymentNo
 99:         {
100:             get { return paymentNo; }
101:             set { paymentNo = value; }
102:         }
103: 
104:         public double Payment
105:         {
106:             get { return payment; }
107:             set { payment = value; }
108:         }
109: 
110:         public double Principle
111:         {
112:             get { return principle; }
113:             set { principle = value; }
114:         }
115: 
116:         public double Interest
117:         {
118:             get { return interest; }
119:             set { interest = value; }
120:         }
121: 
122:         public double Balance
123:         {
124:             get { return balance; }
125:             set { balance = value; }
126:         }
127:     }
128: 
129: }
130: 

Here is the output of this program.

Amortization

Advertisements

Responses

  1. […] coverage of graph and I wanted to test couple of graph. I already made a loan amortization program here so it would be a perfect choice for me to make a graph for that application. I changed the user […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: