How do I make this code more efficient? - vb.net

The point of this code is when a user inputs into the textbox how many papers they want printed it would cost $0.30 per paper, if the user purchase more than 499 the price will change to $0.28 per paper etc. But when they check the radiobutton for ink it will cost extra $0.10 per 10 pages printed. I have figure out how to code it but its look messy.
Dim paper As Decimal
Dim price As Decimal
Dim cardstock As Decimal
Dim finalprice As Decimal
Dim remainder As Decimal
Dim extra As Decimal
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click, btn.Click
paper = Val(Me.txtinput.Text)
If noink.Checked = True Then
If paper <= 499 Then
price = paper * 0.3
Me.lblprice.Text = price
ElseIf paper <= 749 Then
price = paper * 0.28
Me.lblprice.Text = price
ElseIf paper <= 999 Then
price = paper * 0.27
Me.lblprice.Text = price
ElseIf price <= 1000 Then
price = paper * 0.25
Me.lblprice.Text = price
End If
End If
End Sub
Private Sub withink_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles withink.Click
If paper <= 499 Then
price = paper * 0.3
ElseIf paper <= 749 Then
price = paper * 0.28
ElseIf paper <= 999 Then
price = paper * 0.27
ElseIf price <= 1000 Then
price = paper * 0.25
End If
remainder = paper Mod 10
cardstock = paper - remainder
extra = cardstock / 100
finalprice = extra + price
Me.lblprice.Text = finalprice
End Sub

first, try use just as checkbox instead of a radiobutton and check that checkstate, so you only need one control
second, I like the idea of having to check the textinput directly and don't use a button to check like E.Solicito suggested but thats up to your preferences
third, here's the code
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.click
If txtinput.Text.Length = 0 Then
paper = 0
Else
paper = Val(Me.txtinput.Text)
End If
Check_Ink()
End Sub
Private Sub Checkbox_Status(sender As Object, e As EventArgs) Handles chb_withink.CheckedChanged
Check_Ink()
End Sub
Private Sub Check_Ink()
Select Case paper
Case paper <= 499
price = paper * 0.3
Case paper <= 749
price = paper * 0.28
Case paper <= 999
price = paper * 0.27
Case Else
price = paper * 0.25
End Select
If chb_withink.Checked Then
remainder = paper Mod 10
cardstock = paper - remainder
extra = cardstock / 100
price += extra
End If
Me.lblprice.Text = price
End Sub

Try this
Private Sub txtinput_TextChanged(sender As Object, e As EventArgs) Handles txtinput.TextChanged
If txtinput.Text = Nothing Then
txtinput.Text = 0
End If
paper = Val(Me.txtinput.Text)
Check_Ink()
End Sub
Private Sub RadioButton_Status(sender As Object, e As EventArgs) Handles noink.CheckedChanged, withink.CheckedChanged
Check_Ink()
End Sub
Public Sub Check_Ink()
If noink.Checked = True Then
If paper <= 499 Then
price = paper * 0.3
Me.lblprice.Text = price
ElseIf paper <= 749 Then
price = paper * 0.28
Me.lblprice.Text = price
ElseIf paper <= 999 Then
price = paper * 0.27
Me.lblprice.Text = price
ElseIf price <= 1000 Then
price = paper * 0.25
Me.lblprice.Text = price
End If
Else
If paper <= 499 Then
price = paper * 0.3
ElseIf paper <= 749 Then
price = paper * 0.28
ElseIf paper <= 999 Then
price = paper * 0.27
ElseIf price <= 1000 Then
price = paper * 0.25
End If
remainder = paper Mod 10
cardstock = paper - remainder
extra = cardstock / 100
finalprice = extra + price
Me.lblprice.Text = finalprice
End If
End Sub

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

I cant calculate the correct numbers via select case

