给我编写一个复杂的学生教师一卡通管理系统
时间: 2024-03-06 12:52:10 浏览: 50
好的,下面是一个基于C++的学生教师一卡通管理系统的示例代码。该系统包括了充值、消费、查询余额、添加学生和教师等多个功能,并使用了SQLite作为数据库。
```c++
#include <iostream>
#include <string>
#include <sqlite3.h>
using namespace std;
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
cout << azColName[i] << ": " << argv[i] << endl;
}
cout << endl;
return 0;
}
class Card {
public:
Card(int id, string name, float balance) : id(id), name(name), balance(balance) {};
int getId() { return id; }
string getName() { return name; }
float getBalance() { return balance; }
void setBalance(float bal) { balance = bal; }
private:
int id;
string name;
float balance;
};
class Student : public Card {
public:
Student(int id, string name, float balance, string major) : Card(id, name, balance), major(major) {};
string getMajor() { return major; }
private:
string major;
};
class Teacher : public Card {
public:
Teacher(int id, string name, float balance, string department) : Card(id, name, balance), department(department) {};
string getDepartment() { return department; }
private:
string department;
};
class CardManager {
public:
CardManager() {
rc = sqlite3_open("test.db", &db);
if( rc ) {
cout << "Can't open database: " << sqlite3_errmsg(db) << endl;
} else {
cout << "Opened database successfully" << endl;
}
// create table
string sql = "CREATE TABLE CARD("
"ID INT PRIMARY KEY NOT NULL,"
"NAME TEXT NOT NULL,"
"TYPE TEXT NOT NULL,"
"BALANCE REAL NOT NULL);";
rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
cout << "SQL error: " << zErrMsg << endl;
sqlite3_free(zErrMsg);
} else {
cout << "Table created successfully" << endl;
}
}
~CardManager() {
sqlite3_close(db);
}
void addStudent(int id, string name, float balance, string major) {
Student s(id, name, balance, major);
cards.push_back(s);
string sql = "INSERT INTO CARD (ID, NAME, TYPE, BALANCE) "
"VALUES (" + to_string(id) + ", '" + name + "', 'student', " + to_string(balance) + ")";
rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
cout << "SQL error: " << zErrMsg << endl;
sqlite3_free(zErrMsg);
} else {
cout << "Student added successfully" << endl;
}
}
void addTeacher(int id, string name, float balance, string department) {
Teacher t(id, name, balance, department);
cards.push_back(t);
string sql = "INSERT INTO CARD (ID, NAME, TYPE, BALANCE) "
"VALUES (" + to_string(id) + ", '" + name + "', 'teacher', " + to_string(balance) + ")";
rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
cout << "SQL error: " << zErrMsg << endl;
sqlite3_free(zErrMsg);
} else {
cout << "Teacher added successfully" << endl;
}
}
void recharge(int id, float amount) {
for (auto& c : cards) {
if (c.getId() == id) {
float bal = c.getBalance() + amount;
c.setBalance(bal);
string sql = "UPDATE CARD SET BALANCE=" + to_string(bal) + " WHERE ID=" + to_string(id);
rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
cout << "SQL error: " << zErrMsg << endl;
sqlite3_free(zErrMsg);
} else {
cout << "Recharge successful" << endl;
}
return;
}
}
cout << "Card not found" << endl;
}
void consume(int id, float amount) {
for (auto& c : cards) {
if (c.getId() == id) {
float bal = c.getBalance() - amount;
if (bal < 0) {
cout << "Insufficient balance" << endl;
return;
}
c.setBalance(bal);
string sql = "UPDATE CARD SET BALANCE=" + to_string(bal) + " WHERE ID=" + to_string(id);
rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
cout << "SQL error: " << zErrMsg << endl;
sqlite3_free(zErrMsg);
} else {
cout << "Consumption successful" << endl;
}
return;
}
}
cout << "Card not found" << endl;
}
void queryBalance(int id) {
for (auto& c : cards) {
if (c.getId() == id) {
cout << "Balance of card " << id << ": " << c.getBalance() << endl;
return;
}
}
cout << "Card not found" << endl;
}
private:
vector<Card> cards;
sqlite3 *db;
char *zErrMsg = 0;
int rc;
};
int main() {
CardManager cm;
cm.addStudent(1001, "Tom", 200.0, "Computer Science");
cm.addTeacher(2001, "Jerry", 500.0, "Mathematics");
cm.recharge(1001, 50.0);
cm.consume(1001, 100.0);
cm.queryBalance(1001);
return 0;
}
```
该代码定义了三个类:Card、Student和Teacher,分别表示一张卡、学生卡和教师卡。CardManager类负责管理卡片,包括添加卡片、充值、消费和查询余额等操作,并使用了SQLite作为数据库来存储卡片信息。在main函数中,我们向CardManager中添加了一个学生卡和一个教师卡,并对学生卡进行了充值、消费和查询余额的操作。
您可以根据您的具体需求对该系统进行修改和扩展。希望能对您有所帮助!
阅读全文