Solution for VB.NET book exercise that outputs monthly balances from a fixed amount deposit
is Given Below:
I’m going through practice exercises from a book and am stuck on a particular example. Full disclosure, this is not an assignment or test but merely me practicing through chapters of a book called Introduction to Visual Basic so off the bat, I apologize to those who feel it is a stupid question to ask. The question goes like this,
Suppose a fixed amount of
money is deposited at the beginning of each month into an
investment paying 6% interest compounded monthly. After each
deposit is made,
new balance] = 1.005 * [previous balance one month ago] +
[fixed amount].
Write a program that requests the fixed amount of
the deposits as input and displays the balance after each of the
first four deposits.
Here is an attached image of the output
I am not sure how to approach this question really.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim amount As Decimal
amount = CDec(txtAmount.Text)
lstResults.Items.Add(MonthlyInvestment(amount)).ToString("C")
End Sub
Function MonthlyInvestment(amount As Decimal) As Decimal
Dim balance As Decimal = amount
Dim newBalance As Decimal
Dim interestRate As Decimal = 0.06
newBalance = 1.005 * balance + amount
Return newBalance
End Function
End Class
You are using a TextBox control which is not the best option for accepting numeric values. My first suggestion would be to swap that out in favor of a NumericUpDown control. You can find the documentation here: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.numericupdown
After that what you should do is setup a For/Next loop to loop from month 2 (since month 1 is the initial deposit) to month n. Outside of your loop you should declare a variable to hold the current total and inside of your loop, you should do your calculation based on the current total.
Take a look at this example using a NumericUpDown:
Dim total = NumericUpDown1.Value
lstResults.Items.Add($"Month 1: {total:c}")
For month = 2 To 4
total = 1.005 * total + NumericUpDown1.Value
lstResults.Items.Add($"Month {month}: {total:c}")
Next
Fiddle: https://dotnetfiddle.net/2CK432
Also, quick side-note, 1.005
is not 6 percent. Not sure if that’s a typo in your code.
Here’s an example you can refer to.
Private count As Decimal
Private monthCount As Integer = 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If count = 0 Then
count = CDec(txtAmount.Text)
lstResults.Items.Add($"Month {monthCount}: " + count.ToString("C"))
monthCount += 1
Else
lstResults.Items.Add($"Month {monthCount}: " + MonthlyInvestment(count).ToString("C"))
monthCount += 1
End If
End Sub
Function MonthlyInvestment(balance As Decimal) As Decimal
count = 1.005 * balance + CDec(txtAmount.Text)
Return count
End Function
Result of my test.
Hi all thank you for your help. I went back and looked at my previous work and indeed as one said, breaking down these problems I was able to see a pattern. This is what I did
Public Class Form1
Dim balance As Double = 0 'global variable, We need to access it everywhere in the project
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim originalDeposit As Double = CDbl(txtAmount.Text)
'clears the listbox and asaaign 0 to balance when the new blance is being entered
lstResults.Items.Clear()
balance = 0
' Option 1 displays results
'lstOutput.Items.Add("Month 1: " & newBalance(originalDeposit).ToString("C"))
'lstOutput.Items.Add("Month 2: " & newBalance(originalDeposit).ToString("C"))
'lstOutput.Items.Add("Month 3: " & newBalance(originalDeposit).ToString("C"))
'lstOutput.Items.Add("Month 4: " & newBalance(originalDeposit).ToString("C"))
' Option 2 - displays results
For i As Integer = 1 To 4
lstResults.Items.Add("Month " & i & ": " & newBalance(originalDeposit).ToString("C"))
Next
End Sub
‘calculates new balance. This function is called 4 times. It increase the balance
‘and returns its new value
Function newBalance(originalDeposit As Double) As Double
balance = 1.005 * (balance) + originalDeposit
Return balance
End Function
End Class