Total value not updating when I update VB.NET - vb.net

The user inputs what they want in the forms. For example, in the snacks the user wants 2 burgers and it will cost 160 each - which is shown in total value.
Total value:
Then, the user changed their mind and decided they want 6 burgers instead of 2. The total value is still 160 when "Place Order" is clicked. I expect that the value will update when the user changes the input. How can I fix this?
(VB.NET)
Public Class Formpreceipt
Dim party = Formrooms.txtparty.Text
Dim partyy As Double
Dim bday = Formrooms.txtbday.Text
Dim bdayy As Double
Dim vip = Formrooms.txtvip.Text
Dim vipp As Double
Dim deluxe = Formrooms.txtdeluxe.Text
Dim deluxee As Double
Dim fries = Formsnacks.txtfries.Text
Dim friess As Double
Dim burger = Formsnacks.txtburger.Text
Dim burgerr As Double
Dim cupcake = Formsnacks.txtcupcake.Text
Dim cupcakee As Double
Dim pizza = Formsnacks.txtpizza.Text
Dim pizzaa As Double
Dim icedtea = Formdrinks.txticedtea.Text
Dim icedteaa As Double
Dim soda = Formdrinks.txtsoda.Text
Dim sodaa As Double
Dim soju = Formdrinks.txtsoju.Text
Dim sojuu As Double
Dim beer = Formdrinks.txtbeer.Text
Dim beerr As Double
Private Sub Formpreceipt_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnplaceorder.Click
partyy = party * 200
bdayy = bday * 300
vipp = vip * 400
deluxee = deluxe * 500
friess = fries * 40
burgerr = Val(burger) * 80
cupcakee = cupcake * 35
pizzaa = pizza * 150
icedteaa = icedtea * 20
sodaa = soda * 35
sojuu = soju * 80
beerr = beer * 100
txtotal.Text = "₱" & Format(partyy + bdayy + vipp + deluxee + friess + burgerr + cupcakee + pizzaa + icedteaa + sodaa + sojuu + beerr)
End Sub
End Class

There is room for improvement on your design, but as suggested by user9938, move the assignments to the Click() event of your button.
Change:
partyy = party * 200
bdayy = bday * 300
vipp = vip * 400
deluxee = deluxe * 500
friess = fries * 40
burgerr = burger * 80
cupcakee = cupcake * 35
pizzaa = pizza * 150
icedteaa = icedtea * 20
sodaa = soda * 35
sojuu = soju * 80
beerr = beer * 100
To:
partyy = Formrooms.txtparty.Text * 200
bdayy = Formrooms.txtbday.Text * 300
vipp = Formrooms.txtvip.Text * 400
deluxee = Formrooms.txtdeluxe.Text * 500
friess = Formsnacks.txtfries.Text * 40
burgerr = Formsnacks.txtburger.Text * 80
cupcakee = Formsnacks.txtcupcake.Text * 35
pizzaa = Formsnacks.txtpizza.Text * 150
icedteaa = Formdrinks.txticedtea.Text * 20
sodaa = Formdrinks.txtsoda.Text * 35
sojuu = Formdrinks.txtsoju.Text * 80
beerr = Formdrinks.txtbeer.Text * 100

If I read your code, in the Button1_Click you have burgherr = burger * 80
First of all I suggest you to get the Value of variable burger like Val(burger) * 80
after than, in your code I can't see where you update and use the total value... I expect that you have something like TextBoxTotalValue.Text = burgherr.ToString()

Related

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

Visual Basic Read from Keyboard

