Java编写一个酒店管理程序,实现下功能:查询功能订房功能退房功能选择服务功能和结账功能并建立测试类
时间: 2024-03-23 22:36:39 浏览: 48
Java实现酒店管理系统的预订和退房,并支持查看酒店信息.zip
5星 · 资源好评率100%
好的,以下是我为您编写的Java酒店管理程序,实现了查询、订房、退房、选择服务和结账等功能,并且还包括了一个测试类:
```java
import java.util.Scanner;
public class HotelManagementSystem {
public static void main(String[] args) {
Hotel hotel = new Hotel(); // 创建酒店实例
Scanner input = new Scanner(System.in); // 创建Scanner对象
while (true) {
System.out.println("欢迎使用酒店管理系统,请输入对应数字进行操作:");
System.out.println("1.查询房间信息 2.订房 3.退房 4.选择服务 5.结账 6.退出系统");
int choice = input.nextInt(); // 获取用户输入的操作选项
switch (choice) {
case 1:
hotel.showRooms(); // 查询房间信息
break;
case 2:
hotel.bookRoom(); // 订房
break;
case 3:
hotel.checkOut(); // 退房
break;
case 4:
hotel.chooseService(); // 选择服务
break;
case 5:
hotel.bill(); // 结账
break;
case 6:
System.out.println("欢迎下次再来!");
System.exit(0); // 退出系统
break;
default:
System.out.println("输入有误,请重新输入!");
}
}
}
}
class Hotel {
private Room[] rooms; // 房间数组
private int numOfRooms; // 房间数量
private int[] services; // 服务数组
private int numOfServices; // 服务数量
private int[] bill; // 账单数组
private int totalBill; // 总账单金额
public Hotel() {
// 初始化酒店信息
numOfRooms = 10;
rooms = new Room[numOfRooms];
for (int i = 0; i < numOfRooms; i++) {
rooms[i] = new Room(i + 1, "单人间", 100, false);
}
numOfServices = 3;
services = new int[numOfServices];
for (int i = 0; i < numOfServices; i++) {
services[i] = 0;
}
bill = new int[numOfServices];
totalBill = 0;
}
// 查询房间信息
public void showRooms() {
System.out.println("房间号\t类型\t价格\t是否空闲");
for (int i = 0; i < numOfRooms; i++) {
System.out.println(rooms[i].getRoomNumber() + "\t" + rooms[i].getType() + "\t" + rooms[i].getPrice() + "\t" + rooms[i].isAvailable());
}
}
// 订房
public void bookRoom() {
System.out.println("请输入要订的房间号:");
Scanner input = new Scanner(System.in);
int roomNum = input.nextInt();
if (roomNum < 1 || roomNum > numOfRooms) {
System.out.println("房间号输入有误!");
return;
}
if (rooms[roomNum - 1].isAvailable()) {
rooms[roomNum - 1].setAvailable(false);
System.out.println("订房成功!");
} else {
System.out.println("该房间已经被占用!");
}
}
// 退房
public void checkOut() {
System.out.println("请输入要退的房间号:");
Scanner input = new Scanner(System.in);
int roomNum = input.nextInt();
if (roomNum < 1 || roomNum > numOfRooms) {
System.out.println("房间号输入有误!");
return;
}
if (!rooms[roomNum - 1].isAvailable()) {
rooms[roomNum - 1].setAvailable(true);
System.out.println("退房成功!");
} else {
System.out.println("该房间没有人入住!");
}
}
// 选择服务
public void chooseService() {
System.out.println("请选择服务:");
System.out.println("1.早餐 2.叫醒服务 3.洗衣服务");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
if (choice < 1 || choice > numOfServices) {
System.out.println("服务选择有误!");
return;
}
System.out.println("请输入服务费用:");
int cost = input.nextInt();
services[choice - 1] = 1;
bill[choice - 1] = cost;
System.out.println("服务添加成功!");
}
// 结账
public void bill() {
System.out.println("房费:" + getRoomBill());
for (int i = 0; i < numOfServices; i++) {
if (services[i] == 1) {
totalBill += bill[i];
}
}
System.out.println("服务费:" + totalBill);
System.out.println("总费用:" + (getRoomBill() + totalBill));
// 清空账单信息
for (int i = 0; i < numOfServices; i++) {
services[i] = 0;
bill[i] = 0;
}
totalBill = 0;
}
// 计算房费
private int getRoomBill() {
int roomBill = 0;
for (int i = 0; i < numOfRooms; i++) {
if (!rooms[i].isAvailable()) {
roomBill += rooms[i].getPrice();
}
}
return roomBill;
}
}
class Room {
private int roomNumber; // 房间号
private String type; // 房间类型
private int price; // 房间价格
private boolean available; // 房间是否空闲
public Room(int roomNumber, String type, int price, boolean available) {
this.roomNumber = roomNumber;
this.type = type;
this.price = price;
this.available = available;
}
public int getRoomNumber() {
return roomNumber;
}
public String getType() {
return type;
}
public int getPrice() {
return price;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
}
```
您可以将程序保存为HotelManagementSystem.java文件并编译运行。测试类已经在程序中包含,您可以根据需要进行修改。
阅读全文