'Program:       Chapter 06 Hands-On

'Programmer:    Bradley Shedd

'Date:          August 16, 2006

'Description:   Calculates the total based on what the customer wants.

'Form:          Billing.vb

 

Public Class billing

    'Declare project wide variable

    Friend decGrandTot, decAvg As Decimal

    Friend intCustCount As Decimal

 

    'Declare module-level variables.

    Private decSubTot, decTotal As Decimal

 

    'Decalre constants

    Const decTAX_RATE As Decimal = 0.08D

    Const decCAPP_PRICE As Decimal = 2D

    Const decESPRESSO_PRICE As Decimal = 2.25D

    Const decLATTE_PRICE As Decimal = 1.75D

    Const decICED_PRICE As Decimal = 2.5D

 

 

    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

        ' calculate and displays amounts and totals.

        Dim decPrice, decTax, decItemAmnt As Decimal

        Dim intQuantity As Integer

 

        With Me

            If .rbCapp.Checked Then

                decPrice = decCAPP_PRICE

            ElseIf .rbEspres.Checked Then

                decPrice = decESPRESSO_PRICE

            ElseIf .rbLatte.Checked Then

                decPrice = decLATTE_PRICE

            ElseIf .rbICapp.Checked Or .rbILatte.Checked Then

                decPrice = decICED_PRICE

            Else

                MessageBox.Show("Please make a drink selection", "Selection Required", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End If

 

            'Calculate extended price

            Try

                intQuantity = Integer.Parse(.txtQuantity.Text)

                decItemAmnt = decPrice * intQuantity

                decSubTot += decItemAmnt

                If .chkTax.Checked Then

                    'Call a function procedure

                    decTax = FindTax(decSubTot)

                Else

                    decTax = 0

                End If

                decTotal = decSubTot + decTax

                .txtAmount.Text = decItemAmnt.ToString("c")

                .txtSubtot.Text = decSubTot.ToString("n")

                .txtTax.Text = decTax.ToString("n")

                .txtTotal.Text = decTotal.ToString("C")

                'allow change for new order only

                .chkTax.Enabled = True

                .btnClear.Enabled = True

            Catch quantityException As FormatException

                MessageBox.Show("Enter the quantity.", "Data entry error", MessageBoxButtons.OK, MessageBoxIcon.Information)

                With .txtQuantity

                    .Focus()

                    .SelectAll()

                End With

            End Try

        End With

    End Sub

    Private Function FindTax(ByVal DecAmount As Decimal) As Decimal

        'calc sales tax

        Return DecAmount * decTAX_RATE

    End Function

 

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

 

        With Me

            .txtAmount.Clear()

            With .txtQuantity

                .Clear()

                .Focus()

            End With

        End With

    End Sub

 

    Private Sub NewOrderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewOrderToolStripMenuItem.Click

        'clear the current order and add to the totals

        Dim responseDialogResult As System.Windows.Forms.DialogResult

        Dim messageString As String

 

        'confirm clear of the current order

        messageString = "Clear the current order figures?"

        responseDialogResult = MessageBox.Show(messageString, "Clear Order", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

 

        With Me

            If responseDialogResult = System.Windows.Forms.DialogResult.Yes Then

                btnClear_Click(sender, e)

                .txtSubtot.Clear()

                .txtTax.Clear()

                .txtTotal.Clear()

                Try

                    If decSubTot <> 0 Then

                        decGrandTot += decTotal

                        intCustCount += 1

                        decSubTot = 0

                        decTotal = 0

                    End If

                Catch

                    messageString = "Error in calculations"

                    MessageBox.Show(messageString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End Try

 

                With .chkTax

                    .Enabled = True

                    .Checked = False

                End With

                .btnClear.Enabled = False

            End If

        End With

 

    End Sub

 

    Private Sub SummaryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryToolStripMenuItem.Click

        Dim messageString As String

 

        If decTotal <> 0 Then

            NewOrderToolStripMenuItem_Click(sender, e)

        End If

 

        If intCustCount > 0 Then

            Try

                decAvg = decGrandTot / intCustCount

                summary.ShowDialog()

            Catch

                messageString = "Error in calculations."

                MessageBox.Show(messageString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try

        Else

            messageString = "No sales data to summarize."

            MessageBox.Show(messageString, "Coffee Sales Summary", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

    End Sub

 

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        Me.Close()

    End Sub

 

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click

        AboutBox.ShowDialog()

    End Sub

 

    Private Sub FontToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click

        With Me.FontDialog1

            .Font = Me.txtSubtot.Font

            .ShowDialog()

            Me.txtSubtot.Font = .Font

            Me.txtTax.Font = .Font

            Me.txtTotal.Font = .Font

        End With

    End Sub

 

    Private Sub ColorToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click

        With Me.ColorDialog1

            .Color = Me.txtSubtot.ForeColor

            .ShowDialog()

            Me.txtSubtot.ForeColor = .Color

            Me.txtTax.ForeColor = .Color

            Me.txtTotal.ForeColor = .Color

        End With

    End Sub

 

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

 

    End Sub

 

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

 

    End Sub

 

    Private Sub ClearItemToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearItemToolStripMenuItem.Click

 

        'Clear any selected radio item

        rbCapp.Checked = False

        rbEspres.Checked = False

        rbLatte.Checked = False

        rbILatte.Checked = False

        rbICapp.Checked = False

    End Sub

End Class

 

 

'Program:       Chapter 06 Hands-On

'Programmer:    Bradley Shedd

'Date:          August 16, 2006

'Description:   Calculates the total based on what the customer wants.

'Form:          Summary.vb

 

Public Class summary

 

    Private Sub summary_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated

        'Get the data

 

        With Me

            .txtTotSales.Text = billing.decGrandTot.ToString("C")

            .txtAvgSales.Text = billing.decAvg.ToString("C")

            .txtNumCust.Text = billing.intCustCount.ToString()

        End With

    End Sub

 

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click

        Me.Hide()

    End Sub

 

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

 

    End Sub

End Class

 

Homepage