I am trying to implement a simple calculation to output Total Price using Visual Basic.NET
I want to read Original Price and Shipping Weight then use it in the calculation. The problem is,
When I enter any values, both variables will return only first digit
For example, if enter 23 it will return 2
Dim originalPrice As Double
Dim commissionPrice As Double
Dim shippingWeight As Double
Dim totalPrice As Double
Console.Write("Enter Original Price: ")
originalPrice = Double.Parse(Console.ReadLine(originalPrice))
Console.Write("Enter Shipping Weight: ")
shippingWeight = Double.Parse(Console.ReadLine(shippingWeight))
shippingWeight = shippingWeight * 7
If (originalPrice + shippingWeight >= 200) Then
commissionPrice = (originalPrice + shippingWeight) * 0.03
Else
commissionPrice = 5
End If
totalPrice = commissionPrice + originalPrice + shippingWeight
Console.WriteLine(originalPrice)
Console.WriteLine(commissionPrice)
Console.WriteLine(shippingWeight)
Console.WriteLine(totalPrice)
I think that your problem is that you are passing the variable to the Console.read()
In this way should work.
Dim originalPrice As Double
Dim commissionPrice As Double
Dim shippingWeight As Double
Dim totalPrice As Double
Console.Write("Enter Original Price: ")
originalPrice = Double.Parse(Console.ReadLine())
Console.Write("Enter Shipping Weight: ")
shippingWeight = Double.Parse(Console.ReadLine())
shippingWeight = shippingWeight * 7
If (originalPrice + shippingWeight >= 200) Then
commissionPrice = (originalPrice + shippingWeight) * 0.03
Else
commissionPrice = 5
End If
totalPrice = commissionPrice + originalPrice + shippingWeight
Console.WriteLine(originalPrice)
Console.WriteLine(commissionPrice)
Console.WriteLine(shippingWeight)
Console.WriteLine(totalPrice)
Console.Read()

VB2013, Possible Arithmetic Error

In Shadowrun 5e there is a rule concerning explosives in confined spaces called the 'Chunky Salsa Effect'. I am trying to create a program that simulates the 'Chunky Salsa Effect'. I have most of the program working, however, as the size of the space [meters] increases, the number of blast reflections [bounces] increases. It should be the inverse, the blast falloff [minusMeters] should decrease the [bounces]. [walls] determines the number of surfaces the blast reflects off of. [damageDice] is equal to the base damage of the explosive [baseDamageDice] + (([damageDice]/2) per reflection). For some reason [damageDice] and [bounces] do not decrease when [meters] is increased.
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim meters As Integer = 0
Dim dice As Integer = 0
Dim damageDice As Integer = 0
Dim baseDamageDice As Integer = 0
Dim bounces As Integer = 0
Dim hits As Integer = 0
Dim walls As Integer = 0
Dim minusMeters As Integer = 0
Dim explosiveType As Integer = 0
Dim ap As String = "-"
Dim diceRoll As Integer = 0
Randomize()
walls = CInt(txtWalls.Text)
explosiveType = cbExplosiveType.SelectedIndex
If explosiveType < 0 Then
explosiveType = 0
cbExplosiveType.SelectedIndex = 0
End If
Select Case explosiveType
Case 0
minusMeters = 1
baseDamageDice = 18
ap = "+5"
Case 1
minusMeters = 2
baseDamageDice = 16
ap = "-2"
Case 2
minusMeters = 4
baseDamageDice = 24
ap = "-4"
Case 3
minusMeters = 4
baseDamageDice = 24
ap = "-10"
Case 4
minusMeters = 1
baseDamageDice = 23
ap = "+5"
Case 5
minusMeters = 2
baseDamageDice = 21
ap = "-2"
End Select
diceRoll = CInt(Math.Floor((6 - 1 + 1) * Rnd())) + 1
If diceRoll >= 5 Then
hits = hits + 1
End If
dice = baseDamageDice
For x = 1 To walls
meters = CInt(txtMeters.Text)
damageDice = baseDamageDice
Do
damageDice = Math.Round(damageDice / 2)
dice = dice + damageDice
meters = meters - minusMeters
bounces = bounces + 1
Loop While meters >= 0
Next
For i As Integer = 1 To dice
diceRoll = CInt(Math.Floor((6 - 1 + 1) * Rnd())) + 1
If diceRoll >= 5 Then
hits = hits + 1
End If
Next
lblHitsPerBounce.Text = Math.Round(hits / bounces)
lblTimes.Text = bounces
lblResult.Text = dice
lblNumHits.Text = hits
lblAPOutput.Text = ap
End Sub

