A hotel’s occupancy rate is calculated as follows: Occupancy rate = number of rooms occupied total number of rooms Write a program that calculates the occupancy rate for each fl oor of a hotel. The program should start by asking for the number of fl oors in the hotel. A loop should then iterate once for each fl oor. During each iteration, the loop should ask the user for the number of rooms on the fl oor and the number of them that are occupied. After all the iterations, the program should display the number of rooms the hotel has, the number of them that are occupied, the number that are vacant, and the occupancy rate for the hotel. Input Validation: Do not accept a value less than 1 for the number of floors. Do not accept a number less than 10 for the number of rooms on a floor.
**I believe I have the program done correctly, I just can't figure out how to get it not to accept a number less than 10 for the number of rooms on a floor.** Here is the code:
package hotel;
import java.util.Scanner;
public class Hotel {
public static void main(String[]args) {
int floors, occupiedRooms = 0, totalRooms = 0;
Scanner input = new Scanner(System.in);
System.out.print("How many floors are present: ");
floors = input.nextInt();
for(int i = 1; i <= floors; i++) {
System.out.print("Level " + i + ": How many rooms are there: ");
int totalRoomsOnLevel = input.nextInt();
System.out.print("Level " + i + ":How many are occupied: ");
int occupiedRoomsOnLevel = input.nextInt();
System.out.println();
totalRooms += totalRoomsOnLevel;
occupiedRooms += occupiedRoomsOnLevel;
}
System.out.println();
System.out.println("Total hotel rooms: " + totalRooms);
int vacantRooms = totalRooms - occupiedRooms;
System.out.println("Total vacant rooms: " + vacantRooms);
double occupancyRate = (double)occupiedRooms / totalRooms;
System.out.println("Occupancy rate: " + occupancyRate);
}
}
6 answers
For making sure there are 10 storeys or more, you can modify the input segment:
System.out.print("How many floors are present: ");
floors = input.nextInt();
while(floors<10){
// print Number of floors must be at least 10
floors = input.nextInt();
}
But this would continue indefinitely until user enters a number greater than 10.
A better way is to use a for-loop to loop a maximum (say 5) of times after which a message will appear and the program terminates.
floors=int(input("Enter number of floors in the hotel: "))
if floors>0:
s=0
total_rooms=[]
total_occupied=[]
for i in range(0,floors):
while(True):
rooms=int(input("Enter number of rooms on the floor: "))
if rooms <10:
print("Invalid Input : Numbers of Rooms entered less than 10")
else:
break
while(True):
occupied=int(input("Enter number of rooms that are occupied: "))
if occupied > rooms:
print("Invalid Input : occupied Rooms entered greater than Available Rooms")
else:
total_rooms.append(rooms)
total_occupied.append(occupied)
s=1
break
if s==1:
print("\t")
print("Total numbers of floors in the hotel: ",floors)
for i in range(0,len(total_rooms)):
print("Number of rooms on",i+1," floor: ",total_rooms[i])
print("Numbers of rooms occupied on",i+1," floor: ",total_occupied[i])
print("Number of rooms vacant on ",i+1," floor: ",total_rooms[i]-total_occupied[i])
rate=100*total_occupied[i] /total_rooms[i]
print("occupancy rate of ",i+1," floor: ",round(rate,2))
print("\t")
print("Total numbers of rooms in hotel: ",sum(total_rooms))
print(" Total numbers of rooms that are occupied: ",sum(total_occupied))
print("Numbers of rooms that are vacant: ",sum(total_rooms)- sum(total_occupied))
occupancy_rate=100* sum(total_occupied)/sum(total_rooms)
print("occupancy rate of hotel : ",round(occupancy_rate,2))
else:
print("Invalid Input")