Cis56 Catching up
Program directives:
This example is intended to begin to bring our class up to date due to the two storm cancellations.
I have put together this program and will go over it with you step by step.
I purposely convoluted it to help you to follow what Im doing. While sounding strange, to say the least, the idea here is that if you end up understanding this along with me, you will be well on your way to recovery.
Your assignment 3 should be much easier for you once you understand this example I will do with you because this example is more involved and more difficult.
What makes it more difficult is that due to the lack of classes, we must still do everything in click events. When we get to procedures and functions, a lot of what is being done in the command buttons will be done in separate routines and in a more organized way.
Now here is the scenario.
Note that here we are not dealing with realism. Our focus will be on programming logic and VB.
There is a manufacturer who sells shirts.
A customer, either a person or a company, places an order. We will call this a CUSTOMER TRANSACTION.
With each customer transaction, purchases of shirts take place. Each of these shirt purchases, done separately within each order we will call a SHIRT TRANSACTION.
The shirts are sold in three types, based on sizes.
One type is Sm. M Lg. (small, medium, large) and each of these sells for $10.00.
The second type is XL (Extra large) and each of these sells for $11.00.
The third type is XXL (Extra, Extra large) and each of these sells for $12.00
Each shirt can be purchased with a pocket, a monogram or both.
Monograms cost $2.00 more each.
Pockets cost $1.00 more each.
Also, there are some customers who hold a manufacturers discount card, which is worth 10% off the final total amount of the purchase.
A clerk enters data as follows.
A customer ID number
A customer name, either a person or company name
The type of shirt
The quantity of that type of shirt
Specifications as to whether or not monograms and/or pockets are to be added
Whether or not a discount applies
When a discount applies, the specification is entered only once.
To enter the data and specifications:
Entering the data is done as follows:
The ID and name are entered each through a text box.
The Id and name are both alphanumeric fields (string data)
The Id and name fields must be entered.
During processing, these two text boxes must be checked to be sure that there is data in them
The id must also be 5 characters long, no more, no less so, this must also be checked.
The type of shirt is selected via option buttons. There should be a button for each type, hence: 3 option buttons.
Along with each respective option button, there should be a text box in which the clerk will enter the quantity AFTER the option button is selected.
Therefore upon clicking the option button, focus should be set to the respective text box.
At least one option button MUST be selected in a shirt transaction and there must be a value greater than zero entered in the respective quantity text box.
The monogram and pocket will each have a check box the clerk can click if either or both of these features are to be applied.
If the discount is selected, it should then immediately be disabled, to ensure that it is not checked again in the same CUSTOMER TRANSACTION.
Processing and Output:
In processing, the id and name criteria, is checked first.
Then we check the to see if the discount check box has been checked. If it has it remains disabled. If it has not been checked, we disable it there because, for our purposes, an unchecked discount box means the customer is not a discount holder.
Then, based on which option button has been clicked, we check the validity of the respective quantity text box.
In the end of this check we will also be able to determine if an option button has indeed been clicked.
Once all the checking is done, we will proceed to calculate our purchase.
Note here that we will be using both modular and local
variables.
Local variables, as you all now know have a scope that is limited to the procedure or event in which it is declared. It cannot be referenced from other procedures or events in the form module.
Modular variables have a scope of reference throughout the entire form module and can be referenced from any point in the form module.
What I will then do is declare the variables that I will need throughout my form module in the general declarations area and these will be, for the most part my running total variables.
Those variables that I only use in a command click event, I will declare and use in that event as local.
Displays and running totals:
In this program, I have used both a picture box and a text box for different displays. I was not too concerned with the line up but more on showing you how you would use them as display tools.
Each individual shirt transaction will display in the picture box as follows:
The specific type of shirt with the quantity and the total cost of the type
At the same time, the customer id, the current date and the customers name will display in the text box.
Every time I clear for a new shirt transaction, the text box will display the latest running totals for the current customer transaction. This means that I am keeping running totals for each of the shirt transactions, for the text box display.
Every time I clear using the order complete, I run my totals for my final display when I select summary. Each order complete signifies the end of a customer transaction so since I have processed a customers order I add one to a total variable for keeping an account of how many customers transacted business that day.
So, the final display will be displayed in the picture box and will show the following:
The final total of each type of shirt and its respective final total cost
The total of all the shirts together and the final total amount
The total of all the customers.
This is what shirt transaction displays look like in the picture box.
Sm. Med. Lg. Shirts total: 10 Cost 108.00
This is what the name would look like in the text box.
CURRENT CUSTOMER 12345
This is what the display in the text box would look like whenever the clear is pressed.
TOTALS FOR CUSTOMER NUMBER: 12345 2/23/03
TOTAL SM M LG SHIRTS: 10 100.00
TOTAL XL SHIRTS: 0
0.00
TOTAL XXL SHIRTS:
0 0.00
TOTAL COST FOR THIS CUSTOMER: $100.00
With each successive press of the clear button, these totals would change to reflect the running shirt transaction totals.
Again, Im not looking for neatness here.
The summary will appear in the picture box and would look like this:
DAILY TOTAL FOR: 2/23/03
Total Sm. Med. Lg. Shirts: 0 0.00
Total XL shirts: 0 0.00
Total XXL shirts: 0 0.00
Total All Shirts: 0 $0.00
Total All Customers: 0
Checking and
calculations:
Cecking the id:
'BEFORE I DO ANYTHING FURTHER, I NEED TO MAKE SURE THAT I HAVE AN ID AND THAT
'THE ID IS 5 CHARACTERS LONG.
'I DO THIS HERE.
If txtCustId.Text = "" Then
MsgBox "You must enter data in the id field." & vbCrLf & _
"Please do so now.", vbOKOnly, "Missing Id"
txtCustId.SetFocus
Exit Sub
ElseIf Not Len(txtCustId.Text) = 5 Then
MsgBox "The Id field must be 5 charaters long." & vbCrLf & _
"Please enter an id of valid length now.", vbOKOnly, "Incorrect Id length"
txtCustId.SetFocus
txtCustId.SelStart = 0
txtCustId.SelLength = Len(txtCustId.Text)
Exit Sub
End If
CHECKING THE NAME:
'NOW I HAVE TO CHECK TO BE SURE THAT I HAVE DATA IN THE
NAME FIELD.
If txtCustName.Text = "" Then
MsgBox "You must have data in the name field." & vbCrLf & _
"Please enter a name now.", vbOKOnly, "Missing Name"
txtCustName.SetFocus
Exit Sub
End If
CHECKING THE QUANTITY TEXT BOXES AND ULTIMATELY THAT AT LEAST ONE OPTION BUTTON WAS SELECTED.
'NOW AS PER THE DIRECTIVES, I NEED TO BE SURE THAT IN COMPLIANCE WITH ANY OF THE OPTION
'BUTTONS CLICKED, THAT A CORRESPONDING QUANTITY VALUE IS ENTERED IN THE RESPECTIVE TEXT BOX.
'I NOW CHECK THAT IN CORRESPONDENCE TO THE OPTION BUTTON PRESSED, THAT THE QUANTITY IS ENTERED
'AND THE DATA ENTERED IS NUMERIC. IF ALL IS OK THEN I WILL ASSIGN THE VALUE OF THE QUANTITY TO
'THE CORRESPONDING VARIABLE I DECLARED ABOVE. IF NOT, THEN THE USER WILL BE PROMPTED AND
'THE APPROPRIATE RESETTING WILL TAKE PLACE.
'SO, THE FOLLOWING PROCEDURE BOTH CHECKS THE VALIDITY OF THE QUANTITIES AND ASSIGNS THE
'VALUES.
If optSmMLg.Value = True Then
If txtSmMLgQty = "" Then
MsgBox "The quantity text box field must have a value." & vbCrLf & _
"Please enter the value now.", vbOKOnly, "Missing quantity"
txtSmMLgQty.SetFocus
Exit Sub
ElseIf Not IsNumeric(txtSmMLgQty.Text) Then
MsgBox "The quantity value entered, " & txtSmMLgQty.Text & " is non numeric." & _
vbCrLf & "Please enter a valid numeric value at this time.", vbOKOnly, "Non numeric quantity"
txtSmMLgQty.SetFocus
txtSmMLgQty.SelStart = 0
txtSmMLgQty.SelLength = Len(txtSmMLgQty.Text)
Exit Sub
Else
intSmMLgShirt = Val(txtSmMLgQty.Text)
intTotalQuantity = intSmMLgShirt
strShirtTypeHold = "Sm. Med. Lg. shirts"
End If
ElseIf optXL.Value = True Then
If txtXLQty.Text = "" Then
MsgBox "The quantity text box field must have a value." & vbCrLf & _
"Please enter the value now.", vbOKOnly, "Missing quantity"
txtXLQty.SetFocus
Exit Sub
ElseIf Not IsNumeric(txtXLQty.Text) Then
MsgBox "The quantity value entered, " & txtXLQty.Text & " is non numeric." & _
vbCrLf & "Please enter a valid numeric value at this time.", vbOKOnly, "Non numeric quantity"
txtXLQty.SetFocus
txtXLQty.SelStart = 0
txtXLQty.SelLength = Len(txtXLQty.Text)
Exit Sub
Else
intXLShirt = Val(txtXLQty.Text)
intTotalQuantity = intXLShirt
strShirtTypeHold = "Extra large shirts"
End If
ElseIf optXXL.Value = True Then
If txtXXLQty.Text = "" Then
MsgBox "The quantity text box field must have a value." & vbCrLf & _
"Please enter the value now.", vbOKOnly, "Missing quantity"
txtXXLQty.SetFocus
Exit Sub
ElseIf Not IsNumeric(txtXXLQty.Text) Then
MsgBox "The quantity value entered, " & txtXXLQty.Text & " is non numeric." & _
vbCrLf & "Please enter a valid numeric value at this time.", vbOKOnly, "Non numeric quantity"
txtXXLQty.SetFocus
txtXXLQty.SelStart = 0
txtXXLQty.SelLength = Len(txtXXLQty.Text)
Exit Sub
Else
intXXLShirt = Val(txtXXLQty.Text)
intTotalQuantity = intXXLShirt
strShirtTypeHold = "Extra extra large shirts"
End If
Else
MsgBox "No option button has been selected." & vbCrLf & _
"Please select one now.", vbOKOnly, "No option button selected"
Exit Sub
End If
DOING THE CALCULATIONS:
'NOW THAT I HAVE COMPLETED MY CHECKING, I CAN NOW BEGIN TO DO MY TRANSACTION CALCULATIONS.
'I FIRST CHECK TO DETERMINE WHICH OPTION BUTTON WAS PRESSED AND I ASSIGN THE CONSTANT VALUE
'TIMES THE QUANTITY ENTERED IN THE RESPECTIVE QUANTITY BOX TO THE curTotalAmount VARIABLE I DECLARED ABOVE.
'AS A RESULT OF THIS IF, I HAVE THE COST OF THE TOTAL NUMBER OF A PARTICULAR SHIRT I ORDER
'IN A TRANSACTION.
If optSmMLg.Value = True Then 'IF OPTION IS CLICKED
curTotalAmount = curSmMLgCost * intSmMLgShirt 'CALCULATE BASIC PRICE
If chkMonogram.Value = Checked MONOGRAM REQUESTED?
curTotalAmount = curTotalAmount + (curMonogramCost * intSmMLgShirt) 'ADD COST OF
End If 'TO THE CURRENT AMOUNT
If chkPocket.Value = True Then 'IS A POCKET REQUESTED? curTotalAmount = curTotalAmount + (curPocketCost * intSmMLgShirt) 'ADD COST OF POCKET TO TOT AMT.
curSmMLgAmount = curSmMLgAmount + curTotalAmount 'Run the total for SmMLg shirts.
ElseIf optXL.Value = True Then
curTotalAmount = curXLCost * intXLShirt
If chkMonogram.Value =
Checked Then
curTotalAmount = curTotalAmount + (curMonogramCost *
intXLShirt)
End If
If chkPocket.Value =
Checked Then
curTotalAmount = curTotalAmount + (curPocketCost *
intXLShirt)
End If
curXLAmount = curXLAmount + curTotalAmount 'Run the total for XL shirts
ElseIf optXXL.Value = True Then
curTotalAmount = curXXLCost * intXXLShirt
If chkMonogram.Value =
Checked Then
curTotalAmount = curTotalAmount + (curMonogramCost *
intXXLShirt)
End If
If chkPocket.Value =
Checked Then
curTotalAmount = curTotalAmount + (curPocketCost *
intXXLShirt)
End If
curXXLAmount = curXXLAmount + curTotalAmount
'NOW I MUST CHECK TO SEE IF THE CUSTOMER HOLDS A SPECIAL DISCOUNT CARD.
'IF SO, I CALCULATE THE TOTAL AMOUNT TO BE THE TOTAL AMOUNT I CALCULATED ABOVE, LESS
'10%. THIS THEN, WILL BECOME MY ACTUAL TOTAL AMOUNT.
'IF NO DISCOUNT IS TAKEN THEN THE TOTAL AMOUNT WILL STAND AS IT WAS JUST CACULATED.
If chkDiscount.Value = Checked Then
curTotalAmount = curTotalAmount - (curTotalAmount * sngDiscountAmt)
End If