-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHotelOccupancy.java
76 lines (64 loc) · 2.48 KB
/
HotelOccupancy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//HotelOccupancy.java
import java.util.Scanner; // Needed for the Scanner class
/**
This program will calculate the occupancy rate for each floor of
a hotel. It will ask for the number of floors, the number of rooms
on each floor, and the number of rooms that are occupied. At the
end, it will display the number rooms, the number of rooms occupied,
the number of vacancies, and the occupancy rate of the hotel.
*/
public class HotelOccupancy
{
public static void main(String[] args)
{
// Needed variables
int floor;
int roomsPerFloor;
int occupiedRooms;
int numFloors;
double roomsTotal;
double occupiedRoomsTotal;
// Create a Scanner object
Scanner keyboard = new Scanner(System.in);
// Ask the user how many floors
System.out.println("How many floors does the hotel have?");
numFloors = keyboard.nextInt();
// Input validation to ensure floors is always more than 1
while(numFloors < 1)
{
System.out.print("Invalid input. Please enter a number of floors " +
"greater than 0: ");
numFloors = keyboard.nextInt();
}
// Set accumulators to zero
roomsTotal = 0.0;
occupiedRoomsTotal = 0.0;
// Begin a loop to calcuate the number of vacancies
for (floor = 0; floor != numFloors; floor++)
{
System.out.println();
System.out.println("Floor number " + (floor + 1) + ": ");
System.out.println("-----------------");
System.out.println("Total rooms: ");
roomsPerFloor = keyboard.nextInt();
roomsTotal += roomsPerFloor;
// Input validation to ensure rooms per floor is always more than 10
while (roomsPerFloor < 10)
{
System.out.println("Invalid input. Please enter a number of rooms " +
"greater than 10:");
roomsPerFloor = keyboard.nextInt();
}
System.out.println("Occupied rooms: ");
occupiedRooms = keyboard.nextInt();
occupiedRoomsTotal += occupiedRooms;
}
// calculate and display results
double occupancyRate = (occupiedRoomsTotal / roomsTotal) * 100;
double vacantRooms = (roomsTotal - occupiedRoomsTotal);
System.out.println("Total rooms in the hotel: " + roomsTotal);
System.out.println("Total occupied rooms = " + occupiedRoomsTotal);
System.out.println("Total vacant rooms = " + vacantRooms);
System.out.printf("The occupancy rate is %.1f%%", occupancyRate);
}
}