12-12-05

Students answer to case study 4A and 4B

(form for program below)

Dim hr, grosspay, regularpay, taxamount, taxrate, netpay As Double

Dim hw, empid As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

empid = InputBox("ENTER YOUR ID")

TextBox1.Text = empid

hw = InputBox("ENTER HOURS WORKED")

TextBox2.Text = hw

hr = InputBox("ENTER YOUR HOURLYRATE")

TextBox3.Text = hr

grosspay = hr * hw

TextBox4.Text = grosspay

If grosspay > 500 Then

taxrate = 0.3

ElseIf grosspay > 200 Then

taxrate = 0.2

Else

taxrate = 0.1

End If

TextBox5.Text = taxrate

If RadioButton1.Checked Then

taxrate = taxrate + 0.05

ElseIf RadioButton3.Checked Then

taxrate = taxrate - 0.05

End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

taxamount = grosspay * taxrate

TextBox6.Text = taxamount

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

netpay = grosspay - taxamount

TextBox7.Text = netpay

End Sub

End Class

The program below include changes to format the currency

Dim hr, grosspay, regularpay, taxamount, taxrate, netpay As Double

Dim hw, empid As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

empid = InputBox("ENTER YOUR ID")

TextBox1.Text = empid

hw = InputBox("ENTER HOURS WORKED")

TextBox2.Text = hw

hr = InputBox("ENTER YOUR HOURLYRATE")

TextBox3.Text = hr

grosspay = hr * hw

TextBox4.Text = Format(grosspay, "currency")

If grosspay > 500 Then

taxrate = 0.3

ElseIf grosspay > 200 Then

taxrate = 0.2

Else

taxrate = 0.1

End If

TextBox5.Text = taxrate

If RadioButton1.Checked Then

taxrate = taxrate + 0.05

ElseIf RadioButton3.Checked Then

taxrate = taxrate - 0.05

End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

taxamount = grosspay * taxrate

TextBox6.Text = Format(taxamount, "currency")

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

netpay = grosspay - taxamount

TextBox7.Text = Format(netpay, "currency")

End Sub

End Class

The program below is from visual basic studio 2005 done by a student

Public Class Form1

    Dim C As Integer = 0  'Counter

    'Dim x As Integer = 100 'Max amount of items

    Dim ItemID(100) As String

    Dim ItemName(100) As String

    Dim ItemPrice(100) As Double

    Dim ItemQty(100) As Double

    Dim ItemSubTotal(100) As Double

    Dim OrderSubTotal As Double

    Dim TaxesSubTotal As Double

    Dim SubTotal As Double

    Dim Tax(100) As Double

    Dim Taxes As Double

    Dim Total As Double

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub rbTaxable_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbTaxable.CheckedChanged

        'Calculates Tax Sub Total and assigns it to Array.

        Tax(C) = Val(ItemSubTotal(C)) * 0.1

        TaxesSubTotal = Tax(C)

        'Formats Tax Sub Total and displays it to user.

        Label9.Text = FormatCurrency(TaxesSubTotal)

    End Sub

    Private Sub txtItemQty_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemQty.LostFocus

        'Calculates Item Sub Total and assigns it to Array.

        ItemSubTotal(C) = txtItemPrice.Text * txtItemQty.Text

        'Formats data to currency and displays for user

        txtItemSubTotal.Text = FormatCurrency(ItemSubTotal(C))

    End Sub

    Private Sub btnComputeSubTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeSubTotal.Click

        'Assigns data in textboxes to arrays.

        ItemID(C) = Val(metxtItemID.Text)

        ItemName(C) = txtItemName.Text

        ItemPrice(C) = txtItemPrice.Text

        ItemQty(C) = txtItemQty.Text

        'Updates new subtotal in OrderSubTotal variable.

        OrderSubTotal = Val(OrderSubTotal) + Val(ItemSubTotal(C))

        'Formats OrderSubTotal to currency and displays for user.

        txtOrderSubTotal.Text = FormatCurrency(OrderSubTotal)

        'Resets Radio Button to default (not selected).

        rbTaxable.Checked = False

        'Clears Textboxes for new entry.

        metxtItemID.Clear()

        txtItemName.Clear()

        txtItemPrice.Clear()

        txtItemQty.Clear()

        txtItemSubTotal.Clear()

        Label9.Text = ""

        'Sets focus to Item ID textbox when user accepts item.

        metxtItemID.Focus()

        'Advances counter for Arrays.

        C = C + 1

    End Sub

    Private Sub txtItemPrice_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItemPrice.LostFocus

        'Formats Item Price to currency when fiels loses focus

        txtItemPrice.Text = FormatCurrency(txtItemPrice.Text)

    End Sub

    Private Sub ComputeTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComputeTotal.Click

        Dim j As Integer = 0

        Dim i As Integer

        For i = 0 To C

            'Calculates total taxes when user selects compute total button.

            Taxes = Taxes + Tax(i)

            'Calculates total including taxes when user selects compute total button.

            Total = Total + ItemSubTotal(i) + Tax(i)

        Next

        'Converts total taxes and Total to currency.

        txtTotal.Text = FormatCurrency(Total)

        txtTax.Text = FormatCurrency(Taxes)

        'Prints list of purchases

        'lblItemIDdatacol.Text = ""

        For i = 0 To C

            'Prints data to columns.

            If ItemID(i) > 0 Then lblItemIDdatacol.Text = lblItemIDdatacol.Text & vbNewLine & ItemID(i)

            If ItemName(i) > "" Then lblItemNamedatacol.Text = lblItemNamedatacol.Text & vbNewLine & ItemName(i)

            If ItemPrice(i) > 0 Then lblItemPricedatacol.Text = lblItemPricedatacol.Text & vbNewLine & FormatCurrency(ItemPrice(i))

            If ItemQty(i) > 0 Then lblItemQtydatacol.Text = lblItemQtydatacol.Text & vbNewLine & ItemQty(i)

            If ItemSubTotal(i) > 0 Then lblItemSubTotaldatacol.Text = lblItemSubTotaldatacol.Text & vbNewLine & FormatCurrency(ItemSubTotal(i))

            'Allows tax amount to be zero and keep its place.

            'If ItemSubTotal(i) > 0 Then lblTaxdatacol.Text = lblTaxdatacol.Text & vbNewLine & FormatCurrency(Tax(i))

        Next i

        'Prints sub total data at bottom of columns.

        lblItemQtydatacol.Text = lblItemQtydatacol.Text & vbNewLine & vbNewLine & "Sub Totals"

        lblItemSubTotaldatacol.Text = lblItemSubTotaldatacol.Text & vbNewLine & vbNewLine & FormatCurrency((Total - Taxes))

        lblItemQtydatacol.Text = lblItemQtydatacol.Text & vbNewLine & vbNewLine & "Taxes"

        lblItemSubTotaldatacol.Text = lblItemSubTotaldatacol.Text & vbNewLine & vbNewLine & FormatCurrency((Taxes))

        lblItemQtydatacol.Text = lblItemQtydatacol.Text & vbNewLine & vbNewLine & "Grand Total"

        lblItemSubTotaldatacol.Text = lblItemSubTotaldatacol.Text & vbNewLine & vbNewLine & FormatCurrency((Total))

    End Sub

End Class

 

 The program below was done by a student using VB2005. Example Phase 6 page 213

  

 

 

 

