Created
March 12, 2011 03:09
-
-
Save azcoov/866998 to your computer and use it in GitHub Desktop.
POS / 408 MortgagePaymentCalculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' | |
' File: MortgagePaymentCalculator.vb | |
' Author: Billy Coover | |
' Date: March 11, 2011 | |
' | |
' Change request 16 from SR-mf-003. Create a VB.NET program that calculates and displays the monthly | |
' payment for a $200,000 loan at 5.75% interest for 30 years | |
' | |
' Quality Control | |
' Version 0 | 03/11/2011 | Write the program in VB.Net (not Web based) using a loan amount of $200,000 with an interest rate of 5.75% and a 30 year term. Display the mortgage payment amount. Insert comments to document the program. There is No Requirement for the user to enter values. | |
' Version 0.1 | 03/11/2011 | Added a Mortgage object rather than just hard coding mortgage amounts. | |
' Version 0.2 | 03/11/2011 | Created and tested the calculation for the monthly mortgage amount. | |
' Version 1.0 | 03/18/2011 | Change to prompt for user input | |
' Version 1.1 | 03/18/2011 | Added amortization schedule to payment calc class | |
' Version 2.0 | 03/22/2011 | Added mortgage list and looped through the list to calculate the pauments for each mortgage | |
' Version 2.1 | 03/29/2011 | Added mortgage list and looped through the list to calculate the pauments for each mortgage | |
' Version 3.0 | 03/29/2011 | Added mortgage list and looped through the list to calculate the pauments for each mortgage and display amortization schedule | |
' Version 3.1 | 03/31/2011 | Fixed a bug in the payment calc | |
' Version 3.2 | 04/01/2011 | Fixed another bug in the payment calc | |
Imports System | |
Imports System.Diagnostics | |
Imports System.Threading | |
Imports System.Collections.Generic | |
Namespace pos408 | |
''' <summary> | |
''' Mortgage payment calculator | |
''' </summary> | |
Class MortgagePaymentCalculator | |
'local variables | |
Public Shared mortgages As List(Of Mortgage) | |
Public Const principal As [Double] = 200000.0 | |
Public Shared paymentCalculator As PaymentCalculator | |
''' <summary> | |
''' Maint entry point of the program | |
''' </summary> | |
''' <param name="args"> | |
''' null for this application | |
''' </param> | |
Public Shared Sub Main(ByVal args As String()) | |
'Create a list of mortgages | |
mortgages = New List(Of Mortgage)() | |
mortgages.Add(New Mortgage() With { _ | |
.Principal = principal, _ | |
.InterestRate = 0.0535, _ | |
.Term = 7 _ | |
}) | |
mortgages.Add(New Mortgage() With { _ | |
.Principal = principal, _ | |
.InterestRate = 0.055, _ | |
.Term = 15 _ | |
}) | |
mortgages.Add(New Mortgage() With { _ | |
.Principal = principal, _ | |
.InterestRate = 0.0575, _ | |
.Term = 30 _ | |
'}) | |
'Iterate through the list of mortages and calculate the payment amount | |
For i As Integer = 0 To mortgages.Count - 1 | |
Dim counter = i + 1 | |
paymentCalculator = New PaymentCalculator(mortgages(i)) | |
'Print the mortgage properties to screen | |
Console.Write( | |
[String].Format( | |
"Mortgage {0} principal amount is: {1}", | |
counter, | |
mortgages(i).Principal) + Environment.NewLine) | |
Console.Write( | |
[String].Format( | |
"Mortgage {0} term is: {1}", | |
counter, | |
mortgages(i).Term) + Environment.NewLine) | |
Console.Write( | |
[String].Format( | |
"Mortgage {0} interest rate is: {1}", | |
counter, | |
mortgages(i).InterestRate) + Environment.NewLine) | |
'Print the calculated mortgage amount in currency format | |
Console.Write( | |
[String].Format( | |
"The monthly payment amount is for mortgage {0} is: {1} " & vbLf, | |
counter, | |
paymentCalculator.CalculatePayment().ToString("c")) + Environment.NewLine) | |
'Print the amortization schedule | |
paymentCalculator.PrintAmortizationSchedule() | |
Next | |
End Sub | |
End Class | |
''' <summary> | |
''' A mortgage object to store properties of a mortgage | |
''' </summary> | |
Class Mortgage | |
Public Property Principal() As Double | |
Get | |
Return m_Principal | |
End Get | |
Set(ByVal value As Double) | |
m_Principal = Value | |
End Set | |
End Property | |
Private m_Principal As Double | |
Public Property InterestRate() As Double | |
Get | |
Return m_InterestRate | |
End Get | |
Set(ByVal value As Double) | |
m_InterestRate = Value | |
End Set | |
End Property | |
Private m_InterestRate As Double | |
Public Property Term() As Integer | |
Get | |
Return m_Term | |
End Get | |
Set(ByVal value As Integer) | |
m_Term = Value | |
End Set | |
End Property | |
Private m_Term As Integer | |
Public ReadOnly Property MonthlyInterestRate() As Double | |
Get | |
Return InterestRate / 12 | |
End Get | |
End Property | |
Public ReadOnly Property TermInMonths() As Double | |
Get | |
Return Term * 12 | |
End Get | |
End Property | |
End Class | |
''' <summary> | |
''' A payment calculator object that accepts a mortgage object and calculates a monthly payment amount | |
''' </summary> | |
Class PaymentCalculator | |
Public mortgage As Mortgage | |
Public Sub New(ByVal mortgage As Mortgage) | |
Me.mortgage = mortgage | |
End Sub | |
' Calculates the monthly mortgage payment | |
Public Function CalculatePayment() As Double | |
Return ( | |
(Me.mortgage.Principal * | |
(Me.mortgage.InterestRate / 12)) / | |
(1 - Math.Pow(1 + (Me.mortgage.InterestRate / 12), -Me.mortgage.TermInMonths))) | |
End Function | |
' Prints the amortization schedule to screen | |
Public Sub PrintAmortizationSchedule() | |
'Initial variables | |
Dim paymentNumber As Integer = 0 | |
Dim loopIteration As Integer = 0 | |
Dim principalBalance As Double = Me.mortgage.Principal | |
Dim monthlyInterestRate As Double = Me.mortgage.MonthlyInterestRate | |
Dim monthlyPayment As Double = Me.CalculatePayment() | |
Dim currentMonthlyInterest As Double | |
Dim currentMonthlyPrincipal As Double | |
Dim rollingInterest As Double = 0 | |
Dim rollingPrincipal As Double = 0 | |
'basic while loop that goes until the balance is zero or less | |
While principalBalance > 1 | |
'Cycle the payment number for display | |
paymentNumber += 1 | |
'Calculate current monthly interest | |
currentMonthlyInterest = principalBalance * monthlyInterestRate | |
'Calculate this month's Principal payment | |
currentMonthlyPrincipal = monthlyPayment - currentMonthlyInterest | |
'Calculate the new principal balance of the loan after last payment | |
principalBalance = principalBalance - currentMonthlyPrincipal | |
'add rolling balances | |
rollingInterest = rollingInterest + currentMonthlyInterest | |
rollingPrincipal = rollingPrincipal + currentMonthlyPrincipal | |
'print payment #, interest paid, and loan balance | |
Console.WriteLine( | |
"Payment # " + paymentNumber.ToString() + ": Interest paid: " _ | |
+ currentMonthlyInterest.ToString("c") + " Loan balance: " _ | |
+ principalBalance.ToString("c")) | |
'Cycle the loop counter | |
loopIteration += 1 | |
'Print n times, then hesitate if there is still a remaining balance | |
If principalBalance > 0 AndAlso loopIteration >= 10 Then | |
loopIteration = 0 | |
Console.WriteLine(vbLf & " Calculating next group of payments... " & vbLf) | |
Try | |
'sleep for 1000 ms | |
System.Threading.Thread.Sleep(200) | |
'Supress the error for now | |
Catch | |
End Try | |
End If | |
End While | |
Console.WriteLine(vbLf & " Amortization schedule complete." & vbLf) | |
Console.WriteLine("Total cost of taking the loan to term: " + (rollingInterest + rollingPrincipal).ToString("c") & vbLf) | |
Try | |
'sleep for 1000 ms | |
System.Threading.Thread.Sleep(3000) | |
'Supress the error for now | |
Catch | |
End Try | |
End Sub | |
End Class | |
End Namespace |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment