Hotel Occupancy

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 16684 Views - Last Post: 08 May 2014 - 06:48 PM Rate Topic: -----

#1 chris0812   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-September 09

Hotel Occupancy

Post icon  Posted 29 October 2009 - 03:49 PM

The hotel has 8 floors and 30 rooms on each floor. 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.
When the user clicks the "Complete Report" button, a loop should execute and iterate 8 times. Each time the loop iterates, it should display an input box for one of the hotel's floors. The input box should ask the user to enter the number of rooms occupied on that floor. As the user enters a value for each floor, the loop should calculate the occupancy rate for that floor, and display the information for that floor in the list box. When the number of occupied rooms has been entered for all the floors, the application should display the total number of rooms occupied and the overall occupnacy rate of the hotel.

I have some of the code but I'm not sure which way to go now!
Public Class Form1

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click


		Me.Close()
	End Sub

	Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

		'This procedure clears all labels and list box.

		ListBox1.Items.Clear()
		lblOccupancyRate.Text = String.Empty
		lblTotalRooms.Text = String.Empty

	End Sub

	Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReport.Click

		
		'This calculates and displays each floors occupancy rate.
		Dim intCount As Integer			 ' Loop counter
		Dim decOccupancy As Decimal		 ' Occupancy rate
		Dim intTotalRooms As Integer
		Dim intRooms As Integer
		Dim intMax_Floor As Integer
		Dim decOccupancyRate As Decimal
		Dim decTotalOccupancyRate As Decimal
		Dim decRoomRate As Decimal
		
		'Calculate the occupancy rate for each floor, the total of all occupancy rates, and the total rooms occupied.

		For intCount = 0 To intMax_Floor = 240
			decOccupancy = intRooms(intCount) * decRoomRate
			decOccupancyRate(intCount) = decOccupancy
			decTotalOccupancyRate /= decOccupancy
			(intTotalRooms) (&) Int (intCount)
		Next
	End Sub

	Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotalRooms.Click

	End Sub

	Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

		Dim strUserInput As String
		strUserInput = InputBox("Enter the number of rooms occupied.", "Provide a value", "1")
	End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Hotel Occupancy

#2 higgsch   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 126
  • Joined: 26-October 09

Re: Hotel Occupancy

Posted 29 October 2009 - 04:01 PM

Personally, I would have the:
strUserInput = InputBox("Enter the number of rooms occupied.", "Provide a value", "1")
within your loop, that way you have the variable to divide already there.
Was This Post Helpful? 1
  • +
  • -

#3 dzone41   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 218
  • Joined: 21-August 09

Re: Hotel Occupancy

Posted 29 October 2009 - 05:33 PM

Item 1.
Your arrays are not arrays. You are declaring single element variables.
For this situation you can use arrays since you know how many elements you need in them.
To declare an array:
Dim intRooms(30) As Integer


The index's for the array is 0-29.
Or use can use lists.
Your loop looks like it's iterating 240 times, not just 8, once for each floor. So 0 - 7 The loop should iterate and on each iteration the first thing to do is show the input box. Then, use the users info to do your calculations.
The key is to think about how you would do this all on paper, with real numbers.
For intCount = 0 To 7
strUserInput = InputBox("Enter the number of rooms occupied.", "Provide a value", "1")
	decOccupancy(intCount) = CDec(strUserInput) / 30
	ListBox1.Add(decOccupancy)
	decTotalOccupancy += decOccupancy(intCount)
Next
 ListBox1.Add(decTotalOccupancy)
 ListBox1.Add(decTotalOccupancy/240)




I have not tried this code out, but should work ok, may need to format your output to your liking.
Hope thins helps

This post has been edited by dzone41: 29 October 2009 - 05:44 PM

Was This Post Helpful? 1
  • +
  • -

#4 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Hotel Occupancy

Posted 29 October 2009 - 05:46 PM

@dzone41: I suggest you go and read the tutorial on Arrays as you're misinforming people.
Was This Post Helpful? 1
  • +
  • -

#5 dzone41   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 218
  • Joined: 21-August 09

Re: Hotel Occupancy

Posted 30 October 2009 - 01:38 AM

Your right, it was a mistake, my apology, The array with 30 elements should be declared with 29 as in:
Dim myArray(29) As String


