Home

File-Based Applications:
Bethesda Car Rental

 

Rental Orders

To main purpose of a car rental company is to rent car. This is done by receiving orders from a customer and processing such an order. To proceed, a clerk would use a form to enter the customer and the car information. In our application, we will also make sure that name of the clerk who processed an order is specified. Other than than, we will enter as much information as possible to assist the user.

 

Practical Learning Practical Learning: Creating a Serializable Class

  1. To add a new form to the project, on the main menu, click Project -> Add Windows Form...
  2. Set the Name to RentalRates and press Enter
  3. Add a ListView to the form and create its Columns as follows:
     
    (Name) Text TextAlign Width
    colCategory Category   90
    colDaily Daily Right  
    colWeekly Weekly Right  
    colMonthly Monthly Right  
    colWeekend Weekend Right  
  4. Create its Items as follows:
     
    ListViewItem ListViewSubItem ListViewSubItem ListViewSubItem ListViewSubItem
      Text Text Text Text
    Economy 32.95 29.75 22.95 19.95
    Compact 39.95 34.75 29.95 24.95
    Standard 45.95 39.75 35.95 32.95
    Full Size 49.95 42.75 38.95 35.95
    Mini Van 55.95 50.75 45.95 42.95
    SUV 55.95 50.75 45.95 42.95
    Truck 42.75 38.75 35.95 32.95
    Van 69.95 62.75 55.95 52.95
  5. Complete the design of the form as follows:
     
  6. To add a new class to the project, on the main menu, click Project -> Add Class...
  7. Set the Class Name to RentalOrder and press Enter
  8. Access the RentalOrder.h file and change it as follows:
     
    <Serializable()> Public NotInheritable Class RentalOrder
        Public ReceiptNumber As Integer
        Public ProcessedBy As String
        Public CarSelected As String
        Public Make As String
        Public Model As String
        Public Year As Integer
        Public CarCondition As String
        Public CustDrvLicNbr As String
        Public CustName As String
        Public CustAddress As String
        Public CustCity As String
        Public CustState As String
        Public CustZIPCode As String
        Public CustCountry As String
        Public TankLevel As String
        Public Mileage As Long
        Public StartDate As DateTime
        Public EndDate As DateTime
        Public Days As Integer
        Public RateApplied As Double
        Public SubTotal As Double
        Public TaxRate As Double
        Public TaxAmount As Double
        Public OrderTotal As Double
    
        Public Sub New()
    
        End Sub
    End Class
  9. To add a new form to the project, on the main menu, click Project -> Add Windows Form
  10. Set the Name to RentalOrders and press Enter
  11. Design the form as follows:
     
    Control Text Name Other Properties
    GroupBox Order Identification    
    Label Processed By:    
    ComboBox   cboEmployees  
    GroupBox Car Selected    
    ComboBox   cboCars  
    Label Make:    
    TextBox   txtMake  
    Label Model:    
    TextBox   txtModel  
    Label Year:    
    TextBox   txtCarYear  
    label Car Condition:    
    ComboBox   cboCarConditions  
    GroupBox Customer    
    ComboBox   cboCustomers  
    Label Name:    
    TextBox   txtCustName  
    Label Address:    
    TextBox   txtCustAddress  
    TextBox   txtCustCity  
    TextBox MD txtCustState  
    TextBox   txtCustZIPCode  
    TextBox USA txtCustCountry  
    GroupBox Order Evaluation    
  12. Right-click anywhere in the form and click View Code
  13. In the top section of the file, type:
     
    Imports System.IO
    Imports System.Runtime.Serialization.Formatters.Soap
    
    Public Class RentalOrders
        Inherits System.Windows.Forms.Form
    
        Private lstRentalOrders As ArrayList
  14. In the Class Name combo box, select (CleaningOrders Events)
  15. In the Method Name combo box, select Load and implement the event as follows:
     
    Private Sub RentalOrders_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            lstRentalOrders = New ArrayList
            Dim lstEmployees As ArrayList = New ArrayList
            Dim bcrSoap As SoapFormatter = New SoapFormatter
            Dim strFilename As String = "Employees.bcr"
    
            If File.Exists(strFilename) Then
                Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                lstEmployees = bcrSoap.Deserialize(bcrStream)
                bcrStream.Close()
    
                Dim firstName As String, lastName As String, title As String
    
                cboEmployees.Items.Clear()
    
                For Each empl As Employee In lstEmployees
                    firstName = empl.FirstName
                    lastName = empl.LastName
                    title = empl.Title
    
                    cboEmployees.Items.Add(lastName & ", " & firstName & " - " & title)
                Next
            End If
    
            strFilename = "Cars.bcr"
            Dim lstCars As ArrayList = New ArrayList
    
            If File.Exists(strFilename) Then
                Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                lstCars = bcrSoap.Deserialize(bcrStream)
                bcrStream.Close()
    
                cboCars.Items.Clear()
    
                For Each vehicle As Car In lstCars
                    cboCars.Items.Add(vehicle.TagNumber)
                Next
            End If
    
            strFilename = "Customers.bcr"
            Dim lstCustomers As ArrayList = New ArrayList
    
            If File.Exists(strFilename) Then
                Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                lstCustomers = bcrSoap.Deserialize(bcrStream)
                bcrStream.Close()
    
                cboCustomers.Items.Clear()
    
                For Each cust As Customer In lstCustomers
                    cboCustomers.Items.Add(cust.DrvLicNbr)
                Next
            End If
    End Sub
  16. In the Class Name combo box, select cboCars
  17. In the Method Name combo box, select SelectedIndexChanged and implement the event as follows:
     
    Private Sub cboCars_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCars.SelectedIndexChanged
            Dim strFilename As String = "Cars.bcr"
            Dim lstCars As ArrayList = New ArrayList
            Dim bcrSoap As SoapFormatter = New SoapFormatter
    
            Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
            lstCars = bcrSoap.Deserialize(bcrStream)
            bcrStream.Close()
    
            For Each vehicle As Car In lstCars
                If vehicle.TagNumber = cboCars.Text Then
                    Me.txtMake.Text = vehicle.Make
                    Me.txtModel.Text = vehicle.Model
                    Me.txtCarYear.Text = vehicle.Year.ToString()
                    Exit Sub
                End If
            Next
    End Sub
  18. In the Class Name combo box, select cboCustomers
  19. In the Method Name combo box, select SelectedIndexChanged and implement the event as follows:
     
    Private Sub cboCustomers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCustomers.SelectedIndexChanged
            Dim strFilename As String = "Customers.bcr"
            Dim lstCustomers As ArrayList = New ArrayList
            Dim bcrSoap As SoapFormatter = New SoapFormatter
    
            Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
            lstCustomers = bcrSoap.Deserialize(bcrStream)
            bcrStream.Close()
    
            For Each cust As Customer In lstCustomers
                If cust.DrvLicNbr = cboCustomers.Text Then
                    Me.txtCustName.Text = cust.FullName
                    Me.txtCustAddress.Text = cust.Address
                    Me.txtCustCity.Text = cust.City
                    Me.txtCustState.Text = cust.State
                    Me.txtCustZIPCode.Text = cust.ZIPCode
                    Me.txtCustCountry.Text = cust.Country
                    Exit Sub
                End If
            Next
    End Sub
  20. In the Class Name combo box, select btnRateApplied
  21. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnRateApplied_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRateApplied.Click
            Dim frmRates As RentalRates = New RentalRates
            frmRates.Show()
    End Sub
  22. In the Class Name combo box, select dtpEndDate
  23. In the Method Name combo box, select ValueChanged and implement the event as follows:
     
    Private Sub dtpEndDate_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpEndDate.ValueChanged
            Dim dteStart As DateTime = Me.dtpStartDate.Value
            Dim dteEnd As DateTime = Me.dtpEndDate.Value
            Dim tme As TimeSpan = dteEnd.Subtract(dteStart)
            Dim days As Integer = tme.Days
    
            Me.txtDays.Text = CStr(days)
    End Sub
  24. In the Class Name combo box, select txtRateApplied
  25. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtRateApplied_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRateApplied.Leave
            Dim days As Integer = CInt(Me.txtDays.Text)
            Dim rateApplied As Double = Decimal.Parse(Me.txtRateApplied.Text)
            Dim subTotal As Double = days * rateApplied
            Me.txtSubTotal.Text = subTotal.ToString("F")
            Dim taxRate As Double = Decimal.Parse(Me.txtTaxRate.Text)
            Dim taxAmount As Double = subTotal * taxRate / 100
            Me.txtTaxAmount.Text = taxAmount.ToString("F")
            Dim totalOrder As Double = subTotal + taxAmount
            Me.txtOrderTotal.Text = totalOrder.ToString("F")
    End Sub
  26. In the Class Name combo box, select btnSave
  27. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim receiptNumber As Integer = 0
            Dim lstRentalOrders As ArrayList = New ArrayList
            Dim strFilename As String = "RentalOrders.bcr"
            Dim bcrSoap As SoapFormatter = New SoapFormatter
    
            If File.Exists(strFilename) Then
                Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                lstRentalOrders = bcrSoap.Deserialize(bcrStream)
    
                Dim order As RentalOrder = lstRentalOrders(lstRentalOrders.Count - 1)
                receiptNumber = order.ReceiptNumber
                bcrStream.Close()
            Else
    
                Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
                bcrSoap.Serialize(bcrStream, lstRentalOrders)
                bcrStream.Close()
            End If
    
            Dim rntOrder As RentalOrder = New RentalOrder
    
            rntOrder.ReceiptNumber = receiptNumber + 1
            rntOrder.ProcessedBy = Me.cboEmployees.Text
            rntOrder.CarSelected = Me.cboCars.Text
            rntOrder.Make = Me.txtMake.Text
            rntOrder.Model = Me.txtModel.Text
            rntOrder.Year = CInt(Me.txtCarYear.Text)
            rntOrder.CarCondition = Me.cboCarConditions.Text
            rntOrder.CustDrvLicNbr = Me.cboCustomers.Text
            rntOrder.CustName = Me.txtCustName.Text
            rntOrder.CustAddress = Me.txtCustAddress.Text
            rntOrder.CustCity = Me.txtCustCity.Text
            rntOrder.CustState = Me.txtCustState.Text
            rntOrder.CustZIPCode = Me.txtCustZIPCode.Text
            rntOrder.CustCountry = Me.txtCustCountry.Text
            rntOrder.TankLevel = Me.cboTankLevels.Text
            rntOrder.Mileage = CLng(Me.txtMileage.Text)
            rntOrder.StartDate = Me.dtpStartDate.Value
            rntOrder.EndDate = Me.dtpEndDate.Value
            rntOrder.Days = CInt(Me.txtDays.Text)
            rntOrder.RateApplied = CDbl(Me.txtRateApplied.Text)
            rntOrder.SubTotal = CDbl(Me.txtSubTotal.Text)
            rntOrder.TaxRate = CDbl(Me.txtTaxRate.Text)
            rntOrder.TaxAmount = CDbl(Me.txtTaxAmount.Text)
            rntOrder.OrderTotal = CDbl(Me.txtOrderTotal.Text)
    
            lstRentalOrders.Add(rntOrder)
    
            Dim stmOrders As FileStream = New FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
            bcrSoap.Serialize(stmOrders, lstRentalOrders)
            stmOrders.Close()
    
            Me.cboEmployees.Text = ""
            Me.cboCars.Text = ""
            Me.txtMake.Text = ""
            Me.txtModel.Text = ""
            Me.txtCarYear.Text = "2000"
            Me.cboCarConditions.Text = ""
            Me.cboCustomers.Text = ""
            Me.txtCustAddress.Text = ""
            Me.txtCustAddress.Text = ""
            Me.txtCustCity.Text = ""
            Me.txtCustState.Text = "MD"
            Me.txtCustZIPCode.Text = ""
            Me.txtCustCountry.Text = "USA"
            Me.cboTankLevels.Text = ""
            Me.txtMileage.Text = ""
            Me.dtpStartDate.Value = DateTime.Now
            Me.dtpEndDate.Value = DateTime.Now
            Me.txtDays.Text = "0"
            Me.txtRateApplied.Text = "24.95"
            Me.txtSubTotal.Text = "0.00"
            Me.txtTaxRate.Text = "7.75"
            Me.txtTaxAmount.Text = "0.00"
            Me.txtOrderTotal.Text = "0.00"
            Me.cboEmployees.Focus()
    End Sub
  28. Display the first form, Switchboard, and complete its design as follows:
     
    Control Name Text
    Button btnRentalOrders Rental Orders
    Button btnEmployees Employees
    Button btnCustomers Customers
  29. Double-click the Rental Orders button and implement its Click event as follows:
     
    Private Sub btnRentalOrders_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRentalOrders.Click
            Dim frmOrders As RentalOrders = New RentalOrders
            frmOrders.Show()
    End Sub
  30. In the Class Name combo box, select btnClose
  31. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
            End
    End Sub
  32. Execute the application and create a few rental orders
     
  33. Close the forms and return to your programming environment
  34. Return to the RentalOrders form and double-click the Open button to implement its Click event as follows:
     
    Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
            Dim lstRentalOrders As ArrayList = New ArrayList
            Dim strFilename As String = "RentalOrders.bcr"
            Dim bcrSoap As SoapFormatter = New SoapFormatter
            Dim receiptNumber As Integer = CInt(Me.txtReceiptNumber.Text)
    
            If File.Exists(strFilename) Then
                Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                lstRentalOrders = bcrSoap.Deserialize(bcrStream)
    
                bcrStream.Close()
    
                For Each order As RentalOrder In lstRentalOrders
                    If order.ReceiptNumber = receiptNumber Then
                        Me.cboEmployees.Text = order.ProcessedBy
                        Me.cboCars.Text = order.CarSelected
                        Me.txtMake.Text = order.Make
                        Me.txtModel.Text = order.Model
                        Me.txtCarYear.Text = CStr(order.Year)
                        Me.cboCarConditions.Text = order.CarCondition
                        Me.cboCustomers.Text = order.CustDrvLicNbr
                        Me.txtCustName.Text = order.CustName
                        Me.txtCustAddress.Text = order.CustAddress
                        Me.txtCustCity.Text = order.CustCity
                        Me.txtCustState.Text = order.CustState
                        Me.txtCustZIPCode.Text = order.CustZIPCode
                        Me.txtCustCountry.Text = order.CustCountry
                        Me.cboTankLevels.Text = order.TankLevel
                        Me.txtMileage.Text = CStr(order.Mileage)
                        Me.dtpStartDate.Value = order.StartDate
                        Me.dtpEndDate.Value = order.EndDate
                        Me.txtDays.Text = CStr(order.Days)
                        Me.txtRateApplied.Text = CStr(order.RateApplied)
                        Me.txtSubTotal.Text = CStr(order.SubTotal)
                        Me.txtTaxRate.Text = CStr(order.TaxRate)
                        Me.txtTaxAmount.Text = CStr(order.TaxAmount)
                        Me.txtOrderTotal.Text = CStr(order.OrderTotal)
                    End If
                Next
            Else
                MsgBox("There is no rental order with that receipt number!")
                Exit Sub
            End If
    End Sub
  35. In the Class Name combo box, select btnClose
  36. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
    End Sub
  37. Execute the application and try opening previous created orders
 

Previous Copyright © 2005-2016, FunctionX