Im trying to get the discounted price using select case but i keep getting the regular price
I select student and click on yoga and personal trainer option then i put 11 months according to the calculation its supppose to be 76.50 monthly fee , total 841.50 but i get 85 monthly and total 935 . Help Thank you
Protected Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim decMontlyFee As Decimal
Dim decTotalFee As Decimal
Dim discount As Double
Dim intMonths As Integer
Dim decAdultFee As Decimal = 40
Dim decChildFee As Decimal = 20
Dim decStudentFee As Decimal = 25
Dim decSeniorFee As Decimal = 30
Dim decYogaFee As Decimal = 10
Dim decKarateFee As Decimal = 30
Dim decTrainerFee As Decimal = 50
If radAdult.Checked = True Then
decMontlyFee = decAdultFee
ElseIf radChild.Checked = True Then
decMontlyFee = decChildFee
ElseIf radStudent.Checked = True Then
decMontlyFee = decStudentFee
ElseIf radSenior.Checked = True Then
decMontlyFee = decSeniorFee
End If
If chkYoga.Checked = True Then
decMontlyFee += decYogaFee
End If
If chkTrainer.Checked = True Then
decMontlyFee += decTrainerFee
End If
If chkKarate.Checked = True Then
decMontlyFee += decKarateFee
End If
Select Case intMonths
Case Is <= 3
discount = 0
Case 4 To 6
discount = decMontlyFee * 0.05
Case 7 To 9
discount = decMontlyFee * 0.08
Case Is >= 10
discount = decMontlyFee * 0.1
End Select
decMontlyFee -= discount
decTotalFee = decMontlyFee * txtMonths.Text
lblMonthlyFee.Text = decMontlyFee.ToString("c")
lblTotalFee.Text = decTotalFee.ToString("c")
End Sub
End Class
You will need to read the value for intMonths because it is only declared and not assigned.
Assign your intMonths to the TextBox value. Simplify your syntax.
Protected Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim decMontlyFee As Decimal
Dim decTotalFee As Decimal
Dim intMonths As Integer = Integer.Parse(txtMonths.Text)
Dim decAdultFee As Decimal = 40
Dim decChildFee As Decimal = 20
Dim decStudentFee As Decimal = 25
Dim decSeniorFee As Decimal = 30
Dim decYogaFee As Decimal = 10
Dim decKarateFee As Decimal = 30
Dim decTrainerFee As Decimal = 50
decMontlyFee = If(radAdult.Checked, decAdultFee, decMontlyFee)
decMontlyFee = If(radChild.Checked, decChildFee, decMontlyFee)
decMontlyFee = If(radStudent.Checked, decStudentFee, decMontlyFee)
decMontlyFee = If(radSenior.Checked, decSeniorFee, decMontlyFee)
decMontlyFee += If(chkYoga.Checked, decYogaFee, 0)
decMontlyFee += If(chkTrainer.Checked, decTrainerFee, 0)
decMontlyFee += If(chkKarate.Checked, decKarateFee, 0)
Select Case intMonths
Case Is <= 3
decMontlyFee *= 1
Case 4 To 6
decMontlyFee *= 0.95
Case 7 To 9
decMontlyFee *= 0.92
Case Is >= 10
decMontlyFee *= 0.9
End Select
decTotalFee = decMontlyFee * intMonths
lblMonthlyFee.Text = decMontlyFee.ToString("c")
lblTotalFee.Text = decTotalFee.ToString("c")
End Sub

How can I round double values?

My question is the bigmac works fine when I press the bigmacadd button and minus button. But when I press the mcdouble add button, I get a bunch of numbers. How can I make it so it shows 2.50 as decimal places?
Public Class Form1
Const bigmac As Decimal = 4D
Const mcdouble As Decimal = 2.25
Dim tax As Decimal
Dim price As Decimal
Dim quantity As Integer
Dim finaltotal As Decimal
Private Sub Btnbigmacadd_Click(sender As Object, e As EventArgs) Handles Btnbigmacadd.Click
quantity = quantity + 1
txtquan.Text = quantity
price += bigmac
txtprice.Text = price
tax = price * 0.15
txttax.Text = tax
finaltotal = price + tax
txtfinaltotal.Text = finaltotal
End Sub
Private Sub btnbigmacminus_Click(sender As Object, e As EventArgs) Handles btnbigmacminus.Click
quantity = quantity - 1
txtquan.Text = quantity
price -= bigmac
txtprice.Text = price
tax = price * 0.15
txttax.Text = tax
finaltotal = price + tax
txtfinaltotal.Text = finaltotal
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
quantity = quantity + 1
txtquan.Text = quantity
price += mcdouble
txtprice.Text = price
tax = price * 0.15
txttax.Text = tax
finaltotal = price + tax
txtfinaltotal.Text = finaltotal
End Sub
End Class
You can round tax to two decimal places using the Math.Round method
tax = Math.Round(price * 0.15D, 2)
txttax.Text = tax.ToString()
finaltotal = price + tax
txtfinaltotal.Text = finaltotal.ToString()
If want to keep the extra decimal places in tax and finaltotal, but show only two decimal places in the text boxes, you can use the ToString("N2") method.
tax = price * 0.15D
txttax.Text = tax.ToString("N2")
finaltotal = price + tax
txtfinaltotal.Text = finaltotal.ToString("N2")

