Question

I have started over and everything is working as intended except for 2 of the accumulating totals, the calculation is not working the way I need it to. Instead of adding previous sales to the total it tacks the new sale on the end...

for example: I input 1 Snowboard and 1 Snowboard with Boots, this works, 1 is shown in the summary for both Snowboards and Snowboards with Boots. However when I try to input again in order to keep a running total of sales I have a problem, I enter 1 Snowboard and 1 Snowboard with Boots, and the summaries show 11 rather than 2. Why does this happen?

   ' Declare module-level variables and constants.
Private SnowBoardsSold, BootsSold, SaleCount As Integer
Private ItemsSold As Integer
Private TotalSales As Decimal
Const SNOWBOARD_RATE As Decimal = 20D
Const BOOTS_RATE As Decimal = 30D


Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
    ' Calculate the prices
    Dim SnowBoards, Boots As Integer
    Dim SnowBoardSale, BootsSale, ThisSale, AverageSalesEver As Decimal

    Try
        ' Convert the Snowboard input to numeric variable.
        SnowBoards = Integer.Parse(SnowboardsTextBox.Text)

        Try
            ' Convert Boots if Snowboard was successful.
            Boots = Integer.Parse(WithBootsTextBox.Text)
            ' Calculate values for sale.
            SnowBoardSale = SnowBoards * SNOWBOARD_RATE
            BootsSale = Boots * BOOTS_RATE
            ThisSale = SnowBoardSale + BootsSale

            ' Calculate summary values.
            TotalSales += ThisSale
            BootsSold += BootsSale
            SnowBoardsSold += SnowBoardSale
            SaleCount += 1
            AverageSalesEver = TotalSales / SaleCount

            ' Format and display prices for the sale.
            SnowBoardsPriceTextBox.Text = SnowBoardSale.ToString("c")
            BootsPriceTextBox.Text = BootsSale.ToString("c")
            TotalPriceTextBox.Text = ThisSale.ToString("c")

            ' Format and display values for the summary.
            SnowBoardRentalTextBox.Text += SnowBoards.ToString()
            BootsRentalTextBox.Text += Boots.ToString()
            TotalChargesTextBox.Text = TotalSales.ToString("c")
            AverageChargeTextBox.Text = AverageSalesEver.ToString("c")

        Catch BootsException As FormatException
            ' Handle a Boots exception.
            MessageBox.Show("Please enter numbers.", "Data Entry Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
            With WithBootsTextBox
                .Focus()
                .SelectAll()
            End With
        End Try

    Catch SnowBoardsException As FormatException
        ' Handle a SnowBoard exception.
        MessageBox.Show("Please enter numbers.", "Data Entry Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
        With SnowboardsTextBox
            .Focus()
            .SelectAll()
        End With

    Catch AnException As Exception
        'Handle any other exception.
        MessageBox.Show("Error: " & AnException.Message)
    End Try
End Sub
Was it helpful?

Solution

First, you might want to change the way you grab the numbers:

'Convert snowboard input value to numeric variable.
SnowboardInteger = Integer.Parse(SnowboardsTextBox.Text)

to:

Dim SnowboardInteger As Integer
If TryParse.Integer(SnowboardsTextBox.Text, SnowboardInteger) = False then
 ' if it parses, SnowboardInteger will have the value, 
 'else the function returns false
     MessageBox.Show("Please enter numbers")
End if

Also, you are parsing the textboxes twice. Once in the declaration then right after. This doesnt look right:

SnowboardSaleCountInteger += 1
WithBootsSaleCountInteger += 1
TotalSaleCountInteger += TotalChargesSumInteger   ' ?????
AverageChargeInteger = TotalChargesSumInteger / TotalSaleCountInteger

Adding 'charges' to a 'counter' doesnt seem right. If you want the avg shouldnt it be:

TotalSaleCountInteger = SnowboardSaleCountInteger + WithBootsSaleCountInteger 

If you want fractions such as 'xx.yy' in the result, AverageChargeInteger should be a Double or Decimal, unless whole dollars are ok for the assignment.

If you need to accumulate from previous clicks, then you need to move some declarations out of the procedure up by SnowboardSumInteger, WithBootsSumInteger so they can accumulate. As it is, WithBootsSaleCountInteger, TotalSaleCountInteger dont do anything and are always 1. I wonder if as a 'SaleCounter' they shouldnt increase by the value in the textbox rather than just 1 (dont have the assignment in front of me).

One more thing you might need to do is here:

SnowboardPriceInteger = SnowboardInteger * SNOWBOARD_RENTAL_RATE
WithBootsPriceInteger = WithBootsInteger * WITH_BOOTS_RENTAL_RATE
TotalPriceInteger = SnowboardPriceInteger + WithBootsPriceInteger

Your constants are Decimal (SNOWBOARD_RENTAL_RATE) so, the result of the calc has to go into a Decimal var, but SnowboardPriceInteger and the other are not. An integer cannot store a fractional result from the multiplication or division.

something like:

' sales accumulators
 Private TotalSales As Decimal = 0
 Private ItemsSold As Integer = 0

Click Event:

' this sale:
Dim Snowboards As Integer 
Dim Boots As Integer

' get the values from the text boxes, then:
Dim SnowBoardSale As Decimal = (SnowBoards * SnowBoardRate)
Dim BootsSale As Decimal = (Boots * BootsRate)
Dim ThisSale As Decimal =  SnowBoardSale  + BootsSale 

 ' update accumulators
TotalSales += ThisSale
ItemsSold += (Boots + Snowboards)

' calc the average
Dim AverageSalesEver AS Decimal = TotalSales  / ItemsSold 

' Display results in textboxes
'(this exercise is left to the student  ;)  )

HTH

I think you just confused yourself with too many variables and ones left over from old attempts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top