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