13.宾馆客房管理系统 题目描述:设计一个宾馆客房管理系统,以菜单方式工作。 功能需求: (1)设计一个房间类Room,记录的客房信息包括客房号、客房类型、客房价格、空闲情况、客人姓名、客人身份证号、入住时间、退房时间,预付费用等信息。 (2)客房信息的录入功能。 (3)客人入住登记。 (4)客房信息浏览功能,浏览全部客房的信息。支持按照空房、姓名、身份证号、房间号查询。 (5)客人退房结算。 (6)使用外部文件存储相关数据。
时间: 2024-03-11 11:51:39 浏览: 96
以下是一个简单的宾馆客房管理系统的设计,实现了题目所要求的全部功能:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
class Room {
public:
Room(string id = "", string type = "", double price = 0.0, bool empty = true, string name = "", string idNumber = "", string checkInTime = "", string checkOutTime = "", double prepaidFee = 0.0) {
this->id = id;
this->type = type;
this->price = price;
this->empty = empty;
this->name = name;
this->idNumber = idNumber;
this->checkInTime = checkInTime;
this->checkOutTime = checkOutTime;
this->prepaidFee = prepaidFee;
}
void display() {
cout << "客房号:" << id << endl;
cout << "客房类型:" << type << endl;
cout << "客房价格:" << price << endl;
cout << "空闲情况:" << (empty ? "空闲" : "已入住") << endl;
if (!empty) {
cout << "客人姓名:" << name << endl;
cout << "客人身份证号:" << idNumber << endl;
cout << "入住时间:" << checkInTime << endl;
cout << "退房时间:" << checkOutTime << endl;
cout << "预付费用:" << prepaidFee << endl;
}
}
string getId() const {
return id;
}
string getType() const {
return type;
}
double getPrice() const {
return price;
}
bool isEmpty() const {
return empty;
}
void setName(string name) {
this->name = name;
}
void setIdNumber(string idNumber) {
this->idNumber = idNumber;
}
void setCheckInTime() {
time_t t = time(nullptr);
tm *localTime = localtime(&t);
char buffer[80];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localTime);
checkInTime = buffer;
}
void setCheckOutTime() {
time_t t = time(nullptr);
tm *localTime = localtime(&t);
char buffer[80];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localTime);
checkOutTime = buffer;
}
void setPrepaidFee(double prepaidFee) {
this->prepaidFee = prepaidFee;
}
private:
string id; // 客房号
string type; // 客房类型
double price; // 客房价格
bool empty; // 空闲情况
string name; // 客人姓名
string idNumber; // 客人身份证号
string checkInTime; // 入住时间
string checkOutTime; // 退房时间
double prepaidFee; // 预付费用
};
vector<Room> rooms;
// 读取客房信息
void loadRooms() {
ifstream fin("rooms.txt");
if (fin) {
string id, type, name, idNumber, checkInTime, checkOutTime;
double price, prepaidFee;
bool empty;
while (fin >> id >> type >> price >> empty >> name >> idNumber >> checkInTime >> checkOutTime >> prepaidFee) {
Room room(id, type, price, empty, name, idNumber, checkInTime, checkOutTime, prepaidFee);
rooms.push_back(room);
}
fin.close();
}
}
// 保存客房信息
void saveRooms() {
ofstream fout("rooms.txt");
if (fout) {
for (const auto &room : rooms) {
fout << room.getId() << " " << room.getType() << " " << room.getPrice() << " " << room.isEmpty() << " " << room.getName() << " " << room.getIdNumber() << " " << room.getCheckInTime() << " " << room.getCheckOutTime() << " " << room.getPrepaidFee() << endl;
}
fout.close();
}
}
// 客房信息录入
void addRoom() {
string id, type;
double price;
cout << "请输入客房号:";
cin >> id;
cout << "请输入客房类型:";
cin >> type;
cout << "请输入客房价格:";
cin >> price;
Room room(id, type, price);
rooms.push_back(room);
saveRooms();
cout << "录入成功!" << endl;
}
// 客人入住登记
void checkIn() {
string id, name, idNumber;
cout << "请输入客房号:";
cin >> id;
auto it = find_if(rooms.begin(), rooms.end(), [id](const Room &room) { return room.getId() == id; });
if (it == rooms.end()) {
cout << "客房不存在!" << endl;
return;
}
if (!it->isEmpty()) {
cout << "客房已入住!" << endl;
return;
}
cout << "请输入客人姓名:";
cin >> name;
cout << "请输入客人身份证号:";
cin >> idNumber;
it->setName(name);
it->setIdNumber(idNumber);
it->setCheckInTime();
it->setPrepaidFee(0.0);
it->display();
saveRooms();
cout << "登记成功!" << endl;
}
// 客房信息浏览
void displayRooms() {
int choice;
cout << "请选择查询方式:1.空房查询 2.姓名查询 3.身份证号查询 4.客房号查询:";
cin >> choice;
switch (choice) {
case 1: {
cout << "空房列表:" << endl;
for (const auto &room : rooms) {
if (room.isEmpty()) {
room.display();
cout << endl;
}
}
break;
}
case 2: {
string name;
cout << "请输入客人姓名:";
cin >> name;
cout << "查询结果:" << endl;
for (const auto &room : rooms) {
if (!room.isEmpty() && room.getName() == name) {
room.display();
cout << endl;
}
}
break;
}
case 3: {
string idNumber;
cout << "请输入客人身份证号:";
cin >> idNumber;
cout << "查询结果:" << endl;
for (const auto &room : rooms) {
if (!room.isEmpty() && room.getIdNumber() == idNumber) {
room.display();
cout << endl;
}
}
break;
}
case 4: {
string id;
cout << "请输入客房号:";
cin >> id;
cout << "查询结果:" << endl;
for (const auto &room : rooms) {
if (room.getId() == id) {
room.display();
cout << endl;
}
}
break;
}
default: {
cout << "无效选择!" << endl;
break;
}
}
}
// 客人退房结算
void checkOut() {
string id;
cout << "请输入客房号:";
cin >> id;
auto it = find_if(rooms.begin(), rooms.end(), [id](const Room &room) { return room.getId() == id; });
if (it == rooms.end()) {
cout << "客房不存在!" << endl;
return;
}
if (it->isEmpty()) {
cout << "客房未入住!" << endl;
return;
}
double fee = it->getPrepaidFee() + it->getPrice();
it->setEmpty(true);
it->setName("");
it->setIdNumber("");
it->setCheckOutTime();
it->setPrepaidFee(0.0);
it->display();
saveRooms();
cout << "结算成功!应付款:" << fee << endl;
}
int main() {
loadRooms();
while (true) {
int choice;
cout << "------------------------" << endl;
cout << "1.客房信息录入" << endl;
cout << "2.客人入住登记" << endl;
cout << "3.客房信息浏览" << endl;
cout << "4.客人退房结算" << endl;
cout << "0.退出系统" << endl;
cout << "------------------------" << endl;
cout << "请选择功能:";
cin >> choice;
switch (choice) {
case 1: {
addRoom();
break;
}
case 2: {
checkIn();
break;
}
case 3: {
displayRooms();
break;
}
case 4: {
checkOut();
break;
}
case 0: {
cout << "谢谢使用!" << endl;
return 0;
}
default: {
cout << "无效选择!" << endl;
break;
}
}
}
}
```
这段代码中,客房信息使用Room类来表示,包含了客房号、客房类型、客房价格、空闲情况、客人姓名、客人身份证号、入住时间、退房时间和预付费用等信息。使用vector来保存所有客房的信息,并通过文件来进行持久化存储。
程序提供了四个功能:客房信息录入、客人入住登记、客房信息浏览和客人退房结算。客房信息录入和客人入住登记会改变客房的状态,因此需要保存客房信息。客房信息浏览提供了四种查询方式,可以按照空房、姓名、身份证号和客房号进行查询。客人退房结算会计算客人应付的费用,并将客房状态变为空闲。
阅读全文