This is the problem that I am working on: http://www.chegg.com/homework-help/sal
ID: 3665209 • Letter: T
Question
This is the problem that I am working on: http://www.chegg.com/homework-help/salesperson-earns-weekly-base-salary-plus-commission-sales-q-chapter-5-problem-3pe-solution-9780077398576-exc
I keep getting 4 errors regarding option strict on does not allow conversions from string to double, double to string and decimal to string. Tried using tryParse which does not work. Below is my code - please help me make this project work with Option Strict On (the errors occur with the code for the Pay Menu Option)
Public Class Form1
'Constants
Const QUOTA As Double = 1000D
Const COMMISSION_RATE As Decimal = 0.15D
Const BASE_RATE As Decimal = 250D
'Double Variables
Private Sales As Double = 0
Private Commission As Double = 0
Private TotalSales As Double = 0
Private TotalComm As Double = 0
Private TotalPay As Double = 0
'String Variable
Private MessageString As String
'Function to calculate commission
Private Function CommissionFunction(ByVal Sales As Double) As Double
Sales = Double.Parse(WeeklySalesText.Text)
If Sales >= QUOTA Then
Commission = Sales * COMMISSION_RATE
End If
Return Commission
End Function
'Pay Menu Option
Private Sub PayToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PayToolStripMenuItem.Click
Commission = 0
Commission = CommissionFunction(WeeklySalesText.Text)
CommissionLabel.Text = Commission.ToString()
Sales = Double.Parse(WeeklySalesText.Text)
If Commission > 0 Then
TotalPayLabel.Text = Commission + Sales
TotalComm += Commission
End If
TotalPayLabel.Text = BASE_RATE
TotalSales = TotalSales + Sales
TotalPay = TotalPay + BASE_RATE + Commission
End Sub
'Summary Menu Option
Private Sub SummaryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SummaryToolStripMenuItem.Click
MessageString = "Total Sales: " &
TotalSales.ToString("C") &
Environment.NewLine &
Environment.NewLine &
"Total Commission: " &
TotalComm.ToString("F2")
MessageBox.Show(MessageString, "Sales Commission Summary",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
End Sub
'Exit Menu Option
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
'Clear Menu Option
Private Sub ClearToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ClearToolStripMenuItem.Click
EmpNameText.Clear()
WeeklySalesText.Clear()
CommissionLabel.Text = ""
TotalPayLabel.Text = ""
With EmpNameText.Focus()
End With
End Sub
'Font Menu Option
Private Sub FontToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FontToolStripMenuItem.Click
With FontDialog
.ShowDialog()
.Font = TotalPayLabel.Font
TotalPayLabel.Font = .Font
End With
End Sub
'Color Menu Option
Private Sub ColorToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ColorToolStripMenuItem.Click
With ColorDialog
.ShowDialog()
.Color = TotalPayLabel.ForeColor
TotalPayLabel.ForeColor = .Color
End With
End Sub
Explanation / Answer
Option Explicit On
Option Strict On
Public Class Form1
'Constants
Const QUOTA As Double = 1000
Const COMM_RATE As Double = 0.15
Const BASE_PAY As Double = 250
Public Function CalculateCommission(ByVal seller As Seller) As Double
'If the seller's weekly sales are bigger or equal to the quota, calculates and returns the commission
'Else it returns 0
If seller.WeeklySales >= QUOTA Then
Dim commission As Double = seller.WeeklySales * COMM_RATE
Return commission
Else
Return 0
End If
End Function
Private Sub btnPay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPay.Click
'This button displays the listbox selected seller's info on a Messagebox
Dim strMessage As String
'If there is a selected item it proceeds, if not it shows an error message
If ListBox1.SelectedIndex > -1 Then
Dim currentSeller As Seller = DirectCast(ListBox1.SelectedItem, Seller)
Dim commission As Double = CalculateCommission(currentSeller)
If commission > 0 Then
strMessage = "Seller's name: " & currentSeller.Name & Environment.NewLine & _
"Weekly sales: " & currentSeller.WeeklySales.ToString & Environment.NewLine & _
"Commission: " & commission.ToString("0.00") & Environment.NewLine & _
"Total pay: " & (BASE_PAY + commission).ToString("0.00")
MessageBox.Show(strMessage, currentSeller.Name, MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
strMessage = "Seller's name: " & currentSeller.Name & Environment.NewLine & _
"Weekly sales: " & currentSeller.WeeklySales.ToString & Environment.NewLine & _
"Total pay: " & BASE_PAY.ToString("0.00")
MessageBox.Show(strMessage, currentSeller.Name, MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
MessageBox.Show("No seller selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand)
End If
End Sub
Private Sub btnAddSeller_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSeller.Click
Dim wsales As Double
'If there is a seller name AND there is a valid weekly sales number,
'create a new seller, assign its Name and WeeklySales properties and add it to the listbox
If tbSeller.Text <> "" AndAlso Double.TryParse(tbWeeklySales.Text, wsales) Then
Dim newSeller As New Seller
With newSeller
.Name = tbSeller.Text
.WeeklySales = wsales
End With
ListBox1.Items.Add(newSeller)
tbSeller.Clear()
tbWeeklySales.Clear()
tbSeller.Select()
Else
MessageBox.Show("You must enter valid data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand)
End If
End Sub
Private Sub btnSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSummary.Click
'Displays a MessageBox that holds total sales, total commissions and total pay for all salespersons.
'Displays the numbers with two decimal places and dollar signs.
Dim totalSales, currSellerComm, totalCommissions, totalPay As Double
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim currSeller As Seller = DirectCast(ListBox1.Items(i), Seller)
totalSales += currSeller.WeeklySales
currSellerComm = CalculateCommission(currSeller)
If currSellerComm > 0 Then
totalCommissions += currSellerComm
totalPay += (BASE_PAY + currSellerComm)
Else
totalPay += BASE_PAY
End If
Next i
Dim strSummary As String
strSummary = "Total sales: " & totalSales.ToString & Environment.NewLine & _
"Total commissions: " & totalCommissions.ToString("c") & Environment.NewLine & _
"Total pay: " & totalPay.ToString("c")
MessageBox.Show(strSummary, "Summary", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class
Public Class Seller
Public Property Name As String
Public Property WeeklySales As Double
Public Overrides Function ToString() As String
Return Me.Name
End Function
End Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.