Block of Code

Practical Examples for Programmers

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

Carpet Price Calculator

The Westfield Carpet company has asked you to write a Visual Basic application that calculates the price of carpeting. To calculate the price of the carpet, you multiply the area of the floor (width x Length) by the price per square foot of the carpet. For example, the area of a floor this is 12 feet long and 10 feet wide is 120 feet to cover that floor with carpet that cost $8 per square foot would cost $960. You should create a class named rectangle with the following properties as doubles: width, length, Area. The area property should be read-only. Provide a method named CalcArea that calculates the x length and stores the result in the area property. Next, create a class named carpet with the following properties: Color (string), style (string) and price (double).  The application should have a form similar to the one shown in the figure below.  (the carpet price is the price per square foot.) When the calculate button is clicked, the application should copy the data in the text boxes into the appropriate object properties, and then display the area and price.

Carpet Price Calculator

Suggested Control Names and Attributes:

Name Property Text Property Container Control Type Notes
frmCarpetPrice Carpet Price Calculator N/A Form Holds Controls
grpCarpetData Carpet Data frmCarpetPrice GroupBox Holds controls for carpet data
txtColor grpCarpetData TextBox Captures color of carpet
txtStyle grpCarpetData TextBox Captures style of carpet
txtPrice grpCarpetData TextBox Captures carpet price (per square foot)
grpRoomSize Room Size frmCarpetPrice GroupBox Holds controls for room size
txtWidth grpRoomSize TextBox Captures width of room
txtLength grpRoomSize TextBox Captures length of room
lblArea grpRoomSize Label Display’s area of room
lblTotal frmCarpetPrice  Label Displays total cost of carpet
btnCalculate &Calculate frmCarpetPrice Button Triggers event to calculate area and price
btnClear C&lear frmCarpetPrice Button Triggers event to clear form
btnExit E&xit frmCarpetPrice Button Triggers event to close application

Write the Code for frmCarpet:

' Project: Carpet Price Calculator
' Description:  This is the code for the Carpet Class

Public Class Carpet
    Private m_Color As String
    Private m_Style As String
    Private m_Price As Double

    Public Sub New()
        m_Color = ""
        m_Style = ""
        m_Price = 0.0
    End Sub

    Public Property Color As String
        Get
            Return m_Color
        End Get
        Set(value As String)
            m_Color = value
        End Set
    End Property


    Public Property Style As String
        Get
            Return m_Style
        End Get
        Set(value As String)
            m_Style = value
        End Set
    End Property

    Public Property Price As Double
        Get
            Return m_Price
        End Get
        Set(value As Double)
            m_Price = value
        End Set
    End Property

End Class

Write the Code for frmCarpetPrice:

' Project: Carpet Price Calculator
' Description:  User inputs details about the desired carpet and room dimensions.  Details are assigned
'    to two classes (Carpet and Rectangle).  Total area and total price are calculated and displayed.

Public Class frmCarpetPrice
    ' Declare local variables
    Dim objRectangle As Rectangle ' objRectangle is an object of class Rectangle
    Dim objCarpet As Carpet ' objCarpet is an object of class Carpet
    Dim validWidth As Boolean = False  ' variable to determine if input is valid
    Dim validLength As Boolean = False  ' variable to determine if input is valid
    Dim validPrice As Boolean = False  ' variable to determine if input is valid

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim totalPrice As Double = 0
        objRectangle = New Rectangle()  ' create an instance of rectangle
        objCarpet = New Carpet() ' create an instance of carpet
        If (txtColor.Text = "") Or (txtStyle.Text = "") Or (txtPrice.Text = "") Or
            (txtWidth.Text = "") Or (txtLength.Text = "") Then
            MessageBox.Show("One or more fields is blank.  Please try again.")
        Else
            GetCarpetData(objCarpet)
            GetRoomData(objRectangle)
        End If
        If validWidth And validLength And validPrice Then
            totalPrice = objCarpet.Price * objRectangle.Area
            lblTotal.Text = (totalPrice).ToString("C")
        End If
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        ' Clears previous input from the form
        txtColor.Text = ""
        txtStyle.Text = ""
        txtPrice.Text = ""
        txtWidth.Text = ""
        txtLength.Text = ""
        lblArea.Text = ""
        lblTotal.Text = ""
        txtColor.Focus()
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        ' Ends the program when the user clicks on the exit button
        End
    End Sub

    Sub GetRoomData(objRectangle As Rectangle)
        ' Assigns values from the form to the instance of Rectangle
        validWidth = False ' initialize to false in case of prior input
        validLength = False ' initialize to false in case of prior input
        Try
            If (CDbl(txtWidth.Text) > 0) Then
                objRectangle.Width = CDbl(txtWidth.Text)
                validWidth = True
            Else
                MessageBox.Show("Please enter a positive number for width.")
                txtWidth.Text = ""
                validWidth = False
            End If
        Catch ex As Exception
            MessageBox.Show("Please enter a positive number for width.")
            validWidth = False
        End Try
        Try
            If (CDbl(txtLength.Text) > 0) Then
                objRectangle.Length = CDbl(txtLength.Text)
                validLength = True
            Else
                MessageBox.Show("Please enter a positive number for length.")
                txtLength.Text = ""
                validLength = False
            End If
        Catch ex As Exception
            MessageBox.Show("Please enter a positive number for length.")
            validLength = False
        End Try
        If validWidth And validLength Then
            lblArea.Text = CStr(objRectangle.Area)
        End If
    End Sub

    Sub GetCarpetData(objCarpet As Carpet)
        ' Assigns values from the form to the instance of Carpet
        validPrice = False ' initialize to false in case of prior input
        objCarpet.Color = txtColor.Text
        objCarpet.Style = txtStyle.Text
        Try
            If (CDbl(txtPrice.Text) >= 0) Then
                objCarpet.Price = CDbl(txtPrice.Text)
                validPrice = True
            Else
                MessageBox.Show("Price cannot be negative.")
                txtPrice.Text = ""
                validPrice = False
            End If
        Catch ex As Exception
            MessageBox.Show("Please enter a non-negative number for price.")
            validPrice = False
        End Try
    End Sub
End Class

Write the Code for frmRectangle:

' Project: Carpet Price Calculator
' Description:  This is the code for the Rectangle Class

Public Class Rectangle
    Private m_Width As Double
    Private m_Length As Double
    Private m_Area As Double

    Public Sub New()
        m_Width = 0.0
        m_Length = 0.0
        m_Area = 0.0
    End Sub

    Public Property Width As Double
        Get
            Return m_Width
        End Get
        Set(value As Double)
            m_Width = value
        End Set
    End Property

    Public Property Length As Double
        Get
            Return m_Length
        End Get
        Set(value As Double)
            m_Length = value
        End Set
    End Property

    ReadOnly Property Area As Double
        Get
            CalcArea()
            Return m_Area
        End Get
    End Property

    Sub CalcArea()
        m_Area = m_Width * m_Length
    End Sub

End Class

Secondary Sidebar

This is the secondary sidebar

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