In: Computer Science
IN JAVA
Write a program that calculates the occupancy rate for each floor of a hotel. (Use a sentinel value and please point out the sentinel in bold.) The program should start by asking for the number of floors in the hotel. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor 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.
SAMPLE OUTPUT:
Enter number of floors:
2
Enter total rooms at floor 1:
10
Enter total rooms occupied at floor1:
5
Enter total rooms at floor 2:
10
Enter total rooms occupied at floor2:
5
Total rooms: 20
Total occupied rooms: 20
Hotel occupany: 50.0
Solution to the Problem Given in JAVA -
import java.util.Scanner;
import java.text.*;
public class HotelOccupancy {
public static void main(String[] args) {
int floors;
double rooms = 0;
int roomsOccupied = 0;
double totalRms = 0;
double totalRmsOccupied = 0;
double totalVacant = 0;
double occupancyRate = 0.0;
// Create Scanner object for input
Scanner input = new Scanner(System.in);
// Ask User to Enter to Enter floor count
System.out.print("Enter the number of floors: ");
floors = input.nextInt();
// Input validation Floor count must be > 0
while(floors < 1){
System.out.print("Invalid Input. Please enter a number of floors greater than 0: ");
floors = input.nextInt();
}
for(int i=0; i<floors; i++){
// Ask user for the number of rooms on a floor
System.out.print("Enter total rooms at floor (Floor " + (int)(i + 1) + "): ");
rooms = input.nextInt();
// Validation of Room Entered by the user
while(rooms < 10){
System.out.print("Incorrect input. Please a number of rooms greater than 9 \n(Floor " + (int)(i + 1) + "): ");
rooms = input.nextInt();
}
// Prompt user for the number of rooms occupied.
System.out.print("Enter total rooms occupied at floor (Floor " + (int)(i + 1) + "): ");
roomsOccupied = input.nextInt();
// Calculate total rooms
totalRms += rooms;
// Calculate total rooms occupied
totalRmsOccupied += roomsOccupied;
}
// Calculate total vacancy available
totalVacant = totalRms - totalRmsOccupied;
// Calculate occupancy rate
occupancyRate = (totalRmsOccupied/totalRms);
// Decimal formating
NumberFormat df = DecimalFormat.getInstance();
df.setMaximumFractionDigits(2);
// Display Hotel Occupancy data
System.out.println("Total Rooms: " + totalRms + "\nOccupied(QTY): " + totalRmsOccupied +
"\nVacant Rooms(QTY): " + totalVacant + "\nOccupancy Rate: " + df.format(occupancyRate) + "%");
}
}