用Java编写一个酒店管理程序实现查询房间功能、订房功能、退房功能、选择服务功能和结账功能
时间: 2023-12-24 16:06:14 浏览: 135
Java实现酒店管理系统的预订和退房,并支持查看酒店信息.zip
5星 · 资源好评率100%
好的,这里是一个简单的 Java 酒店管理程序的代码示例,它实现了查询房间、订房、退房、选择服务和结账等功能:
```java
import java.util.Scanner;
public class HotelManagementSystem {
private static final int NUM_ROOMS = 10; // 酒店房间数量
private static final double ROOM_PRICE = 100; // 房间价格
private static Room[] rooms; // 所有房间
private static double revenue; // 总收入
public static void main(String[] args) {
rooms = new Room[NUM_ROOMS];
for (int i = 0; i < NUM_ROOMS; i++) {
rooms[i] = new Room(i + 1, "Single", RoomStatus.VACANT, null, 0);
}
revenue = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
displayMenu();
int choice = scanner.nextInt();
switch (choice) {
case 1:
queryRooms();
break;
case 2:
bookRoom();
break;
case 3:
checkOut();
break;
case 4:
selectService();
break;
case 5:
checkOutAll();
break;
case 6:
displayRevenue();
break;
case 7:
System.out.println("程序已退出,欢迎下次再来!");
System.exit(0);
break;
default:
System.out.println("无效的操作,请重新输入!");
break;
}
}
}
// 显示程序菜单
private static void displayMenu() {
System.out.println("欢迎使用酒店管理系统!");
System.out.println("1. 查询房间");
System.out.println("2. 订房");
System.out.println("3. 退房");
System.out.println("4. 选择服务");
System.out.println("5. 结账");
System.out.println("6. 显示总收入");
System.out.println("7. 退出程序");
System.out.print("请选择操作(1-7):");
}
// 查询房间
private static void queryRooms() {
System.out.println("房间号\t房间类型\t房间状态\t客人姓名\t服务费用");
for (Room room : rooms) {
System.out.println(room);
}
}
// 订房
private static void bookRoom() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要订的房间号:");
int roomNumber = scanner.nextInt();
Room room = getRoom(roomNumber);
if (room == null) {
System.out.println("房间不存在,请重新输入!");
return;
}
if (room.getStatus() != RoomStatus.VACANT) {
System.out.println("该房间已被占用,请重新选择!");
return;
}
System.out.print("请输入客人姓名:");
String customerName = scanner.next();
room.setStatus(RoomStatus.BOOKED);
room.setCustomerName(customerName);
System.out.println("订房成功!");
}
// 退房
private static void checkOut() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要退的房间号:");
int roomNumber = scanner.nextInt();
Room room = getRoom(roomNumber);
if (room == null) {
System.out.println("房间不存在,请重新输入!");
return;
}
if (room.getStatus() == RoomStatus.VACANT) {
System.out.println("该房间未被占用,无法退房!");
return;
}
double serviceFee = room.getServiceFee();
revenue += ROOM_PRICE + serviceFee;
room.setStatus(RoomStatus.VACANT);
room.setCustomerName(null);
room.setServiceFee(0);
System.out.println("退房成功!");
}
// 选择服务
private static void selectService() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要选择服务的房间号:");
int roomNumber = scanner.nextInt();
Room room = getRoom(roomNumber);
if (room == null) {
System.out.println("房间不存在,请重新输入!");
return;
}
if (room.getStatus() == RoomStatus.VACANT) {
System.out.println("该房间未被占用,无法选择服务!");
return;
}
System.out.println("请选择服务类型:");
System.out.println("1. 早餐(10元)");
System.out.println("2. 洗衣(20元)");
System.out.println("3. 清洁(30元)");
int serviceType = scanner.nextInt();
double serviceFee = 0;
switch (serviceType) {
case 1:
serviceFee = 10;
break;
case 2:
serviceFee = 20;
break;
case 3:
serviceFee = 30;
break;
default:
System.out.println("无效的服务类型,请重新选择!");
return;
}
room.setServiceFee(room.getServiceFee() + serviceFee);
System.out.println("选择服务成功!");
}
// 退房所有房间
private static void checkOutAll() {
for (Room room : rooms) {
if (room.getStatus() != RoomStatus.VACANT) {
double serviceFee = room.getServiceFee();
revenue += ROOM_PRICE + serviceFee;
room.setStatus(RoomStatus.VACANT);
room.setCustomerName(null);
room.setServiceFee(0);
}
}
System.out.println("所有房间已退房!");
}
// 显示总收入
private static void displayRevenue() {
System.out.println("总收入为:" + revenue + "元");
}
// 根据房间号获取房间对象
private static Room getRoom(int roomNumber) {
for (Room room : rooms) {
if (room.getRoomNumber() == roomNumber) {
return room;
}
}
return null;
}
}
enum RoomStatus {
VACANT, BOOKED, OCCUPIED
}
class Room {
private int roomNumber;
private String roomType;
private RoomStatus status;
private String customerName;
private double serviceFee;
public Room(int roomNumber, String roomType, RoomStatus status, String customerName, double serviceFee) {
this.roomNumber = roomNumber;
this.roomType = roomType;
this.status = status;
this.customerName = customerName;
this.serviceFee = serviceFee;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public String getRoomType() {
return roomType;
}
public void setRoomType(String roomType) {
this.roomType = roomType;
}
public RoomStatus getStatus() {
return status;
}
public void setStatus(RoomStatus status) {
this.status = status;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public double getServiceFee() {
return serviceFee;
}
public void setServiceFee(double serviceFee) {
this.serviceFee = serviceFee;
}
@Override
public String toString() {
return roomNumber + "\t" + roomType + "\t\t" + status + "\t\t" + customerName + "\t\t" + serviceFee;
}
}
```
这个程序的具体实现细节可能还需要根据实际需求进行调整,但是基本的框架和思路应该是类似的。
阅读全文