1 Replies - 4131 Views - Last Post: 29 February 2012 - 07:33 PM Rate Topic: -----

#1 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Hotel Occupancy rate calculation

Posted 29 February 2012 - 07:14 PM

Hello! I am trying to build a hotel occupancy program Write a program that calculates the occupancy rate for each floor of a hotel. The program should start by asking for the number of floors that the hotel has. 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 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

Is This A Good Question/Topic? 0
  • +

Replies To: Hotel Occupancy rate calculation

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Hotel Occupancy rate calculation

Posted 29 February 2012 - 07:33 PM

These 2 formulas are wrong

totalrooms = (floors * rooms); //Calculate the Number of rooms
totalroomsocc = (totalrooms - occrooms); //Calculate the total number of rooms occupied

and not positionned at the good place.
The totalrooms shoud be = totalrooms + rooms;
and be placed inside the for() loop that prompts for each room in each floor
same thing for the totalroomsocc

The total number is room is not nbFloor * nbRooms in the last floor

Indent your code correctly and it will become obvious

	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);
			}
// compute totalrooms and totalroomsocc here
		}


		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



Declaring your variables where youneed them avoid this type or error


int rooms = 0, //Number of available rooms in each floor
occrooms = 0, //Number of rooms occupied

should be declare inside the for() loop, so you can't, by mistake, use them outside the for() loop

This post has been edited by pbl: 29 February 2012 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1