java简易酒店管理系统
时间: 2023-12-06 08:38:39 浏览: 80
JAVA简易酒店管理系统
以下是一个简单的Java酒店管理系统的实现,包括房间预定、退房和查询房间状态等功能:
```java
import java.util.Scanner;
public class Hotel {
private Room[][] rooms; // 酒店房间数组
private int totalRooms; // 酒店总房间数
private int reservedRooms; // 预定的房间数
// 构造方法,初始化酒店房间数组
public Hotel(int rows, int cols) {
rooms = new Room[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
rooms[i][j] = new Room((i + 1) * 100 + (j + 1)); // 给每个房间编号
}
}
totalRooms = rows * cols;
reservedRooms = 0;
}
// 预定房间
public void reserveRoom(int roomNum) {
int row = (roomNum / 100) - 1;
int col = (roomNum % 100) - 1;
if (rooms[row][col].isAvailable()) {
rooms[row][col].reserve();
reservedRooms++;
System.out.println("预定成功!");
} else {
System.out.println("该房间已被预定,请选择其他房间!");
}
}
// 退房
public void checkout(int roomNum) {
int row = (roomNum / 100) - 1;
int col = (roomNum % 100) - 1;
if (!rooms[row][col].isAvailable()) {
rooms[row][col].checkout();
reservedRooms--;
System.out.println("退房成功!");
} else {
System.out.println("该房间未被预定,无法退房!");
}
}
// 查询房间状态
public void checkRoomStatus(int roomNum) {
int row = (roomNum / 100) - 1;
int col = (roomNum % 100) - 1;
if (rooms[row][col].isAvailable()) {
System.out.println("该房间未被预定!");
} else {
System.out.println("该房间已被预定!");
}
}
// 显示酒店房间状态
public void displayRoomStatus() {
System.out.println("酒店房间状态:");
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
System.out.print(rooms[i][j].getRoomNum() + ": ");
if (rooms[i][j].isAvailable()) {
System.out.println("空闲");
} else {
System.out.println("已预定");
}
}
}
System.out.println("总房间数:" + totalRooms);
System.out.println("已预定房间数:" + reservedRooms);
System.out.println("空闲房间数:" + (totalRooms - reservedRooms));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Hotel hotel = new Hotel(5, 5); // 创建一个5行5列的酒店
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 预定房间");
System.out.println("2. 退房");
System.out.println("3. 查询房间状态");
System.out.println("4. 显示酒店房间状态");
System.out.println("5. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入要预定的房间号:");
int roomNum = scanner.nextInt();
hotel.reserveRoom(roomNum);
break;
case 2:
System.out.println("请输入要退房的房间号:");
roomNum = scanner.nextInt();
hotel.checkout(roomNum);
break;
case 3:
System.out.println("请输入要查询的房间号:");
roomNum = scanner.nextInt();
hotel.checkRoomStatus(roomNum);
break;
case 4:
hotel.displayRoomStatus();
break;
case 5:
System.exit(0);
default:
System.out.println("无效的选择!");
}
}
}
}
class Room {
private int roomNum; // 房间号
private boolean available; // 房间是否空闲
public Room(int roomNum) {
this.roomNum = roomNum;
available = true;
}
public int getRoomNum() {
return roomNum;
}
public boolean isAvailable() {
return available;
}
public void reserve() {
available = false;
}
public void checkout() {
available = true;
}
}
```
阅读全文