饭卡管理系统&结构体(+本地存储)

#include<iostream>
#include <fstream>
using namespace std;
struct card{
    string name;
    int id;
    int money;
    bool build_card(int x_id,string x_name){
        name=x_name;
        id=x_id;
        return true;
    }
    bool chong(int x_money){
        money+=x_money;
        return true;
    }
    bool hua(int x_money){
        if(money>=x_money&&x_money>0){
            money-=x_money;
            return true;
        }
        return false;
    }
};
int main(){
    card cardlist[100];
    int firstid=1000;
    int number=0;
    string data;
    ifstream file;
    file.open("1.txt");
    while(getline(file,data)){    
	    if(data.length()>5){
         	int lt;
            string _name;
			int _id,_money;
        	lt=data.find("|");
        	_name=data.substr(0,lt);
        	data=data.substr(lt+1);
        	cout<<_name<<endl;
        	
        	lt=data.find("|");
        	_id=stoi(data.substr(0,lt));
        	data=data.substr(lt+1);
			cout<<_id<<endl;
			
			lt=data.find("|");
        	_money=stoi(data.substr(0,lt));
        	data=data.substr(lt+1);
			cout<<_money<<endl;

			cardlist[number].name=_name;
        	cardlist[number].id=_id;
        	cardlist[number].money=_money;
			
			number++;
        }
        //eof 是否到了文件末尾
        if(file.eof()){
            break;   
        }
	}
    file.close();
    while(1){
        cout<<"*饭卡管理系统*"<<endl<<"1.办卡"<<"2.充值"<<"3.消费"<<"4.查询余额"<<endl;
        int index;
        cin>>index;
        card c;
        bool have=false;
        int c_money=0;
        switch(index){
        case 1:            
            cout<<"请输入姓名:";
            cin>>c.name;
            c.id=firstid+number;
            cardlist[number]=c;
            cout<<"办卡成功"<<cardlist[number].name<<",你的卡号:"<<cardlist[number].id<<endl;
            number++;
             
            break;
        case 2:
            cout<<"请输入你的卡号:";
            int _cid;
            cin>>_cid;
            for(int i=0;i<=number;i++){
                if(cardlist[i].id==_cid){
                    cout<<cardlist[i].name<<"同学你好,请输入充值金额:";
                     
                    cin>>c_money;
                    c.money+=c_money;
                    cardlist[number].money+=c.money;
                    cout<<"充值成功,目前金额:"<<cardlist[number].money<<endl;
                    have=true;
                    break;
                }
            }
            if(have==false){
                cout<<"对不起,没有查询到该卡号"<<endl;
            }
            break;
        case 3:
            cout<<"请输入你的卡号:";
            cin>>_cid;
            have=false;
            for(int i=0;i<=number;i++){
                if(cardlist[i].id==_cid){
                    cout<<cardlist[i].name<<"同学你好,请输入花费金额:";
                     
                    cin>>c_money;
                    c.money+=c_money;
                    cardlist[number].money-=c.money;
                    cout<<"花费成功,目前金额:"<<cardlist[number].money<<endl;
                    have=true;
                    break;
                }
            }
            if(have==false){
                cout<<"对不起,没有查询到该卡号"<<endl;
            }
            break;
        case 4:
                cout<<"请输入你的卡号:";
                cin>>_cid;
                have=false;
                for(int i=0;i<=number;i++){
                    if(cardlist[i].id==_cid){
                        cout<<cardlist[i].name<<"同学你好目前余额:"<<cardlist[number].money<<endl;
                        have=true;
                        break;
                    }
                }
                if(have==false){
                    cout<<"对不起,没有查询到该卡号"<<endl;
                }
                break;
    	}
	}
	return 0;
}

  

 

posted @ 2023-12-02 10:01  王ys  阅读(28)  评论(0编辑  收藏  举报