THAT will give it 30 elements 0-29.
Like I said, I was not able to try out the code last night, but I did this morning and I did find OTHER, mistakes. Here it is corrected
For intCount As Integer = 0 To 7 
	 strUserInput = InputBox("Enter the number of rooms occupied. 
	 _", "Provide a value", "1")
	 decOccupancy(intCount) = CDec(strUserInput) / 30
	 ListBox1.Items.Add(decOccupancy(intCount))
	 decTotalOccupancy += CDec(strUserInput)
Next
ListBox1.Items.Add(decTotalOccupancy)
ListBox1.Items.Add(decTotalOccupancy / 240)



Thank you for catching my simple mistake Adam. It gave more a chance to go back and get the others.
Was This Post Helpful? 1
  • +
  • -

#6 chris0812   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-September 09

Re: Hotel Occupancy

Posted 30 October 2009 - 10:10 AM

Still not working for me.....it's telling me that decOccupancy isn't declared and once I get the input window, the program stops running.
Public Class Form1

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click


		Me.Close()
	End Sub

	Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

		'This procedure clears all labels and list box.

		ListBox1.Items.Clear()
		lblOccupancyRate.Text = String.Empty
		lblTotalRooms.Text = String.Empty

	End Sub

	Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReport.Click

		
		'This calculates and displays each floors occupancy rate.
		Dim intRooms(29) As String						  ' Loop counter
		Dim decOccupancy As Decimal						 ' Occupancy
		Dim intTotalRooms As Integer						' Use as accumulator
		Dim decOccupancyRate As Decimal
		Dim decTotalOccupancyRate As Decimal
		Dim decRoomRate As Decimal
		Dim strUserInput As String
		Dim decTotalOccupancy As Decimal
		Dim intCount As Integer
		'ListBox1.Items.Add

		'Calculate the occupancy rate for each floor, the total of all occupancy rates, and the total rooms occupied.
		For intCount As Integer = 0 To 7
	 strUserInput = InputBox("Enter the number of rooms occupied. _", "Provide a value")
			decOccupancy(intCount) = CDec(strUserInput) / 30
			ListBox1.Items.Add(decOccupancy(intCount))
			decTotalOccupancy += CDec(strUserInput)
		Next
		ListBox1.Items.Add(decTotalOccupancy)
		ListBox1.Items.Add(decTotalOccupancy / 240)

		If intRooms(29) > 30 Then
			MessageBox.Show("Please enter a number greater than 0 and less than 30")
		End If
		'Display the running total.
		lblOccupancyRate.Text = decTotalOccupancyRate.ToString("c")


	End Sub

	Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotalRooms.Click

	End Sub

	Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
		'ListBox1.Items.Add
	End Sub
End Class




View Postdzone41, on 30 Oct, 2009 - 12:38 AM, said:

Your right, it was a mistake, my apology, The array with 30 elements should be declared with 29 as in:
Dim myArray(29) As String


THAT will give it 30 elements 0-29.
Like I said, I was not able to try out the code last night, but I did this morning and I did find OTHER, mistakes. Here it is corrected
For intCount As Integer = 0 To 7 
	 strUserInput = InputBox("Enter the number of rooms occupied. 
	 _", "Provide a value", "1")
	 decOccupancy(intCount) = CDec(strUserInput) / 30
	 ListBox1.Items.Add(decOccupancy(intCount))
	 decTotalOccupancy += CDec(strUserInput)
Next
ListBox1.Items.Add(decTotalOccupancy)
ListBox1.Items.Add(decTotalOccupancy / 240)



Thank you for catching my simple mistake Adam. It gave more a chance to go back and get the others.

Was This Post Helpful? 0
  • +
  • -

#7 dzone41   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 218
  • Joined: 21-August 09

Re: Hotel Occupancy

Posted 30 October 2009 - 11:53 AM

decOccupancy is an array, declare it as such.
You will get an error using intRooms like that, you have not assigned anything to this array.
Instead of:
If intRooms(29) > 30 Then........


use
If CInt(strUserInput) > 30 Then.........


but it has to be inside of the For...Next loop or it will not do any good.
Check your variable declarations against the variables that you are actually using. If you want to use the code I gave you, which I tested, and it does work, you will have to match up the variable declarations to it.
One other thing, after you show the message box if a user enters a number higher than 30, You will need to go back up to the Line before the input box was displayed so the user can enter another number. Try the Goto statement and a line label, that would do nicely. If you don't, code will just continue to execute and the floor will be skipped.
Was This Post Helpful? 1
  • +
  • -

#8 chris0812   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-September 09

Re: Hotel Occupancy

Posted 30 October 2009 - 03:24 PM

View Postdzone41, on 30 Oct, 2009 - 10:53 AM, said:

decOccupancy is an array, declare it as such.
You will get an error using intRooms like that, you have not assigned anything to this array.
Instead of:
If intRooms(29) > 30 Then........


use
If CInt(strUserInput) > 30 Then.........


but it has to be inside of the For...Next loop or it will not do any good.
Check your variable declarations against the variables that you are actually using. If you want to use the code I gave you, which I tested, and it does work, you will have to match up the variable declarations to it.
One other thing, after you show the message box if a user enters a number higher than 30, You will need to go back up to the Line before the input box was displayed so the user can enter another number. Try the Goto statement and a line label, that would do nicely. If you don't, code will just continue to execute and the floor will be skipped.

Quote

Okay, I've made a few changes but take a look at this....this is due tonight and I'm afraid I'm not going to get this done. when I debug this, I get an error...."there is no source code availabel for this location"....I should have never taken this class because I just can't seem to get it right.....also this is the only error that I get at the bottom of VB...Variable 'intCount' hides a variable in an enclosing block.

Public Class Form1

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click


		Me.Close()
	End Sub

	Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

		'This procedure clears all labels and list box.

		ListBox1.Items.Clear()
		lblOccupancyRate.Text = String.Empty
		lblTotalRooms.Text = String.Empty

	End Sub

	Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReport.Click

		
		'This calculates and displays each floors occupancy rate.
		Dim decOccupancy As Array
		Dim decTotalOccupancyRate As Decimal
		Dim strUserInput As String
		Dim decTotalOccupancy As Decimal
		Dim intCount As Integer
		'ListBox1.Items.Add

		'Calculate the occupancy rate for each floor, the total of all occupancy rates, and the total rooms occupied.
		For intCount As Integer = 0 To 7
	 strUserInput = InputBox("Enter the number of rooms occupied. Provide a value")
			decOccupancy(intCount) = CDec(strUserInput) / 30
			ListBox1.Items.Add(decOccupancy(intCount))
			decTotalOccupancy += CDec(strUserInput)
		Next
		Do
			ListBox1.Items.Add(decTotalOccupancy)
			ListBox1.Items.Add(decTotalOccupancy / 240)
		Loop

		If CInt(strUserInput) > 30 Then
			MessageBox.Show("Please enter a number greater than 0 and less than 30")
		End If

		'Display the running total.
		lblOccupancyRate.Text = decTotalOccupancyRate.ToString("c")


	End Sub

	Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotalRooms.Click

	End Sub

	Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#9 dzone41   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 218
  • Joined: 21-August 09

Re: Hotel Occupancy

Posted 30 October 2009 - 04:32 PM

Simple....remove the top declaration of intCount.
It is declared in the code line:
For intCount As Integer = 1 To 7


Was This Post Helpful? 1
  • +
  • -

#10 chris0812   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-September 09

Re: Hotel Occupancy

Posted 30 October 2009 - 04:43 PM

View Postdzone41, on 30 Oct, 2009 - 03:32 PM, said:

Simple....remove the top declaration of intCount.
It is declared in the code line:
For intCount As Integer = 1 To 7


Quote

Okay I did that but now it's telling that Unused local variable: 'decTotalOccupancyRate' and Warning Variable 'decOccupancy' is used before it has been assigned a value. A null reference exception could result at runtime.

Was This Post Helpful? 0
  • +
  • -

#11 dzone41   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 218
  • Joined: 21-August 09

Re: Hotel Occupancy

Posted 30 October 2009 - 05:21 PM

I told you how to declare the array
Dim decOccupancy As Array ' -----incorrect
Dim decOccupancy(7) As Decimal ' ------Correct


Also
You ARE using decTotalOccupancyRate before you assign it a value
So, assign it a value. You program dictates that the value should be:
decTotalOccupancyRate = (decTotalOccupancy / 240)


Then you use it for the label.
It is my bed time, I will stay up a little longer, hurry
Was This Post Helpful? 1
  • +
  • -

#12 dzone41   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 218
  • Joined: 21-August 09

Re: Hotel Occupancy

Posted 30 October 2009 - 05:57 PM

It looks like that Do Loop you have, is going to hang up you pragram, there is no exit for it, do not use it, there is no need, Simply remove the "Do" and the "Loop". Also the message box that you want to show if the user enters a number > 30 will never show, you did not put it in the For Next loop like I suggested and there is no Goto or line Label. Just leave it, you should be ok, it just won't ever display, or erase it completely.Forget the Goto and Line label.
Get rid of the Do Loop!!!!

Then you should have a functional program at least.
Was This Post Helpful? 1
  • +
  • -

#13 hhh0505   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-November 09

Re: Hotel Occupancy

Posted 05 November 2009 - 08:51 PM

Was this ever finished? i have the same assignment can you show me your code pelase?

View Postdzone41, on 30 Oct, 2009 - 04:57 PM, said:

It looks like that Do Loop you have, is going to hang up you pragram, there is no exit for it, do not use it, there is no need, Simply remove the "Do" and the "Loop". Also the message box that you want to show if the user enters a number > 30 will never show, you did not put it in the For Next loop like I suggested and there is no Goto or line Label. Just leave it, you should be ok, it just won't ever display, or erase it completely.Forget the Goto and Line label.
Get rid of the Do Loop!!!!

Then you should have a functional program at least.

Was This Post Helpful? 0
  • +
  • -

#14 hhh0505   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-November 09

Re: Hotel Occupancy

Posted 06 November 2009 - 11:44 AM

Pleae Chris if you have your code still can i see it? This is what i have so far....but its not working... please help... i keep getting string to int not possible...

  'This calculates and displays each floors <strong class="highlight">occupancy</strong> rate.		
		Dim intOccupancy(7) As Integer
		Dim intTotOccupancy As Integer = 0
		Dim decOccupancyRate As Decimal
		Dim strUserInput As String
		Dim decOccupancy As Decimal
		Dim intCount As Byte
		Dim intRoomCount As Integer 'User can say 2 billion and shouldn't overflow		
		'ListBox1.Items.Add	
		For intCount = 0 To 7
			intRoomCount = -1
			While intRoomCount < 0 Or intRoomCount > 30
				strUserInput = InputBox("Enter the number of rooms occupied. Provide a value")
				If IsNumeric(strUserInput) Then intRoomCount = strUserInput
				If intRoomCount < 0 Or intRoomCount > 30 Then
					MessageBox.Show("Please enter a number >= 0 and <= 30")
				End If
			End While
			intOccupancy(intCount) = intRoomCount
			intTotOccupancy += intRoomCount
			decOccupancyRate = CDec(intRoomCount) / 30
			ListBox1.Items.Add(decOccupancyRate * 100)
		Next
		strUserInput = "Total <strong class="highlight">occupancy</strong>= " & intTotOccupancy.ToString()
		ListBox1.Items.Add(strUserInput)
		decOccupancyRate = CDec(CDec(intTotOccupancy) / 240.0)
		ListBox1.Items.Add(decOccupancyRate * 100)

This post has been edited by hhh0505: 06 November 2009 - 12:07 PM

Was This Post Helpful? 0
  • +
  • -

#15 chris0812   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-September 09

Re: Hotel Occupancy

Posted 06 November 2009 - 01:12 PM

The code you see at the top of the page IS my code.....I never got mine to run properly either.....

View Posthhh0505, on 6 Nov, 2009 - 10:44 AM, said:

Pleae Chris if you have your code still can i see it? This is what i have so far....but its not working... please help... i keep getting string to int not possible...

  'This calculates and displays each floors <strong class="highlight">occupancy</strong> rate.		
		Dim intOccupancy(7) As Integer
		Dim intTotOccupancy As Integer = 0
		Dim decOccupancyRate As Decimal
		Dim strUserInput As String
		Dim decOccupancy As Decimal
		Dim intCount As Byte
		Dim intRoomCount As Integer 'User can say 2 billion and shouldn't overflow		
		'ListBox1.Items.Add	
		For intCount = 0 To 7
			intRoomCount = -1
			While intRoomCount < 0 Or intRoomCount > 30
				strUserInput = InputBox("Enter the number of rooms occupied. Provide a value")
				If IsNumeric(strUserInput) Then intRoomCount = strUserInput
				If intRoomCount < 0 Or intRoomCount > 30 Then
					MessageBox.Show("Please enter a number >= 0 and <= 30")
				End If
			End While
			intOccupancy(intCount) = intRoomCount
			intTotOccupancy += intRoomCount
			decOccupancyRate = CDec(intRoomCount) / 30
			ListBox1.Items.Add(decOccupancyRate * 100)
		Next
		strUserInput = "Total <strong class="highlight">occupancy</strong>= " & intTotOccupancy.ToString()
		ListBox1.Items.Add(strUserInput)
		decOccupancyRate = CDec(CDec(intTotOccupancy) / 240.0)
		ListBox1.Items.Add(decOccupancyRate * 100)

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3