Block of Code

Practical Examples for Programmers

  • Home
  • C++
  • Java
  • Visual Basic
  • Discrete Math
  • Ask a Question

Rental Cost

The table below gives the price schedule for Eddie’s Equipment Rental. Full-day rentals cost one-and-a-half times the half-day rentals. In Visual Basic, write a program that displays the table in a list box when an appropriate button is clicked on and displays a bill in another list box based on the item number and time period chosen by a customer. The bill should include a $30.00 deposit.  A sample output is shown below.

Piece of Equipment / Half-Day / Full-Day

1. Rug cleaner  / $16.00  / $24.00

2. Lawn mower / $12.00  / $18.00

3. Paint sprayer / $20.00  / $30.00

Rental Cost
Suggested Control Names and Attributes:

Name Property Text Property Control Type Notes
 frmRentalCost Welcome To Eddie’s Equipment Rentals Form Holds Controls
 btnDisplayRates Display Rental Rates Button Triggers event to display rental rates
 lstRates N/A ListBox Displays rental rates
 mtbItemNum N/A MaskedTextBox Captures item #
 mtbDuration N/A Button Triggers event to display bill
 lstBill N/A ListBox Displays bill

Write the Code:

' Project: Rental Cost
' Description: Calculates the total cost for rental equipment
' User chooses options for rental equipment and length of rental
' Total cost is computed and output

Public Class frmRentalCost

   Private Sub btnDisplayRates_Click(sender As Object, e As EventArgs) Handles btnDisplayRates.Click
      lstRates.Items.Clear()
      lstRates.Items.Add("Price of Equipment Half-Day Full-Day")
      lstRates.Items.Add("1. Rug cleaner $16.00 $24.00")
      lstRates.Items.Add("2. Lawn mower $12.00 $18.00")
      lstRates.Items.Add("3. Paint sprayer $20.00 $30.00")
   End Sub

   Private Sub btnDisplayBill_Click(sender As Object, e As EventArgs) Handles btnDisplayBill.Click
      ' Declare and initialize local variables
      Dim duration As String = " "
      Dim itemNum As Integer = CInt(mtbItemNum.Text)
      Dim itemCost As Integer = 0
      Dim totalCost As Integer = 0

      'Display bill based on input data
      lstBill.Items.Clear()
      lstBill.Items.Add("Receipt from Eddie's Equipment Rental")
      lstBill.Items.Add(" ")
      If (mtbDuration.Text = "H") Or (mtbDuration.Text = "h") Then
         ' display prices for half day rental
         duration = "H"
         Select Case itemNum
            Case 1
               lstBill.Items.Add("Rug cleaner: $16.00 (Half Day Rental)")
               itemCost = 16
            Case 2
               lstBill.Items.Add("Lawn mower $12.00 (Half Day Rental)")
               itemCost = 12
            Case 3
               lstBill.Items.Add("Paint sprayer $20.00 (Half Day Rental)")
               itemCost = 20
            Case Else
               MessageBox.Show("You have entered an invalid item number")
         End Select
      ElseIf (mtbDuration.Text = "F") Or (mtbDuration.Text = "f") Then
         ' display prices for full day rental
         duration = "F"
         Select Case itemNum
            Case 1
               lstBill.Items.Add("Rug cleaner: $24.00 (Full Day Rental)")
               itemCost = 24
            Case 2
               lstBill.Items.Add("Lawn mower $18.00 (Full Day Rental)")
               itemCost = 18
            Case 3
               lstBill.Items.Add("Paint sprayer $30.00 (Full Day Rental)")
               itemCost = 30
         End Select
      Else
         MessageBox.Show("You have entered an invalid duration")
      End If

      ' Calculate and display the total
      totalCost = itemCost + 30
      lstBill.Items.Add("Deposit: = $30.00")
      lstBill.Items.Add(" ")
      lstBill.Items.Add("Total: " & totalCost.ToString("C"))
   End Sub

End Class

Secondary Sidebar

This is the secondary sidebar

Copyright © 2023 · Metro Pro Theme on Genesis Framework · WordPress · Log in