酒店计划 - 添加客户

时间:2016-03-05 22:24:47

标签: java function netbeans

下面我创建了一个酒店计划,允许客户留在房间里。我最近能够创建一个功能来查看我的程序的所有当前房间,但是我无法在addCustomer()函数中将添加客户编写正确的代码。这是我目前的代码:

package hotelprogram;

import java.util.Scanner;

public class HotelProgram {

// global variables 

static String[] hotel = new String[7];

private static void initialise(String hotelRef[]) {

    for (int x = 0; x < 6; x++) {
        hotelRef[x] = "e";
        System.out.println("initilise");
    }
}

private static void viewALL() {

    // Views all current rooms

    for (int x = 0; x < 6; x++) {
        if (!hotel[x].equals("e")) {
            System.out.println("Room " + x + " occupied by " + hotel[x]);
        }else{
            System.out.println("Room" + x + " is Empty");
        }
    }
}

private static void addCustomer()
{
    // Add a customer

    Scanner sc = new Scanner (System.in);

    int roomNumfc;
    int roomNamefc;

    for (int x = 0; x < 6; x++)
    {
        System.out.println("Select room number to add a customer: ");
        roomNumfc = sc.nextInt();
        System.out.println("New customer to add: ");
        roomNamefc = sc.nextInt();

    }


}


public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String roomName;
    int roomNum = 0;
    char selectChoice;

    for (int x = 0; x < 6; x++) {
        hotel[x] = "";
        initialise(hotel);
    }

    while (roomNum < 6) {
        // Displays current rooms

        for (int x = 0; x < 6; x++) {

            // displays an empty room

            if (hotel[x].equals("e")) {
                System.out.println("Room" + x + " is Empty");
            }
        }
        // Enter a room prompt

        System.out.println("Enter a room number from (0-5) or higher to stop (10 to display menu): ");
        roomNum = input.nextInt();

        if (roomNum == 10) {

            // menu if roomNum = 10

            System.out.println("Make a selection: ");
            System.out.println("A. Add a customer to room");
            System.out.println("V. View all rooms");
            selectChoice = input.next().charAt(0);

            switch (selectChoice) {
            case 'A':
                addCustomer();
                break;
            case 'V':
                viewALL();
                break;
            default:
                System.out.println("Bad input, exiting program...");
                break;
            }
            break;
        }
        // Name current room promt

        System.out.println("Please name this room: " + roomNum + " :");
        roomName = input.next();
        hotel[roomNum] = roomName;

        // Room displayed
        for (int x = 0; x < 6; x++) {
            if (!hotel[x].equals("e")) {
                System.out.println("Room " + x + " occupied by " + hotel[x]);
            }
        }
    }
  }
}

我不太确定如何访问所使用的全局数组,可以真正使用一些帮助。提前谢谢!

2 个答案:

答案 0 :(得分:0)

除了收集主要输入(最多)之外,不要放置程序逻辑。创建类的实例并在该实例上调用方法:

public static void main(String... args){
    HotelProgram hotel = new HotelProgram();
    //...gather input
    //while input != 10
        //get room num
        //get room name
        hotel.addCustomer(num, name);
        hotel.printRooms();

}

通过这样做,您遵循面向对象的设计并避免讨厌使用全局数组/变量。

这是你的代码被修改以消除全局变量并且更友好:

public class HotelProgram {
    private String[] rooms = new String[7];

    public HotelProgram(){
        initialise();
    }

    private void initialise() {
        for (int x = 0; x < rooms.length; x++) {
            rooms[x] = "e";
        }
        System.out.println("initilise is complete.");
    }

    public void printRooms() {
        for (int i = 0; i < rooms.length; i++){
            if(rooms[i].equals("e")){
                System.out.println("Room(" + (i+1) + ") is Empty.");
            }
            else{
                System.out.println("Room(" + (i+1) + ") is occupied by " + rooms[i]);
            }
        }
    }

    public void addCustomer(Scanner sc){
        int roomNum = 0;
        while(roomNum-1 < 0 || roomNum-1 > rooms.length-1){
            System.out.println("Enter a room number from (1-" + (rooms.length) + "):");
            roomNum = sc.nextInt();
        }

        System.out.println("Please provide the customer name for room " + (roomNum) + " :");
        sc.nextLine();
        String customer = sc.nextLine();
        rooms[roomNum-1] = customer;
    }

    public void printMenu(){
         System.out.println("\nMake a selection: ");
         System.out.println("A. Add a customer to room");
         System.out.println("V. View all rooms");
         System.out.println("M. Print this menu.");
         System.out.println("E. Exit\n");
    }


    public static void main(String[] args) {
        HotelProgram hotel = new HotelProgram();
        Scanner sc = new Scanner (System.in);
        hotel.printMenu();

        String selection = "";
        while(true){
            selection = sc.nextLine();
            switch(selection){
                 case "E":
                    System.out.println("Thanks for visiting. Have a good day!");
                    System.exit(0);
                case "A":
                    hotel.addCustomer(sc);
                    break;
                case "V":
                    hotel.printRooms();
                    break;
                case "M":
                    hotel.printMenu();
                    break;
                default:
                    System.out.println("Bad input (" + selection + "), Try Again...");
            }
        }
    }
}

答案 1 :(得分:0)

这不是好的OO设计。我强烈建议您更改程序逻辑。此外,通过getter和setter方法更好地访问变量,避免使用静态变量。但是,如果要在main()中访问静态数组,则应创建类的实例

HotelProgram myhotel = new HotelProgram();
myhotel.hotel[i]=.... // i can be any of the allowed array indices.