# vb mail order Case study. I need help with calculating the percentages

Page 1 of 1

## 2 Replies - 12699 Views - Last Post: 09 April 2011 - 04:24 AMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=226763&amp;s=ec2278203af9e246acb297fb39b223ed&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 clarkstar1

Reputation: 3
• Posts: 14
• Joined: 25-March 11

# vb mail order Case study. I need help with calculating the percentages

Posted 08 April 2011 - 10:31 PM

hi. im studying at uni. and i need to do this as homework. i have been able to do the exercise's in the first 2 chapters and half the exercise's in the third chapter of the text book (programming in visual basic 2008, Julia Case Bradley & Anita C. Millspaugh). but this one is making my head want to explode. please point me in the right direction with this.

heres the problem.
The company has instituted a bonus program to give
its employees an incentive to sell more. For every dollar
the store makes in a four-week period, the employees
receive 2 percent of sales. The amount of bonus
each employee receives is based upon the percentage
of hours he or she worked during the bonus period (a
total of 160 hours).
The Calculate button will determine the bonus
earned by this employee, and the Clear button will
clear only the name, hours-worked, and bonus amount
fields. A Print button allows the user to print the form.
Do not allow missing or bad input data to cancel the
program; instead display a message to the user.

here's my code so far. its giving me the wrong results. i have tried many different ways,(hence the commented code) but I just dont know where im going wrong.

```Public Class VB_Sales

Const BONUSHOURS As Decimal = 160

Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click

Dim hoursPercent As Decimal

Dim bonus As Decimal

hoursPercent = (EmpHoursTextBox.Text) / 160

bonus = (StoreTotalSalesTextBox.Text) * (0.02 / hoursPercent)

'Dim name As String
'Dim hours As Integer
'Dim sales As Integer
'Dim total As Integer
'Dim Commision As Decimal = 0.2
'Dim bonus As Integer

'sales = StoreTotalSalesTextBox.Text
'name = EmpNameTextBox.Text
'hours = EmpHoursTextBox.Text
'bonus = (sales / BONUSHOURS) * Commision

'(BONUSHOURS / hours)

BonusTextBox.Text = bonus.ToString("C")
'ResultsTextBox.Text = name & (hours * (sales / BONUSHOURS) * Commision)

' * (Commision)).ToString("C")

End Sub

Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close()

End Sub

Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click

End Sub

Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
PrintForm1.Print()
End Sub

Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
PrintForm1.Print()
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()

End Sub

Private Sub CalculateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateToolStripMenuItem.Click

End Sub

Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click

End Sub
End Class

```

#### Attached image(s)

Is This A Good Question/Topic? 0

## Replies To: vb mail order Case study. I need help with calculating the percentages

### #2 RedRabbit

Reputation: 3
• Posts: 71
• Joined: 09-May 10

## Re: vb mail order Case study. I need help with calculating the percentages

Posted 09 April 2011 - 01:06 AM

What exactly do you mean by the wrong results? Why dont you try stepping through your code line by line in visual studio and check the values and what happens along the way, you are bound to find the problem.

Thats what I do when I have logic errors and 99% of the time I spot the problem easily

Looking through your code real quick it seems that you are declaring most of your values as integers, as long as you are using integers it should be fine but you do some calculations with decimals so I would recommend you declare them as decimal.

Also when you getting the values from the text boxes use the convert.toint32 or convert.toDecimal depending on the approach you choose with your variable declarations

``` 'Dim name As String
'Dim hours As Integer
'Dim sales As Decimal
'Dim total As Decimal
'Dim Commision As Decimal = 0.2
'Dim bonus As Decimal

'sales = convert.todecimal(StoreTotalSalesTextBox.Text)
'name = EmpNameTextBox.Text
'hours = EmpHoursTextBox.Text
'bonus = (sales / BONUSHOURS) * Commision

```

This post has been edited by RedRabbit: 09 April 2011 - 01:15 AM

### #3 clarkstar1

Reputation: 3
• Posts: 14
• Joined: 25-March 11

## Re: vb mail order Case study. I need help with calculating the percentages

Posted 09 April 2011 - 04:24 AM

RedRabbit, on 09 April 2011 - 01:06 AM, said:

What exactly do you mean by the wrong results? Why dont you try stepping through your code line by line in visual studio and check the values and what happens along the way, you are bound to find the problem.

Thats what I do when I have logic errors and 99% of the time I spot the problem easily

Looking through your code real quick it seems that you are declaring most of your values as integers, as long as you are using integers it should be fine but you do some calculations with decimals so I would recommend you declare them as decimal.

Also when you getting the values from the text boxes use the convert.toint32 or convert.toDecimal depending on the approach you choose with your variable declarations

