Getting balance from code in button - vb.net

I am having a little bit of trouble calculating the balance in my code.
In this program (balance = balance * (1 + interestRate). The thing is that we are in 2013 and the balance will be calculated for the year 2015-2017, which is picked by a checkbox the user clicks in the program, and also the user picks what type of account they will have (Ie Savings: 7% interest, Corporate: 5% interest).
They will then click the mediaEstimatedFund button and it will ask them to enter a balance for the type of account that they chose (Savings, Corporate, or both). It should then calculate the balance for the (Year, Account, and Balance amount) that they chose.
Here is the code I have and it is not giving me the right balances (Example: If the user chose $2000.00, a Savings account at 7% and the year 2016, it should give me the estimated budget of $2621.60), but it is not!
Year Savings Account Corporate Account Saving + Corporate
2013 $2000.00 $1000.00 $3000.00
2014 $2140.00 $1050.00 $3190.00
2015 $2289.8 $1102.5 $3392.3
2016 $2450.086 $1157.625 $3607.711
2017 $2621.59202 $1215.50625 $3837.09827
Public Class Form1
Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click
Dim interestRate, initialBalanceSavings, initialBalanceCorporate, finalBalance, theYear, balance, subTotal As Double
txtBoxEstimatedBudget.Enabled = False
txtBoxAgenciesNeeded.Enabled = False
If radButtonTraditional.Checked Then
txtBoxAgenciesNeeded.Text = 3
ElseIf radButtonEMedia.Checked Then
txtBoxAgenciesNeeded.Text = 2
End If
If checkBoxSavings.Checked Then
interestRate = 0.07
ElseIf checkBoxCorporate.Checked Then
interestRate = 0.05
ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then
interestRate = 0.12
End If
If radButton2015.Checked Then
theYear = 2015
End If
If radButton2016.Checked Then
theYear = 2016
End If
If radButton2017.Checked Then
theYear = 2017
End If
Dim inputtedData As String
If checkBoxSavings.Checked Then
Do
inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to cancel calculation!")
Exit Sub
Else
initialBalanceSavings = CType(inputtedData, Single)
If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000
End If
If checkBoxCorporate.Checked Then
Do
inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to Cancel calculation!")
Exit Sub
Else
initialBalanceCorporate = CType(inputtedData, Single)
If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000
End If
finalBalance = initialBalanceSavings + initialBalanceCorporate
For theYear = 2013 To 2017
subTotal = finalBalance * (1 + interestRate)
finalBalance = subTotal
Next
txtBoxEstimatedBudget.Text = FormatCurrency(finalBalance)
End Sub

I wasn't allowed to edit Zafs answer to include the extra line, so here is my take. The important point being that the initialization of finalBalance is outside of the for-loop.
finalBalance = initialBalanceSavings + initialBalanceCorporate
For theYear = 2013 To 2016
subtotal = finalBalance * (1 + interestRate)
finalBalance = subTotal
Next

For TheYear = 2013 to 2016
SubTotal = balance * (1 + interestRate)
balance = SubTotal
Next
UPDATE
The code block above is just to give you an IDEA of HOW to do the loop and what should be calculated INSIDE the loop. To get the actual ANSWER you MUST remember that 2013 is the CURRENT year so you would not ACTUALLY calculate the interest for that year. And key to the way in which you accomplish this is the formatting of your loop, which in reality should begin the FOLLOWING year.
For Year = 2013 + 1 to 2016
'blah blah blah
Next

Related

How to find federal taxes based on salary ranges

I'm designing a software that does basic payroll for a school project. I'm stuck on this one part where I have to figure out what the federal tax will be based on the employee salary. This is the chart that tells you the rate of the tax based on salary range.
I tried this code,
Dim federaltaxrate As Integer
Dim federaltax = (salary.Text * federaltaxrate)
If salary.Text >= 0 Then
If salary.Text <= 50 Then
federaltaxrate = 0
End If
ElseIf salary.text <= 500 Then
If salary.Text >= 50 Then
federaltaxrate = 0.1
End If
ElseIf salary.text <= 2500 Then
If salary.Text >= 500 Then
federaltaxrate = 45 + 0.15 * salary.Text - 500
End If
ElseIf salary.text <= 5000 Then
If salary.Text >= 2500 Then
federaltaxrate = 345 + 0.2 * salary.Text - 2500
End If
ElseIf salary.text >= 5000 Then
federaltaxrate = 845 + 0.25 * salary.Text - 5000
End If
Else
End If
I have a listbox that shows other information as well but this is what I used to show the calculated info in the listbox.
ListBox1.Items.Add("Federal Tax: $" + federaltax.ToString)
When I run this code and input in a random salary, the federal tax shows up as 0.
Do I need to convert the salary into weekly gross pay, if so how would I go on about writing the code that finds the federal tax rate based on the salary and it's range.
You might be having trouble with order of precedence of the arithmetic operations. I think a Select Case is cleaner.
Private Function GetFederalTax(GrossPay As Decimal) As Decimal
Dim FederalTax As Decimal
Select Case GrossPay
Case < 50
FederalTax = 0
Case < 500
FederalTax = CDec((GrossPay - 51) * 0.1)
Case < 2500
FederalTax = CDec(((GrossPay - 500) * 0.15) + 45)
Case < 5000
FederalTax = CDec(((GrossPay - 2500) * 0.2) + 345)
Case Else
FederalTax = CDec(((GrossPay - 5000) * 0.25) + 845)
End Select
Return FederalTax
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim grossPay As Decimal
If Decimal.TryParse(TextBox1.Text, grossPay) Then
Dim tax = GetFederalTax(grossPay)
Debug.Print($"Gross {grossPay} Tax {tax}")
ListBox1.Items.Add(String.Format("Federal Tax {0}, Gross Pay {1}", tax, grossPay)) '***EDIT***
Else
MessageBox.Show("Please enter a valid number.")
End If
End Sub
The sample test produced the following in the Immediate Window.
Gross 45 Tax 0
Gross 700 Tax 75
Gross 8000 Tax 1595
Gross 2501 Tax 345.2
Gross 2800 Tax 405
Firstly, your Boolean logic is all wrong. If the salary value was 51, you'd satisfy the first outer condition (>= 0) and then fail the first inner condition (<= 50). There would not be any further comparisons performed - which you would know if you'd debugged - so no tax calculation would ever be performed.
Secondly, your calculations are OK but you're misusing the results. Those calculation get the amount of tax to be paid, not the rate. The rates are 10%, 15%, 20% and 25%, which are already contained in your calculations. Get rid of that second variable and just assign the results of appropriate calculations to the one variable.
I would do it like this:
Dim salaryAmount = CDec(salary.Text)
Dim taxAmount As Decimal = Decimal.Zero
If salaryAmount > 5000D Then
taxAmount = 845D + 0.25D * (salaryAmount - 5000D)
ElseIf salaryAmount > 2500D Then
taxAmount = 345D + 0.2D * (salaryAmount - 2500D)
ElseIf salaryAmount > 500D Then
taxAmount = 45D + 0.15D * (salaryAmount - 500D)
ElseIf salaryAmount > 50D Then
taxAmount = 0.1D * (salaryAmount - 50D)
End If
'Use taxAmount here.
This uses appropriate data types throughout, i.e. it does not perform arithmetic on String values and it uses Decimal for currency values. The D suffix on the literals forces them to be type Decimal rather than Integer or Double.
It also works from biggest to smallest to simplify the Boolean expressions.
The Nested If should be combined like below as it is missing few cases
If salary.Text >= 0 And salary.Text <= 50 Then
federaltaxrate = 0
ElseIf salary.text <= 500 And salary.Text >= 50 Then
federaltaxrate = 0.1
ElseIf salary.text <= 2500 AND salary.Text >= 500 Then
federaltaxrate = 45 + 0.15 * salary.Text - 500
End If

How to remove decimal from variable in vb.net

I have created a code to work out pay of workers, and to - 1 of pay for charity if it is over 100, and every 10 over 100 -1. But I need to get rid of the decimal with new pay variable. At the end of the if function
Dim donation As Single
Dim newpay As Single
Dim hourlyRate As Decimal
Dim pay As Decimal
Dim hours As Single
Sub Main()
Console.WriteLine("how many hours have you worked this week?")
hours = Console.ReadLine
Console.WriteLine("what is your hourly pay?")
hourlyRate = Console.ReadLine
pay = hours * hourlyRate
newpay = pay
If pay > 100 Then
pay = pay - 1
newpay = newpay - 100
donation = donation + 1
newpay = newpay / 10
Math.Floor(newpay)
pay = pay - newpay
donation = donation + newpay
End If
Console.WriteLine("you earned £" & pay & " and donated £" & donation & " to charity")
Console.ReadLine()
End Sub
Use the Math.Floor function to round down
SO if newpay was 3.6
Math.Floor(newpay)
will return 3
SO
Use the result of this function
donation = donation + Math.Floor(newpay)
OR
newpay = Math.Floor(newpay)

Two balance won't add up if both checkboxes are checked

I am writing a program that should calculate the balance with the given information (Which checkbox they choose (Savings (7%), Corporate(5%), or both, then the year they choose (2015, 2016, 2017), and lastly the amount they put in when cued to enter balance (And that is based on what type of account they choose in the first step). I have gotten all of the totals to show up correctly, except for when they choose both account types (Savings and Corporate). To me their should be a line that just adds the two totals if both are chosen, but that isn't the case. At least for me it isn't.
The total I get if I enter the balance of $2000 for savings and choose year 2015 the total is $2289.80 which is correct and then if I choose 2015 for the corporate account and enter $1000 the total is $1102.50. So if I chose both of the accounts and entered the same numbers but checked both checkboxes I get $3434.70, but I should get $3392.30!
Here is the code I have, any help would be much appreciated! ( I think it might literally be just one line of code and it is killing me!)
Public Class Form1
Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click
Dim interestRate, initialBalanceSavings, initialBalanceCorporate, finalBalance, year, theYear, subTotal As Double
Dim savingsInterestRate As Double = 0
Dim corporateInterestRate As Double = 0
Dim savingsSubTotal As Double = 0
Dim corporateSubTotal As Double = 0
txtBoxEstimatedBudget.Enabled = False
txtBoxAgenciesNeeded.Enabled = False
If radButtonTraditional.Checked Then
txtBoxAgenciesNeeded.Text = 3
ElseIf radButtonEMedia.Checked Then
txtBoxAgenciesNeeded.Text = 2
End If
If checkBoxSavings.Checked Then
savingsInterestRate = 0.07
ElseIf checkBoxCorporate.Checked Then
corporateInterestRate = 0.05
End If
If checkBoxSavings.Checked = False And checkBoxCorporate.Checked = False Then
MsgBox("Please chose an account type to proceed!", MsgBoxStyle.Critical, "Error")
End If
If radButton2015.Checked Then
theYear = 2015
ElseIf radButton2016.Checked Then
theYear = 2016
ElseIf radButton2017.Checked Then
theYear = 2017
End If
Dim inputtedData As String
If checkBoxSavings.Checked Then
Do
inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to cancel calculation!")
Exit Sub
Else
initialBalanceSavings = CType(inputtedData, Single)
If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000
End If
If checkBoxCorporate.Checked Then
Do
inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to Cancel calculation!")
Exit Sub
Else
initialBalanceCorporate = CType(inputtedData, Single)
If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000
End If
savingsSubTotal = initialBalanceSavings
corporateSubTotal = initialBalanceCorporate
For year = 2013 To theYear - 1
If savingsInterestRate > 0 Then
savingsSubTotal = savingsSubTotal * (1 + savingsInterestRate)
End If
If corporateInterestRate > 0 Then
corporateSubTotal = corporateSubTotal * (1 + corporateInterestRate)
End If
Next
finalBalance = savingsSubTotal + corporateSubTotal
txtBoxEstimatedBudget.Text = FormatCurrency(finalBalance)
End Sub
I believe the erroneous logic is in this block of code:
finalBalance = initialBalanceSavings + initialBalanceCorporate
For year = 2013 To theYear - 1
subTotal = finalBalance * (1 + interestRate)
finalBalance = subTotal
Next
Combined with this block of code:
If checkBoxSavings.Checked Then
interestRate = 0.07
ElseIf checkBoxCorporate.Checked Then
interestRate = 0.05
End If
In the above, even if something checks both checkboxes the interest rate is set at 0.07 so in your loop if someone calculates 2015 you will do something like give you a final total of: 3000 * 1.07 * 1.07, whereas you really want (2000 * 1.07 * 1.07) + (1000 * 1.05 * 1.05)
In essence because you only use one variable to hold the interest rate (as well as only having one variable to hold sub-totals) your code cannot account properly for a scenario where someone selects both accounts. I would suggest having 2 variables to hold the interest rates:
Dim savingsInterestRate as Double = 0
Dim corporateInterestRate as Double = 0
And 2 variables to hold the sub-totals:
Dim savingsSubTotal as Double = 0
Dim corporateSubTotal as Double = 0
And in your For loop do:
savingsSubTotal = initialBalanceSavings
corporateSubTotal = initialBalanceCorporate
For year = 2013 To theYear - 1
If savingsInterestRate > 0 Then
savingsSubTotal = savingsSubTotal * (1 + savingsInterestRate)
End If
If corporateInterestRate > 0 Then
corporateSubTotal = corporateSubTotal * (1 + corporateInterestRate)
End If
Next
finalBalance = savingsSubTotal + corporateSubTotal
And change your interest rate check to:
If checkBoxSavings.Checked Then
savingsInterestRate = 0.07
End If
If checkBoxCorporate.Checked Then
corporateInterestRate = 0.05
End If
So you can account for the person's selection (using the ElseIf means that if they check both, it will discard the corporate rate since checkBoxSavings.Checked is already true sp it never falls into the ElseIf block)

VB Simple If statements (h/w)

I don't deal with VB. I am helping my mother with her homework and just cant think straight anymore on what to do with this statement.
Private Sub btnTotal_Click(sender As Object, e As EventArgs) Handles btnTotal.Click
Dim intPackA As Integer = 0
Dim intPackB As Integer = 0
Dim intPackC As Integer = 0
Dim SavingsPriceB As Decimal = 0.0
Dim SavingsPriceC As Decimal = 0.0
Dim TotalPrice As Decimal = 0.0
lblTotal.Text = String.Empty
If radPackA.Checked Then
TotalPrice = 9.95
lblTotal.Text = TotalPrice
If Integer.TryParse(txtHours.Text, intPackA) Then
If intPackA > 10 Then
TotalPrice = TotalPrice + ((intPackA - 10) * 2)
lblTotal.Text = TotalPrice
End If
End If
If chkSavings.Checked Then
SavingsPriceB = 14.95 + ((intPackB - 20) * 1)
SavingsPriceC = 19.95
If TotalPrice < SavingsPriceB And TotalPrice < SavingsPriceC Then
lblTotal.Text = TotalPrice & ", no savings with Package B or C"
End If
End If
ElseIf radPackB.Checked Then
TotalPrice = 14.95
lblTotal.Text = TotalPrice
If Integer.TryParse(txtHours.Text, intPackB) Then
If intPackB > 20 Then
TotalPrice = TotalPrice + ((intPackB - 20) * 1)
lblTotal.Text = TotalPrice
End If
End If
If chkSavings.Checked Then
End If
ElseIf radPackC.Checked Then
TotalPrice = 19.95
lblTotal.Text = TotalPrice
End If
If chkNonprofit.Checked Then
TotalPrice = Format((TotalPrice - ((TotalPrice / 100) * 20)), ".00")
lblTotal.Text = TotalPrice & ControlChars.CrLf & "Non-Profit Organization discount applied"
End If
End Sub
It's the If chkSavings.Checked that's giving me problem.
This is the program as designed. There is a label bellow the packages that displays the total.
When the Potential Savings is checked, it should also display the amount you could save if you use a different package.
So if I put Package A, 5 hours, 20% discount it should say $7.96, no savings with Package B or C. For Package A, 25 hours it should say $39.95, save $20.00 with Package B, and save $20.00 with Package C
The code I have does not print it even the first part.
Package A and less then 10 hours = $9.95, every additional hour is $2.00 more
Package B and less then 20 hours = $14.95, every additional hour is $1.00 more
Package C with unlimited hours is = $19.95
So My question is, what am I doing wrong in my code or how could I achieve what I am looking for.

Visual Basic - "If" Statements Not Showing Result In Label

I'm working on an assignment where the program needs to calculate and display the total cost for shipping a package, based on weight and whether it is being shipped to Continental U.S., Alaska, or Hawaii. When I click the Calculate button, though, the label that is supposed to display the total is left blank. I've looked through this and tried placing the calculation in different parts/at the end of the "If" statements. Here's what I have thus far, any help would be appreciated:
Dim decWeight As Decimal
Dim decTotalCost As Decimal
Dim decDestination As Decimal
Dim decRate As Decimal
If IsNumeric(txtWeight.Text) Then
decWeight = Convert.ToDecimal(txtWeight.Text)
If decWeight <= 30 > 0 Then
If radContinental.Checked Then
decDestination = 1
ElseIf radHawaii.Checked Then
decDestination = 1.2
ElseIf radAlaska.Checked Then
decDestination = 1.26
End If
If decWeight <= 2 Then
decRate = 3.69
ElseIf decWeight <= 4 > 2 Then
decRate = 4.86
ElseIf decWeight <= 6 > 4 Then
decRate = 5.63
ElseIf decWeight <= 8 > 6 Then
decRate = 5.98
ElseIf decWeight <= 10 > 8 Then
decRate = 6.28
ElseIf decWeight <= 30 > 10 Then
decRate = 15.72
End If
decTotalCost = decRate * decDestination
lblTotalCost.Text = decTotalCost.ToString("C")
ElseIf decWeight <= 0 Then
MsgBox("Please Enter a Positive Weight.", , "Input Error")
ElseIf decWeight > 30 Then
MsgBox("You Have Entered " & decWeight.ToString() & ". Please Enter a Weight Under 30 Pounds", , "Input Error")
End If
ElseIf txtWeight.Text = "" Then
MsgBox("Please Enter the Weight", , "Input Error")
Else : MsgBox("Please Enter a Number", , "Input Error")
End If
You should try this if statement: If decWeight <= 30 and decWeight > 0 Then
This will check if the decWeight is less than or equal to 30 and make sure that it is 'non-zero' Hope this helps :-)