简体   繁体   English

预订应用程序,达到条件时禁用按钮

[英]Reservation application, disable button when condition reached

I want to create a simple reservation application, like when you want to book movie ticket online, except this is a much simplified version. 我想创建一个简单的预订应用程序,就像您要在线预订电影票时一样,只是这是一个非常简化的版本。

I have a class names reservation with 8 toggle buttons and 1 button, the user will get to input number of seat they want to book in another class, let's just say 4. In reservation, after the user choose 4 seats, the rest of the toggle button will be disabled to prevent user from choosing more seats. 我有一个带有8个切换按钮和1个按钮的班级名称预订,用户将输入要在另一个班级预订的座位的数量,我们只说4。在预订中,用户选择4个席位后,其余的切换按钮将被禁用,以防止用户选择更多的座位。 The user will then click the save button, and the data will then be inputted in the Database. 然后,用户将单击“保存”按钮,然后将数据输入到数据库中。 Next time when another user open reservation, all of the toggle buttons will be re-enabled except those 4 seats previous user has booked earlier, obviously. 下次当另一个用户打开预订时,显然, 所有切换按钮将重新启用,除了先前用户已提前预订的那4个席位。 Everything is working so far except the part that i bold. 到目前为止,除我加粗的部分外,其他所有东西都在工作。

Here's my code : 这是我的代码:

private static final JToggleButton[] btn = new JToggleButton[8];
private int totalrow, a;
java.sql.Connection connection = null;
Statement statement4 = null;
ResultSet resultSet = null;

private void formWindowActivated(java.awt.event.WindowEvent evt) {  // This is where i load the information that has been saved in the database before.                                   

    try {

        connection = DriverManager.getConnection("jdbc:derby:
        statement4 = (Statement) connection.createStatement();
        resultSet = statement4.executeQuery("SELECT reservationid, seatno, validator FROM reservation WHERE reservationid = '" + time.id + "'");

        String seatno, validator, rsid, id;

        btn[0] = seat1;
        btn[1] = seat2;
        btn[2] = seat3;
        btn[3] = seat4;
        btn[4] = seat5;
        btn[5] = seat6;
        btn[6] = seat7;
        btn[7] = seat8;

        int i = 0;
        while(resultSet.next()){
            seatno = resultSet.getString("seatno");
            validator = resultSet.getString("validator");
            rsid = resultSet.getString("reservationid");

            if(seatno.equals(btn[i].getText()) && validator.equals("reserved")){ //checking if seat i is reserved
                btn[i].setSelected(true);
            }
            else{
                btn[i].setSelected(false);
            }
            i++;
        }

    } catch (SQLException ex) {
         Logger.getLogger(seat.class.getName()).log(Level.SEVERE, null, ex);
    }
}   

public void actionPerformed(ActionEvent e) { // This actionlistener is used to count, if User has choose 4 seats, the code will disable the rest of the button

    try {
        connection = DriverManager.getConnection("jdbc:derby://localhost:1527/Project","root","root");
        statement = (Statement) connection.createStatement();

        btn[0] = seat1;
        btn[1] = seat2;
        btn[2] = seat3;
        btn[3] = seat4;
        btn[4] = seat5;
        btn[5] = seat6;
        btn[6] = seat7;
        btn[7] = seat8;

        a = 0;
        for(JToggleButton btns : btn){
            if (btns.isSelected()){
                a++;
                System.out.println(a);

                if (! btns.isSelected()){
                    System.out.println(a);
                    a--;
                }
            }  
        }

        for(JToggleButton btns : btn){
            if (a >= Integer.parseInt(BookingUI.passenger)){ // This is number of Passenger taken from another class. In this case is 4.
                if(! btns.isSelected()){
                    btns.setEnabled(false);
                }
            }

            else{
                btns.setEnabled(true);
            }
        }

    } catch (SQLException ex) {
        Logger.getLogger(seat.class.getName()).log(Level.SEVERE, null, ex);
    }
}  

Regarding my title, based on my current code and logic, it might not be possible to re-enable the toggle button. 关于我的标题,根据我当前的代码和逻辑,可能无法重新启用切换按钮。 So the better way to do it might be to use another logic to disable the toggle button instead. 因此,执行此操作的更好方法可能是使用其他逻辑来禁用切换按钮。

In the image , seat 1,4,5,8 are chosen by the user. 图像中 ,座位1,4,5,8由用户选择。 And the rest of the buttons are disabled 其余按钮被禁用

Since it's a long implementation, it's difficult to answer with a code solution. 由于实现时间长,因此很难用代码解决方案来回答。 There for I will mention my suggestion here. 在那里,我将在这里提及我的建议。

You have to declare your JToggleButton Array like this btn = new JToggleButton[8]; 您必须像这样btn = new JToggleButton[8];声明您的JToggleButton数组 btn = new JToggleButton[8]; in a public static context where every class could access it. 在每个类都可以访问它的公共静态上下文中。 And when the user reserved a seat you should save the data to DB (or keep in the program memroy, eg : HashMap) on which user reserved the which seat. 并且,当用户预订了一个席位时,您应该将数据保存到DB(或保留在程序存储器中,例如:HashMap)中。

After that when you loading the data from the Database you should loop through and get the booked seats which previously inserted and then by accessing that predefined JToggleButton Array and set each booked seat by doing this btns[i].setEnabled(false); 之后,当您从数据库中加载数据时,您应该循环浏览并获取先前插入的预定座位,然后通过访问该预定义的JToggleButton Array并通过执行此操作btns[i].setEnabled(false);设置每个预定座位btns[i].setEnabled(false); (i for the booked index of the seat). (i为预定的座位索引)。
Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM