我正在尝试将roomNo的数据存储在occupiedRooms HashSet中。我不确定如何存储值。到目前为止,我已经知道了:一个验证条件,如果正确的话,应该将roomNo存储在HashSet中;但是,事实并非如此。

public void checkIn (String roomNo, String guest) {

    if(!(validRoom(roomNo) && roomTaken(roomNo))) {
        System.out.println(guest + " booked in to room number: " + roomNo);
        occupiedRooms.add(roomNo);
    }
}

最佳答案

尝试这个:

   public void checkIn (String roomNo, String guest) {
       if (validRoom(roomNo)) {
        if (!roomTaken(roomNo)) {
            System.out.println(guest + " booked in to room number: " + roomNo);
            occupiedRooms.add(new Room(roomNo,guest));
           }
        }
    }


public boolean roomTaken (String roomNo){
        if(!(occupiedRooms.contains(roomNo))){
            return false;
        }
        else{
            return true;

        }

    }

10-08 02:19