功能:1、操作台上出现一个界面,让操作员能够方便的选择所需要进行的操作,包括登记入住、查询房间的入住情况、查询当前费用、结账退房等操作。
2、对不同标准的房间实施不同的收费标准,也可以按天收费或按小时收费,可根据顾客需要在登记的入住的时候进行选择。
3、在结账退房时,根据入住的时间,计费方式和房间单价计算出总费用。
数据结构设计
首先,定义几个关键的数据结构来组织信息:
- Room类:表示一个房间,包含房间号、房间类型、入住时间、计费方式等信息。
- Hotel类:管理所有房间,提供操作接口,如登记入住、查询房间状态、计算费用等。
实现步骤
步骤1:定义Room类
#include <string>
#include <ctime>
// 定义房间的计费方式
enum class ChargeType {
ByHour, // 按小时计费
ByDay // 按天计费
};
// 定义房间类
class Room {
private:
int roomNumber; // 房间号
std::string roomType; // 房间类型
bool occupied; // 是否已被入住
time_t checkInTime; // 入住时间
ChargeType chargeType; // 计费方式
double rate; // 单价
public:
Room(int number, std::string type, ChargeType charge, double rate)
: roomNumber(number), roomType(type), occupied(false), checkInTime(0), chargeType(charge), rate(rate) {}
// 获取房间号
int getRoomNumber() const { return roomNumber; }
// 获取房间类型
std::string getRoomType() const { return roomType; }
// 获取房间是否已经入住
bool isOccupied() const { return occupied; }
// 入住房间,记录入住时间
void checkIn() {
occupied = true;
checkInTime = time(nullptr); // 使用当前时间作为入住时间
}
// 结账退房,计算费用
double checkOut() {
if (!occupied) {
return 0.0; // 房间未被入住,费用为0
}
time_t currentTime = time(nullptr);
double duration; // 入住时长
if (chargeType == ChargeType::ByHour) {
duration = difftime(currentTime, checkInTime) / 3600.0; // 换算为小时
} else {
duration = difftime(currentTime, checkInTime) / 86400.0; // 换算为天
}
double totalCharge = duration * rate;
// 清空房间信息
occupied = false;
checkInTime = 0;
return totalCharge;
}
};
步骤2:定义Hotel类
#include <iostream>
#include <vector>
#include "Room.h"
class Hotel {
private:
std::vector<Room> rooms; // 所有房间的容器
public:
Hotel(int numRooms) {
// 初始化指定数量的房间
for (int i = 1; i <= numRooms; ++i) {
// 这里简单起见,假设所有房间都是同一种类型和收费标准
Room room(i, "标准间", ChargeType::ByDay, 50.0); // 房间单价为50元/天
rooms.push_back(room);
}
}
// 显示所有房间的状态(是否被占用)
void displayRoomStatus() const {
std::cout << "房间状态:" << std::endl;
for (const auto& room : rooms) {
std::cout << "房间号:" << room.getRoomNumber() << ",";
if (room.isOccupied()) {
std::cout << "已入住" << std::endl;
} else {
std::cout << "空闲" << std::endl;
}
}
std::cout << std::endl;
}
// 登记入住
void checkIn(int roomNumber) {
rooms[roomNumber - 1].checkIn(); // 房间编号从1开始,而vector从0开始
std::cout << "成功登记入住房间号:" << roomNumber << std::endl;
}
// 结账退房
void checkOut(int roomNumber) {
double totalCharge = rooms[roomNumber - 1].checkOut(); // 房间编号从1开始,而vector从0开始
std::cout << "成功退房房间号:" << roomNumber << ",总费用:" << totalCharge << "元" << std::endl;
}
};
步骤3:主函数调用
#include <iostream>
#include "Hotel.h" // 引入Hotel类定义
int main() {
int numRooms = 10; // 假设酒店有10个房间
Hotel hotel(numRooms);
int choice;
int roomNumber;
do {
// 显示菜单
std::cout << "=== 酒店管理系统 ===" << std::endl;
std::cout << "1. 登记入住" << std::endl;
std::cout << "2. 结账退房" << std::endl;
std::cout << "3. 显示房间状态" << std::endl;
std::cout << "4. 退出" << std::endl;
std::cout << "请输入您的选择: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "请输入要登记入住的房间号: ";
std::cin >> roomNumber;
hotel.checkIn(roomNumber);
break;
case 2:
std::cout << "请输入要结账退房的房间号: ";
std::cin >> roomNumber;
hotel.checkOut(roomNumber);
break;
case 3:
hotel.displayRoomStatus();
break;
case 4:
std::cout << "正在退出程序..." << std::endl;
break;
default:
std::cout << "无效的选择,请重新输入。" << std::endl;
}
} while (choice != 4);
return 0;
}