使用面向对象C++写一份校园卡管理系统,要求文件输入输出,面向对象
时间: 2023-06-19 15:10:18 浏览: 87
好的,下面是一份简单的校园卡管理系统的代码,实现了学生信息的录入、修改、删除、查询和显示功能。代码中使用了面向对象的思想,实现了类的封装和继承,并且使用文件输入输出实现了信息的持久化存储。
```c++
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// 学生类
class Student {
public:
Student() {}
Student(string id, string name, double balance) : id(id), name(name), balance(balance) {}
void setId(string id) {
this->id = id;
}
void setName(string name) {
this->name = name;
}
void setBalance(double balance) {
this->balance = balance;
}
string getId() const {
return id;
}
string getName() const {
return name;
}
double getBalance() const {
return balance;
}
friend ostream& operator<<(ostream& os, const Student& s) {
os << s.id << " " << s.name << " " << s.balance << endl;
return os;
}
private:
string id; // 学号
string name; // 姓名
double balance; // 余额
};
// 学生信息管理系统类
class StudentManager {
public:
void addStudent(const Student& s) {
students.push_back(s);
}
void deleteStudent(const string& id) {
for (auto it = students.begin(); it != students.end(); ++it) {
if (it->getId() == id) {
students.erase(it);
break;
}
}
}
void modifyStudent(const string& id, const string& name, double balance) {
for (auto& s : students) {
if (s.getId() == id) {
s.setName(name);
s.setBalance(balance);
break;
}
}
}
Student* getStudent(const string& id) {
for (auto& s : students) {
if (s.getId() == id) {
return &s;
}
}
return nullptr;
}
void displayAllStudents() {
for (auto& s : students) {
cout << s;
}
}
void saveToFile(const string& filename) {
ofstream outfile(filename);
for (auto& s : students) {
outfile << s;
}
outfile.close();
}
void loadFromFile(const string& filename) {
ifstream infile(filename);
if (infile) {
students.clear();
string id, name;
double balance;
while (infile >> id >> name >> balance) {
students.push_back(Student(id, name, balance));
}
infile.close();
}
}
private:
vector<Student> students; // 学生信息列表
};
// 主函数
int main() {
StudentManager manager;
manager.loadFromFile("students.txt");
int choice = 0;
do {
cout << "-----------------------------" << endl;
cout << "1. 录入学生信息" << endl;
cout << "2. 修改学生信息" << endl;
cout << "3. 删除学生信息" << endl;
cout << "4. 查询学生信息" << endl;
cout << "5. 显示所有学生信息" << endl;
cout << "6. 退出程序" << endl;
cout << "请输入您的选择(1-6):" << endl;
cin >> choice;
switch (choice) {
case 1: {
string id, name;
double balance;
cout << "请输入学生学号:" << endl;
cin >> id;
cout << "请输入学生姓名:" << endl;
cin >> name;
cout << "请输入学生余额:" << endl;
cin >> balance;
manager.addStudent(Student(id, name, balance));
manager.saveToFile("students.txt");
cout << "学生信息录入成功!" << endl;
break;
}
case 2: {
string id, name;
double balance;
cout << "请输入要修改的学生学号:" << endl;
cin >> id;
Student* s = manager.getStudent(id);
if (s) {
cout << "请输入学生姓名:" << endl;
cin >> name;
cout << "请输入学生余额:" << endl;
cin >> balance;
manager.modifyStudent(id, name, balance);
manager.saveToFile("students.txt");
cout << "学生信息修改成功!" << endl;
}
else {
cout << "没有找到对应的学生信息!" << endl;
}
break;
}
case 3: {
string id;
cout << "请输入要删除的学生学号:" << endl;
cin >> id;
manager.deleteStudent(id);
manager.saveToFile("students.txt");
cout << "学生信息删除成功!" << endl;
break;
}
case 4: {
string id;
cout << "请输入要查询的学生学号:" << endl;
cin >> id;
Student* s = manager.getStudent(id);
if (s) {
cout << "学号:" << s->getId() << " 姓名:" << s->getName() << " 余额:" << s->getBalance() << endl;
}
else {
cout << "没有找到对应的学生信息!" << endl;
}
break;
}
case 5: {
manager.displayAllStudents();
break;
}
case 6: {
cout << "程序已退出!" << endl;
break;
}
default: {
cout << "输入的选项不合法,请重新输入!" << endl;
break;
}
}
} while (choice != 6);
return 0;
}
```
在该代码中,我们定义了一个学生类 `Student`,其中包括学号、姓名和余额等信息,并且提供了成员函数 `setId`、`setName`、`setBalance`、`getId`、`getName`、`getBalance` 和 `operator<<` 等方法,用于设置和获取学生信息,以及输出学生信息。
我们还定义了一个学生信息管理系统类 `StudentManager`,其中包括添加学生、删除学生、修改学生、查询学生、显示所有学生和保存到文件等方法。在该类中,我们使用了 `vector` 容器来存储学生信息,实现了对学生信息的增删改查和显示,并且使用了文件输入输出实现了信息的持久化存储。
最后,在主函数中,我们通过循环菜单来实现用户对学生信息的操作,并且在每次操作后都将学生信息保存到文件中,以保证信息的持久化存储。
以上就是一份简单的校园卡管理系统的代码。当然,该代码还有很多可以改进和优化的地方,比如可以加入输入验证、异常处理等机制,以提高系统的健壮性和可靠性。