Why is my Select Case not working

Okay my question goes as follows; I think i coded everything right execpt for the part where i do my select case, I want the first 3 Flavours to only cost 55 cents but when I do my code it always makes the scoops 65 cents no matter what icecream type i select and i dont know how to make it change, i thought i had it right but it isnt working
Public Class frmJoeyIceCreamParlour
Const MIN_SCOOPS = 1
Const MAX_SCOOPS = 9
Const BASIC_FLAVOUR = 0.55
Const PREMIUM_FLAVOUR = 0.65
Const TOPPING = 0.6
Const DEEZ_NUTS = 0.5
Const WHIPPED_CREAM = 0.65
Public scoopEntry As Single
Public scoopType As Double
Public runningTotal As Double
Private Sub frmJoeyIceCreamParlour_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstFlavours.Items.Add("Vanilla")
lstFlavours.Items.Add("Chocolate")
lstFlavours.Items.Add("Strawberry")
lstFlavours.Items.Add("Mango")
lstFlavours.Items.Add("Bananna")
lstFlavours.Items.Add("Grape")
lstFlavours.Items.Add("Mint Chocolate Chip")
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If txtScoops.Text = Nothing Then
MessageBox.Show("Please enter a value in the scoops category.")
txtScoops.Focus()
ElseIf Not IsNumeric(txtScoops.Text) Then
MessageBox.Show("Entry must be numeric! Please try again.")
txtScoops.Focus()
ElseIf txtScoops.Text < MIN_SCOOPS Or txtScoops.Text > MAX_SCOOPS Then
MessageBox.Show("Please enter a number between 1 and 9 scoops.")
txtScoops.Focus()
ElseIf lstFlavours.SelectedItem = Nothing Then
MessageBox.Show("Please select a flavour.")
ElseIf rdoNoTopping.Checked = False And rdoOneTopping.Checked = False And rdoTwoTopping.Checked = False And rdoThreeTopping.Checked = False Then
MessageBox.Show("Please select the amount of toppings you would like.")
Else
Dim number As Integer = 7
Select Case number
Case 1 To 3
scoopType = BASIC_FLAVOUR
Case 4 To 7
scoopType = PREMIUM_FLAVOUR
runningTotal = scoopType * Double.Parse(txtScoops.Text)
End Select
If rdoOneTopping.Checked = True Then
runningTotal = runningTotal + TOPPING
ElseIf rdoTwoTopping.Checked = True Then
runningTotal = runningTotal + (TOPPING * 2)
ElseIf rdoThreeTopping.Checked = True Then
runningTotal = runningTotal + (TOPPING * 3)
End If
If chkWhippedCream.Checked = True Then
runningTotal = runningTotal + WHIPPED_CREAM
End If
If chkNuts.Checked = True Then
runningTotal = runningTotal + DEEZ_NUTS
End If
lblOutputTotal.Text = (FormatCurrency(runningTotal))
End If
End Sub
End Class
You have it hard coded to use 7 via the line just above your Select case number statement: Dim number As Integer = 7. You should instead be looking up the selected index by doing something like Dim number As Integer = lstFlavours.SelectedIndex
Dim number As Integer = lstFlavours.SelectedIndex
Select Case number
Case 1 To 3
scoopType = BASIC_FLAVOUR
Case 4 To 7
scoopType = PREMIUM_FLAVOUR
End Select
runningTotal = scoopType * Double.Parse(txtScoops.Text)

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.