Need Visual Basics code The Westfield Carpet company has asked you to write a Vi
ID: 3575523 • Letter: N
Question
Need Visual Basics code
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.
Explanation / Answer
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
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.