[Help] Price calculations.

Posts 1–4 of 4 · Page 1 of 1
[Help] Price calculations.
okay so i have to build a calculate button To calculate the amount due add the price of the accessories and the exterior finish to the base price and display the result in the subtotal control. Calculate the sales tax on the subtotal and display the result in the total control. Then subtract the trade in amount from the total and display the result in the amount due control.

This is My Current Code..If u guys could Take a Look at it Perhaps find any mistakes plz point them out...because its not working
[highlight=vb.net]
Public Class VBAutoCenter

'Declares Constant values that will never change
Const TAX_RATE_Decimal As Decimal = 0.08D
Const DETAILING_STANDARD_Decimal As Decimal = 1D
Const DETAILING_PEARLIZED_Decimal As Decimal = 345.72D
Const DETAILING_CUSTOMIZED_Decimal As Decimal = 599.9D
Const COMPUTER_NAVIGATION_Decimal As Decimal = 174123D
Const LEATHER_INTERIOR_Decimal As Decimal = 987.41D
Const STEREO_SYSTEM_Decimal As Decimal = 425.76D
Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click

Dim CarSalesPriceDecimal, AccesoriesFinishDecimal, SubTotalDecimal, SaleTaxDecimal, TotalDecimal, TradeDecimal, AmountDecimal As Decimal
Dim ExteriorFinishDecimal As Decimal

If StereoCheckBox.Checked Then
AccesoriesFinishDecimal = STEREO_SYSTEM_Decimal
ElseIf LeatherCheckBox.Checked Then
AccesoriesFinishDecimal = LEATHER_INTERIOR_Decimal
ElseIf NavigationCheckBox.Checked Then
AccesoriesFinishDecimal = COMPUTER_NAVIGATION_Decimal
End If

If StandardRadioButton.Checked Then
ExteriorFinishDecimal = DETAILING_STANDARD_Decimal
ElseIf PearlizedRadioButton.Checked Then
ExteriorFinishDecimal = DETAILING_PEARLIZED_Decimal
ElseIf CustomizedRadioButton.Checked Then
ExteriorFinishDecimal = DETAILING_CUSTOMIZED_Decimal
End If

Try
CarSalesPriceDecimal = Decimal.Parse(CarPriceTextBox.Text)
Try
AccesoriesFinishDecimal = Decimal.Parse(AccesoriesFinishLabel.Text)
ExteriorFinishDecimal = Decimal.Parse(AccesoriesFinishLabel.Text)
SubTotalDecimal = CarSalesPriceDecimal + AccesoriesFinishDecimal
SaleTaxDecimal = TAX_RATE_Decimal * SubTotalDecimal
TotalDecimal = SubTotalDecimal + SaleTaxDecimal
Catch ex As Exception

End Try


CarPriceTextBox.Text = CarSalesPriceDecimal.ToString("C")
AccesoriesFinishLabel.Text = AccesoriesFinishDecimal.ToString("C")
TotalSubLabel.Text = SubTotalDecimal.ToString("C")
SaleTaxLabel.Text = SaleTaxDecimal.ToString("C")
TotalAmountLabel.Text = TotalDecimal.ToString("C")
TradeTextBox.Text = TradeDecimal.ToString("C")
AmountDisplayLabel.Text = AmountDecimal.ToString("C")

Catch ex As Exception

End Try
End Sub
[/highlight]
When you say it's 'not working' care to elaborate? Is something not being added correctly?

At the moment the thing I see is the If-Elseif-Elseif. If and Elseif are mutually exclusive, the elseif will only be tested if the previous evaluation fails. So in this case you're saying that a car can have ONE of either: Stereo, Leather Interior or Computer Navigation, you sure that's right?

Also, you do NOT need to nest Try catches in this case, you're not handling individual exceptions anyway, so just leave 1 big try-catch.

Need more info about what's going wrong exactly.
My professor said that i have to have a nest catch in order to catch any variables that are inserted in to the texbox.
how would i make the The group box to have multiple choice options so like if they want the stereo car system and navigation?? how would u add them?
Quote Originally Posted by yair View Post
My professor said that i have to have a nest catch in order to catch any variables that are inserted in to the texbox.
how would i make the The group box to have multiple choice options so like if they want the stereo car system and navigation?? how would u add them?
While it would be true that nesting the second catch would lead to you getting more specific info about where the error occured (i.e in parsing the decimals), there's no point in having it there because you haven't put anything between Catch ex As Exception and End Try, so basically it's just a general Try Catch block, so there's no need to nest it. Also, Try-Catch is uber slow.

You can also use Decimal.TryParse opposed to Decimal.Parse, something like this:

[highlight=vb.net]
If Decimal.TryParse(AccesoriesFinishLabel.Text, AccesoriesFinishDecimal) _
AndAlso Decimal.TryParse(AccesoriesFinishLabel.Text, ExteriorFinishDecimal) Then
SubTotalDecimal = CarSalesPriceDecimal + AccesoriesFinishDecimal
SaleTaxDecimal = TAX_RATE_Decimal * SubTotalDecimal
TotalDecimal = SubTotalDecimal + SaleTaxDecimal
End If
[/highlight]

While I was writing that out i noticed that both AccesoriesFinishDecimal and ExteriorFinishDecimal are getting their values from AccesoriesFinishLabel, you sure that's right?

Also, you can use CheckBoxes for multiple options. Personally I would recode the whole thing and use an Enum, but that's just me.
Posts 1–4 of 4 · Page 1 of 1
This thread is closed for replies.
Talk with us