0

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");
    }
}
3
  • You should format your code properly. Commented Mar 5, 2016 at 15:11
  • @MikeCat Fixed thanks Commented Mar 5, 2016 at 15:17
  • I'd recommend worrying less about your text user interface and more about the API for the problem. Get your Hotel and Room abstractions right and the user interfaces will be easy. Two tips: No magic numbers (e.g. 10 rooms) and use a loop to fill up that array of Rooms. Commented Mar 5, 2016 at 15:17

2 Answers 2

1

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();
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply, how would I show this exactly? I am new to Java
It still doesn't seem to work properly, I think I have my loops set up wrong?
@JamesPatterson Try the do while loop
Hmm your code is confusing me, I want menu to be displayed and entry '1' to ask the user for the number/ name of the room. Where should I put the do while loop?
@JamesPatterson You don't have to, you can ask for the first choice before the while (like you did) and re ask for the choice at the end of each iteration to get the exit condition. This will save you the asking outside the loop.
|
1

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){}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.