Program displaying wrong values, what needs to be changed?

My program to display employee records works fine other than the face that it displays incorrect values when it comes to pay. What needs to be changed?
Sub AddPlist(month As String, age As String, employeenumberid As Integer, hourlyrateline As Integer)
Dim hoursworkedline As String
Dim normalhoursworked As Integer
Dim overtimerate As String
Dim normalpay As Decimal
Dim overtimepay As Decimal
Dim overtimehoursworked As Integer
Dim grosspay As Decimal
Dim taxrate As Decimal
Dim taxdeducted As Decimal
Dim nideducted As Decimal
Dim netpay As Decimal
Do
Console.Write("Please enter hoursworked: ")
hoursworkedline = Console.ReadLine()
If Not hoursworkedline IsNot Nothing OrElse Not IsNumeric(hoursworkedline) Then Console.Write("hoursworked must be entered!!: ")
Loop While Not hoursworkedline IsNot Nothing OrElse Not IsNumeric(hoursworkedline)
If hoursworkedline > 40 Then
normalhoursworked = 40
overtimerate = hourlyrateline * 0.15
overtimehoursworked = hoursworkedline - 40
normalpay = normalhoursworked * hourlyrateline
overtimepay = overtimehoursworked * overtimerate
grosspay = normalpay + overtimepay
taxrate = If(age < 18, 18, If(age < 60, 25, If(age > 60, 20, Nothing)))
taxdeducted = grosspay * taxrate
nideducted = grosspay * ((grosspay / 100) * 7)
netpay = grosspay - taxdeducted - nideducted
Else
normalhoursworked = hoursworkedline
overtimerate = hourlyrateline * 0.15
overtimehoursworked = 0
normalpay = normalhoursworked * hourlyrateline
overtimepay = 0
grosspay = normalpay + overtimepay
taxrate = If(age < 18, 18, If(age < 60, 25, If(age > 60, 20, Nothing)))
taxdeducted = grosspay * taxrate
nideducted = grosspay * ((grosspay / 100) * 7)
netpay = grosspay - taxdeducted - nideducted
End If
One thing I noticed wrong is you're only paying 15% of the hourly rate for overtime. I think you mean to multiply by 1.5 not .15
Plus you have a lot of duplication in your code consider this:
If hoursworkedline > 40 Then
normalhoursworked = 40
overtimehoursworked = hoursworkedline - 40
Else
normalhoursworked = hoursworkedline
overtimehoursworked = 0
End If
overtimerate = hourlyrateline * 1.5
normalpay = normalhoursworked * hourlyrateline
overtimepay = overtimehoursworked * overtimerate
grosspay = normalpay + overtimepay
'Also your taxrate algorithm doesn't allow for someone exactly 60
taxrate = If(age < 18, 18, If(age < 60, 25, If(age >= 60, 20, Nothing)))
'Another thing your multiplying the grosspay by whole numbers not percentages
taxdeducted = grosspay * (taxrate / 100)
'Not sure what outcome you expect from this but it doesn't look quite right
nideducted = grosspay * ((grosspay / 100) * 7)
netpay = grosspay - taxdeducted - nideducted

losing precision while calculating cost

