卡号管理系统

wangyihang-xh / 2024-01-27 / 原文

#include <iostream>
#include <fstream>
using namespace std;
class card{
    //属性
    public:
        string name;
        int id;
        string classroom;
        string phone;
        float money;
        float consumption1;
        string creat_time;
        int lost = 0;
        //方法 
        //消费
        bool consumption(float m){
            if(money>=m && m>0){
                return true;
            }
            return  false;
        } 
}; 
int main(int argc, char** argv) {
    card cardlist[100];
    int startid = 1000;
    int number = 0;//当前有几个人办卡
    int toplimit = 100; 
    ifstream file;
    string data;
    file.open("1.txt");
    while(getline(file,data)){
        card c;
        if(data.length()>10){
            c.id = startid+number;
            c.name = data.substr(0,data.find("|"));
            data = data.substr(data.find("|")+1);
            c.classroom = data.substr(0,data.find("|"));
            data = data.substr(data.find("|")+1);
            c.phone = data.substr(0,data.find("|"));
            data = data.substr(data.find("|")+1);
            c.money = stof(data.substr(0,data.find("|")));
            data = data.substr(data.find("|")+1);
            c.lost = stoi(data.substr(0,data.find("|")));
            data = " ";
            cardlist[number] = c;
            number++;
            /*int i = 0,length;
            c.id = startid+number;
            do{
                length = data.find("|");
                if(i==0){
                    c.name = data.substr(0,length);
                }else if(i==1){
                    c.classroom = data.substr(0,length);
                }else if(i==2){
                    c.phone = data.substr(0,length);
                }else if(i==3){
                    c.money = stof(data.substr(0,length));
                }else if(i==4){
                    c.lost = stoi(data.substr(0,length));
                }
                data = data.substr(length+1);
                i++;
            }while(length>0 && length<=data.length());
            cardlist[number] = c;
            number++;*/
        }
        if(file.eof()){
            break;        
        }
    }
    file.close();
    while(1){
        cout<<"太康一高附属学校充值系统"<<endl
            <<"*********1.开卡*********"<<endl
            <<"*********2.充值*********"<<endl
            <<"*********3.消费*********"<<endl
            <<"*********4.修改信息*****"<<endl
            <<"*********5.查询信息*****"<<endl
            <<"*********6.查询余额*****"<<endl
            <<"*********7.挂失*********"<<endl
            <<"*********8.退钱*********"<<endl
            <<"*********9.查询人数*****"<<endl
            <<"*********10.退出系统****"<<endl;
        int index;
        cin>>index;
        card c;
        bool have;
        float money;
        int _cid;
        string _name;
        string _classroom; 
        string _phone;
        switch(index){
            case 1:                
                cout<<"请输入你的姓名:";
                cin>>c.name;
                cout<<"请输入你的班级:";
                cin>>c.classroom;
                cout<<"请输入你的手机号:";
                cin>>c.phone;
                c.id = startid+number;
                c.money = 0;
                cardlist[number] = c;
                cout<<"创卡成功,你的卡号是:"<<c.id<<endl;
                number++;
                break;
            case 2:
                cout<<"请输入你的卡号:";
                cin>>_cid;
                have = false;
                for(int i = 0;i<=number;i++){
                    if(cardlist[i].lost){
                        cout<<cardlist[i].name<<"同学,该卡号已挂失"<<endl;
                        have = true;
                        break;
                    }
                    if(cardlist[i].id==_cid){
                        cout<<cardlist[i].name<<"同学你好,请输入充值金额:";
                        cin>>money;
                        cardlist[i].money += money;
                        have = true;
                        cout<<"充值成功"<<endl;
                        break;
                    } 
                } 
                if(have==false){
                    cout<<"对不起,没有该卡号"<<endl;
                }
                break;
            case 3:
                cout<<"请输入你的卡号:";
                cin>>_cid;
                have = false;
                for(int i = 0;i<=number;i++){
                    if(cardlist[i].lost){
                        cout<<cardlist[i].name<<"同学,该卡号已挂失"<<endl;
                        have = true;
                        break;
                    }
                    if(cardlist[i].id==_cid){
                        cout<<cardlist[i].name<<"同学你好,请输入消费金额:";
                        cin>>money;
                        if(!cardlist[i].consumption(money) || cardlist[i].consumption1>toplimit){
                            cout<<"您因某种原因而不能消费"<<endl;
                            have = true;
                            break;
                        }else{
                            cardlist[i].money -= money;
                        }
                        have = true;
                        cardlist[i].consumption1 += money;
                        cout<<"消费成功"<<endl;
                        cout<<"您当前的余额为:"<<cardlist[i].money<<endl;
                        break;
                    } 
                }
                if(have==false){
                    cout<<"对不起,没有该卡号"<<endl;
                }
                break;
            case 4:
                cout<<"请输入你要修改信息的卡号:";
                cin>>_cid;
                have = false;
                for(int i = 0;i<=number;i++){
                    if(cardlist[i].lost){
                        cout<<cardlist[i].name<<"同学,该卡号已挂失"<<endl;
                        have = true;
                        break;
                    }
                    if(cardlist[i].id==_cid){
                        cout<<"请输入你要修改的信息(1姓名,2班级,3手机号):";
                        int _index;
                        cin>>_index;
                        switch(_index){
                            case 1:
                                cout<<"请输入你的姓名:";
                                cin>>_name;
                                if(cardlist[i].name==_name){
                                    cout<<"请输入你要修改的姓名:";
                                    cin>>_name;
                                    cardlist[i].name = _name;
                                    cout<<"修改成功"<<endl;
                                    have = true;
                                }else{
                                    cout<<"您的姓名和办此卡人的姓名不一致,因此您不能修改信息"<<endl;
                                    have = true;
                                    break;
                                }
                                break;
                            case 2:
                                cout<<"请输入你的班级:";
                                cin>>_classroom;
                                if(cardlist[i].classroom==_classroom){
                                    cout<<"请输入你要修改的班级:";
                                    cin>>_classroom;
                                    cardlist[i].classroom = _classroom;
                                    cout<<"修改成功"<<endl;
                                    have = true;
                                }else{
                                    cout<<"您的班级和办此卡人的班级不一致,因此您不能修改信息"<<endl;
                                    have = true;
                                    break;
                                }
                                break;
                            case 3:
                                cout<<"请输入你的手机号:";
                                cin>>_name;
                                if(cardlist[i].phone==_phone){
                                    cout<<"请输入你要修改的手机号:";
                                    cin>>_phone;
                                    cardlist[i].phone = _phone;
                                    cout<<"修改成功"<<endl;
                                    have = true;
                                }else{
                                    cout<<"您的手机号和办此卡人的手机号不一致,因此您不能修改信息"<<endl;
                                    have = true;
                                    break;
                                }
                                break;
                        }
                    }
                }
                if(have==false){
                    cout<<"对不起,没有该卡号"<<endl;
                }
                break;
            case 5:
                cout<<"请输入你要查询的卡号:";
                cin>>_cid;
                have = false;
                for(int i = 0;i<=number;i++){
                    if(cardlist[i].id==_cid){
                        cout<<"姓名:"<<cardlist[i].name<<endl;
                        cout<<"班级:"<<cardlist[i].classroom<<endl;
                        cout<<"电话号码:"<<cardlist[i].phone<<endl;
                        have = true;
                        break;
                    } 
                }
                if(have==false){
                    cout<<"对不起,没有该卡号"<<endl;
                }
                break;
            case 6:
                cout<<"请输入你的卡号:";
                cin>>_cid;
                have = false;
                for(int i = 0;i<=number;i++){
                    if(cardlist[i].id==_cid){
                        cout<<cardlist[i].name<<"同学你好,你的余额是:"<<cardlist[i].money<<endl;
                        have = true;
                        break;
                    } 
                } 
                if(have==false){
                    cout<<"对不起,没有该卡号"<<endl;
                }
                break;
            case 7:
                cout<<"请输入你要挂失的卡号:";
                cin>>_cid;
                cout<<"请输入你的姓名:";
                cin>>_name;
                cout<<"请输入你的班级:"; 
                cin>>_classroom;
                cout<<"请输入你的手机号:"; 
                cin>>_phone;
                have = false;
                for(int i = 0;i<=number;i++){
                    if(cardlist[i].lost){
                        cout<<"该卡号已被挂失过了"<<endl;
                        have = true;
                        break;
                    } 
                    if(cardlist[i].id==_cid){
                        if(cardlist[i].name==_name){
                            if(cardlist[i].classroom==_classroom){
                                if(cardlist[i].phone==_phone){
                                    cardlist[i].lost = true;
                                    cout<<"挂失成功"<<endl;
                                    have = true;
                                    break;
                                }else{
                                    cout<<"您的手机号和办此卡人的手机号不一致,因此您不能挂失此卡号"<<endl;
                                    have = true;
                                }
                            }else{
                                cout<<"您的班级和办此卡人的班级不一致,因此您不能挂失此卡号"<<endl;
                                have = true;
                            }
                        }else{
                            cout<<"您的姓名和办此卡人的姓名不一致,因此您不能挂失此卡号"<<endl;
                            have = true;
                        }
                    }
                } 
                if(have==false){
                    cout<<"对不起,没有该卡号"<<endl;
                }
                break;
            case 8:
                cout<<"请输入你的卡号:";
                cin>>_cid;
                have = false;
                for(int i = 0;i<=number;i++){
                    if(cardlist[i].lost){
                        cout<<cardlist[i].name<<"同学,该卡号已挂失"<<endl;
                        have = true;
                        break;
                    }
                    if(cardlist[i].id==_cid){
                        cout<<cardlist[i].name<<"同学你好,请输入退钱金额:";
                        int money;
                        cin>>money;
                        if(!cardlist[i].consumption(money)){
                            cout<<"您的余额不足"<<endl;
                            have = true;
                            break;
                        }
                        have = true;
                        cout<<"退钱成功"<<endl;
                        break;
                    } 
                } 
                if(have==false){
                    cout<<"对不起,没有该卡号"<<endl;
                }
                break;
            case 9:
                cout<<"目前已有"<<number<<"人办卡"<<endl;
                break;
            case 10:
                system("pause");
                return 0;
        }
        ofstream outfile;
        outfile.open("1.txt");
        for(int i = 0;i<number;i++){
            outfile<<cardlist[i].name<<"|"<<cardlist[i].classroom<<"|"<<cardlist[i].phone<<"|"<<cardlist[i].money<<"|"<<cardlist[i].lost<<"|"<<"\n";
        }
        system("pause");
        system("cls");
    }
    /*
    卡片:
        属性:
            姓名:string 张三 
            卡号:int  123456 
            班级:string 三2班 
            余额:float  0.2
            余额单位: 元 
            办卡时间:string 2023 11 19 08 40 
            手机号:string 
            办卡地点:string 充值点 
            身份证:string  
            家庭住址:string
            每日消费上限:int 50
            消费记录: string[]
            最后一次消费时间:string 
            充值记录:string[]
            挂失状态:bool  
        方法:
            开卡 参数1:姓名、班级、 
            充值 参数1:int 金额 
            刷卡 参数1:float 金额 参数2:string 地点
            挂失: 参数1 卡号
            余额查询:参数1  卡号 
            修改姓名:参数1 卡号 参数2 新名字
            删除卡片:参数1 卡号
            退钱:参数1 卡号 参数2 金额
            增加利息:参数1 卡号 参数2 金额          
    
    */
    return 0;
}