Saturday, March 14, 2009

I'm super proud of this

My most complex application so far. I'm proud of this. It's taken several revisions, many after I submitting it for grading... :)

This application calculates a grand total of sales based on the following:
A) User input of quantity sold of each of 3 software packages
B) Input is checked to verify positive number and not text and display the appropriate error message.
C) Each package has a different cost (3 costs)
D) Discounts taken based on quantity sold (5 discount levels)
E) Individual package sales and grand total sales is displayed
F) Check box option to only show the grand total
G) Buttons to clear the values and exit the application

Once I figure out loops this week, the program will probably shrink. In a few weeks I'll learn arrays which should shrink it even more.




Public Class frmSoftwareSalesv2

Private Sub btnCalculate_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnCalculate.Click

'Declare constants for discounts
Dim constLEVEL0_DISC_RATE As Decimal = 0D 'Qty 1-9 discount
Dim constLEVEL1_DISC_RATE As Decimal = 0.2D 'Qty 10-19 discount
Dim constLEVEL2_DISC_RATE As Decimal = 0.3D 'Qty 20-49 discount
Dim constLEVEL3_DISC_RATE As Decimal = 0.4D 'Qty 50-99 discount
Dim constLEVEL4_DISC_RATE As Decimal = 0.5D 'Qty +100 discount

'Declare discount ranges
Dim constRANGE_0_START As Integer = 0
Dim constRANGE_0_END As Integer = 9
Dim constRANGE_1_START As Integer = 10
Dim constRANGE_1_END As Integer = 19
Dim constRANGE_2_START As Integer = 20
Dim constRANGE_2_END As Integer = 49
Dim constRANGE_3_START As Integer = 50
Dim constRANGE_3_END As Integer = 99
Dim constRANGE_4_START As Integer = 100

'Declare constants for error messages
Dim constERROR_WHOLE_NUMBER_A As String = "Quantity of Package A Sold must be a whole number"
Dim constERROR_WHOLE_NUMBER_B As String = "Quantity of Package B Sold must be a whole number"
Dim constERROR_WHOLE_NUMBER_C As String = "Quantity of Package C Sold must be a whole number"

Dim contERROR_NEGATIVE_NUMBER As String = "Negative numbers are not allowed."

'Declare constants for total text
Dim constLABEL1 As String = "Package A: "
Dim constLABEL2 As String = "Package B: "
Dim constLABEL3 As String = "Package C: "
Dim constLABEL4 As String = "Grand Total: "

'Declare constants for retail cost
Dim constRETAIL_COST_PKG_A As Decimal = 99D 'Cost of Pkg A
Dim constRETAIL_COST_PKG_B As Decimal = 199D 'Cost of Pkg B
Dim constRETAIL_COST_PKG_C As Decimal = 299D 'Cost of Pkg C

'Declare Variables
Dim intASold As Integer 'Qty A packages sold
Dim intAGrossSales As Decimal 'Pkg A gross sales
Dim intADiscount As Decimal 'Pkg A discount
Dim intANetSales As Decimal 'Pkg A gross less discount

Dim intBSold As Integer 'Qty B packages sold
Dim intBGrossSales As Decimal 'Pkg B gross sales
Dim intBDiscount As Decimal 'Pkg B discount
Dim intBNetSales As Decimal 'Pkg B gross less discount

Dim intCSold As Integer 'Qty C packages sold
Dim intCGrossSales As Decimal 'Pkg C gross sales
Dim intCDiscount As Decimal 'Pkg C discount
Dim intCNetSales As Decimal 'Pkg C gross less discount

Dim intGrandTotal As Decimal 'Grand Total


'Check Package A Sold for integer
Try
intASold = CInt(Me.txtQtyASold.Text)
Catch ex As Exception
MessageBox.Show(constERROR_WHOLE_NUMBER_A, _
Me.Text)

With Me.txtQtyASold
.SelectAll()
.Focus()
End With

Exit Sub
End Try

'Check Package A Sold for negative numbers
If intASold < intbsold =" CInt(Me.txtQtyBSold.Text)" intcsold =" CInt(Me.txtQtyCSold.Text)" intagrosssales =" intASold" intbgrosssales =" intBSold" intcgrosssales =" intCSold" intadiscount =" intAGrossSales" intadiscount =" intAGrossSales" intadiscount =" intAGrossSales" intadiscount =" intAGrossSales">= constRANGE_4_START
intADiscount = intAGrossSales * constLEVEL4_DISC_RATE
End Select

'Find discount for Package B
Select Case intBSold
Case constRANGE_0_START To constRANGE_0_END
intBDiscount = intBGrossSales * constLEVEL0_DISC_RATE
Case constRANGE_1_START To constRANGE_1_END
intBDiscount = intBGrossSales * constLEVEL1_DISC_RATE
Case constRANGE_2_START To constRANGE_2_END
intBDiscount = intBGrossSales * constLEVEL2_DISC_RATE
Case constRANGE_3_START To constRANGE_3_END
intBDiscount = intBGrossSales * constLEVEL3_DISC_RATE
Case Is >= constRANGE_4_START
intBDiscount = intBGrossSales * constLEVEL4_DISC_RATE
End Select

'Find discount for Package C
Select Case intCSold
Case constRANGE_0_START To constRANGE_0_END
intCDiscount = intCGrossSales * constLEVEL0_DISC_RATE
Case constRANGE_1_START To constRANGE_1_END
intCDiscount = intCGrossSales * constLEVEL1_DISC_RATE
Case constRANGE_2_START To constRANGE_2_END
intCDiscount = intCGrossSales * constLEVEL2_DISC_RATE
Case constRANGE_3_START To constRANGE_3_END
intCDiscount = intCGrossSales * constLEVEL3_DISC_RATE
Case Is >= constRANGE_4_START
intCDiscount = intCGrossSales * constLEVEL4_DISC_RATE
End Select

'Calculate Net Sales
intANetSales = intAGrossSales - intADiscount
intBNetSales = intBGrossSales - intBDiscount
intCNetSales = intCGrossSales - intCDiscount

'Calculate the Grand Total
intGrandTotal = intANetSales + intBNetSales + intCNetSales

'Display the Results
If chkGrandOnly.Checked = True Then
lblSalesResults.Text = constLABEL4 _
& intGrandTotal.ToString("c")

ElseIf chkGrandOnly.Checked = False Then
lblSalesResults.Text = constLABEL1 & intANetSales.ToString("c") _
& ControlChars.NewLine _
& constLABEL2 & intBNetSales.ToString("c") _
& ControlChars.NewLine _
& constLABEL3 & intCNetSales.ToString("c") _
& ControlChars.NewLine _
& constLABEL4 & intGrandTotal.ToString("c")

End If

End Sub

Private Sub btnClear_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnClear.Click

'Clear the form, reset the check box, set focus to Package A.
txtQtyASold.Clear()
txtQtyBSold.Clear()
txtQtyCSold.Clear()
lblSalesResults.Text = String.Empty
chkGrandOnly.Checked = False
txtQtyASold.Focus()

End Sub

Private Sub btnExit_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnExit.Click

'Exit the application by closing the form.
Me.Close()

End Sub

End Class

2 comments:

otgirl said...

Wow... your smarts is like greek to me. Actually, more like Chinese because I can actually read Greek. AND you ride a motorcycle!

Serena said...

It was like Chinese to me before this class too. And Greek is /still/ like Greek to me, so more power to you for you linguistic skills!

I'll take a motorcycle over the pot-smoking drivers you deal with on your bike. That's pretty scary.