Oscar
Oscar

Reputation: 531

Menu loop after name, Java

I am trying to create a small Hotel application with a menu system. So far the user is asked for a room number (0-9) and a room name, once this is entered I want the user to be returned to the menu where the other menu options can be entered. I don't think the other menu options are working currently either :(

Here is my code so far:

package hotel;
import java.util.*;

public class Hotel2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        Room[] myHotel = new Room[10];
        myHotel[0] = new Room ();
        myHotel[1] = new Room ();
        myHotel[2] = new Room ();
        myHotel[3] = new Room ();
        myHotel[4] = new Room ();
        myHotel[5] = new Room ();
        myHotel[6] = new Room ();
        myHotel[7] = new Room ();
        myHotel[8] = new Room ();
        myHotel[9] = new Room ();

        String roomName;
        String menuEntry = null;
        int roomNum = 0;
        String[] hotel = new String[11]; 
        for (int x = 0; x < 10; x++ ) hotel[x] = "";

        initialise(hotel);

        while ( roomNum < 10 )
        { 

            System.out.println("Please enter one of the following options:\n1) Add customer\n2) Delete customer from room\n3 )View all empty rooms\n4) Find a customer\n5) Load program from text file\n6) Order rooms alphabetically\n7) Store program into text file\n8) View all rooms\nInput:");
            menuEntry = input.next();

            while (menuEntry.equals("1"))
            { 
                System.out.println("Enter room number (0-9):" );
                roomNum = input.nextInt();

                if (roomNum < 10)
                {  
                        System.out.println("Enter name for room " + roomNum +" :" ) ;
                        roomName = input.next();
                        hotel[roomNum] = roomName ;      
                }
            }


            if (menuEntry.equals("V"))
            {
                for (int x = 0; x < 10; x++ )
                {
                System.out.println("room " + x + " occupied by " + hotel[x]);
                }
            }

            if (menuEntry.equals("E"));
            {

            }

            if (menuEntry.equals ("D"))
            {
            System.out.println("Enter the room number which you would like to delete a customer from:");
            roomNum = input.nextInt();
            hotel[roomNum] = "empty";
            }
        }   
    }

    private static void initialise( String hotelRef[] ) {
        for (int x = 0; x < 10; x++ ) hotelRef[x] = "empty";
        System.out.println( "initilise\n");
    }
}

Upvotes: 0

Views: 439

Answers (2)

Guy
Guy

Reputation: 50864

Your while loops waits for menuEntry to be different than 1, but you never change menuEntry inside the loop. Change it to do while loop

do { 
    System.out.println("Please enter one of the following options:\n1) Add customer\n2) Delete customer from room\n3 )View all empty rooms\n4) Find a customer\n5) Load program from text file\n6) Order rooms alphabetically\n7) Store program into text file\n8) View all rooms\nInput:");
    menuEntry = input.next();
    System.out.println("Enter room number (0-9):" );
    roomNum = input.nextInt();
    if (roomNum < 10)
    {  
         System.out.println("Enter name for room " + roomNum +" :" ) ;
         roomName = input.next();
         hotel[roomNum] = roomName ;      
    }
} while (menuEntry.equals("1"));

You should also insert the rest of the options into the loop and make the conditions to match the options in the menu.

Another option with while loop

System.out.println("Please enter one of the following options:\n1) Add customer\n2) Delete customer from room\n3 )View all empty rooms\n4) Find a customer\n5) Load program from text file\n6) Order rooms alphabetically\n7) Store program into text file\n8) View all rooms\nInput:");
menuEntry = input.next();

while (menuEntry.equals("1")) {
    System.out.println("Enter room number (0-9):" );
    roomNum = input.nextInt();
    if (roomNum < 10)
    {  
         System.out.println("Enter name for room " + roomNum +" :" ) ;
         roomName = input.next();
         hotel[roomNum] = roomName ;      
    }

    System.out.println("Please enter one of the following options:\n1) Add customer\n2) Delete customer from room\n3 )View all empty rooms\n4) Find a customer\n5) Load program from text file\n6) Order rooms alphabetically\n7) Store program into text file\n8) View all rooms\nInput:");
    menuEntry = input.next();
}

Upvotes: 1

pitaside
pitaside

Reputation: 678

Something like this help your code to look more cleaner and understandable

while ( roomNum < 10 && menuEntry.equals("1") ){
     //then perform your if statement
     // or preferabley use a switch(menuEntry){}
}

Upvotes: 1

Related Questions