Results 1 to 6 of 6
  1. #1
    Anthony5347 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Displaying results

    Ok so basically this program works fine but I am having trouble with one thing. Our professor wants us to display the amount of rooms and rooms occupied for every floor and the occupancy rate. Also the total rooms, rooms occupied and total occupancy rate. I am able to do the last part; I am just having a hard time trying to display the information for every floor. For example:
    Say the user inputs 2 floors with all the information, in the very end it should display:

    first floor: 100 rooms, 80 occupied. Then occupancy rate is 80%

    second floor: 80 rooms, 52 occupied. The occupancy rate is 65%

    The hotel has 180 rooms together, 132 occupied, 48 vacant, the overall occupancy rate is 73.3%

    I mean, how do I go about saving the data from each iteration of the loop and in the end displaying it? I realize I will need a calculation for the occupancy rate for each floor, but first I want to be able to at least display the information. I dont want the program to display the results for every floor after every iteration, I want it to display the info at the very end after all the iterations have been done.









    Java Code:
    //Chapter 4 HW
    
    import java.util.*;
    
    public class hotelOccupancy
    {
       public static void main(String[] args)
       {
          //Variable Declaration and Scanner class
          double floorNumber, roomAmount, roomOccupied;
          double occupancyRate, totalRooms = 0, totalOccupied = 0; 
          double totalVacancy = 0, overallRate = 0;
          Scanner userInput = new Scanner(System.in);
          
          //Get number of floors
          System.out.print("Enter the number of floors: ");
          floorNumber = userInput.nextDouble();
          
          //input validation for floor amount
          while (floorNumber < 1)
          {
             System.out.println("Floor number must be greater than 1.");
             System.out.print("Please enter again: ");
             floorNumber = userInput.nextDouble();
          }
          
          //Loop to get the rest of the data
          for (int counter = 1; counter <= floorNumber; counter++)
          {
            //Get number of rooms per floor
             System.out.println("Enter the number of rooms on floor " + counter + ".");
             System.out.print(" There must be at least 10 rooms: ");
             roomAmount = userInput.nextDouble(); 
                
             //input validation for the amount of rooms   
             while (roomAmount < 10)
             {
                System.out.println("You entered less than 10 rooms.");
                System.out.print("Please enter again: ");
                roomAmount = userInput.nextDouble();       
             }   
            
             //Get the number of occupied rooms
             System.out.print("How many rooms for floor " + counter +
                            " are occupied: ");
             roomOccupied = userInput.nextDouble();               
             
             //input validation for the occupancy
             while (roomOccupied > roomAmount)
             {
                System.out.println("Room occupancy cannot be greator than" +
                               " the number of rooms on that floor.");
                System.out.print("Please enter again the amount of rooms occupied: ");
                roomOccupied = userInput.nextDouble();  
             }
             
             //Caluclations
             totalRooms = totalRooms + roomAmount;        
             
             totalOccupied = totalOccupied + roomOccupied;
          
          }
          
          totalVacancy = totalRooms - totalOccupied;
             
          overallRate = totalOccupied * 100 / totalRooms;
         
          
          
          System.out.printf("The hotel has %.0f rooms together, ", totalRooms);
          System.out.printf("%.0f occupied, ", totalOccupied); 
          System.out.printf("%.0f vacant, ", totalVacancy);
          System.out.printf("the overall occupancy rate is %.2f%%", overallRate);
       }
    }
    Last edited by Anthony5347; 09-24-2013 at 05:39 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Displaying results

    This would be much easier if you had a Room class which had instance variables that hold what floor it is on. Then you could search through all your rooms and check which floor they are on.

    BTW allowing the user to input different number of rooms for each floor seems dumb. Imagine what a building would look like if floor 1 had 20 rooms, floor 2 had 10 rooms and floor 3 had 100 rooms.

  3. #3
    Anthony5347 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Displaying results

    Yeah I know this question might sound dumb, but thats what the professor is asking us >.>
    Hmm, since we have not been introduced to the Room class is there a "dumber" or more basic approach to this, or is the Room class my only option?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Displaying results

    The inner for loop is executing for each floor. Somewhere in that loop you will need to keep track of the occupancy for each room. Then once all floor data has been entered you can calculate the percentage.

  5. #5
    Anthony5347 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Displaying results

    If I use a variable to hold that value however, wont that information that it is holding be replaced by the new information from the next iteration? The only thing I have been able to come up with is this code to put in the for loop, before it exits:

    System.out.println("Room " + counter + " has: " + roomAmount +
    " rooms, and " + roomOccupied + " rooms occupied.");

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Displaying results

    If I understand the requirements you only need the information once for each floor. If the info for each floor needs to be displayed together at the end then you need to store the information for each floor somewhere.

Similar Threads

  1. How do I put my results in GUI?
    By dynamix in forum SWT / JFace
    Replies: 1
    Last Post: 03-17-2013, 04:39 PM
  2. Displaying the right details of a resource taken from a JSP results page
    By rushhour in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-20-2011, 11:36 AM
  3. Displaying results from the servlet to jsp web page
    By rushhour in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-17-2010, 01:13 PM
  4. Java - Displaying Search Results in jsp using STRUTS
    By selvi in forum Web Frameworks
    Replies: 0
    Last Post: 08-12-2010, 08:48 AM
  5. Problem with displaying search results from an array
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 04:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •