java酒店住房系统管理源代码
时间: 2023-11-09 16:02:50 浏览: 151
Java酒店住房系统管理源代码的主要功能是对酒店的客房进行管理和操作。以下是一个简单的示例代码:
```java
import java.util.ArrayList;
import java.util.List;
class Room {
private int roomId;
private boolean isOccupied;
private String guestName;
public Room(int roomId) {
this.roomId = roomId;
this.isOccupied = false;
this.guestName = "";
}
public int getRoomId() {
return roomId;
}
public boolean isOccupied() {
return isOccupied;
}
public String getGuestName() {
return guestName;
}
public void setOccupied(boolean isOccupied) {
this.isOccupied = isOccupied;
}
public void setGuestName(String guestName) {
this.guestName = guestName;
}
}
class Hotel {
private List<Room> rooms;
public Hotel() {
this.rooms = new ArrayList<>();
}
public void addRoom(Room room) {
rooms.add(room);
}
public void checkIn(int roomId, String guestName) {
Room room = getRoomById(roomId);
if (room != null && !room.isOccupied()) {
room.setOccupied(true);
room.setGuestName(guestName);
System.out.println("Successfully checked in.");
} else {
System.out.println("Room is not available for check-in.");
}
}
public void checkOut(int roomId) {
Room room = getRoomById(roomId);
if (room != null && room.isOccupied()) {
room.setOccupied(false);
room.setGuestName("");
System.out.println("Successfully checked out.");
} else {
System.out.println("Room is not occupied.");
}
}
private Room getRoomById(int roomId) {
for (Room room : rooms) {
if (room.getRoomId() == roomId) {
return room;
}
}
return null;
}
}
public class HotelManagementSystem {
public static void main(String[] args) {
Hotel hotel = new Hotel();
hotel.addRoom(new Room(101));
hotel.addRoom(new Room(102));
hotel.addRoom(new Room(103));
hotel.checkIn(101, "John Doe");
hotel.checkOut(102);
}
}
```
以上代码实现了一个简单的酒店客房管理系统。酒店类(`Hotel`)管理房间对象(`Room`)的列表,并提供了添加房间、入住和退房等功能。客房对象包含房间号、入住状态和客人姓名等属性,并提供了相应的 getter 和 setter 方法。
在示例的 `main` 方法中,创建了一个酒店对象并初始化了三个房间。然后进行了一次入住和退房操作。程序会根据房间的状态进行相应的处理,并在控制台输出操作结果。
阅读全文