宿舍管理系统用c++写出来
时间: 2023-07-19 07:11:06 浏览: 104
好的,以下是一个简单的宿舍管理系统的C++代码,包含了学生、宿舍房间和宿舍楼类的设计和实现,以及一些基本的操作函数。这个代码仅供参考,您可以根据实际需求进行修改和优化。
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 学生类
class Student {
public:
Student(string name, string id, string gender, int age):
m_name(name), m_id(id), m_gender(gender), m_age(age) {}
string getName() const { return m_name; }
string getId() const { return m_id; }
string getGender() const { return m_gender; }
int getAge() const { return m_age; }
private:
string m_name; // 姓名
string m_id; // 学号
string m_gender; // 性别
int m_age; // 年龄
};
// 宿舍房间类
class Room {
public:
Room(int id, int capacity): m_id(id), m_capacity(capacity) {}
int getId() const { return m_id; }
int getCapacity() const { return m_capacity; }
Student* getStudent() const { return m_student; }
void setStudent(Student* student) { m_student = student; }
private:
int m_id; // 房间号
int m_capacity; // 容纳人数
Student* m_student = nullptr; // 当前住户
};
// 宿舍楼类
class Building {
public:
Building(string name, int floors, int roomsPerFloor, int capacityPerRoom):
m_name(name), m_floors(floors), m_roomsPerFloor(roomsPerFloor), m_capacityPerRoom(capacityPerRoom) {
for (int i = 1; i <= floors; i++) {
for (int j = 1; j <= roomsPerFloor; j++) {
m_rooms.push_back(new Room(i * 100 + j, capacityPerRoom));
}
}
}
~Building() {
for (auto room : m_rooms) {
delete room;
}
}
string getName() const { return m_name; }
int getFloors() const { return m_floors; }
int getRoomsPerFloor() const { return m_roomsPerFloor; }
int getCapacityPerRoom() const { return m_capacityPerRoom; }
Room* getRoom(int roomId) const { return m_rooms[roomId - 1]; }
private:
string m_name; // 楼名
int m_floors; // 层数
int m_roomsPerFloor;// 每层房间数
int m_capacityPerRoom; // 每间房容纳人数
vector<Room*> m_rooms; // 房间列表
};
// 宿舍管理系统类
class DormitorySystem {
public:
DormitorySystem() {}
// 添加学生
void addStudent(Student* student) {
m_students.push_back(student);
}
// 查询学生信息
void findStudent(string id) {
for (auto student : m_students) {
if (student->getId() == id) {
cout << "Name: " << student->getName() << endl;
cout << "ID: " << student->getId() << endl;
cout << "Gender: " << student->getGender() << endl;
cout << "Age: " << student->getAge() << endl;
return;
}
}
cout << "Student not found!" << endl;
}
// 入住
void checkIn(string id, int roomId, Building* building) {
Student* student = findStudentById(id);
if (student == nullptr) {
cout << "Student not found!" << endl;
return;
}
Room* room = building->getRoom(roomId);
if (room->getStudent() != nullptr) {
cout << "Room is occupied!" << endl;
return;
}
if (room->getCapacity() < 1) {
cout << "Room is full!" << endl;
return;
}
room->setStudent(student);
cout << "Check in successfully!" << endl;
}
// 退房
void checkOut(string id, int roomId, Building* building) {
Student* student = findStudentById(id);
if (student == nullptr) {
cout << "Student not found!" << endl;
return;
}
Room* room = building->getRoom(roomId);
if (room->getStudent() == nullptr) {
cout << "Room is empty!" << endl;
return;
}
if (room->getStudent()->getId() != id) {
cout << "This student does not live in this room!" << endl;
return;
}
room->setStudent(nullptr);
cout << "Check out successfully!" << endl;
}
private:
vector<Student*> m_students; // 学生列表
// 根据学号查找学生
Student* findStudentById(string id) {
for (auto student : m_students) {
if (student->getId() == id) {
return student;
}
}
return nullptr;
}
};
int main() {
DormitorySystem system;
// 创建学生
Student* s1 = new Student("Alice", "1001", "female", 18);
Student* s2 = new Student("Bob", "1002", "male", 19);
Student* s3 = new Student("Charlie", "1003", "male", 20);
// 添加学生
system.addStudent(s1);
system.addStudent(s2);
system.addStudent(s3);
// 创建宿舍楼
Building* building1 = new Building("Building 1", 5, 10, 2);
Building* building2 = new Building("Building 2", 3, 8, 3);
// 入住和退房
system.checkIn("1001", 101, building1);
system.checkIn("1002", 201, building1);
system.checkIn("1003", 301, building1);
system.checkOut("1002", 201, building1);
system.checkIn("1002", 201, building1);
system.checkIn("1001", 101, building2);
system.checkIn("1002", 201, building2);
system.checkIn("1003", 301, building2);
system.checkOut("1001", 101, building2);
system.checkOut("1002", 201, building2);
// 查询学生信息
system.findStudent("1001");
system.findStudent("1002");
system.findStudent("1003");
// 释放内存
delete s1;
delete s2;
delete s3;
delete building1;
delete building2;
return 0;
}
```
这个代码实现了添加学生、查询学生信息、入住和退房等基本功能,同时使用了学生、宿舍房间和宿舍楼三个类来存储和管理信息。在实际应用中,您可能需要根据实际需求进行修改和优化。
阅读全文