Programming Challenge #5 in Starting Out with Visual Basic 7th Edition: I\'m try
ID: 3821205 • Letter: P
Question
Programming Challenge #5 in Starting Out with Visual Basic 7th Edition: I'm trying to work on this question, but I'm really stuck on it and trying to make the previous version as it states in the instructions got me stuck too. Any help is appreciated! I've made the GUI just like the picture in the bottom of the post. Programming Challenge 4 in Chapter 5 asked you to create an application that measured room occupancy percentages in a hotel. Briefly, it was described this way: Create an application that calculates the occupancy rate for each floor, and the overall occupancy rate for the hotel. The occupancy rate is the percentage of rooms occupied, and may be calculated by dividing the number of rooms occupied by the number of rooms. The implementation of this application in Chapter 5 was constrained by the inability to use arrays and lists, so we suggested that you automatically increment the combo box index to select floors in strict ascending sequence (1, 2, 3, etc.). For this assignment, modify your previous application so that it uses an array (or a list) to hold the occupancy counts for all of the floors (8 floors, 30 rooms on each floor). This change to the program code will allow the user to select floor numbers in the combo boc in any order. In Figure 8-50, for example, the user has selected the second and sixth floors, entering occupancy counts for each. In the figure, the user also clicked the Totals button. The ListBox was updated each time the user entered an occupancy count and clicked the Save button. The user may select floors in any order, even replacing a value for a floor that was entered before.Please work from the code below that I have so far. A list box and combo box is used. I need to finish this application while using try & catch and using " Do While Loop"
Public Class Form1 Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click 'Declare Variables and Constant Const NUMBER_ROOMS_PER_FLOOR As Integer = 30 Dim intRoomsOccupied As Integer Dim dblOccupancyRate As Double Dim strTemp As String Dim intCounter As Integer
'Check for user input
Try 'Do calculation and display 'cboFloor.SelectedIndex = 0 Do While intCounter <= 8 intRoomsOccupied = CInt(txtNumberOfOccupiedRooms.Text) dblOccupancyRate = intRoomsOccupied / NUMBER_ROOMS_PER_FLOOR 'Assign calculation result as string to strTemp strTemp = "Floor" & cboFloor.SelectedIndex.ToString() & "Rooms Occupied" & intRoomsOccupied & "Occupancy Rate" & dblOccupancyRate.ToString("p") lstFloorOccupancyData.Items.Add(strTemp) txtNumberOfOccupiedRooms.Text = String.Empty txtNumberOfOccupiedRooms.Focus() cboFloor.SelectedIndex = cboFloor.SelectedIndex + 1 intCounter += 1 Loop 'Display Catch ex As Exception
End Try End Sub
End Class
Photos from the problem are below. Public Class Form1 Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click 'Declare Variables and Constant Const NUMBER_ROOMS_PER_FLOOR As Integer = 30 Dim intRoomsOccupied As Integer Dim dblOccupancyRate As Double Dim strTemp As String Dim intCounter As Integer
'Check for user input
Try 'Do calculation and display 'cboFloor.SelectedIndex = 0 Do While intCounter <= 8 intRoomsOccupied = CInt(txtNumberOfOccupiedRooms.Text) dblOccupancyRate = intRoomsOccupied / NUMBER_ROOMS_PER_FLOOR 'Assign calculation result as string to strTemp strTemp = "Floor" & cboFloor.SelectedIndex.ToString() & "Rooms Occupied" & intRoomsOccupied & "Occupancy Rate" & dblOccupancyRate.ToString("p") lstFloorOccupancyData.Items.Add(strTemp) txtNumberOfOccupiedRooms.Text = String.Empty txtNumberOfOccupiedRooms.Focus() cboFloor.SelectedIndex = cboFloor.SelectedIndex + 1 intCounter += 1 Loop 'Display Catch ex As Exception
End Try End Sub
End Class
Photos from the problem are below.
4. Hotel Occupancy floor. Create an application The EIGrande Hotel has 8 floors and 30 rooms on each overall occupancy rate for that calculates the occupancy rate for each floor, and the the hotel. The occupancy rate is the percentage of rooms occupied, and may be calcu lated by dividing the number of rooms occupied by the number of rooms. For exam- ple, if 18 rooms on the first floor are occupied, the occupancy rate is as follows: 18/30 0.6 or 60%.
Explanation / Answer
Here is the following code for finding out the hotel occupancy rate also which satisfies the given criteria in the question.
// EXIT Button
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
//CLEAR Button
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
lstHotData.Items.Clear()
lblRmsOcc.Text = ""
lblOvrOccRate.Text = ""
End Sub
//REPORT Button
Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReport.Click
Dim count As Integer
Dim ocRate As String
Dim RmNum As Integer = 0
Dim strRmNUM As String = ""
Dim totRmNum As Integer = 0
Dim FloorNum As String = ""
For count = 1 To 8
FloorNum = "Floor # " & count
strRmNUM = InputBox("Enter the Number of Rooms Occupied", _
"Floor #" & count & " ", 0)
Try
If strRmNUM < 0 Or strRmNUM > 30 Then 'Tests if RmNum is less than zero or greater than thirty
strRmNUM = InputBox("Number of Rooms Occupied Must be Between 0 and 30" _
& CrLf & CrLf & "Try Again!", _
"INPUT ERROR!" & count & " ", 0)
End If
RmNum = CInt(strRmNUM)
Catch ex As InvalidCastException 'If cancel is clicked it returns a zero length string
strRmNUM = InputBox("Number of Rooms Occupied Must be Between 0 and 30" _
& CrLf & CrLf & "Try Again!", "INPUT ERROR!" & count & " ", 0)
If strRmNUM = "" Then
MessageBox.Show("YOU have FAILED to Input the correct Room Value. You Must Start Over!", "INPUT ERROR 2")
GoTo EndofSub 'Allows you to get out gracefully if CANCEL is clicked
End If
End Try
//Calculation of Occupancy rate
ocRate = FormatPercent(RmNum / 30)
lstHotData.Items.Add(FloorNum & Tab & " Rooms Occupied: " & RmNum & " Occupancy Rate: " & ocRate)
totRmNum = totRmNum + RmNum
EndofSub:
End Sub
End Class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.