Washington State University



Managing More Complex Programs program is typical of the next step in your learning of systems development. Managing larger programs. This program is not much harder in complexity, but it does require a lot of different controls and variables to calculate and display payroll for a current transaction and to keep totals for three different states.Managing complexity takes organization. Take a good look at the names of the controls, most of the guesswork is removed. You read the name of the control and you know what it is used for. Also in the blue section of running totals, take a look at the labels. They are not named haphazardly, they are named carefully so for example all the labels to display net pay are all named similarly.Another best practice for organization is that all the code for declaring variables is on one location, all the code for data validation is in one location, all the code for displaying running totals is in one location. This is similar to putting your clothes in a dresser after washing and drying them. If you put all your socks in one drawer they will be easier to find and manage, etc.Use any organizational trick you can. You will finish your projects faster.Finally while session variable allow you to pass values from one page to another, doing this opens up a security hole. It’s better to save values to a database and have then next page read them from a database. You can also use a multiview control (shown later) which gives a tabbed interface to the page.Code:'This program calculates payroll, and demonstrates a robust error-checking methodology. One thing to learn from this program and ponder for yourself (as you develop your own programming style), is how decide many variables a program needs. You can have too many! Too many variables means you have too much to keep track of. One heuristic is that if you are going to use the value from a web control more than a few times then assign it to a variable to speed things up.'The next step in your learning is to gain practice planning out more complext programs that are more lines of code, and have more design decisions and calculations to perform and keep track of.'BTW an interesting upgrade to the progam would be to keep track of the number of overtime hours per state and compare them to limits set by management. Project managers typically keep a close tab on overtime paid to help ensure projects do not go over budget.Partial Class IntroDevClass_StarterPayroll Inherits System.Web.UI.Page 'this program can calculate payroll for one employee and keep running totals for all of employees processed. Running totals for total pay, net pay and taxes by state are kept for each of 3 states. The payroll clerk needs to know how much money to set aside for taxes. A later version of this program will use a data structure to simplify the code and keep track of running totals. 'These are the public shared are variables that can be used to keep track of running totals Public Shared gdecWAGP As Decimal 'grosspay for 3 states Public Shared gdecIDGP As Decimal Public Shared gdecORGP As Decimal Public Shared gdecWATX As Decimal 'taxes collected for 3 states Public Shared gdecIDTX As Decimal Public Shared gdecORTX As Decimal Public Shared gintIDEmployees As Integer '# of employees that have had payroll calculated Public Shared gintOREmployees As Integer Public Shared gintWAEmployees As Integer Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 'Before you start reading this code, make a plan for what the code should do. Could you define all the requirements needed? Could you plan out all the programming steps that are needed? It is a good exercise to make a plan and then compare it to the code you see here. 'Remember all local variables should go at the top of a progam so they do not get lost. Use these variables to store the # hours the employee worked and the employee's payrate Dim decHours As Decimal Dim decPayRate As Decimal ' if overtime is calculated use this variable to store the amount of overtime pay Dim decOTPay As Decimal ' use this variable to store the gross, net pay and taxes for the employee Dim decGrossPay As Decimal Dim decNetPay As Decimal Dim decTaxes As Decimal 'the following pay calculation algorithm, requires that a valid number was typed into the textbox. What if a bogus entry was made? We need some type of data Validation. So we check if a number was types in, and stop the program if data entry error is found. If a number is entered, then convert it to a decimal value and assign it to the decimal variable. If IsNumeric(txtHours.Text) Then decHours = CDec(txtHours.Text) lblError.Visible = False Else 'this code runs if there is sa data entry error. if what the user types what does not resemble to be a # in the textbox, then show an error message and stop the program. lblError.Visible = True lblError.Text = "hours entered must be numeric" Exit Sub End If If IsNumeric(txtPayrate.Text) Then 'if the user typed what resembles a # (using the IsNumeric function) into the textbox then convert the number to a decimal value (CDEC) 'and assign it to the decimal variable decPayRate = CDec(txtPayrate.Text) lblError.Visible = False Else lblError.Visible = True lblError.Text = "payrate entered must be numeric" Exit Sub End If 'make sure a value was selected for the state taxes, stop program if no state selected If rblStates.SelectedIndex = -1 Then lblError.Visible = True lblError.Text = "Select a state" Exit Sub End If 'OK now we can perform the payroll processing, the program will not crash because the important data entry has been validated. If decHours <= 40 Then 'If hours worked are <=40 then calculate gross without worrying about overtime decGrossPay = decHours * decPayRate End If If decHours > 40 Then 'If hours worked are over 40 then grosspay is a combination of base plus overtime pay. In US overtime is typically paid at 150% of regular hourly rate. We are not going to worry about shift differential, double-pay for sundays, holidays, etc. decOTPay = (decHours - 40) * (decPayRate * 1.5) 'calc the OT pay which is # OT hours times time and a half add the regular and OT pay together to calc grosspay decGrossPay = (40 * decPayRate) + decOTPay End If 'calculate the taxes and net pay (Netpay = grosspay minus taxes) decTaxes = decGrossPay * rblStates.SelectedValue decNetPay = decGrossPay - decTaxes 'show output to the program user - this is or the current employee 'you have to convert numbers (or dates) to text before you show it in a label or textbox that is what the .tostring is. The C = currency, with default of 2 decimal places. C0 = currency with 0 decimal places, you can also use P for percent, N for number. lblGrossPay.Text = decGrossPay.ToString("C") lblNetPay.Text = decNetPay.ToString("C") lblTaxes.Text = decTaxes.ToString("C") 'the section below is used to increment the 3 global variables for the state that the employee labor was recorded for. the state value passed in from the radiobutton list is the test condition. In case the value being tested was Idaho, then update the global variables for Idaho, etc. A lot of problems can be solved with Select Case. Select Case rblStates.SelectedItem.Text Case "Idaho" gdecIDGP += decGrossPay gdecIDTX += decTaxes gintIDEmployees += 1 Case "Oregon" gdecORGP += decGrossPay gdecORTX += decTaxes gintOREmployees += 1 Case "Washington" gdecWAGP += decGrossPay gdecWATX += decTaxes gintWAEmployees += 1 End Select Call RefreshLabels() 'this is how you envoke (call) another procedure making that code run. 'you use general subprocedures typically for 'helper' procedures that you call from many places in your program 'Now we take the program to a new level. While it is much safer to put the values into database tables and then let other pages retrieve those values, here we simulate that funcitonality using project-level scope variables. The values in these 6 session variables can be accessed by other webpages in the same project. While the values in the public shared variables are available anywhere in one page; the values in session variables can be used ACROSS DIFFERENT web pages. Session("gdecIDGP") = gdecIDGP Session("gdecORGP") = gdecORGP Session("gdecWAGP") = gdecWAGP Session("gdecWATX") = gdecWATX Session("gdecIDTX") = gdecIDTX Session("gdecORTX") = gdecORTX End Sub Private Sub RefreshLabels() 'we fill up the 12 labels with text lblIdahoGP.Text = FormatCurrency(gdecIDGP, 0) lblOregonGP.Text = FormatCurrency(gdecORGP, 0) lblWAGP.Text = FormatCurrency(gdecWAGP, 0) lblIDTX.Text = FormatCurrency(gdecIDTX, 0) lblORTX.Text = FormatCurrency(gdecORTX, 0) LBLWATX.Text = FormatCurrency(gdecWATX, 0) 'rather than make 3 more variables you can just do the calculation in code. Perhaps a good rule is if the value 'is a KPI (key performance indicator, aka key figure, then store the value into a variable lblIDNP.Text = FormatCurrency(gdecIDGP - gdecIDTX) lblORNP.Text = FormatCurrency(gdecORGP - gdecORTX) lblWANP.Text = FormatCurrency(gdecWAGP - gdecWATX) lblIDEmp.Text = gintIDEmployees lblOREmp.Text = gintOREmployees lblWAEmp.Text = gintWAEmployees End SubEnd Class ................
................

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

Google Online Preview   Download