-1

在预订菜单中,我主要有 3 个选项:a、b 和 c。选项 'a' - 选择房间类型。5种不同的房型可供选择。选项“b”- 为每个房间选择附加组件。每个附加组件都可以额外付费选择。选项 'c' - 从进行预订页面返回到主页

我的程序需要显示 5 种不同类型的房间。此时客户只能选择选项“a”、“b”或“c”。

如果客户在选择房间时输入了无效的号码(包括1到5),系统会给出错误信息并提示用户重新输入。一旦用户输入了他/她想要的房间类型,他们可以继续输入每个房间的数量房间类型。选择房型后,系统会提示您选择每种房型的附加组件。

这是我下面的代码。当用户选择选项“b”时,我无法从选项“a”获取存储在整数数组列表中的附加组件数量。

我初始化了 2 个数组列表,但我不确定它们是否正确。第一个数组列表是存储房间类型和日期的字符串。第二个数组列表是整数,它存储房价、房间需求数量、附加组件数量、晚数、用户选择的附加选项和附加组件的数量。

我对编码很陌生,所以需要帮助。

import java.util.*;
import java.io.*;

public class RoomSelection {

    public RoomSelection() throws InputMismatchException {

        String choiceStr;//initialize choiceStr which is use for reading lines from scanner input
        char choiceChar;//initialize choiceStr which is use for reading character from scanner input
        int choice;//initialize choiceStr which is use for reading integer from scanner input
        String datee;
        String[] roomType = {"Single Room", "Double Room", "Deluxe Room", "Junior Room", "Suite"}; //Initialize a array for room type
        Integer[] priceRoom = {160, 200, 280, 380, 500}; //Initialize a array for room prices
        Integer[] priceAdd = {25, 60, 70, 100}; //Initialize a array for add-on prices

        ArrayList<String> roomAndDate = new ArrayList<String>();
        ArrayList<Integer> all = new ArrayList<Integer>();
        Scanner input = new Scanner(System.in); //Initialize a scanner input

        System.out.println("Room Selection");
        System.out.println("==============\n");
        System.out.println("[a] Room Type");
        System.out.println("[b] Add-Ons");
        System.out.println("[c] Main Menu");
        System.out.println("Type 'a' to select Room Type and state the desire quantity for each type.");
        System.out.println("Type 'b' to select the Add-Ons.");
        System.out.println("Type 'c' to exit from  the Booking Menu.");
        System.out.println("Please enter your option (a, b or c): ");

        choiceStr = input.nextLine();
        choiceChar = choiceStr.charAt(0);
        while (true) {

            switch (choiceChar) {
                case 'a':
                    System.out.println("Room Type");
                    System.out.println("=====================================================");
                    System.out.println("(1) Single Room (1 person) - Price: S$160");
                    System.out.println("(2) Double Room (2 persons) - Price: S$200");
                    System.out.println("(3) Deluxe Room (2 persons) - Price: S$280");
                    System.out.println("(4) Junior Suite (2 persons) - Price: S$380");
                    System.out.println("(5) Suite (2 persons) - Price: S$500\n");
                    System.out.println("Enter Room types (Enter '1' to '5')");
                    choice = input.nextInt();
                    while (choice > 5) {
                        if (choice > 5) {
                            System.out.println("Please enter number between '1' to '5'!");
                            choice = input.nextInt();
                        }
                    }

                    String roomTypess = roomType[choice - 1];
                    roomAndDate.add(roomTypess);
                    int storePricee = priceRoom[choice - 1];
                    all.add(storePricee);

                    System.out.println("Number of rooms required (maximum 10): ");
                    choice = input.nextInt();
                    while (choice > 10) {
                        if (choice > 10) {
                            System.out.println("Please enter again!");
                            choice = input.nextInt();
                        }
                    }
                    all.add(choice);

                    for (int i = 0; i < choice; i++) {
                        System.out.println("Enter the date of checked-in (dd/mm/yy) for " + roomAndDate.get(0) + " " + (i + 1));
                        choiceStr = input.nextLine();
                        choiceStr = input.nextLine();
                        roomAndDate.add(choiceStr);
                        System.out.println(roomAndDate);
                        System.out.println("Enter number of Add-on for " + roomAndDate.get(0) + " " + (i + 1) + ": ");
                        choice = input.nextInt();
                        while (choice > 4) {
                            if (choice > 4) {
                                System.out.println("Please enter again! Choose only option 1 to 4");
                                choice = input.nextInt();
                            }
                        }
                        all.add(choice);
                        System.out.println("Number of night(s) required (maximum 30) for " + roomAndDate.get(0) + " " + (i + 1) + ": ");
                        choice = input.nextInt();
                        while (choice > 30) {
                            if (choice > 30) {
                                System.out.println("Please enter again! Maximum is 30 days!");
                                choice = input.nextInt();
                            }
                        }
                        all.add(choice);


                    }

                    new RoomSelection();
                    break;
                case 'b':
                    System.out.println("Add-Ons");
                    System.out.println("=====================================================");
                    System.out.println("(1) Breakfast voucher (1 person) per day - Price: S$25");
                    System.out.println("(2) Spa voucher (1 person) - Price: S$60");
                    System.out.println("(3) Half Day Tour voucher (1 person) - Price: S$70");
                    System.out.println("(4) Full Day Tour voucher (1 person) - Price: $100\n");



                    for (int i = 0; i < (Integer) all.get(3); i++) {

                        System.out.println("Enter Add-On option");
                        choice = input.nextInt();
                        while (choice > 4) {
                            if (choice > 4) {
                                System.out.println("Please enter again! Choose only option 1 to 4");
                                choice = input.nextInt();
                            }
                        }
                        all.add(choice);
                        System.out.println("Enter quantity required for Add-On option " + (i + 1) + ": ");
                        choice = input.nextInt();
                        all.add(choice);
                    }


                    break;
                case 'c':
                    new MainPage1();
                    break;
                default:
                    continue;
            }
        }
    }
}
4

1 回答 1

1

我看了你的代码,有些东西你不需要,你需要改进。

  1. 而不是读取整数input.nextInt();,您应该读取输入字符串并尝试执行Integer.parseInt(yourString)并使用结​​果(如果它抛出 aNumberFormatException你会知道字符串不是有效的整数)。
  2. 对于while循环,您应该排除值的情况0
  3. 当条件包含在其中时,if从语句内部消除语句。whilewhileif
  4. 创建具有所需功能的对象数组列表,而不是两个数组列表:
    • create 和 object 具有作为字段的房间类型,成本,...。
    • 在函数外部定义和分配这个对象数组列表,RoomSelection()这样您就可以一直访问它。
    • 如果您RoomSelection()在列表中定义它,当您调用时,您将new RoomSelection();创建列表的新实例而不是使用现有的实例。

希望我能有所帮助。最后一点非常重要。

于 2012-07-27T08:53:39.413 回答