用C++写高校员工信息管理系统,人员包括教师,实验员,行政人员,教师兼行政人员,功能包括添加,查询,显示,编辑,删除,统计,保存,读取
时间: 2023-08-04 11:07:02 浏览: 22
好的,这个高校员工信息管理系统可以分为以下几个模块:
1. 员工类(Teacher、LabStaff、Admin、TeacherAdmin):这些类都是继承于基类Employee,每个员工类都包含一些特定属性(如教师类包含授课科目等),以及一些公共属性(如姓名、性别、年龄等)和方法(如获取员工信息、修改员工信息等)。
2. 员工管理类(EmployeeManager):这个类用于实现对员工信息的添加、查询、显示、编辑、删除、统计、保存、读取等功能。其中,添加和编辑功能需要根据用户选择的员工类型创建相应的对象,并向对象中添加员工信息。查询和删除功能需要根据用户输入的员工ID进行查找。统计功能用于计算各类员工的数量和平均年龄等信息。保存和读取功能用于将员工信息存储到文件中或从文件中读取员工信息。
3. 界面类(Interface):这个类用于实现与用户交互的界面,包括菜单、输入输出等。
下面是一个简单的示例代码:
```cpp
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
// 员工基类
class Employee {
public:
Employee() {}
Employee(int id, string name, char sex, int age)
: m_id(id), m_name(name), m_sex(sex), m_age(age) {}
virtual void display() = 0; // 显示员工信息
int getId() { return m_id; }
string getName() { return m_name; }
char getSex() { return m_sex; }
int getAge() { return m_age; }
void setName(string name) { m_name = name; }
void setSex(char sex) { m_sex = sex; }
void setAge(int age) { m_age = age; }
protected:
int m_id; // 员工ID
string m_name; // 姓名
char m_sex; // 性别
int m_age; // 年龄
};
// 教师类
class Teacher : public Employee {
public:
Teacher() {}
Teacher(int id, string name, char sex, int age, string course)
: Employee(id, name, sex, age), m_course(course) {}
void display() {
cout << "ID: " << m_id << endl;
cout << "Name: " << m_name << endl;
cout << "Sex: " << m_sex << endl;
cout << "Age: " << m_age << endl;
cout << "Course: " << m_course << endl;
}
string getCourse() { return m_course; }
void setCourse(string course) { m_course = course; }
private:
string m_course; // 授课科目
};
// 实验员类
class LabStaff : public Employee {
public:
LabStaff() {}
LabStaff(int id, string name, char sex, int age, string lab)
: Employee(id, name, sex, age), m_lab(lab) {}
void display() {
cout << "ID: " << m_id << endl;
cout << "Name: " << m_name << endl;
cout << "Sex: " << m_sex << endl;
cout << "Age: " << m_age << endl;
cout << "Lab: " << m_lab << endl;
}
string getLab() { return m_lab; }
void setLab(string lab) { m_lab = lab; }
private:
string m_lab; // 实验室名称
};
// 行政人员类
class Admin : public Employee {
public:
Admin() {}
Admin(int id, string name, char sex, int age, string department)
: Employee(id, name, sex, age), m_department(department) {}
void display() {
cout << "ID: " << m_id << endl;
cout << "Name: " << m_name << endl;
cout << "Sex: " << m_sex << endl;
cout << "Age: " << m_age << endl;
cout << "Department: " << m_department << endl;
}
string getDepartment() { return m_department; }
void setDepartment(string department) { m_department = department; }
private:
string m_department; // 部门名称
};
// 教师兼行政人员类
class TeacherAdmin : public Teacher, public Admin {
public:
TeacherAdmin() {}
TeacherAdmin(int id, string name, char sex, int age, string course, string department)
: Employee(id, name, sex, age), Teacher(id, name, sex, age, course), Admin(id, name, sex, age, department) {}
void display() {
cout << "ID: " << m_id << endl;
cout << "Name: " << m_name << endl;
cout << "Sex: " << m_sex << endl;
cout << "Age: " << m_age << endl;
cout << "Course: " << Teacher::getCourse() << endl;
cout << "Department: " << Admin::getDepartment() << endl;
}
};
// 员工管理类
class EmployeeManager {
public:
EmployeeManager() {}
// 添加员工
void addEmployee() {
int id, age;
string name;
char sex, type;
cout << "Please input ID: ";
cin >> id;
cout << "Please input Name: ";
cin >> name;
cout << "Please input Sex(M/F): ";
cin >> sex;
cout << "Please input Age: ";
cin >> age;
cout << "Please select employee type(T/L/A/TA): ";
cin >> type;
Employee *employee = NULL;
switch (type) {
case 'T':
string course;
cout << "Please input Course: ";
cin >> course;
employee = new Teacher(id, name, sex, age, course);
break;
case 'L':
string lab;
cout << "Please input Lab: ";
cin >> lab;
employee = new LabStaff(id, name, sex, age, lab);
break;
case 'A':
string department;
cout << "Please input Department: ";
cin >> department;
employee = new Admin(id, name, sex, age, department);
break;
case 'TA':
string course_ta, department_ta;
cout << "Please input Course: ";
cin >> course_ta;
cout << "Please input Department: ";
cin >> department_ta;
employee = new TeacherAdmin(id, name, sex, age, course_ta, department_ta);
break;
default:
cout << "Invalid type!" << endl;
return;
}
m_employeeList.push_back(employee);
cout << "Add employee successfully!" << endl;
}
// 查询员工
void queryEmployee() {
int id;
cout << "Please input ID: ";
cin >> id;
for (int i = 0; i < m_employeeList.size(); i++) {
if (m_employeeList[i]->getId() == id) {
m_employeeList[i]->display();
return;
}
}
cout << "Employee not found!" << endl;
}
// 显示所有员工
void displayAllEmployee() {
for (int i = 0; i < m_employeeList.size(); i++) {
m_employeeList[i]->display();
cout << endl;
}
}
// 编辑员工信息
void editEmployee() {
int id;
cout << "Please input ID: ";
cin >> id;
for (int i = 0; i < m_employeeList.size(); i++) {
if (m_employeeList[i]->getId() == id) {
cout << "Please input new Name: ";
string name;
cin >> name;
cout << "Please input new Sex(M/F): ";
char sex;
cin >> sex;
cout << "Please input new Age: ";
int age;
cin >> age;
m_employeeList[i]->setName(name);
m_employeeList[i]->setSex(sex);
m_employeeList[i]->setAge(age);
cout << "Edit employee successfully!" << endl;
return;
}
}
cout << "Employee not found!" << endl;
}
// 删除员工
void deleteEmployee() {
int id;
cout << "Please input ID: ";
cin >> id;
for (vector<Employee*>::iterator it = m_employeeList.begin(); it != m_employeeList.end(); it++) {
if ((*it)->getId() == id) {
m_employeeList.erase(it);
cout << "Delete employee successfully!" << endl;
return;
}
}
cout << "Employee not found!" << endl;
}
// 统计员工信息
void statEmployee() {
int teacherCount = 0, labStaffCount = 0, adminCount = 0, teacherAdminCount = 0;
int totalAge = 0;
for (int i = 0; i < m_employeeList.size(); i++) {
if (dynamic_cast<Teacher*>(m_employeeList[i])) {
teacherCount++;
} else if (dynamic_cast<LabStaff*>(m_employeeList[i])) {
labStaffCount++;
} else if (dynamic_cast<Admin*>(m_employeeList[i])) {
adminCount++;
} else if (dynamic_cast<TeacherAdmin*>(m_employeeList[i])) {
teacherAdminCount++;
}
totalAge += m_employeeList[i]->getAge();
}
cout << "Teacher count: " << teacherCount << endl;
cout << "Lab staff count: " << labStaffCount << endl;
cout << "Admin count: " << adminCount << endl;
cout << "Teacher admin count: " << teacherAdminCount << endl;
cout << "Average age: " << totalAge / m_employeeList.size() << endl;
}
// 保存员工信息到文件
void saveToFile() {
ofstream ofs("employee.txt");
for (int i = 0; i < m_employeeList.size(); i++) {
ofs << m_employeeList[i]->getId() << " " << m_employeeList[i]->getName() << " "
<< m_employeeList[i]->getSex() << " " << m_employeeList[i]->getAge() << " ";
if (dynamic_cast<Teacher*>(m_employeeList[i])) {
ofs << "T " << dynamic_cast<Teacher*>(m_employeeList[i])->getCourse() << endl;
} else if (dynamic_cast<LabStaff*>(m_employeeList[i])) {
ofs << "L " << dynamic_cast<LabStaff*>(m_employeeList[i])->getLab() << endl;
} else if (dynamic_cast<Admin*>(m_employeeList[i])) {
ofs << "A " << dynamic_cast<Admin*>(m_employeeList[i])->getDepartment() << endl;
} else if (dynamic_cast<TeacherAdmin*>(m_employeeList[i])) {
ofs << "TA " << dynamic_cast<TeacherAdmin*>(m_employeeList[i])->getCourse() << " "
<< dynamic_cast<TeacherAdmin*>(m_employeeList[i])->getDepartment() << endl;
}
}
ofs.close();
cout << "Save to file successfully!" << endl;
}
// 从文件中读取员工信息
void readFromFile() {
ifstream ifs("employee.txt");
if (!ifs.is_open()) {
cout << "Open file failed!" << endl;
return;
}
while (!ifs.eof()) {
int id, age;
string name, course, lab, department;
char sex, type;
ifs >> id >> name >> sex >> age >> type;
switch (type) {
case 'T':
ifs >> course;
m_employeeList.push_back(new Teacher(id, name, sex, age, course));
break;
case 'L':
ifs >> lab;
m_employeeList.push_back(new LabStaff(id, name, sex, age, lab));
break;
case 'A':
ifs >> department;
m_employeeList.push_back(new Admin(id, name, sex, age, department));
break;
case 'TA':
ifs >> course >> department;
m_employeeList.push_back(new TeacherAdmin(id, name, sex, age, course, department));
break;
default:
break;
}
}
ifs.close();
cout << "Read from file successfully!" << endl;
}
private:
vector<Employee*> m_employeeList; // 员工列表
};
// 界面类
class Interface {
public:
Interface() {}
void showMenu() {
cout << "================ Employee Management System ================" << endl;
cout << "1. Add employee" << endl;
cout << "2. Query employee" << endl;
cout << "3. Display all employee" << endl;
cout << "4. Edit employee" << endl;
cout << "5. Delete employee" << endl;
cout << "6. Stat employee" << endl;
cout << "7. Save to file" << endl;
cout << "8. Read from file" << endl;
cout << "0. Exit" << endl;
cout << "============================================================" << endl;
}
void run() {
EmployeeManager em;
int choice;
while (true) {
showMenu();
cout << "Please input your choice: ";
cin >> choice;
switch (choice) {
case 1:
em.addEmployee();
break;
case 2:
em.queryEmployee();
break;
case 3:
em.displayAllEmployee();
break;
case 4:
em.editEmployee();
break;
case 5:
em.deleteEmployee();
break;
case 6:
em.statEmployee();
break;
case 7:
em.saveToFile();
break;
case 8:
em.readFromFile();
break;
case 0:
cout << "Thanks for using!" << endl;
return;
default:
cout << "Invalid choice!" << endl;
break;
}
}
}
};
int main() {
Interface ui;
ui.run();
return 0;
}
```
相关推荐
















