Washington State University



More on user controls and using variables for calculations.-63528511500'This webpage demonstrates a web based point of sale program for an ice cream dispensary. The program calculates how much to charge a customer buying an ice cream cone, and also records daily totals depending on the shift when the transaction occurred (days, nights, and gameday). Gameday sales are for sales on a football game day when many visitors are in town. The program also keeps daily grand totals to reflect all business.'The program created is easier to use than the current point of sale cash register used on the WSU campus. The webpage calculates how much to charge a customer for their custom designed ice cream cone. Advanced built-in features of this webpage include the saving of multiple transactions calculated as a running total of the number of transacions, the sum total, and current average transaction revenue. 'The webpage also demonstrates a wider variety of standard data input web controls. 'We want the webpage to read the data entry, total up the price for the ice crean cone with all of its options, and then display the sales price in a textbox somemething like "Kobe ordered 2 scoops of chocolate in a waffle cone with sprinkles. Charge $3.50.... 'The .selectedItem.text value for each of the list controls demonstrated in this webpage makes data entry and processing super easy and actually removes the need to use more cumbersone select case structure. We use the .selecteditem.text property to capture the selection made in any of the list controls (radiobuttonlists, dropdowns, listboxes, and checkboxlists- this just pulls the text that was selected from the list. Partial Class Controls Inherits System.Web.UI.Page 'Notice the different declaration location of the three variables below, they are created outside the procedure. Most variables are created inside a sub procedure and therefore are called local to that sub procedure. Local variable are created when the procedure runs and they are disposed of when the sub procedure is over. They are called local in scope. But what if you need to use the value in that variable in a different proceure? 'If you want the value in a variable to live longer then you have to create it at the page level. These are called page level variables, or global variables. The scope (accessibility) of the variable is then page level. Your author faithfully puts a g in front of the name of global variables as a reminder that they are global. Here we don't access the variable from different procedures (that is for another day), but global variable are also used to keep running totals. For example the variables used here keep track of ALL the transactions until the web browser is closed. Because web browsers closing dispose of the global variables, programmers use databases to store data permanently (covered later). 'Global variables are created using the term Public Shared rather than Dim (which is used to declare local variables). Here we count he number of customers for the day, sum up the cash received, and calculate the average of the transaction. Businesses care that each transaction meets a certain threshold. For example in 2016 Starbucks made a major push to get the average sale up to $4 per cup, resulting in a huge leap in revenues, and stock value. ' A lot pf planning went into this program - pleaes be sure to also plan out your programs before starting to code. Actually this program is not that difficult, but there is so much commenting that it looks difficult. Public Shared gintCustomers As Integer Public Shared gdectotalRevenue As Decimal Public Shared gdecAverageRevenue As Decimal 'Here we create variables for each of the three shift categories. The next line creates variables to hold the daily total for revenue for each shift type. Public Shared gdecDaysRevenue, gdecNightsRevenue, gdecGameDayRevenue As Decimal 'These next variables are used to keep count of the number of transactions (TA) for each shift category. Management wants to know when the most business occurs, days, nights, and compare that to gameday when there are many visitors in town. Public Shared gintDaysTA, gintNightsTA, gintGameDayTA As IntegerProtected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'First we define a string (meaning text) variable that will hold the ice cream flavors chosen. The second variable is used to calculate the calculated transaction total. The decTotal variable stores the accumulated cost for the base ice cream cone and adds-on the extra costs according to the customer's ice cream cone configuration. Recall, it is convention to put variables at the top of the program. Dim strFlavors As String Dim dectotal As Decimal 'a good programmer of interactive web webpages or apps, must learn to perform data validation on ALL DATA ENTRY, because the wrong data entered (wrong data type or missing data) can crash the webpage.This procedure uses list controls and requires data entry in the listbox and drop down list. So first we ensure that the program user made selections in all the mandatory data fields. If any data entry violations (meaning anything that would break our program) are found the program is stopped and an error message is shown. 'This code segment reads, tests for data entry errors. If any are found the program is halted and an error message is shown. If a selection was not made in the first listbox, or a name wasn't typed into the textbox, or if a selection was not made to specify the ice cream cone type, then stop (exit) the program (sub procedure). In the listbox 0, 1,2 are valid selections (the 0 item is ok). 'In the dropdownlist 1,2,3 are valid selections. The -1 item means nothing was selected and the 0 item reads "select a cone" so this also means a valid item was not selected. So the ddlCone we have to test for -1 and 0, both are problems. If user entry is problematic show an error message in a label and stop the program. If lstScoops.SelectedIndex = -1 OrElse txtName.Text = Nothing OrElse ddlCone.SelectedIndex <= 0 Then lblError.Text = "Please check data entry" lblError.Visible = True Exit Sub End If 'The program would have stopped at this point if there were data entry errors, so the code below only gets run if the data entry is complete and formed correctly, so now do some processing. Here when any of the checkboxes are checked then we augment (add to) a string variable called strFlavor. The symbol '&' concatenates--adds the extra word to the prior text stored in the string variable) with the ice cream flavor. Many IF THEN statements are simple and can be written on one line. BUT DO NOT PUT EXIT SUB AT THE END OF THE LINE 'Now we make sure a shift was selected If rblShift.SelectedIndex = -1 Then lblError.Text = "Be sure to select a shift" lblError.Visible = True End If 'Starting here a long list of data and values are put together and displayed in the textbox. First we build a string (text variable to show the ice cream flavors chosen. Notice the clever use of &= concatenation. The .text property of the checkbox is the text that is shown on the webpage. There are 3 checkboxes, so one at a time we see if they were selected (checked) If chkVanilla.Checked = True Then strFlavors &= chkVanilla.Text & " " If chkChocolate.Checked = True Then strFlavors &= chkChocolate.Text & " " If chkStrawberry.Checked = True Then strFlavors &= chkStrawberry.Text & " " 'The program designer designate the .selectedvalue properties when the webform was made, look for the properties window of the control (on the .aspx page not the coding pae). You can find this by right-clicking the control and selecting properties, or by simply pressing F4. Then in the propoerties window look for the .items collection and open it up. You can also click the little corner box in each of the list controls and select edit items. Read the text values and numeric values for each of the list box selection. In teh above code we use the .text information and in the code below we use the .value information. ' the .selectedvalue property (shown below) of a list box, dropdown list, or checkbox list provices the numeric value associated with the listbox or dropdownlist selection made. So easy. In the code below, don't forget when you see an = sign the numeric value is be copied to the the object on the left of the = sign. dectotal = lstScoops.SelectedValue 'the price for the scoops of creamy ice cream 'the code here is more advanced, when you see the += symbol that means add the value of the item selected to the variable that is used to calculate the price of the cone (dectotal) dectotal += ddlCone.SelectedValue 'price for different type of cones'start to show the outout. The trick of reading the current system clock and adding that to the output text is shown (datetime.now) We are using the & symbol to bind together (concatenate) many different pieces of text, mostly from the user entry webpage. txtOutput.Text = DateTime.Now.ToLongDateString & vbNewLine & vbNewLine & txtName.Text & " ordered " & lstScoops.SelectedItem.Text & " of " & strFlavors & "in a " & ddlCone.SelectedItem.Text 'Here toppings are optional, some customers are purists and do not want any toppings on their ice cream. Since use of this control is optional we do not ensure something was selected, as we do with controls where a selection is mandatory. There is no code that runs if the .selectedindex = -1 or the .selectedindex < 0. Rather we check if something WAS selected rather than check if something WAS NOT seleceted. 'if a toppings selection was made (the 0 value means nothing was selected in the control) then add that information to the output that is shown. In this version of the web page program users can only specify one topping. Loops are needed and will be explained in depth at a later time. Multi-select controls need their own program to be fully explained. If rblToppings.SelectedIndex >= 0 Then txtOutput.Text &= " with " & rblToppings.SelectedItem.Text 'the value in the dectotal variable is being added to. We take the number in the value selected (the .selectedvalue) and add it to the variable that is used to calculate the cost for the cone. dectotal += rblToppings.SelectedValue End If 'if the customer is in the VIP club, then give a discount and change the output and pricing. The .checked is how you see if a check box has been selected. If chkVIP.Checked = True Then txtOutput.Text &= vbNewLine & vbNewLine & txtName.Text & " is a VIP customer, remind them that they received a 25% discount and offer them a free topping." dectotal = dectotal - (dectotal * 0.25) ' price = (price - discount) - you can write this formula more quickly using ... ' dectotal *= 0.75 End If 'add the price to the text output. Notice this code was outside the prior if-then statement as it is needed regardless of the customer being a VIP or not. txtOutput.Text &= vbNewLine & vbNewLine & "charge " & dectotal.ToString("C2") '----------------- now increment the global variables! increment the daily count of customers after this transaction (plus 1) gintCustomers += 1 'add the current transaction to the daily total (this is the total cash received for the day) gdectotalRevenue += dectotal 'calculate the current average cash per transaction gdecAverageRevenue = gdectotalRevenue / gintCustomers 'These next variables are used to keep count of the number of transactions (TA) for each shift category. Management wants to know when the most business occurs, days, nights, and compare that to gameday when there are many visitors in town. Select Case rblShift.SelectedItem.Text Case "Days" gdecDaysRevenue += dectotal gintDaysTA += 1 Case "Nights" gdecNightsRevenue += dectotal gintNightsTA += 1 Case "Gameday" gdecGameDayRevenue += dectotal gintGameDayTA += 1 End Select 'use some fancy concatenation to provide tracking information for the manager. Here we display the values of the global variables. Notice that this is a different textbox than prior. txtDailyTotals.Text = TextAlign.Right txtDailyTotals.Text = "Daily Totals: " & vbNewLine & vbNewLine & "Number cones: " & gintCustomers & vbNewLine & "Sales revenue: " & FormatCurrency(gdectotalRevenue) & vbNewLine & "Avg Sale: " & FormatCurrency(gdecAverageRevenue) 'Use more fancy concatenation to show the shift totals based on whether the transaction occured on the dayshift, nightshift, or a football gameday. 'You can use either FormatCurrency(decimalVariableName, 0) or decimalVariableName.tostring("C0") to display a number with a $ sign and 0 decimals. If you want to show the cents then use 2 (for 2 decimal places) rather than 0. txtShiftTotals.Text = "Daily totals:" & vbNewLine & "Days: " & FormatCurrency(gdecDaysRevenue, 0) & " revenue from " & gintDaysTA & " transactions" & vbNewLine & vbNewLine & "Nights: " & FormatCurrency(gdecNightsRevenue, 0) & " revenue from " & gintNightsTA & " transactions" & vbNewLine & vbNewLine & "Gameday: " & FormatCurrency(gdecGameDayRevenue, 0) & " revenue from " & gintGameDayTA & " transactions" lblError.Visible = False 'the data entry must be good if the program got down to this line, so now clear out any prior error message. You can make any control .visible = false (ie not shown) when you create the program, then change the .visible property to =true in code when the control is needed. Here we changed an error message's visibility to true only when needed, and now turn it back to false to clear up the display of the webpage. End Sub#Region "Utilities" Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init 'show the current date at the top of the screen. Notice the name of this procedure, it is very special. It is the procedure that runs JUST ONE TIME per session, when the web page is first rendered. Label1.Text = DateTime.Now.ToLongDateString End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click 'clear out the values in the form controls txtName.Text = Nothing lstScoops.SelectedIndex = -1 ddlCone.SelectedIndex = -1 rblToppings.SelectedIndex = -1 chkVIP.Checked = False txtOutput.Text = False chkChocolate.Checked = False chkStrawberry.Checked = False chkVanilla.Checked = False End Sub#End Region 'Future programs will allow more flexibility in ordering (such as ability to have more than one topping which requires looping the items collection) and better tracking and displaying of the # of each flavor in the customer order. Also the visibility of the manager's summary information could be password-protected. Can you think of other changes needed?End Class ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download