请用C++写一段实现景区预约管理系统的代码
时间: 2023-11-09 09:05:31 浏览: 93
以下是用C++实现景区预约管理系统的简单代码示例:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义预约信息结构体
struct Reservation {
string name; // 预约人姓名
int age; // 预约人年龄
string id; // 预约人身份证号
int num; // 预约人数量
string date; // 预约日期
};
// 定义景区类
class ScenicSpot {
private:
int limit; // 每日游客人数上限
vector<Reservation> reservations; // 预约信息列表
public:
ScenicSpot(int limit) {
this->limit = limit;
}
// 添加预约信息
bool addReservation(Reservation reservation) {
// 检查预约日期是否已满
int count = 0;
for (Reservation r : reservations) {
if (r.date == reservation.date) {
count += r.num;
}
}
if (count + reservation.num > limit) {
cout << "预约失败:该日期的预约人数已满!" << endl;
return false;
}
// 添加预约信息
reservations.push_back(reservation);
cout << "预约成功!" << endl;
return true;
}
// 取消预约
bool cancelReservation(string name, string id, string date) {
for (int i = 0; i < reservations.size(); i++) {
if (reservations[i].name == name &&
reservations[i].id == id &&
reservations[i].date == date) {
reservations.erase(reservations.begin() + i);
cout << "取消预约成功!" << endl;
return true;
}
}
cout << "取消预约失败:未找到该预约信息!" << endl;
return false;
}
// 查询预约信息
void queryReservation(string name, string id) {
for (Reservation r : reservations) {
if (r.name == name && r.id == id) {
cout << "姓名:" << r.name << endl;
cout << "年龄:" << r.age << endl;
cout << "身份证号:" << r.id << endl;
cout << "预约人数:" << r.num << endl;
cout << "预约日期:" << r.date << endl;
return;
}
}
cout << "未找到该预约信息!" << endl;
}
// 获取剩余可预约名额
int getRemainingCapacity(string date) {
int count = 0;
for (Reservation r : reservations) {
if (r.date == date) {
count += r.num;
}
}
return limit - count;
}
};
int main() {
ScenicSpot scenicSpot(100); // 创建一个每日游客人数上限为100的景区
// 添加预约信息
Reservation reservation1 = {"张三", 25, "310101199001010001", 3, "2022-05-01"};
scenicSpot.addReservation(reservation1);
Reservation reservation2 = {"李四", 30, "310101199201020002", 2, "2022-05-01"};
scenicSpot.addReservation(reservation2);
// 取消预约
scenicSpot.cancelReservation("张三", "310101199001010001", "2022-05-01");
// 查询预约信息
scenicSpot.queryReservation("李四", "310101199201020002");
// 获取剩余可预约名额
int remainingCapacity = scenicSpot.getRemainingCapacity("2022-05-01");
cout << "2022-05-01 剩余可预约名额:" << remainingCapacity << endl;
return 0;
}
```
以上代码只是一个简单的示例,实际的景区预约管理系统还需要更完善的功能和更复杂的实现。
阅读全文