图书馆座位预约管理系统的C++语言的代码
时间: 2023-10-11 16:13:01 浏览: 209
好的,以下是一个简单的C++实现的图书馆座位预约管理系统的代码,仅供参考:
```c++
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
// 座位类
class Seat {
public:
Seat(int id) {
id_ = id;
status_ = 0;
user_name_ = "";
}
int get_id() {
return id_;
}
int get_status() {
return status_;
}
string get_user_name() {
return user_name_;
}
void reserve(string user_name) {
status_ = 1;
user_name_ = user_name;
}
void cancel_reservation() {
status_ = 0;
user_name_ = "";
}
private:
int id_; // 座位ID
int status_; // 座位状态:0-未预约,1-已预约
string user_name_; // 预约用户名
};
// 用户类
class User {
public:
User(string name) {
name_ = name;
seat_id_ = -1;
}
string get_name() {
return name_;
}
int get_seat_id() {
return seat_id_;
}
void reserve_seat(int seat_id) {
seat_id_ = seat_id;
}
void cancel_reservation() {
seat_id_ = -1;
}
private:
string name_; // 用户名
int seat_id_; // 预约的座位ID
};
// 座位管理类
class SeatManager {
public:
SeatManager(int num_seats) {
for (int i = 1; i <= num_seats; i++) {
Seat seat(i);
seats_.push_back(seat);
}
}
void show_all_seats() {
cout << "所有座位信息:" << endl;
cout << "座位ID\t状态\t预约用户" << endl;
for (int i = 0; i < seats_.size(); i++) {
cout << seats_[i].get_id() << "\t";
cout << seats_[i].get_status() << "\t";
cout << seats_[i].get_user_name() << endl;
}
}
void reserve_seat(string user_name, int seat_id) {
if (seat_id < 1 || seat_id > seats_.size()) {
cout << "座位ID无效!" << endl;
return;
}
Seat *seat = &seats_[seat_id - 1];
if (seat->get_status() == 1) {
cout << "该座位已经被预约了!" << endl;
return;
}
seat->reserve(user_name);
cout << "预约成功!" << endl;
}
void cancel_reservation(string user_name) {
for (int i = 0; i < users_.size(); i++) {
if (users_[i].get_name() == user_name) {
int seat_id = users_[i].get_seat_id();
if (seat_id != -1) {
Seat *seat = &seats_[seat_id - 1];
seat->cancel_reservation();
cout << "取消预约成功!" << endl;
} else {
cout << "您没有预约任何座位!" << endl;
}
users_[i].cancel_reservation();
return;
}
}
cout << "您没有预约任何座位!" << endl;
}
void add_user(string user_name) {
User user(user_name);
users_.push_back(user);
}
void reserve_seat(string user_name, string seat_id_str) {
int seat_id = stoi(seat_id_str);
reserve_seat(user_name, seat_id);
}
void cancel_reservation(string user_name, string seat_id_str) {
int seat_id = stoi(seat_id_str);
Seat *seat = &seats_[seat_id - 1];
if (seat->get_user_name() != user_name) {
cout << "您没有预约该座位!" << endl;
return;
}
seat->cancel_reservation();
cout << "取消预约成功!" << endl;
}
private:
vector<Seat> seats_; // 座位数组
vector<User> users_; // 用户数组
};
// 用户管理类
class UserManager {
public:
void add_user(string user_name) {
if (users_.find(user_name) != users_.end()) {
cout << "该用户名已存在!" << endl;
return;
}
SeatManager *seat_manager = get_seat_manager();
if (seat_manager != nullptr) {
seat_manager->add_user(user_name);
users_.insert(pair<string, SeatManager *>(user_name, seat_manager));
}
cout << "用户添加成功!" << endl;
}
void reserve_seat(string user_name, string seat_id_str) {
if (users_.find(user_name) == users_.end()) {
cout << "该用户不存在!" << endl;
return;
}
SeatManager *seat_manager = users_[user_name];
seat_manager->reserve_seat(user_name, seat_id_str);
}
void cancel_reservation(string user_name, string seat_id_str) {
if (users_.find(user_name) == users_.end()) {
cout << "该用户不存在!" << endl;
return;
}
SeatManager *seat_manager = users_[user_name];
seat_manager->cancel_reservation(user_name, seat_id_str);
}
private:
SeatManager *get_seat_manager() {
int num_seats;
cout << "请输入座位数:";
cin >> num_seats;
if (num_seats <= 0) {
cout << "座位数必须大于0!" << endl;
return nullptr;
}
SeatManager *seat_manager = new SeatManager(num_seats);
return seat_manager;
}
map<string, SeatManager *> users_; // 用户名和座位管理器的映射
};
int main() {
UserManager user_manager;
while (1) {
cout << "请选择操作:" << endl;
cout << "1. 添加用户" << endl;
cout << "2. 预约座位" << endl;
cout << "3. 取消预约" << endl;
cout << "4. 查看所有座位信息" << endl;
cout << "5. 退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: {
string user_name;
cout << "请输入用户名:";
cin >> user_name;
user_manager.add_user(user_name);
break;
}
case 2: {
string user_name, seat_id;
cout << "请输入用户名:";
cin >> user_name;
cout << "请输入座位ID:";
cin >> seat_id;
user_manager.reserve_seat(user_name, seat_id);
break;
}
case 3: {
string user_name, seat_id;
cout << "请输入用户名:";
cin >> user_name;
cout << "请输入座位ID:";
cin >> seat_id;
user_manager.cancel_reservation(user_name, seat_id);
break;
}
case 4: {
SeatManager seat_manager(0);
seat_manager.show_all_seats();
break;
}
case 5: {
cout << "再见!" << endl;
exit(0);
}
default: {
cout << "无效操作!" << endl;
break;
}
}
}
return 0;
}
```
这个代码示例使用了面向对象的设计,分别定义了座位类、用户类、座位管理类、用户管理类,以及一个主函数来处理用户的输入和输出。其中,座位管理类和用户管理类都包含一个座位数组和一个用户数组,座位数组用于存储所有座位的状态和预约信息,用户数组用于存储所有用户的预约信息。
如果您需要实现更复杂的功能,可以在这个基础上进行扩展。
阅读全文