Input Validation: Do not accept a number less than one for the number of floors. Do not accept a number less than 10 for the number of rooms on a floor. The equation is: Occupancy rate = number of rooms occupied / total number of rooms. As far as the code is concerned, I think it is good except I don't know why my occupied rooms is not display /calculating incorrectly. Here's my code:
import javax.swing.JOptionPane; //needed for GUI
import java.text.DecimalFormat; //needed to format the Output
public class DTHotelOccupancy
{//Begin class
public static void main(String[] args)
{//Begin main method
String input; //To hold the user's input
final int MIN_FLOORS = 1; //Minimum amount of floors
final int MIN_ROOMS = 10; //Minimum amount of rooms per floor
int floors, //Number of floors in hotel
rooms = 0, //Number of available rooms in each floor
occrooms = 0, //Number of rooms occupied
totalrooms = 0, //Number of total rooms
totalroomsocc = 0, //Total Number of rooms occupied
vacant = 0; //Number of vacant rooms
double occrate = 0; //Occupancy rate
DecimalFormat formatter = new DecimalFormat("#,##0.0"); //format the scores
//Get the number of floors in the hotel
input =JOptionPane.showInputDialog("How many Floors are in the Hotel?\n"+
"\tThe number of floors must be at least " + MIN_FLOORS);
//Convert floors into integer
floors = Integer.parseInt(input);
//Validate the number of floors entered.
while (floors < MIN_FLOORS)
{
input =JOptionPane.showInputDialog("You have Entered a value less than the 1"+
"How many Floors are in the Hotel? "+
"The number of floors must be at least " + MIN_FLOORS);
//Convert floors into integer
floors = Integer.parseInt(input);
}
//For Loop
for (int i = 1; i <= floors;i++)
{
input = JOptionPane.showInputDialog("\tHow many rooms are on floor " + i + "? ");
//Convert rooms into integer
rooms = Integer.parseInt(input);
//Validate the number of rooms per floor
while (rooms < 10)
{
input = JOptionPane.showInputDialog("You have entered the number of rooms less than 10 per floor!"+
"\t\nHow many rooms are on floor " + i + "? ");
//Convert rooms into integer
rooms = Integer.parseInt(input);
}
input = JOptionPane.showInputDialog("How many rooms on floor " + i + " are occupied? ");
//Convert occupied rooms into integer
occrooms = Integer.parseInt(input);
while (occrooms > rooms)
{
input = JOptionPane.showInputDialog("You have entered the number of occupied rooms greater than the nummber of rooms per floor!"+
"\t\nHow many rooms are on floor " + i + "? ");
//Convert occupiedrooms into integer
occrooms = Integer.parseInt(input);
}
}
totalrooms = (floors * rooms); //Calculate the Number of rooms
totalroomsocc = (totalrooms - occrooms); //Calculate the total number of rooms occupied
vacant = (totalrooms - totalroomsocc); //Calculate the Number of rooms Vacant
occrate = ((double)occrooms / totalrooms); //Calculate OccupancyRate
//Display the results
JOptionPane.showMessageDialog(null, "\tTotal Rooms:" + totalrooms +
"\t\nRooms occupied: " +totalroomsocc+
"\t\nRooms vacant: " +vacant +
"\t\nOccupancy Rate: " + formatter.format(occrate),
"Results",JOptionPane.INFORMATION_MESSAGE);
//End the program.
System.exit(0);
}//End main method
}//End class
Please help me out with the calculating for the total rooms occupied
Thank you

New Topic/Question
Reply



MultiQuote



|