Java填充和清空数组

时间:2016-04-16 16:54:25

标签: java user-interface

我正在开发一个有这些方法的酒店管理程序:bookRoom()和vacateRoom()。我认为这是索引的问题。当我试图腾出一个房间时,比如让我说我预订了房间1然后腾出房间1,当我去重新预订房间2而不是房间1的书。我也有不同的房间单人床和双人床和它不承认双人房被占用(16-45)

这是我的vacateRoom方法

public void vacateRoom()
{
    boolean isOccupied = true;
    int inputRoomNumber = Integer.parseInt((String) roomNumbersComboBox.getSelectedItem());
    if (inputRoomNumber >= 1 && inputRoomNumber <5)
    {
        if(functionRoom[inputRoomNumber].isOccupied());
        {
            functionRoom[inputRoomNumber].vacate();
            isOccupied = false;
        }
    }
    else if((inputRoomNumber  >=5 && inputRoomNumber  <45))
    {

        if(bedRoom[inputRoomNumber].isOccupied())
        {
            bedRoom[inputRoomNumber].vacate();
            isOccupied = false;
        }
    }
    if(isOccupied == false)

        JOptionPane.showMessageDialog(null, "Room Number "+(inputRoomNumber) + " vacated." + "", 
             "", JOptionPane.PLAIN_MESSAGE);
    else
    JOptionPane.showMessageDialog(null, "Not an occupied room." + null, 
             "", JOptionPane.PLAIN_MESSAGE);

这是我的bookRoom方法

public void bookRoom()
{

    int counter;        
    String name = customerNameBox.getText();
    String payeeName =  customerPayeeBox.getText();
    String numberOfBeds = String.valueOf(numberOfBedsComboBox.getSelectedItem());

        counter = 0;

        if(functionRadio.isSelected())
        {
            while(counter < 5 && functionRoom[counter].isOccupied())
            {
                counter++;
            }
            if(counter == 5)
            {
                JOptionPane.showMessageDialog(null, "No more function rooms available" + ""88, 
                     "", JOptionPane.PLAIN_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Booked into room " + (counter+1), 
                     "", JOptionPane.PLAIN_MESSAGE);
                functionRoom[counter].occupyRoom(name, payeeName);
            }
        }

        if(bedroomRadio.isSelected())
        {               
            String numberOfBed = (String) numberOfBedsComboBox.getSelectedItem();
            if (numberOfBed.equals("Single Bed"))    
            {
                counter = 0;
                while(bedRoom[counter].isOccupied() && counter < 10 )
                    counter++;
                if(counter == 10)
                {
                    JOptionPane.showMessageDialog(null, "No Single Bed Room available" + null, 
                             "", JOptionPane.PLAIN_MESSAGE);
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Booked into room " + (counter+6), 
                             "", JOptionPane.PLAIN_MESSAGE);
                    bedRoom[counter].occupyRoom(name, payeeName);

                }
            }

            String numberOfBed1 = String.valueOf(numberOfBedsComboBox.getSelectedItem());
            if (numberOfBed1.equals("Double Bed"))
            {
                while(bedRoom[counter].isOccupied() && counter < 40)
                {
                    counter++;
                }

                if (counter == 40)
                {
                    JOptionPane.showMessageDialog(null, "No more rooms available" + null, 
                     "", JOptionPane.PLAIN_MESSAGE);
                }
                else 
                {
                    JOptionPane.showMessageDialog(null, "Booked into room " + (counter +16), 
                     "", JOptionPane.PLAIN_MESSAGE);                        
                    bedRoom[counter].occupyRoom(name, payeeName);
                }
            }
        }
}

1 个答案:

答案 0 :(得分:0)

数组从索引0开始。

如果你腾出房间,也无需检查房间是否空出:

if (inputRoomNumber >= 1 && inputRoomNumber <5)
{
    if(functionRoom[inputRoomNumber].isOccupied());
    {
        functionRoom[inputRoomNumber].vacate();
        isOccupied = false;
    }
}

可以是:

functionRoom[inputRoomNumber].vacate();

因为你无论如何都要离开。