1. Member
Join Date
Sep 2013
Posts
10
Rep Power
0

## Displaying results

Ok so basically this program works fine but I am having trouble with one thing. Our professor wants us to display the amount of rooms and rooms occupied for every floor and the occupancy rate. Also the total rooms, rooms occupied and total occupancy rate. I am able to do the last part; I am just having a hard time trying to display the information for every floor. For example:
Say the user inputs 2 floors with all the information, in the very end it should display:

first floor: 100 rooms, 80 occupied. Then occupancy rate is 80%

second floor: 80 rooms, 52 occupied. The occupancy rate is 65%

The hotel has 180 rooms together, 132 occupied, 48 vacant, the overall occupancy rate is 73.3%

I mean, how do I go about saving the data from each iteration of the loop and in the end displaying it? I realize I will need a calculation for the occupancy rate for each floor, but first I want to be able to at least display the information. I dont want the program to display the results for every floor after every iteration, I want it to display the info at the very end after all the iterations have been done.

Java Code:
```//Chapter 4 HW

import java.util.*;

public class hotelOccupancy
{
public static void main(String[] args)
{
//Variable Declaration and Scanner class
double floorNumber, roomAmount, roomOccupied;
double occupancyRate, totalRooms = 0, totalOccupied = 0;
double totalVacancy = 0, overallRate = 0;
Scanner userInput = new Scanner(System.in);

//Get number of floors
System.out.print("Enter the number of floors: ");
floorNumber = userInput.nextDouble();

//input validation for floor amount
while (floorNumber < 1)
{
System.out.println("Floor number must be greater than 1.");
floorNumber = userInput.nextDouble();
}

//Loop to get the rest of the data
for (int counter = 1; counter <= floorNumber; counter++)
{
//Get number of rooms per floor
System.out.println("Enter the number of rooms on floor " + counter + ".");
System.out.print(" There must be at least 10 rooms: ");
roomAmount = userInput.nextDouble();

//input validation for the amount of rooms
while (roomAmount < 10)
{
System.out.println("You entered less than 10 rooms.");
roomAmount = userInput.nextDouble();
}

//Get the number of occupied rooms
System.out.print("How many rooms for floor " + counter +
" are occupied: ");
roomOccupied = userInput.nextDouble();

//input validation for the occupancy
while (roomOccupied > roomAmount)
{
System.out.println("Room occupancy cannot be greator than" +
" the number of rooms on that floor.");
System.out.print("Please enter again the amount of rooms occupied: ");
roomOccupied = userInput.nextDouble();
}

//Caluclations
totalRooms = totalRooms + roomAmount;

totalOccupied = totalOccupied + roomOccupied;

}

totalVacancy = totalRooms - totalOccupied;

overallRate = totalOccupied * 100 / totalRooms;

System.out.printf("The hotel has %.0f rooms together, ", totalRooms);
System.out.printf("%.0f occupied, ", totalOccupied);
System.out.printf("%.0f vacant, ", totalVacancy);
System.out.printf("the overall occupancy rate is %.2f%%", overallRate);
}
}```
Last edited by Anthony5347; 09-24-2013 at 04:39 AM.

2. ## Re: Displaying results

This would be much easier if you had a Room class which had instance variables that hold what floor it is on. Then you could search through all your rooms and check which floor they are on.

BTW allowing the user to input different number of rooms for each floor seems dumb. Imagine what a building would look like if floor 1 had 20 rooms, floor 2 had 10 rooms and floor 3 had 100 rooms.

3. Member
Join Date
Sep 2013
Posts
10
Rep Power
0

## Re: Displaying results

Yeah I know this question might sound dumb, but thats what the professor is asking us >.>
Hmm, since we have not been introduced to the Room class is there a "dumber" or more basic approach to this, or is the Room class my only option?

4. ## Re: Displaying results

The inner for loop is executing for each floor. Somewhere in that loop you will need to keep track of the occupancy for each room. Then once all floor data has been entered you can calculate the percentage.

5. Member
Join Date
Sep 2013
Posts
10
Rep Power
0

## Re: Displaying results

If I use a variable to hold that value however, wont that information that it is holding be replaced by the new information from the next iteration? The only thing I have been able to come up with is this code to put in the for loop, before it exits:

System.out.println("Room " + counter + " has: " + roomAmount +
" rooms, and " + roomOccupied + " rooms occupied.");

6. ## Re: Displaying results

If I understand the requirements you only need the information once for each floor. If the info for each floor needs to be displayed together at the end then you need to store the information for each floor somewhere.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•