PROGRAMMING EXERCISE

Car Rental Calculation

The Edsel Car Rental Company rents Edsels for \$35 per day plus 10 cents a mile.  Write a program for the rental company so that the clerk can enter the date that the car was rented, the date that the car was returned, the mileage when the car was rented, and the mileage when the car was returned.  Calculate the total charges and display the results.

Note: Use the DateDiff function to determine the number of days that the car was rented.

Before you start this program, do the following tutorial to demonstrate the use of the DateDiff function.

STEP 1: Create a form that looks like the following.  Name the controls as indicated on the call-outs.

STEP 2:  Place the following code in the btnDaysDiff_Click event:

Private Sub btnDaysDiff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDaysDiff.Click

Dim dtmDate1 As Date

Dim dtmDate2 As Date

Dim intDays As Integer

If Not IsDate(txtDate1.Text) Then

MsgBox("Please enter a valid date for Date 1.", _

MsgBoxStyle.Exclamation, _

"Date Rented")

txtDate1.Focus()

Exit Sub

End If

If Not IsDate(txtDate2.Text) Then

MsgBox("Please enter a valid date for Date 2.", _

MsgBoxStyle.Exclamation, _

"Date Returned")

txtDate2.Focus()

Exit Sub

End If

dtmDate1 = CDate(txtDate1.Text)

dtmDate2 = CDate(txtDate2.Text)

intDays = DateDiff(DateInterval.Day, dtmDate1, dtmDate2)

lblDaysDiff.Text = CStr(intDays)

End Sub

An explanation of the code above follows:

First, two Date variables, dtmDate1 and dtmDate2 are declared.  These will store the two dates we are interested in.  An Integer variable intDays is declared to store the number of days difference between the two dates.

The statements below use the IsDate function to test whether or not the values entered in the textboxes are valid dates. The IsDate function recognizes dates in many different formats, such as "11/29/99", "November 29, 1999", "29-Nov-99", and will therefore determine that such strings are valid dates. In the statements below, if the IsDate function does not recognize the string as a valid date, a message box will prompt the user to enter a valid date and the procedure will exit early.

If Not IsDate(txtDate1.Text) Then

MsgBox("Please enter a valid date for Date 1.", _

MsgBoxStyle.Exclamation, _

"Date Rented")

txtDate1.Focus()

Exit Sub

End If

If Not IsDate(txtDate2.Text) Then

MsgBox("Please enter a valid date for Date 2.", _

MsgBoxStyle.Exclamation, _

"Date Returned")

txtDate2.Focus()

Exit Sub

End If

The statements

dtmDate1 = CDate(txtDate1.Text)

dtmDate2 = CDate(txtDate2.Text)

use the CDate function to convert the dates entered in the textboxes to Date datatypes.  (The data type of data entered in textboxes is String, and those strings must be converted to Date data types in order for the DateDiff function to work).  Since these values have been previously validated with the IsDate function, the CDate function should have no problem converting these strings into internal Date datatypes.

The statement

intDays = DateDiff(DateInterval.Day, dtmDate1, dtmDate2)

executes the DateDiff function and stores the result in the intDays variable.  The DateDiff function takes three main arguments: The first argument specifies the interval of time (days, weeks, months, quarters, years, etc.) used to calculate the difference between the two dates.  In this case we used DateInterval.Day because we are interested in days.  The second and third arguments specify the two dates to be compared.  As a rule of thumb, Date1 should be earlier than Date2.

The statement

lblDaysDiff.Text = CStr(intDays)

causes the result to be displayed in the label.

STEP 3:   Run the program and test it with various dates:

A sample run of the rent-a-car program should look something like the following:

Note: The picture of the cars on the form as shown above is for aesthetic purposes only and has no impact on the functionality of the program. However, to display a picture on the form like this, use the PictureBox control, click the ellipsis button (...) of its Image property, then follow the dialog boxes to import a local resource (which is a graphic file in any number of formats, such as .ico, .jpg, .png, .bmp, etc.). The image used in this examples is "CARS.ICO", which is included in the download for this sample project. The Express version of Visual Basic 2010 does not come with a set of icons for use in application programs, however the professional version of Visual Studio 2010 does come with a graphics library. If you have the professional version, you can find these images are zipped up (VS2010ImageLibrary.zip) under ..\Program Files\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary\1033\. Any icons used in any sample program on  thevbprogrammer.com will be included in the download for that sample program. As far as finding images to use in your own programs, there are a number of websites that have free icon sets available, and there are also many professional icon sets available for a cost.