在Java中为房间分配床可以通过以下方式实现:
以下是一个简单的示例代码:
public class Room {
private int roomNumber;
private String roomType;
private int bedCount;
private int allocatedBeds;
public Room(int roomNumber, String roomType, int bedCount) {
this.roomNumber = roomNumber;
this.roomType = roomType;
this.bedCount = bedCount;
this.allocatedBeds = 0;
}
public void allocateBeds(int bedsToAllocate) {
if (allocatedBeds + bedsToAllocate <= bedCount) {
allocatedBeds += bedsToAllocate;
System.out.println(bedsToAllocate + " beds allocated to room " + roomNumber);
} else {
System.out.println("Not enough beds available in room " + roomNumber);
}
}
// Getters and setters for room attributes
}
public class Main {
public static void main(String[] args) {
Room room1 = new Room(101, "Single", 1);
Room room2 = new Room(201, "Double", 2);
room1.allocateBeds(1); // Allocate 1 bed to room 101
room2.allocateBeds(2); // Allocate 2 beds to room 201
room1.allocateBeds(1); // Try to allocate 1 more bed to room 101 (not enough beds available)
}
}
在这个示例中,我们创建了一个Room类,其中包含房间的属性和一个分配床的方法。在主函数中,我们创建了两个房间对象,并分别为它们分配了床。如果尝试分配超过房间可用床位的数量,将会输出相应的提示信息。
请注意,这只是一个简单的示例,实际应用中可能需要更复杂的逻辑来处理床位分配。此外,根据具体需求,还可以添加其他属性和方法来完善房间类的功能。
领取专属 10元无门槛券
手把手带您无忧上云