c++校园卡管理系统
时间: 2023-12-27 22:23:51 浏览: 84
以下是一个简单的C++校园卡管理系统的实现,包括校园卡类和学生类,实现了消费、充值、转账、查询等功能:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
// 校园卡类
class CampusCard {
public:
CampusCard(string id, double balance = 0.0) : m_id(id), m_balance(balance) {}
// 充值
void recharge(double amount) {
m_balance += amount;
cout << "充值成功,当前余额为:" << m_balance << endl;
}
// 消费
bool consume(double amount) {
if (m_balance >= amount) {
m_balance -= amount;
cout << "消费成功,当前余额为:" << m_balance << endl;
m_consumeRecords.push_back(amount);
return true;
} else {
cout << "余额不足,消费失败" << endl;
return false;
}
}
// 转账
bool transfer(CampusCard& other, double amount) {
if (m_balance >= amount) {
m_balance -= amount;
other.m_balance += amount;
cout << "转账成功,当前余额为:" << m_balance << endl;
return true;
} else {
cout << "余额不足,转账失败" << endl;
return false;
}
}
// 获取余额
double getBalance() const {
return m_balance;
}
// 获取消费记录
vector<double> getConsumeRecords() const {
return m_consumeRecords;
}
// 获取校园卡号
string getId() const {
return m_id;
}
private:
string m_id; // 校园卡号
double m_balance; // 余额
vector<double> m_consumeRecords; // 消费记录
};
// 学生类
class Student {
public:
Student(string id, string name, string password) : m_id(id), m_name(name), m_password(password) {}
// 登录
bool login(string password) {
if (password == m_password) {
cout << "登录成功" << endl;
return true;
} else {
cout << "密码错误,登录失败" << endl;
return false;
}
}
// 挂失
void reportLoss() {
m_isLost = true;
cout << "挂失成功" << endl;
}
// 解挂
void cancelLoss() {
m_isLost = false;
cout << "解挂成功" << endl;
}
// 获取校园卡
CampusCard& getCampusCard() {
return m_campusCard;
}
// 获取学号
string getId() const {
return m_id;
}
// 获取姓名
string getName() const {
return m_name;
}
private:
string m_id; // 学号
string m_name; // 姓名
string m_password; // 密码
bool m_isLost = false; // 是否挂失
CampusCard m_campusCard; // 校园卡
};
// 根据学号查找学生
Student* findStudentById(const vector<Student>& students, string id) {
for (auto& student : students) {
if (student.getId() == id) {
return &student;
}
}
return nullptr;
}
// 保存学生信息到文件
void saveStudentsToFile(const vector<Student>& students, string filename) {
ofstream fout(filename);
for (auto& student : students) {
fout << student.getId() << " " << student.getName() << " " << student.getCampusCard().getId() << " " << student.getCampusCard().getBalance() << endl;
}
fout.close();
}
// 从文件中读取学生信息
vector<Student> readStudentsFromFile(string filename) {
vector<Student> students;
ifstream fin(filename);
string id, name, cardId;
double balance;
while (fin >> id >> name >> cardId >> balance) {
students.emplace_back(id, name, "123456"); // 初始密码为123456
students.back().getCampusCard() = CampusCard(cardId, balance);
}
fin.close();
return students;
}
int main() {
vector<Student> students = readStudentsFromFile("students.txt");
// 登录
string id, password;
cout << "请输入学号和密码进行登录:" << endl;
cin >> id >> password;
Student* student = findStudentById(students, id);
while (student == nullptr || !student->login(password)) {
cout << "登录失败,请重新输入学号和密码:" << endl;
cin >> id >> password;
student = findStudentById(students, id);
}
// 操作校园卡
CampusCard& campusCard = student->getCampusCard();
int choice;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 充值" << endl;
cout << "2. 消费" << endl;
cout << "3. 转账" << endl;
cout << "4. 查询余额" << endl;
cout << "5. 查询消费记录" << endl;
cout << "6. 挂失" << endl;
cout << "7. 解挂" << endl;
cout << "8. 退出" << endl;
cin >> choice;
switch (choice) {
case 1: {
double amount;
cout << "请输入充值金额:" << endl;
cin >> amount;
campusCard.recharge(amount);
break;
}
case 2: {
double amount;
cout << "请输入消费金额:" << endl;
cin >> amount;
campusCard.consume(amount);
break;
}
case 3: {
string otherId;
double amount;
cout << "请输入转账对象学号和金额:" << endl;
cin >> otherId >> amount;
Student* otherStudent = findStudentById(students, otherId);
if (otherStudent == nullptr) {
cout << "转账对象不存在" << endl;
} else {
CampusCard& otherCampusCard = otherStudent->getCampusCard();
campusCard.transfer(otherCampusCard, amount);
}
break;
}
case 4: {
cout << "当前余额为:" << campusCard.getBalance() << endl;
break;
}
case 5: {
vector<double> consumeRecords = campusCard.getConsumeRecords();
cout << "消费记录:" << endl;
for (auto record : consumeRecords) {
cout << record << " ";
}
cout << endl;
break;
}
case 6: {
student->reportLoss();
break;
}
case 7: {
student->cancelLoss();
break;
}
case 8: {
saveStudentsToFile(students, "students.txt");
cout << "退出成功" << endl;
return 0;
}
default: {
cout << "无效操作,请重新选择" << endl;
break;
}
}
}
return 0;
}
```
阅读全文