Home

File-Based Applications:
Bethesda Car Rental

 

Cars

Cars are at the center of the rental transactions of our company. A car is the main reason a customer comes to the business. In our application, we will provide all the necessary information related to a car such as its make, model, year, picture, and whether it is available. Because we know that sometimes when renting or choosing a car, a customer may want to know the options available on a particular car, we will also list these basic pieces of information. Finally, we will mark a car as available or not. This will allow the clerk processing an order to know whether the customer can rent the car or not.

We will create two forms related to cars. One form will be used to enter a new car when the company acquires one. On the other hand, when interviewing a customer, if the clerk wants to see a list of the company cars, we will create a form that can help with this, allowing the clerk to navigate among cars for a review.

 

Practical Learning Practical Learning: Processing Cars

  1. Copy the following pictures to the bin sub-folder inside the main folder of the current project (Save them with their default names):
     
  2. Return to your programming environment
  3. To add a new form to the application, on the main menu, click Project -> Add Windows Forms
  4. Set the Name to NewCar and press Enter
  5. Design the form as follows: 
     
    Bethesda Car Rental - New Car
    Control Text Name Other Properties
    Label Text #    
    TextBox   txtTagNumber  
    Label Make:    
    TextBox   txtMake  
    Label Model:    
    TextBox   txtModel  
    Label Year:    
    TextBox   txtYear  
    Label Category:    
    ComboBox   cboCategory DropDownStyle: DropDownList
    Items: Economy
    Compact
    Standard
    Full Size
    Mini Van
    SUV
    Truck
    Van
    CheckBox Cassete Player chkK7Player CheckAlign: MiddleRight
    CheckBox DVD Player chkDVDPlayer CheckAlign: MiddleRight
    CheckBox CD Player chkCDPlayer CheckAlign: MiddleRight
    CheckBox Available chkAvailable CheckAlign: MiddleRight
    PictureBox   pctCar SizeMode: CenterImage
    Label Select Car Picture:    
    ComboBox none.gif cboPictures  
    Button Add Car btnAddCar  
    Button Close btnClose DialogResult: OK
    Form     FormBorderStyle: FixedDialog
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskbar: False
  6. To add a new class to the project, on the main menu, click Project -> Add Class...
  7. Set the Class Name to Car and click Open
  8. Access the Car.cs file and change it as follows:
     
    <Serializable()> Public NotInheritable Class Car
        Public TagNumber As String
        Public Make As String
        Public Model As String
        Public Year As Integer
        Public Category As String
        Public HasK7Player As Integer
        Public HasCDPlayer As Integer
        Public HasDVDPlayer As Integer
        Public PictureName As String
        Public IsAvailable As Integer
    
        Public Sub New()
            TagNumber = "000-000"
            Make = "Make"
            Model = "Model"
            Year = 1960
            Category = "Small"
            HasK7Player = 0
            HasCDPlayer = 0
            HasDVDPlayer = 0
            PictureName = ""
            IsAvailable = 0
        End Sub
    
        Public Sub New(ByVal tag As String, ByVal mk As String, _
                       ByVal mdl As String, ByVal yr As Integer, _
                       ByVal cat As String, ByVal k7 As Integer, _
                       ByVal cd As Integer, ByVal dvd As Integer, _
                       ByVal pct As String, ByVal avl As Integer)
    
            TagNumber = tag
            Make = mk
            Model = mdl
            Year = yr
            Category = cat
            HasK7Player = k7
            HasCDPlayer = cd
            HasDVDPlayer = dvd
            PictureName = pct
            IsAvailable = avl
        End Sub
    End Class
  9. Return to the Customers form. Right-click it and click View Code
  10. In the top section of the file, type:
     
    Imports System.IO
    Imports System.Runtime.Serialization.Formatters.Soap
    
    Public Class NewCar
        Inherits System.Windows.Forms.Form
    
        Private lstCars As ArrayList
    
  11. In the Class Name combo box, select (NewCar Events)
  12. In the Method Name combo box, select Load and implement its event as follows:
     
    Private Sub NewCar_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            lstCars = New ArrayList
            Dim strFilename As String = "Cars.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)
                lstCars = bcrSoap.Deserialize(bcrStream)
                bcrStream.Close()
            Else
                Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
                bcrSoap.Serialize(bcrStream, lstCars)
                bcrStream.Close()
            End If
    
            ' Locate the director that contains the current application
            Dim dirInfo As DirectoryInfo = New DirectoryInfo(".\\")
    
            ' Get a reference to each file in that directory
            Dim lstFiles As FileInfo() = dirInfo.GetFiles()
    
            ' Display the names of the graphics files
            For Each fi As FileInfo In lstFiles
                If fi.Extension.Equals(".gif") Or _
                   fi.Extension.Equals(".jpeg") Or _
                   fi.Extension.Equals(".jpg") Or _
                   fi.Extension.Equals(".bmp") Or _
                   fi.Extension.Equals(".png") Then
                    cboPictures.Items.Add(fi.Name)
                End If
            Next
    
            cboPictures.Text = "none.gif"
    End Sub
  13. In the Class Name combo box, select cboPictures
  14. In the Method Name combo box, select SelectedIndexChanged and implement the event as follows:
     
    Private Sub cboPictures_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPictures.SelectedIndexChanged
            Me.pctCar.Image = Image.FromFile(cboPictures.Text)
    End Sub
  15. In the Class name combo box, select btnAddCar
  16. In the Method Name combo box, select Click and implement it as follows:
     
    Private Sub btnAddCar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCar.Click
            Dim vehicle As Car = New Car
    
            vehicle.TagNumber = Me.txtTagNumber.Text
            vehicle.Make = Me.txtMake.Text
            vehicle.Model = Me.txtModel.Text
            vehicle.Year = CInt(Me.txtYear.Text)
            vehicle.Category = Me.cboCategory.Text
            If Me.chkK7Player.Checked = True Then
                vehicle.HasK7Player = 1
            Else
                vehicle.HasK7Player = 0
            End If
    
            If Me.chkCDPlayer.Checked = True Then
                vehicle.HasCDPlayer = 1
            Else
                vehicle.HasCDPlayer = 0
            End If
    
            If Me.chkDVDPlayer.Checked = True Then
                vehicle.HasDVDPlayer = 1
            Else
                vehicle.HasDVDPlayer = 0
            End If
    
            vehicle.PictureName = cboPictures.Text
    
            If Me.chkAvailable.Checked = True Then
                vehicle.IsAvailable = 1
            Else
                vehicle.IsAvailable = 0
            End If
    
            lstCars.Add(vehicle)
    
            Dim strFilename As String = "Cars.bcr"
            Dim bcrSoap As SoapFormatter = New SoapFormatter
            Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
            bcrSoap.Serialize(bcrStream, lstCars)
            bcrStream.Close()
    
            Me.txtTagNumber.Text = ""
            Me.txtMake.Text = ""
            Me.txtModel.Text = ""
            Me.txtYear.Text = "1960"
            Me.cboCategory.SelectedIndex = 0
            Me.chkK7Player.Checked = False
            Me.chkCDPlayer.Checked = False
            Me.chkDVDPlayer.Checked = False
            cboPictures.Text = "none.gif"
            Me.chkAvailable.Checked = False
            Me.pctCar.Image = Image.FromFile("none.gif")
            Me.txtTagNumber.Focus()
    End Sub
  17. Display the first form, Switchboard.vb [Design]. Add a Button to the form and change its properties as follows:
    (Name): btnNewCar
    Text: New Car
  18. Double-click the New Car button to generate its Click event
  19. Implement the event as follows:
     
    Private Sub btnNewCar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewCar.Click
            Dim car As NewCar = New NewCar
            car.ShowDialog()
    End Sub
  20. Execute the application
  21. Create a few cars as follows:
     
  22. Close the forms and return to your programming environment
  23. To add a new form to the application, on the main menu, click Project -> Add Windows Forms
  24. Set the Name to Cars and press Enter
  25. Design the form as follows: 
     
    Control Text Name Other Properties
    Label Make:    
    TextBox   txtMake  
    Label Model:    
    TextBox   txtModel  
    Label Year:    
    TextBox   txtYear  
    Label Category:    
    TextBox   txtCategory  
    CheckBox Cassete Player chkK7Player CheckAlign: MiddleRight
    CheckBox DVD Player chkDVDPlayer CheckAlign: MiddleRight
    CheckBox CD Player chkCDPlayer CheckAlign: MiddleRight
    CheckBox Available chkAvailable CheckAlign: MiddleRight
    Label Tag #:    
    TextBox   txtTagNumber  
    PictureBox   pctCar  
    Button First btnFirst  
    Button Previous btnPrevious  
    Button Next btnNext  
    Button Last btnLast  
    Button Close btnClose  
    Form     MaximizeBox: False
    StartPosition: CenterScreen
  26. Right-click the form and click View Code
  27. In the top section of the file, type:
     
    Imports System.IO
    Imports System.Runtime.Serialization.Formatters.Soap
    
    Public Class Cars
        Inherits System.Windows.Forms.Form
    
        Private lstCars As ArrayList
        Private CurrentPosition As Integer
  28. In the Class Name combo box, select (Cars Events)
  29. In the Method Name combo box, select Load and implement the event as follows:
     
    Private Sub Cars_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            lstCars = New ArrayList
            CurrentPosition = 0
    
            Dim strFilename As String = "Cars.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)
                lstCars = bcrSoap.Deserialize(bcrStream)
    
                bcrStream.Close()
                Me.btnFirst_Click(sender, e)
            End If
    End Sub
    
    Private Sub ShowCarInformation(ByVal vehicle As Car)
            Me.txtTagNumber.Text = vehicle.TagNumber
            Me.txtMake.Text = vehicle.Make
            Me.txtModel.Text = vehicle.Model
            Me.txtYear.Text = vehicle.Year.ToString()
            Me.txtCategory.Text = vehicle.Category
    
            If vehicle.HasK7Player = 1 Then
                Me.chkK7Player.Checked = True
            Else
                Me.chkK7Player.Checked = False
            End If
    
            If vehicle.HasCDPlayer = 1 Then
                Me.chkCDPlayer.Checked = True
            Else
                Me.chkCDPlayer.Checked = False
            End If
    
            If vehicle.HasDVDPlayer = 1 Then
                Me.chkDVDPlayer.Checked = True
            Else
                Me.chkDVDPlayer.Checked = False
            End If
            Dim strPictureName As String = vehicle.PictureName
    
            Try
                Me.pctCar.Image = Image.FromFile(vehicle.PictureName)
            Catch exc As OutOfMemoryException
                Me.pctCar.Image = Image.FromFile("none.gif")
            End Try
    
            If vehicle.IsAvailable = 1 Then
                Me.chkAvailable.Checked = True
            Else
                Me.chkAvailable.Checked = False
            End If
    End Sub
  30. In the Class Name combo box, select btnFirst
  31. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click
            If lstCars.Count = 0 Then Exit Sub
    
            CurrentPosition = 0
            Dim vehicle As Car = New Car
    
            vehicle = Me.lstCars(CurrentPosition)
    
            ShowCarInformation(vehicle)
    End Sub
  32. In the Class Name combo box, select btnPrevious
  33. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
            If lstCars.Count = 0 Then Exit Sub
    
            If CurrentPosition = 0 Then Exit Sub
    
            CurrentPosition = CurrentPosition - 1
            Dim vehicle As Car = New Car
    
            vehicle = lstCars(CurrentPosition)
    
            ShowCarInformation(vehicle)
    End Sub
  34. In the Class Name combo box, select btnNext
  35. In the Method Name combo box, select Click and implement the Click event as follows:
     
    Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
            If lstCars.Count = 0 Then Exit Sub
    
            If CurrentPosition = lstCars.Count - 1 Then
                Exit Sub
            Else
                CurrentPosition = CurrentPosition + 1
    
                Dim vehicle As Car = New Car
    
                vehicle = lstCars(CurrentPosition)
    
                ShowCarInformation(vehicle)
            End If
    End Sub
  36. In the Class Name combo box, select btnLast
  37. In the Method Name combo box, select Click and implement the Click event as follows:
     
    Private Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click
            If lstCars.Count = 0 Then Exit Sub
    
            CurrentPosition = lstCars.Count - 1
            Dim vehicle As Car = New Car
    
            vehicle = Me.lstCars(CurrentPosition)
    
            ShowCarInformation(vehicle)
    End Sub
  38. Return to the Cars form. Double-click the Close button and implement its Click event as follows:
     
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
    End Sub
  39. Display the first form, Switchboard.vb [Design]. Add a Button to the form and change its properties as follows:
    (Name): btnCarsReview
    Text: Car Review
  40. Double-click the New Car button to generate its Click event
  41. Implement the event as follows:
     
    Private Sub btnCarsReview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCarsReview.Click
            Dim frmCars As Cars = New Cars
            frmCars.Show()
    End Sub
  42. Execute the application and review the list of cars using the Cars form
     
  43. Close the forms and return to your programming environment
 

Previous Copyright © 2005-2012 FunctionX Next