Public Class Form1

    Dim lastName_String(100) As String                   ' Global variables

    Dim firstName_String(100) As String

    Dim empID_decimal(100) As Decimal

    Dim hoursWorked_decimal(100) As Decimal

    Dim hourlyRate_decimal(100) As Decimal

    Dim gross_pay_decimal(100) As Decimal

    Dim net_pay_decimal(100) As Decimal

 

    Dim counterArray_Interger As Integer = 0

    Dim overtimePay_Decimal As Decimal              ' OT pay if any

    Dim grossPay_Decimal As Decimal                 ' Gross pay before deduction

    Dim netPay_Decimal As Decimal                   ' Pay after deduction

    Dim regularPay_Decimal As Decimal               ' regular pay, 40 Hrs or less work week

    Dim taxSubtraction_Decimal As Decimal           ' tax taken out of gross pay

 

 

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7_saveToArray.Click

        '  save to array

        saveToArray()

    End Sub

 

    ' Display array's values

    Private Sub Button8_displayALLdata_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8_displayALLdata.Click

 

        displayArraysValue()

 

    End Sub

    ' EMPLOYEE's ID

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Validate_EmployeeID_Number()         '  sub Procedure

    End Sub

  

 ' EMPLOYEE's hourlyRate

    Private Sub TextBox3_hourlyRate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3_hourlyRate.TextChanged

        Validate_HourlyRate_Number()         '  sub Procedure

    End Sub

 

  ' EMPLOYEE's HoursWorked

    Private Sub TextBox4_hoursWorked_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4_hoursWorked.TextChanged

        Validate_HoursWorked_Number()        '  sub Procedure

    End Sub

  

 ' EMPLOYEE's tax rate

    Private Sub tax_ListBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tax_ListBox1.SelectedIndexChanged

        Dim taxRate As Double = 0.0

        taxRate = returnTAXrate()

        tax_Label.Text = taxRate

 

    End Sub

 

   ' EMPLOYEE's First name

    Private Sub TextBox2_firstName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2_firstName.TextChanged

        ' Not used at this time

    End Sub

 

   ' EMPLOYEE's Last name

    Private Sub TextBox2_Lastname_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2_Lastname.TextChanged

        ' Not used at this time

    End Sub

 

   ' EMPLOYEE's Gross pay

    Private Sub Button3_grosspay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3_grosspay.Click

 

        If Validate_data() Then

            computeGross_Pay()              ' Sub Procedures  No return

        End If

    End Sub

 

    ' EMPLOYEE's OT pay

    Private Sub Button2_OT_pay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2_OT_pay.Click

        If Validate_data() Then

            computeOT_Pay()

        End If

    End Sub

 

    ' EMPLOYEE's Net pay

    Private Sub Button6_netPay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6_netPay.Click

        If Validate_data() Then

            computeNET_Pay()

        End If

    End Sub

 

    '+++++++++++++++++++++++++++++++++++++++++++  Sub Procedures  No return ++++++++++++++++++++++++++++++++++++++++

    Private Sub Validate_EmployeeID_Number()

        Dim isNumberCK As Boolean = True

        isNumberCK = IsNumeric(TextBox1.Text)

 

        If (Not isNumberCK) Then

            TextBox1.Text = ""

        End If

 

    End Sub

 

 

    Private Sub Validate_HourlyRate_Number()

        Dim isNumberCK As Boolean = True                        ' note scope of procedure

        isNumberCK = IsNumeric(TextBox3_hourlyRate.Text)

 

        If (Not isNumberCK) Then

            TextBox3_hourlyRate.Text = ""

        End If

 

    End Sub

 

 

   Private Sub Validate_HoursWorked_Number()

        Dim isNumberCK As Boolean = True

        isNumberCK = IsNumeric(TextBox4_hoursWorked.Text)

 

        If (Not isNumberCK) Then

            TextBox4_hoursWorked.Text = ""

        End If

 

    End Sub

 

 

    Private Sub computeGross_Pay()

        overtimePay_Decimal = cal_OT_Pay()

        regularPay_Decimal = cal_Regular_Pay()

        grossPay_Decimal = regularPay_Decimal + overtimePay_Decimal

        MessageBox.Show("Gross pay = $" & grossPay_Decimal, "Employee's Gross pay", MessageBoxButtons.OK)

 

        ' netPay_Decimal = grossPay_Decimal - taxSubtraction_Decimal

    End Sub

 

 

   Private Sub computeOT_Pay()

        overtimePay_Decimal = cal_OT_Pay()

        MessageBox.Show("Overtime pay = $" & overtimePay_Decimal, "Employee's overtime pay", MessageBoxButtons.OK)

    End Sub

 

 

   Private Sub computeNET_Pay()

        overtimePay_Decimal = cal_OT_Pay()

        regularPay_Decimal = cal_Regular_Pay()

        taxSubtraction_Decimal = cal_TaxOnWages(regularPay_Decimal, overtimePay_Decimal)  ' Note passing of parameters

        grossPay_Decimal = regularPay_Decimal + overtimePay_Decimal

        netPay_Decimal = grossPay_Decimal - taxSubtraction_Decimal

        MessageBox.Show("After tax pay = $" & netPay_Decimal, "Employee's Net pay", MessageBoxButtons.OK)

    End Sub

 

 

   Private Sub computeNET_Pay2()

        overtimePay_Decimal = cal_OT_Pay()

        regularPay_Decimal = cal_Regular_Pay()

        taxSubtraction_Decimal = cal_TaxOnWages(regularPay_Decimal, overtimePay_Decimal)  ' Note passing of parameters

        grossPay_Decimal = regularPay_Decimal + overtimePay_Decimal

        netPay_Decimal = grossPay_Decimal - taxSubtraction_Decimal

    End Sub

 

 

    Private Sub displayArraysValue()

        If Validate_data() Then

 

            TextBox6_Display.Clear()

 

            For count As Integer = 0 To counterArray_Interger - 1

 

                TextBox6_Display.AppendText(empID_decimal(count).ToString.PadLeft(10, "_") & firstName_String(count).PadLeft(10, "_") & lastName_String(count).PadLeft(10, "_") _

                 & hoursWorked_decimal(count).ToString.PadLeft(10, "_") & hourlyRate_decimal(count).ToString.PadLeft(10, "_") & gross_pay_decimal(count).ToString.PadLeft(10, "_") & ControlChars.NewLine)

                MessageBox.Show(count, "Displaying Array Number", MessageBoxButtons.OK)

            Next

 

        End If

 

 

    End Sub

  

 Private Sub saveToArray()

 

        If Validate_data() Then

 

            computeNET_Pay2()   ' will calculate 

            ' overtimePay_Decimal, regularPay_Decimal, taxSubtraction_Decimal, grossPay_Decimal, netPay_Decimal

 

            empID_decimal(counterArray_Interger) = Val(TextBox1.Text)

            firstName_String(counterArray_Interger) = TextBox2_firstName.Text

            lastName_String(counterArray_Interger) = TextBox2_Lastname.Text

            hoursWorked_decimal(counterArray_Interger) = Val(TextBox4_hoursWorked.Text)

            hourlyRate_decimal(counterArray_Interger) = Val(TextBox3_hourlyRate.Text)

            gross_pay_decimal(counterArray_Interger) = grossPay_Decimal

            net_pay_decimal(counterArray_Interger) = netPay_Decimal

 

            counterArray_Interger += 1

            TextBox5_counter.Text = counterArray_Interger                    ' increment counter

 

            TextBox6_Display.Text = empID_decimal(0).ToString.PadLeft(14) & firstName_String(0).PadLeft(14) & lastName_String(0).PadLeft(12) _

             & hoursWorked_decimal(0).ToString.PadLeft(10) & hourlyRate_decimal(0).ToString.PadLeft(10) & gross_pay_decimal(0).ToString.PadLeft(10)

        End If

 

    End Sub

     '++++++++++++++++++++++++++++++++++++++++++++++++++++  End Sub Procedures   +++++++++++++++++++++++++++++++

  

    '++++++++++++++++++++++++++++++++++++++++++++++++    FUNCTION Procedures    +++++++++++++++++++++++++++++++

    Private Function returnTAXrate() As Double

        Dim taxRate As Double

        Select Case tax_ListBox1.Text

            Case "New York"

                tax_Label.Text = "8.65%"

                taxRate = 8.65

            Case "New Jersey"

                tax_Label.Text = "6.50%"

                taxRate = 6.5

            Case "Florida"

                tax_Label.Text = "5.25%"

                taxRate = 5.25

            Case "Washington"

                tax_Label.Text = "7.25%"

                taxRate = 7.25

            Case "Los Angles"

                tax_Label.Text = "9.25%"

                taxRate = 9.25

            Case Else

                tax_Label.Text = "Select Tax %"

                taxRate = 0.0

        End Select

        Return taxRate

    End Function

 

 

    ' FX() Validate form, returns Boolean

    Private Function Validate_data() As Boolean

        ' Fx will check for numerical value in Emp ID, Hr rate, Hr Wk, first and last name

        Dim Validate_data1 As Boolean

 

        If ((Not IsNumeric(TextBox1.Text)) Or (Not IsNumeric(TextBox3_hourlyRate.Text)) Or (Not IsNumeric(TextBox4_hoursWorked.Text)) _

               Or TextBox2_firstName.Text = "" Or TextBox2_Lastname.Text = "" Or tax_Label.Text = "0") Then

            MessageBox.Show("Enter first and last name, Employee's ID, Hrs Wk, Hrly Rate, select a tax rate ", "Error, Please complete the form", _

            MessageBoxButtons.OKCancel)

            Validate_data1 = False

        Else : Validate_data1 = True

        End If

 

        Return Validate_data1

    End Function

 

 

   ' FX() returns OT

    Private Function cal_OT_Pay() As Decimal

        Dim ot As Decimal

        If (Val(TextBox4_hoursWorked.Text) > 40) Then

            ot = (Val(TextBox4_hoursWorked.Text) - 40) * (1.5) * Val(TextBox3_hourlyRate.Text)

        Else : ot = 0

        End If

        Return ot

 

    End Function

 

 

   ' FX() returns regular pay, no OT

    Private Function cal_Regular_Pay() As Decimal

        Dim regPayDay As Decimal

        If (Val(TextBox4_hoursWorked.Text) >= 40) Then

            regPayDay = 40 * Val(TextBox3_hourlyRate.Text)

        Else : regPayDay = Val(TextBox4_hoursWorked.Text) * Val(TextBox3_hourlyRate.Text)

        End If

        Return regPayDay

 

    End Function

 

 

   ' FX() returns tax on wages, note passing Arguments ByVal so that procedure cannot alter original value.

    Private Function cal_TaxOnWages(ByVal regularPay_Decimal, ByVal overtimePay_Decimal) As Decimal

        Dim tax As Decimal        ' tax = (taxrate% * gross pay)/ 100

        tax = (Val(tax_Label.Text) * (regularPay_Decimal + overtimePay_Decimal)) / 100

        Return tax

 

    End Function

 

 

    '++++++++++++++++++++++++++++++++++++++++++++   END   -    FUNCTION Procedures    +++++++++++++++++++++++++

 

 

End Class

 

 

 

 

 

 

 

 

 

 

    Private Sub btnImportData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportData.Click

 

        Fin = New StreamReader("C:\documents and Settings\Greg\Desktop\customer.txt")

        Dim EofTest As Integer = 1

        While EofTest <> 0

            ItemID(C) = Fin.ReadLine

            EofTest = ItemID(C)

            ItemName(C) = Fin.ReadLine

            ItemPrice(C) = Fin.ReadLine

            ItemQty(C) = Fin.ReadLine

            ItemSubTotal(C) = ItemPrice(C) * ItemQty(C)

            If Fin.ReadLine = 1 Then Tax(C) = ItemSubTotal(C) * 0.1

            C = C + 1

        End While

        Label11.Text = C - 1

        Fin.Close()

 

 

    End Sub

End Class