在HashSet中存储数据

时间:2014-12-01 16:23:41

标签: java hashset

我正在尝试将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);
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

   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;

        }

    }

答案 1 :(得分:0)

// validRoom 方法位于//下方     public boolean validRoom (String roomNo){

        String floorNo = roomNo.substring(0,1); 
        String rNo = roomNo.substring(1,3);
        int roomNoI = Integer.parseInt(rNo);
        int floorNoI = Integer.parseInt(floorNo);

        if(!(floorNoI > 7 || floorNoI <= 0 || roomNoI <= 12)){

            System.out.println("sorry room " + floorNoI + roomNoI + " number does not exsist at this hotel");
            return false;

        }

        System.out.println("Floor:- " + floorNoI +"\nRoom:-  "  + rNo );

        return true;
    }


//`And the *roomTaken* method just checks where the *roomNo* *Varable* is  stored in the *hashSet*//

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

        }

    } 

//对不起我刚接触Java的新手     欢呼帮助//