I've got this program to calculate the cost of sheets of paper. First the prices and bundles are declared and intialized. Then I use if else statements with division and modulos to break down the bundle into charging rates. Although my paper bundle amounts are correctly computed, my total costs are off. Can anyone spot what I'm doing wrong here?
Module Paper
Sub Main()
'declare variables
Dim PaperAmountReq As Double
Dim PricePer_1_Sheet As Double = 0.1
Dim PricePer_100_Sheets As Double = 0.055
Dim PricePer_500_Sheets As Double = 0.04
Dim PricePer_1000_Sheets As Double = 0.03
Dim NumberOfSingles As Double = 0
Dim NumberOf100s As Double = 0
Dim NumberOf500s As Double = 0
Dim Numberof1000s As Double = 0
Dim TotalCost As Double
Console.Write("Enter number of sheets of paper needed: ")
PaperAmountReq = Console.Readline
If PaperAmountReq / 1000 >= 1 Then
Numberof1000s = PaperAmountReq \ 1000
PaperAmountReq = PaperAmountReq Mod 1000
End If
If PaperAmountReq / 500 >= 1 Then
NumberOf500s = PaperAmountReq \ 500
PaperAmountReq = PaperAmountReq Mod 500
End If
If PaperAmountReq / 100 >= 1 Then
Numberof100s = PaperAmountReq \ 100
PaperAmountReq = PaperAmountReq Mod 100
End If
If PaperAmountReq / 1 = PaperAmountReq Then
NumberOfSingles = PaperAmountReq
End If
'TotalCost = (Convert.ToDouble(Numberof1000s) * PricePer_1000_Sheets) + (Convert.ToDouble(NumberOf500s) * PricePer_500_Sheets) + (Convert.ToDouble(Numberof100s) * PricePer_100_Sheets) + (Convert.ToDouble(NumberOfSingles) * PricePer_1_Sheet)
TotalCost = (Numberof1000s * PricePer_1000_Sheets) + (NumberOf500s * PricePer_500_Sheets) + (Numberof100s * PricePer_100_Sheets) + (NumberOfSingles * PricePer_1_Sheet)
Console.WriteLine("Number of 1000 packages: " & Numberof1000s)
Console.WriteLine("Number of 500 packages: " & Numberof500s)
Console.WriteLine("Number of 100 packages: " & Numberof100s)
Console.WriteLine("Number of singles packages: " & NumberOfSingles)
Console.Write("Total Cost: " & TotalCost)
End Sub
End Module
With this:
If PaperAmountReq / 1000 >= 1 Then
Numberof1000s = PaperAmountReq \ 1000
PaperAmountReq = PaperAmountReq Mod 1000
End If
if you have 1500, you are setting Numberof1000s equal to 1 - not 1000. So when you calculate your total cost, your value of (presumably) cost/sheet is being multiplied by 1 instead of 1000.
Either change your equation here
TotalCost = (Numberof1000s * PricePer_1000_Sheets) + (NumberOf500s * PricePer_500_Sheets) + (Numberof100s * PricePer_100_Sheets) + (NumberOfSingles * PricePer_1_Sheet)
to be something like
TotalCost = (Numberof1000s * PricePer_1000_Sheets * 1000) + (NumberOf500s * PricePer_500_Sheets * 500) + (Numberof100s * PricePer_100_Sheets * 100) + (NumberOfSingles * PricePer_1_Sheet * 1)
or adjust the PricePer_XXX_Sheets values to be the equivalent values.
Dim PricePer_1_Sheet As Double = 0.1
Dim PricePer_100_Sheets As Double = 0.055 * 100
Dim PricePer_500_Sheets As Double = 0.04 * 500
Dim PricePer_1000_Sheets As Double = 0.03 * 1000
Well your numberof variables should be integers or maybe bigintegers if not big enough as should PaperAmountReq
Your code seems to be assuming integer division
in these segments
If PaperAmountReq / 500 >= 1 Then
NumberOf500s = PaperAmountReq \ 500
PaperAmountReq = PaperAmountReq Mod 500
End if
in actual fact because you are using doubles, it's doing floating point division.
I'd also recommend that you use decimal instead of double for your price? and Total Cost variables.
Doubles are very precise, but often very inaccurate.
Looks like you need to convert the NumberOfXXXs variables to sheets, rather than bundles.
Your formula at the end uses per-sheet costs with per-bundle numbers. Either update the costs to be per-bundle, or update the bundle numbers to equal the round number of sheets in that bundle:
TotalCost = NumberOf1000s * 1000 * PricePer_1000_Sheets
Dim PricePer_100_Sheets As Double = 0.055
Dim PricePer_500_Sheets As Double = 0.04
Dim PricePer_1000_Sheets As Double = 0.03
should be
Dim PricePer_100_Sheets As Double = 0.055 * 100
Dim PricePer_500_Sheets As Double = 0.04 * 500
Dim PricePer_1000_Sheets As Double = 0.03 * 1000