All pastes #763093 Raw Edit

Something

public text v1 · immutable
·published 2007-11-06 03:58 UTC
rendered paste body
'*********************************************************************
'                    yachtChartering.vb
'                   --------------------
'Folder:        yachtChartering
'Project:       Assignment 7.3 - Yacht Chartering
'Programmer:    Adam Reyher (adam@adamreyher.com)
'Date:          Monday, November 5, 2007
'Description:   This program is for chartering yacht trips.
'               Prices will be calcualted basec on yacht size and
'               hourly rate. A summary can be printed.
'*********************************************************************

Option Strict On
Option Explicit On

Public Class mainForm
    'Declare summary values we'll need
    Dim numberOfChartersDecimal As Decimal = 0D
    Dim totalRevenueDecimal As Decimal = 0D
    Dim totalHoursCharteredInteger As Integer = 0I
    Dim averageHoursCharteredDecimal As Decimal

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

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

    Private Function CheckYachtType(ByVal selectedYachtType As String) As Boolean
        Dim duplicateTypeBoolean As Boolean = False
        Dim searchInteger As Integer = 0I
        With Me
            Do While searchInteger < .typeComboBox.Items.Count
                If selectedYachtType.ToLower = .typeComboBox.Items(searchInteger).ToString.ToLower Then
                    duplicateTypeBoolean = True
                    Exit Do
                End If
                searchInteger = searchInteger + 1
            Loop
        End With
        Return duplicateTypeBoolean
    End Function

    Private Sub AddYachtTypeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddYachtTypeToolStripMenuItem.Click
        'Add Yacht Type from the text box
        Dim duplicateTypeBoolean As Boolean = False
        Dim searchInteger As Integer = 0I

        With Me
            'Check for blank entries and duplicates
            If .typeComboBox.Text.Trim <> "" Then
                Do While searchInteger < .typeComboBox.Items.Count
                    If .typeComboBox.Text.ToLower = .typeComboBox.Items(searchInteger).ToString.ToLower Then
                        duplicateTypeBoolean = True
                        Exit Do
                    End If
                    searchInteger = searchInteger + 1
                Loop
                If duplicateTypeBoolean = False Then
                    .typeComboBox.Items.Add(.typeComboBox.Text)
                    .typeComboBox.Text = ControlChars.NullChar
                Else
                    MessageBox.Show("That yacht type already exists", "Duplicate Found", _
                        MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If

            Else
                MessageBox.Show("Yacht type cannot be blank", "Data Entry Error", _
                    MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End With
    End Sub

    Private Sub RemoveYachtTypeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveYachtTypeToolStripMenuItem.Click
        'Remove a yacht type from the list
        With Me
            'Check for blank entries or non existant types
            If .typeComboBox.SelectedIndex <> -1 Then
                .typeComboBox.Items.RemoveAt(.typeComboBox.SelectedIndex)
            Else
                MessageBox.Show("Please enter a yacht type to remove", "Data Entry Error", _
                    MessageBoxButtons.OK, MessageBoxIcon.Information)
                .typeComboBox.Focus()
            End If
        End With
    End Sub

    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
        'Clear the form and focus
        With Me
            .partyTextBox.Text = ControlChars.NullChar
            .hoursTextBox.Text = ControlChars.NullChar
            .sizeComboBox.Text = ControlChars.NullChar
            .typeComboBox.Text = ControlChars.NullChar
            .partyTextBox.Focus()
        End With
    End Sub

    Private Sub ClearForNextCharterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearForNextCharterToolStripMenuItem.Click
        'Recall the code
        clearButton_Click(sender, e)
    End Sub

    Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
        'The big momma of the program ... hold on tight
        'Calculate the price and check for data entry errors
        Dim charterHoursInteger As Integer
        Dim sizeofCharterInteger As Integer

        Dim errorThrownBoolean As Boolean = False
        Dim errorMessageString As String = ""

        Dim charterHourlyCostDecimal As Decimal
        Dim thisCharterCostDecimal As Decimal

        'Check to see if data was entered
        If Me.partyTextBox.Text = "" Then
            errorThrownBoolean = True
            errorMessageString &= "Responsible party is required" & ControlChars.NewLine
        End If
        If Me.hoursTextBox.Text = "" Then
            errorThrownBoolean = True
            errorMessageString &= "Hours chartered is required" & ControlChars.NewLine
        End If
        If Me.sizeComboBox.Text = "" Then
            errorThrownBoolean = True
            errorMessageString &= "Size of charter is required" & ControlChars.NewLine
        End If
        If CheckYachtType(Me.typeComboBox.Text) = False Then
            errorThrownBoolean = True
            errorMessageString &= "Yacht type is required" & ControlChars.NewLine
        End If

        'Display the error message(s) if it has been toggled to TRUE
        If errorThrownBoolean = True Then
            MessageBox.Show(errorMessageString, "Error entering data", _
                MessageBoxButtons.OK, MessageBoxIcon.Information)
            'No errors? Let's proceed
        Else
            Try
                'Convert string to decimal
                charterHoursInteger = Integer.Parse(Me.hoursTextBox.Text)
                Try
                    sizeofCharterInteger = Integer.Parse(Me.sizeComboBox.Text)
                    Try
                        Select Case sizeofCharterInteger
                            Case 22I
                                charterHourlyCostDecimal = 95D
                            Case 24I
                                charterHourlyCostDecimal = 137D
                            Case 30I
                                charterHourlyCostDecimal = 160D
                            Case 32I
                                charterHourlyCostDecimal = 192D
                            Case 36I
                                charterHourlyCostDecimal = 250D
                            Case 38I
                                charterHourlyCostDecimal = 400D
                            Case 45I
                                charterHourlyCostDecimal = 550D
                        End Select

                        'Let's calculate the pay and enable the summary, then finally display the value
                        thisCharterCostDecimal = (charterHourlyCostDecimal * charterHoursInteger)
                        Me.PrintSummaryToolStripMenuItem.Enabled = True

                        'Increment the total summary values
                        numberOfChartersDecimal = numberOfChartersDecimal + 1
                        totalRevenueDecimal = totalRevenueDecimal + thisCharterCostDecimal
                        totalHoursCharteredInteger = totalHoursCharteredInteger + charterHoursInteger
                        averageHoursCharteredDecimal = (totalHoursCharteredInteger / numberOfChartersDecimal)

                        Me.calculatedTextBox.Text = thisCharterCostDecimal.ToString("C")


                    Catch ex As Exception
                        'Some random error happened here
                        MessageBox.Show("Error when calculating value", "Data calculation error", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)

                    End Try
                Catch ex As Exception
                    'If this fires (it shouldn't since the ComboBox is locked), give an error
                    MessageBox.Show("Size of charter must be an integer", "Data entry error", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)

                End Try
            Catch ex As Exception
                ' If this fires, the user has entered a non-integer for the hours
                MessageBox.Show("Hours chartered must be an integer", "Data entry error", _
                    MessageBoxButtons.OK, MessageBoxIcon.Information)
                With Me.hoursTextBox
                    .Focus()
                    .Select()
                End With

            End Try
        End If
    End Sub

    Private Sub summaryPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles summaryPrintDocument.PrintPage
        Dim printXCoorSingle As Single = e.MarginBounds.Left
        Dim printYCoorSingle As Single = e.MarginBounds.Top
        Dim printFont As New Font("New Courier", 16)
        Dim printFontBold As New Font("New Courier", 16, FontStyle.Bold)
        Dim lineHeight As Single = printFont.GetHeight
        Dim printLineStringTitle As String
        Dim printLineStringCharters As String
        Dim printLineStringRevenue As String
        Dim printLineStringAverage As String
        'Print out each line ... printing in VB.NET is horrible!
        printLineStringTitle = "Sunken Ship Yacht Charters" & ControlChars.NewLine _
                        & "by Adam Reyher" & ControlChars.NewLine & ControlChars.NewLine _
                        & "Summary Data"
        e.Graphics.DrawString(printLineStringTitle, printFontBold, Brushes.Black, _
                printXCoorSingle, printYCoorSingle)
        printYCoorSingle += lineHeight * 2

        printLineStringCharters = "Number of Charters: " & numberOfChartersDecimal
        e.Graphics.DrawString(printLineStringCharters, printFont, Brushes.Black, _
                printXCoorSingle, printYCoorSingle + (lineHeight * 2))
        printYCoorSingle += lineHeight

        printLineStringRevenue = "Total Revenue: " & totalRevenueDecimal.ToString("C")
        e.Graphics.DrawString(printLineStringRevenue, printFont, Brushes.Black, _
                printXCoorSingle, printYCoorSingle + (lineHeight * 2))
        printYCoorSingle += lineHeight

        printLineStringAverage = "Average Hours Chartered: " & averageHoursCharteredDecimal
        e.Graphics.DrawString(printLineStringAverage, printFont, Brushes.Black, _
                printXCoorSingle, printYCoorSingle + (lineHeight * 2))
        printYCoorSingle += lineHeight
    End Sub

    Private Sub PrintSummaryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintSummaryToolStripMenuItem.Click
        'Grab the document
        Me.summaryPrintPreview.Document = Me.summaryPrintDocument
        Me.summaryPrintPreview.ShowDialog()
    End Sub

    Private Sub yachtPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles yachtPrintDocument.PrintPage
        Dim printXCoorSingle As Single = e.MarginBounds.Left
        Dim printYCoorSingle As Single = e.MarginBounds.Top
        Dim printFont As New Font("New Courier", 16)
        Dim printFontBold As New Font("New Courier", 16, FontStyle.Bold)
        Dim lineHeight As Single = printFont.GetHeight
        Dim printLineString As String
        Dim indexInteger As Integer
        'Print out each line ... printing in VB.NET is horrible!
        printLineString = "Sunken Ship Yacht Charters" & ControlChars.NewLine _
                        & "by Adam Reyher" & ControlChars.NewLine & ControlChars.NewLine _
                        & "List of Yacht Types"
        e.Graphics.DrawString(printLineString, printFontBold, Brushes.Black, _
                printXCoorSingle, printYCoorSingle)
        printYCoorSingle += lineHeight * 2

        For indexInteger = 0 To Me.typeComboBox.Items.Count - 1
            printLineString = Me.typeComboBox.Items(indexInteger).ToString
            e.Graphics.DrawString(printLineString, _
                printFont, Brushes.Black, printXCoorSingle, printYCoorSingle + (lineHeight * 2))
            printYCoorSingle += lineHeight
        Next
    End Sub

    Private Sub PrintYachtTypesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintYachtTypesToolStripMenuItem.Click
        'Grab the document
        Me.yachtPrintPreview.Document = Me.yachtPrintDocument
        Me.yachtPrintPreview.ShowDialog()
    End Sub

    Private Sub DisplayCountOfYachtTypesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayCountOfYachtTypesToolStripMenuItem.Click
        'Display a count of the yacht types
        Dim yachtCountInteger As Integer

        yachtCountInteger = Me.typeComboBox.Items.Count

        MessageBox.Show("Number of yachts: " & yachtCountInteger, "Yacht Count", _
            MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

End Class