Home > Blockchain >  Is there a way to fix this program in java?
Is there a way to fix this program in java?

Time:04-25

My program is menu driven. The part "A" and "V" options are sorted out but they are in one single code. Is there suggestion I could break that code so I could put them in the appropriate options? I have tried initializing them in a procedure but my code wont run.

What is my program about? Allowing users to choose from 12 rooms for booking. They can enter their name for the booking. My code runs that part. I need to be able to allow the user to view and add their names separately in each options. "A" for adding their names and "V" for viewing the cabins.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        System.out.println("A) Add a customer");
        System.out.println("V) View all cabins");
        System.out.println("E) Display Empty cabins");
        System.out.println("D) Delete customer from cabin");
        System.out.println("F) Find cabin from customer name ");
        System.out.println("S) Store program data into file");
        System.out.println("L) Load program data from file");
        System.out.println("O) View passengers ordered alphabetically by name");
        System.out.println("Q) Quit");

        String options = null;
        Scanner scan = new Scanner(System.in); 
        System.out.println("Welcome to Cruise Ship Menu Program");
        do {
            options = scan.nextLine();
            switch (options) {
                case "A":
                        String roomName; 
                        int roomNum = 0; 
                        String[] hotel = new String[13];
                        //for (int x = 0; x < 12; x   ) hotel[x] = ""; 
                        //initialise 
                        while ( roomNum < 12 ) 
                        { 
                            for (int x = 0; x < 12; x   ) 
                            { 
                                    if (hotel[x].equals("e"))
                                    System.out.println("room "   x   " is empty"); 
                            } 

                            System.out.println("Enter room number (0-11) or 12 to stop:" ); 
                            roomNum = input.nextInt(); 
                            System.out.println("Enter name for room "   roomNum  " :" ) ; 
                            roomName = input.next(); 
                            hotel[roomNum] = roomName ; 
                            for (int x = 0; x < 12; x   ) 
                            { 
                            System.out.println("room "   x   " occupied by "   hotel[x]); 
                            } 
                        }  

                    } 
                    // do what you want
                    break;
                case "V":
                    // do what you want
                    break;
                case "E":
                    // do what you want
                    break;
                case "D":
                    break;
                    // Add the rest of your cases
                case "F":
                    break;
                    // Add the rest of your cases
                case "S":
                    break;
                    // Add the rest of your cases
                case "L":
                    break;
                    // Add the rest of your cases
                case "O":
                    break;
                    // Add the rest of your cases
            }
        } while (!options.equals("Q")); // quitting the program
    }
}

CodePudding user response:

Let's take a quick look at the core issue. First you print which rooms are empty, ask the user for some data and the print ALL the rooms again...

while (roomNum < 12) {
    for (int x = 0; x < 12; x  ) {
        if (hotel[x].equals("e")) {
            System.out.println("room "   x   " is empty");
        }
    }
    //...
    for (int x = 0; x < 12; x  ) {
        System.out.println("room "   x   " occupied by "   hotel[x]);
    }
}

I kind of think that this is just confusing. Simply print all the room data before asking for user input...

for (int x = 0; x < hotel.length; x  ) {
    if (hotel[x].equals("e")) {
        System.out.println("room "   x   " is empty");
    } else {
        System.out.println("room "   x   " is occupied by "   hotel[x]);
    }
}

This will display all the information the user really needs.

Let's go a little deeper...

System.out.println("Enter room number (0-11) or 12 to stop:");
roomNum = input.nextInt();
System.out.println("Enter name for room "   roomNum   " :");
roomName = input.next();

is problematic for a number of reasons...

  1. You're not dealing with what happens if the user doesn't enter a int value and...
  2. nextInt will leave a dangling new line in the input stream, which could interfere with parsing of the input later
  3. next won't take into consideration if the user enters firstname lastname, it will only pick up firstname, meaning that the next execution of nextInt is going to fail (as lastname is still in the stream).
  4. If the user enters a room number out side of the acceptable range (0-11 inclusive), you simply continue on asking for the name and trying to update the array, this is going to cause confusion (as I wanted to exit) and a ArrayIndexOutOfBoundsException

As a general piece of advice, make use of Scanner#nextLine and parse the String separately.

When you get the room number, you MUST validate it, before continuing to ask for the name and deal with the possibility of an out of range value...

if (roomNum >= 0 && roomNum < hotel.length) {
    System.out.println("Enter name for room "   roomNum   " :");
    roomName = input.nextLine();
    hotel[roomNum] = roomName;
} else if (roomNum < 0 || roomNum > 12) {
    System.out.println(roomNum   " is not a valid room number");
}

I also think that do-while loops are greatly under appreciated in these scenarios, you MUST do at least one iteration, so you might as well construct the loop so that the exit condition is done at the end instead (IMHO)

Runnable example...

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String roomName;
        int roomNum = 0;
        String[] hotel = new String[12];
        initialise(hotel);
        do {
            for (int x = 0; x < hotel.length; x  ) {
                if (hotel[x].equals("e")) {
                    System.out.println("room "   x   " is empty");
                } else {
                    System.out.println("room "   x   " is occupied by "   hotel[x]);
                }
            }
            System.out.println("Enter room number (0-11) or 12 to stop:");
            String value = input.nextLine();
            try {
                roomNum = Integer.parseInt(value);
                if (roomNum >= 0 && roomNum < hotel.length) {
                    System.out.println("Enter name for room "   roomNum   " :");
                    roomName = input.nextLine();
                    hotel[roomNum] = roomName;
                } else if (roomNum < 0 || roomNum > 12) {
                    System.out.println(roomNum   " is not a valid room number");
                }
            } catch (NumberFormatException exp) {
                System.out.println(value   " is not a valid room number");
            }
            System.out.println("");
        } while (roomNum != 12);
    }

    private static void initialise(String hotelRef[]) {
        for (int x = 0; x < hotelRef.length; x  ) {
            hotelRef[x] = "e";
        }
    }
}
  • Related