``` 'Dim name As String
'Dim hours As Integer
'Dim sales As Decimal
'Dim total As Decimal
'Dim Commision As Decimal = 0.2
'Dim bonus As Decimal

'sales = convert.todecimal(StoreTotalSalesTextBox.Text)
'name = EmpNameTextBox.Text
'hours = EmpHoursTextBox.Text
'bonus = (sales / BONUSHOURS) * Commision

```

Hi, thanks for your help Red Rabbit. you were right I worked through line by line and got it to give me the correct results. I will paste the updated code below. ( the only problem I still have. is how to stop the employee from entering more than 160 hours. this is driving me crazy. I would luv any suggestions on how to fix this. you can see some of my tries below the "If" statement). should I even be trying this with an if statement. I hate being a noob. lol.
```Public Class VB_Sales

Const BONUSHOURS As Decimal = 160

Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click

'declaring variables
Dim name As String
Dim hours As Integer
Dim sales As Decimal
Dim Commision As Decimal = 100 / 0.2
Dim bonus As Decimal

' start if statement to catch a no name enter
If EmpNameTextBox.TextLength >= 1 Then

' If EmpNameTextBox.TextLength >= 1 Or EmpHoursTextBox.Text <= BONUSHOURS Then
' Or hours <= 160  ?????
' hours = Integer.Parse(EmpHoursTextBox.Text)
' Or hours.ToString <= 160
' these are some of the things I tried. lol. wishful thinking

' start the try statement to catch input errors
Try
'calculate the results
sales = Decimal.Parse(StoreTotalSalesTextBox.Text)
name = (EmpNameTextBox.Text).ToString
hours = Decimal.Parse(EmpHoursTextBox.Text)
bonus = ((sales * hours) / Commision)

'Format and display results
BonusTextBox.Text = bonus.ToString("C")
ResultsTextBox.Text = "Hello " & name & ", congrats on your bonus of " & bonus.ToString("C")

'catch exceptions
Catch ex As FormatException
ResultsTextBox.Clear()
EmpNameTextBox.Clear()
BonusTextBox.Clear()

With EmpHoursTextBox
.Clear()
.Focus()
End With
End Try

' ending the if statement
Else
MessageBox.Show("Please Enter an Employee Name", "Please Enter an Employee Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
EmpNameTextBox.Clear()
BonusTextBox.Clear()
ResultsTextBox.Clear()
With EmpNameTextBox
.Clear()
.Focus()
End With
End If
End Sub

Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close()

End Sub

Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
ResultsTextBox.Clear()
EmpNameTextBox.Clear()
BonusTextBox.Clear()
With EmpHoursTextBox
.Clear()
.Focus()
End With
End Sub

Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
PrintForm1.Print()
End Sub

Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
PrintForm1.Print()
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub

Private Sub CalculateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateToolStripMenuItem.Click
Dim name As String
Dim hours As Integer
Dim sales As Decimal
Dim Commision As Decimal = 100 / 0.2
Dim bonus As Decimal

' start if statement to catch a no name enter
If EmpNameTextBox.TextLength >= 1 Then

' If EmpNameTextBox.TextLength >= 1 Or EmpHoursTextBox.Text <= BONUSHOURS Then
' Or hours <= 160  ?????
' hours = Integer.Parse(EmpHoursTextBox.Text)
' Or hours.ToString <= 160
' these are some of the things I tried. lol. wishful thinking

' start the try statement to catch input errors
Try
'calculate the results
sales = Decimal.Parse(StoreTotalSalesTextBox.Text)
name = (EmpNameTextBox.Text).ToString
hours = Decimal.Parse(EmpHoursTextBox.Text)
bonus = ((sales * hours) / Commision)

'Format and display results
BonusTextBox.Text = bonus.ToString("C")
ResultsTextBox.Text = "Hello " & name & ", congrats on your bonus of " & bonus.ToString("C")

'catch exceptions
Catch ex As FormatException
ResultsTextBox.Clear()
EmpNameTextBox.Clear()
BonusTextBox.Clear()

With EmpHoursTextBox
.Clear()
.Focus()
End With
End Try

' ending the if statement
Else
MessageBox.Show("Please Enter an Employee Name", "Please Enter an Employee Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
EmpNameTextBox.Clear()
BonusTextBox.Clear()
ResultsTextBox.Clear()
With EmpNameTextBox
.Clear()
.Focus()
End With
End If
End Sub

Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
ResultsTextBox.Clear()
EmpNameTextBox.Clear()
BonusTextBox.Clear()
With EmpHoursTextBox
.Clear()
.Focus()
End With
End Sub
End Class

```