还剩33页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
电商交易平台设计C++班级2013211306姓名严浩学号2013211321一.实验目的
1.锻炼我们用C++编程的能力
2.培养我们考虑问题是否全面的能力
3.训练我们的代码优化能力二.根本内容题目一银行门户的设计该门户是一个单独的程序,程序要求至少支持以下功能)注册银行卡,银行可选;1)修改银行卡密码;2)存款取款;3银行卡至少需要有卡号,密码,所属银行名称,持卡人身份证号,卡内金额等内容要求1请把所有的银行名写入文件(或数据库),注册银行卡的时候,要求只能选择已经存在的银行进行注册要求2请做好错误场景的处理,例如读银行文件错误,输入数据不合法等等题目二电商平台的设计该平台是银行门户之外的一个单独的程序,程序要求至少支持以下功能)注册登录支持新用户注册平台账号,已注册用户用平台账号登录平台(要求已注册用户的信息长久保存)1)浏览平台产品信息23)优惠活动支持对同一品类下所有产品打折的活动,支持单笔订单满X减Y的活动)购置产品支持用户添加产品到购物车,查看实际应付的产品价格,提交订单4在题目二我们暂时不考虑提交订单后支付等后续需求要求1电商平台上至少有三类产品如食物、服装、图书等,每类产品中至少有三个具体的产品(如图书中可以有《C++Primer》、《EffertiveC++》等),每个具体的产品请至少包含产品描述,产品原价,产品剩余量等数据所有的产品信息需要存储在数据库或文件中,不能写在代码中,平台管理员通过直接修改数据库或文件,管理本平台上的产品,包括产品的增加和删除,修改数量以及具体产品的属性信息等要求2请至少设计一层继承体系(产品基类-产品子类),设计一个产品基类,然后让图书类、电子产品类和服装类等产品子类类继承它,具体的产品是产品子类的实例对象(〈〈C++Primer是图书类的实例对象)产品基类请至少具有一个虚函数getPrice()用于计算具体产品的价格要求3请通过为每个产品子类定义“品类折扣系数〃来支持对同产品子类下所有产品打折扣的活动(如图书全场5折,则图书类这一产品子类的折扣系数为
0.5)o要求4请做好错误场景的处理题目三网上支付的设计实现题目三会让你设计的平台像个真正的运行在网络上的平台题目三要求在题目
一、二的基础上支持通过网上支付在电商平台上购物,请至少实现以下功能)绑定银行卡支持电商平台账号绑定银行卡同一账号可以绑定多张银行卡,而且银行卡可以属于不同银行1)网上支付选择绑定的任一银行卡支付电商平台上的订单2要求1绑定银行卡和选择银行卡进行支付时都要求提供对应的银行卡密码要求2当在电商平台上绑定银行卡或者进行网上支付的时候,请不要直接翻开某个文件查找银行卡的信息,因为银行卡信息文件是银行门户系统私有的,电商平台系统无法直接访问,而应该由电商平台系统去向银行门户系统对接数据请用socket通信来传送数据要求3请做好错误场景的处理,如绑定银行卡失败,支付失败等return i;return i;bool BankPortal::checkstring cardnum{unsigned int i=0;for;ivBankC.size;i++{ifvBankC[i].getCardnum==cardnum{return true;return false;void BankPortal::output{ofstream foutoutput_;for unsigned int i=0;ivBankC.size;i++{vBankC[i]fout;fout.close;void BankPortal::bankNaming{ifstream finbankname_;string input;whilegetlinefin,input{vBankN.push backinput;fin.close;double BankPortal::StrToDoublestring str,int start,int end{〃把字符串转化成double类型int state=0;int i=0;double num=0;for;startend;++start{if state=二0str[start]isdigitstr[start]num=num*10+sir[start]-O;else ifstate==0str[start]==state=1;else ifstate=二1isdigitstr[start]{num=num*10+str[start]一O;++i;return num/pow10,i;void BankPortal::init{ifstream finoutput_;string cardnum;string password;string bankname;string name;string ID;string remainders;double remainder;whilefincardnum{cardnum=cardnum.substr6;finpassword;password=password.substr6;finbankname;bankname=bankname.substr6;finname;name=nanie.substr6;fin»ID;ID=ID.substr10;finremainders;remainder=StrToDoubleremainders,6,remainders,size;bankcard bankccardnum,password,bankname,name,ID,remainder;vBankC.push backbankc;}fin.close;bankNamingO;int BankPortal::Matchstring cardnum,string password{unsigned int i=0;for;ivBankC.size;i++{if vBankC[i].getCardnum==cardnumvBankC[i].getPassword==password return i;return i;void BankPortal::BindWORD sockVersion=MAKEWORD2,2;WSADATA wsaData;ifWSAStartupsockVersion,fewsaData!=0printf〃嵌套字未翻开!〃;return;〃创立套接字SOCKET slisten=socketAF_INET,SOCK_STREAM,IPPROTO_TCP;ifslisten==INVALID_SOCKET{printfsocket error!〃;return;}〃绑定IP和端口sockaddrin sin;sin.sinfamily=AFINET;sin.sin_port=htons8888;sin.sin addr.S_un.S_addr=INADDR_ANY;ifbindslisten,ITSOCKADDRsin,sizeofsin==SOCKET_ERROR printfz,bind error!〃;〃开始监听iflistenslisten,5==SOCKET_ERROR printflistenerror!〃;return;〃循环接收数据SOCKET sClient;sockaddrin remoteAddr;int nAddrlen=sizeof remoteAddr;char sendBuf
[2];char receiveChoice
[2];nil choice;while trueprintf〃等待连接・・.\n〃;sClient=acceptslisten,SOCKADDR*remoteAddr,fenAddrlen;ifsClient=INVALID_SOCKETprintf accepterror!〃;continue;printf〃接受到一个连接%s\r\n/z,inet_ntoaremoteAddr.sin addr;recvsClient,receiveChoice,2,0;//监听选择choice=receiveChoice
[0]O;if choice==lchar receiveCard
[5];char receivePassword
[30];recvsCl ient,receiveCard,5,0;//接受账号密码recvsClient,receivePassword,30,0;string cardnum;cardnum=receiveCard;string password;password=receivePassword;int index;if index=Match cardnum,passwordvBankC.size{sprintfsendBuf,;sendsClient,sendBuf,strlensendBuf+1,0;〃发送绑定成功sprintf sendBuf,vBankCtindex].getBankname.c_str;〃发送银行类型sendsClient,sendBuf,strlensendBuf+l,0;else{sprintf sendBuf,z,0z,;sendsClient,sendBuf,strlensendBuf+1,0;else ifchoice==2{char receiveCard
[5];char receiveShouldpay
[20];char receivePassword
[30];int index;recvsClient,receiveCard,5,0;//接受账号密码recvsClient,receivePassword,30,0;string cardnum;cardnum=rece iveCard;string password;password=receivePassword;index=Matchcardnum,password;if indexvBankC.size{sprintf sendBuf,;sendsClient,sendBuf,strlensendBuf+1,0;//发送验证成功recvsClient,receiveShouldpay,20,0;double shouldpay;shouldpay=std::atofreceiveShouldpay;ifpaycardnum,shouldpay{sprintf sendBuf,T;sendsClient,sendBuf,strlensendBuf+1,0;〃发送付款成功output;else{cout〃余额缺乏!〃;sprintf sendBuf,〃0〃;sendsClient,sendBuf,strlensendBuf+1,0;〃发送付款成功else{sprintfsendBuf,〃0〃;send sClient,sendBuf,strlen sendBuf+1,0;〃发送验证失败closesocketslisten;WSACleanup;电商平台代码#ifndef_AC0UNTJI_#define_AC0UNT_H_#includestring#includefstream#includevector#include item.hSinclude bankcard.h〃using namespace std;class Account{private:string username;string password;string phuneiiuni;double shouldpay;int cardnum;public:vectorbankcard vBank;vectoritem vltem;double getShouldpay{return shouldpay;int getCardnum{return cardnum;void addCardnumint num cardnum+=num;void clshouldpay{shouldpay=
0.0;void addShouldpaydouble price{shou1dpay+=pr ice;void subShouldpaydouble price{shouldpay-=pri ce;}Account;Accountstring username,string password,string phonenum,vectoritem scart,double shouldpay,int cardnum;voi dsetPasswordstring password;voi dsetPhonestri ngphonenum;string getUsernameO;string getPassword;string getPhonenumO;ofstreamfe operator ofstream ofs;;#endif#ifndef_BANKCARD_H_#define_BANKCARD_H_#includestring#includefstreamusing namespacestd;class bankcard{private:string cardnum;string bankname;public:bankcard{}bankcard string cardnum,string banknamc{this-cardnum=cardnum;thi s-bankname=bankname;string getCardnumO{return cardnum;string getBankname{return bankname;;#endif#ifndef_GOODS H^define_GOODSJ1_#includestring#includefstreamusing namespacestd;〃登录后,
1.查看现有商品{L查看哪一类商品记得显示产品描述,产品原价,产品剩余量,优惠信息}|
2.回退class goods{protected:string name;string description;double originalPrice;int remainder;double discount;〃折扣public:goods{}goods string name,string description,double originalPrice,int remainder,double discount{this-name=name;this-description=description;this-originalPrice=originalPrice;this-remainder=remainder;this-discount=discount;string getName;string getDescription;double getOriginalPrice;int getRemainder;bool subRemainderint num{if remainder〉二numremainder=remainder-num;return true;return false;double getDiscount;virtual double getPrice{return discount*ori ginalPrice;ofstream goods::operator ofslreamofs;;class food:public goods{public:virtual doublegetPrice;food string name,string description,double originalPrice,int remainder,double di scount:goods name,description,originalPrice,remainder,discount{};class clothing:public goods{public:virtual doublegetPrice;clothingstring name,string description,double originalPrice,int remainder,double discount:goods name,description,originalPrice,remainder,discount{};class book:public goods{public:virtual doublegetPrice;book string name,string description,double originalPrice,int remainder,double discount:goods name,description,originalPrice,remainder,discount{};#endif#ifndef_ITEM_H_#define_ITEM_H_#includestringusing namespacestd;class item{protected:string name;int num;doubleprice;public:itemstring name,int num,doubleprice{this-name=namc;this-num=num;this-price=price;string getName{return name;int getNumO{return num;doublegetPrice{return price;;Wendifttifndef_PLATFORM_H_#define_PLATFORM_H_#include WINS0CK
2.H#include stdio.h#pragma commentlib,zzws2_
32.lib〃#include vector#include string#include Account.h〃#include goods.h〃using std::vector;class Platform{private:vectorAccount vAccount;vectorfood vFood;vectorclothing vClothing;vcclurbook vBook;void init;〃初始化void registerAccount;bool loginAccount;//void changePhonestring phonenum;void output;bool checkstring username;int matchstring username,string password;void checkgoodsint account;double StrToDoublestring str,int start,int end;bool addshoppingcartint choice,int index,int num,int account;void checkScartint account;void Bandint index,string cardnum,string password;bool Payint index,string card;public:Platform;;#endifSinclude Account.h〃using std::string;Account::Accountstring username,string password,string phonenum,vectori temscart,double shouldpay,int cardnum{this-username=uscrnamc;this-password=password;thi s-phonenum=phonenum;thi s-vltem=scart;this-shouldpay=shouJ dpay;this-cardnum=cardnum;void Account::setPasswordstring password{this-password=password;void Account::setPhonestring phonenum{this-phonenum=phonenum;stringAccount::getUsername{returnusername;siring Account::gelPassword{return password;string Account::getPhonenum{return phonenum;三.实验方法题目一创立了银行卡类、银行门户类,银行卡里包含了卡号、姓名、密码、身份证号、银行名称、余额几个属性银行门户有操作银行卡,注册银行卡两个功能,操作银行卡又包括存取款,修改密码等功能,账户信息和银行名称保存在文件里,银行卡用vector类型保存题目二创立了电商平台类、账户类、商品类(商品类有3个派生类)、绑定银行卡类、购物车工程类其中账户类中包含了用户名、密码、手机号、购物车、应付款、绑定银行卡、绑定银行卡数量这几个属性,其中购物车里的工程用vector类型保存电商平台中的账户也用vector类型保存,平台的功能有登录、注册,登录后可以绑定银行卡、查看商品并添加购物车、查看购物车和应付金额、结算购物车、退出登录等功能题目三在绑定银行卡和付款时用socket通信来传送数据绑定银行卡时,先向银行门户发送绑定银行卡标志位表示绑定功能,然后发送卡号密码,验证通过后返回成功标志位,电商平台收到后绑定成功付款时,先向银行门户发送付款标志位表示付款功能,,然后发送卡号密码,验证通过后返回成功标志位,电商平台收到后发送应付金额,银行门户收到后调用付款函数,余额缺乏返回余额缺乏标志位,余额够则返回付款成功标志位四.源代码银行门户代码#ifndef_BANKCARD_H_itdefine_BANKCARD_H_#includestring#includefstream usingnamespacestd;class bankcard{private:stringcardnum;stringpassword;stringbankname;stringname;stringID;doubleremainder;public:bankcard{}bankcard stringnum,string password,string bankname,string name,string ID,double remainder;void setPasswordstringpassword;ofstreamfe Account::operatorofstreamofs{ofs,z用户名〃username;ofs,/密码〃password;ofs/z手机号z/phonenum;ofs«,z银行卡数量〃cardnumendl;//ofs«z,购物车〃;//for unsignedi=0;iscart.getvltem.size;i++{//ofsscart.getvltem[i].getName/z z,scart.getvltem[i].getnumO,z z,scart.getvItemO[i].getPrice«endl;//return ofs;#include“goods.h〃using std::string;//goods::goodsstring name,string description,double originalPrice,int remainder,double discountstringgoods::getName{returnname;string goods::getDescription{return description;double goods::getOriginalPrice{return originalPrice;int goods::getRemainder{return remainder;double goods::getDiscount{return discount;double food::getPrice{return discount*originalPrice;double clothing::getPrice{return discount*originalPrice;double book::getPrice{return discount*originalPrice;ofstreamfe goods::operator ofstreamfe ofs{ofs,z名称“〈〈name;ofs/z描述“〈〈description;ofs,z原价z,originalPrice;ofsz/剩余量〃remainder;ofs«,z折扣z/discount«endl;return ofs;#include Platform.h〃#include iostream#include cstdlib#include cmathconst stringAccount_file=Account.txt”;const stringgoods_file=z/goods.txt,z;const string bankcardfile=bankcard.txt”;Platform::Platform{〃登录前,登录注册退出init;whiletrue{cout«endl;cout«〃=========欢送来到MY电商平台==========〃«endl;cout//===================================================//endl;cout«〃==========您想进行什么操作?================z/endl;cout〃=二二二二二二二二二二二二二二二二1:登录账号二二二二二二二二二二二二二二二二〃endl;cout«〃===============二二2:注册账号================〃endl;cout,,=================3:ifitB================,/endl;cout=二=二二二二二二===二=======二=============二二二二二二二二二========〃endl endl;cout«〃请输入您要进行的操作〃;ini select;string Select;while!cinSelect||Select
[0]1||Select
[0]3cout〈〃输入错误,请重新输入〃endl;coutendl;select=std::atoiSelect.c_str;switchselectcase1:if IloginAccountOcout〃输入密码超次数限制!〃《endl;break;case2:registerAccount;break;case3:output;return;default:break;}double Platform::StrToDoublestring str,int start,int end{〃把字符串转化成double类型int state=0;int i=0;double num=0;for;startend;++start{if state=0str[start]isdigitstr[start]num=num*10+str[start]-O;else ifstate==0str[start]==state=1;else ifstate=1isdigitstr[start]{num=num*10+str[start]-O;++i;return num/pow10,i;void Platform::init{ifstream finAccount—;string username;string password;string phonenum;vectoritem scart;string cardnums;int cardnum;whilefinusername{username=username.substr8;finpassword;password=password,substr6;finphonenum;phonenum=phonenum.substr8;fincardnums;cardnums=cardnums.substr12;cardnum=std::atoicardnums.c_str;Account accountusername,password,phonenum,scart,
0.0,cardnum;vAccount.push backaccount;fin.close;ifstream fingoodgoods_;stringname;string description;string originalPrices;double originalPrice;string remainders;int remainder;string discounts;double discount;string goodsnums;int goodsnum;fingoodgoodsnums;goodsnums=goodsnums.substr16;goodsnum=std::atoigoodsnums.c_str;whilegoodsnum0{fingoodname;name=name,substr6;fingooddescription;description=description.substr6;fingoodoriginalPrices;originalPrice=StrToDoubleoriginalPrices,6,originalPrices.size;fingoodremainders;remainders=remainders,substr8;remaindei-sld::atuiremainders.c_str;f ingood»discounts;discount=StrToDouble discounts,6,discounts.size;food Foodname,description,originalPrice,remainder,discount;vFood.pushbackFood;goodsnum一;fingoodgoodsnums;goodsnums=goodsnums.substr16;goodsnum=std::atoigoodsnums.c_str;whilegoodsnum0{fingoodname;name=name,substr6;fingooddescription;description=description,substr6;f ingood»originalPrices;originalPrice=StrToDoubleoriginalPrices,6,originalPrices.size;fingoodremainders;remainders=remainders.substr8;remainder=std::atoiremainders.c_str;f ingood»discounts;discount=StrToDouble discounts,6,discounts.size;clothing clothesname,description,originalPrice,remainder,discount;vClothing.push backclothes;goodsnum一;fingoodgoodsnums;goodsnums=goodsnums.substr16;goodsnum=std::atoigoodsnums.c str;wh i1egoodsnum0{fingoodname;name=name.substr6;fingood»description;description=description.substr6;fingoodoriginalPrices;originalPrice=StrToDoubleoriginalPrices,6,originalPrices.size;f ingoodremainders;remainders=remainders,substr8;remainder=std::atoiremainders.c_str;fingooddiscounts;discount=StrToDoublediscounts,6,discounts,size;book Bookname,description,uriginalPrice,remainder,discount;vBook.push backBook;goodsnum——;fingood,close;ifstream fincardbankcard_;string card;string bankname;int i=0;whilef incardusername{username=username.substr8;for int j=l;j=vAccount[i].getCardnumO;j++{fincardcard;card=card.substr6;fincardbankname;bankname=bankname.substr6;bankcard Cardcard,bankname;vAccount[i],vBank.push_back Card;i++;fincard.close;void Platform::registerAccount{stringusername;string password;string temp;stringphonenum;do{cout«〃用户名〃;cinusername;while checkusernamecout〈〃该用户名已被使用,请重新输入!endl;do{cout〈〃密码〃;cin»password;cout〃再次确认密码〃;cintemp;while password!=tenipcout〃前后输入密码不一致,请重新输入!〃;cuul〃手机号〃;cinphonenum;vectoritem scart;Account accountusername,password,phonenum,scart,
0.0,0;vAccount.push backaccount;cout〈〃注册成功!bool Platform::checkstring username{for unsigned int i=0;ivAccount.size;i++{if username==vAccount[i].getUsername return true;}return false;bool Platform::loginAccount{stringusername;string password;unsigned int index;int chance”do{ifchance0cout〈〃您还有〃5-chance〈〃次时机〃endl;cout«〃用户名〃;cinusername;cout〈〃密码〃;cinpassword;chance++;ifchance==5return false;whileindex=matchusername,password=vAccount.sizecout〈〃用户名或密码输入错误,请重新输入!〃;while true{cout〃=====================〃vAccount[index].getUsername〃你好====================〃endl;cout«〃========二二二二二二请选择您的操作=================,zendl;cout〃=二二二二二二二二二二二二二二二二二二0:绑定专艮行卡二二二二二二二二二二二二二二二二二〃endl;cout«〃==========1:查看现有商品=========〃«endl;cout«〃===========2:查看购物车并付款=========〃«endl;cout«〃======二====二二===二二3:退出登录二二二二二二二二二二二二===二〃endl;cout/,====================================================,zendlendl;coul〈〃请选择您的操作〃;int select;string cardnum;string ePassword;string Select;while!cinSelect||Select
[0]O||Select
[0]3cout〃输入错误,请重新输入〃endl;coutendl;select=std::atoiSelect.c_str;switchselectcase0:cout*〃请输入您的银行卡号〃;cincardnum;cout〃请输入您的密码〃;cin»cPassword;Bandindex,cardnum,ePassword;break;case1:checkgoodsindex;break;case2:checkScartindex;break;case3:return true;break;default:break;int Platform::matchstring username,string password{unsigned int i=0;for;ivAccount.size;i++{ifvAccount[i].getUsername==usernamevAccount[i].getPassword==password returni;}returni;void Platform::output{ufslrcam foulAuuounl_;forunsigned int i=0;ivAccount.size;i++{vAccount[i]fout;fout.closeO;ofstream foutgoodsgoods_;foutgoods〈〃食品类商品数量〃vFood.sizeendl;forunsigned int i=0;ivFood.size;i++{vFood[i]foutgoods;foutgoods〈〃服装类商品数量“〈〈vClothing.sizeendl;for unsigned int i=0;ivClothing.size;i++{vClothing[i]foutgoods;foutgoodsV〈〃图书类商品数量〃〈vBook.size endl;for unsignedinti=0;ivBook.size;i++{vBook[i]foutgoods;ofstream foutcardbankcard_;for unsignedinti=0;ivAccount.size;i++{foutcard,/用户名z,«vAccount[i].getUsernameendl;forintj=0;jvAccount[i].getCardnumO;j++{foutcardzz卡号:,zvAccount[i].vBank[j].getCardnumO;foutcard,/银行,zvAccount Ei].vBank[j].getBankname endl;}foutcard.close;void Platform::checkgoodsint account{cout〃=====食物类商品=====〃end1;for unsignedinti=0;ivFood.size;i++{〈〃号商品〃endl;cout〃名称:,,«vFood[i].getNameendl;cout〃描述,z«vFood[i].getDescriptionendl;cout〃原价,,«vFood[i].getOriginalPriceendl;cout〃剩余量,,«vFood[i].getRemainderendl;cout〃折扣:,,«vFood[i].getDiscountendl;cout/,================================,zendl;cout〃======服装类商品=======〃endl;for unsignedinti=0;ivClothing.size;i++{couti〃号商品〃endl;cout〃名称:,,«vClothing[i].getNameendl;cout〃描述/z«vClothing[i].getDescriptionendl;cout〃原价,z«vClothing[i].getOriginalPriceendl;cout〃剩余量,,«vClothing[i].getRemainderendl;cout〃折扣,z«vClothing[i].getDiscountendl;cout,,================================,,endl;cout〃二二二二二二二二二二图书类商nn==========,,endl;forunsigned inti=0;ivBook.size;i++{cout«i〃号商品〃endl;cout〃名称z,«vBook[i].getNameendl;cout〃描述:,z«vBook[i].getDescriptionendl;cout〃原价:,,«vBook[i].getOriginalPriceendl;coutX〈〃剩余量:z,«vBook[i].getRemainderendl;cout〃折扣:,,«vBook[i].getDiscountendl;cout〃================〃endl;int select;whi1e true{cout«〃==========每笔订单满199减50========〃«endl;cout〃=二二二二二二二二二二二二二二二二二二请选择您的操作=================//«endl;cout«〃=========1:添加商品到购物车=======〃«endl;cout,z================2:0_t一级菜单==============zz«endl;cout,z====================================================,/«endl;cout〃请选择您的操作〃;string Select;while!cinSelect||Select
[0]1||Select
[0]2cout〈〃输入错误,请重新输入〃endl;cout«endl;select=std::atoiSelect.c_str;coutendl;string Choice;switch selectcase1:cout〃请输入您要添加的商品种类:1食品类,2服装类,3图书类〃int choice;while!cinChoice||Choice
[0],T||Choice[]3cout〃输入错误,请重新输入〃〈endl;choice二std::atoiChoice.c_str;void addRemainderdouble money;void subRemainderdouble money;string getCardnumO;string getPasswordO;string getBankname;string getName;string getID;double getRemainder;bool matchPassportstring password;ofstreani operatorofstreamfeofs;;#endifSinclude bankcard.h〃using std::string;bankcard::bankcardstring cardnum,string passport,string bankname,stringname,string11,double remainder{this-cardnum=cardnum;this-〉password=passporl;this-bankname=banknamc;thi s-name=name;this-ID=ID;this-remainder=remainder;void bankcard::setPasswordstring newpassport{this-password=newpassport;void bankcard::addRemainderdouble money”this-remainder+=money;void bankcard::subRemainderdouble monoy{this-remainder=this-remainder-money;string bankcard::getCardnumO{return uardnuni;string bankcard::getPassword{return password;string bankcard::getBankname{cout〃请输入商品序号〃;int index;while!cinChoice||Choice
[0]O||Choice
[0]2cout〃输入错误,请重新输入〃endl;index=std::atoi Choice.c_str;cout«〃请输入添加商品数量〃;int num;do while!cinChoice||Choice
[0]O||Choice
[0]9cout«〃输入错误,请重新输入〃endl;num=std::atoi Choice.c_str;while num〈二0cout〈〃输入错误,请重新输入〃*endl;ifaddshoppingcartchoice,index,num,account{cout〈〃添加购物车成功!〃〈〈endl;else cout〃添加失败!〃《endl;break;case2:return;default:break;void Platform::checkScartint account{cout〃================你的购物车================〃〈〈end1;for unsignedi=0;ivAccount[account].vltem.size;i++{cout〃名称,z«vAccount[account].vltem[i].getName endl;cout〃数量/z«vAccount[account].vltem[i].getNumO«endl;cout〃总价/zvAccount[account].vltem[i].getPriceendl;cout/,==========================================,,endl;ifvAccount[account].getShouldpay=199vAccount[account].subShouldpay50;cout〃折扣和满减后应付金额为〃vAccount[account].getShouldpayendl;cout〈〃确认付款吗?1确认0否〃《endl;intchoice,choicecard;string Select;while!cinSelect||Select
[0]O||Select
[0]1cout〈〃输入错误,请重新输入〃endl;coutendl;choice=std::atoiSelect.c_str;ifchoice==l{ifvAccount[account].getCardnum0{cout〈〃您的银行卡有〃endl;for inti=0;ivAccount[account].getCardnum;i++{couti〃:〃vAccount[account].vBank[i].getCardnum/z“〈〈vAccount[account].vBank[i].getBanknameendl;cout«〃请输入您要选择的银行卡序号〃;dowhile!cin»Select|Select
[0]O I|Select
[0]9cout〈〃输入错误,请重新输入〃«endl;choicecard=std::atoiSelect.c_str;whilechoicecard0||choicecardvAccount[account].getCardnumTcout〈〃输入错误,请重新输入〃endl;ifPayaccount,vAccount[account].vBank[choicecard].getCardnumvAccount[account].clshouldpay;vAccount[account].vltem.clear;cout〈〃支付成功!/z«endl;elsecout〈〃支付失败!〃〈〈endl;else{cout〃您还未绑定银行卡!;bool Platform::addshoppingcartint choice,int index,intnum,intaccount{ifchoice==l{if vFood[inJcx].gelReniainder=iium{item foodivFood[index].getName,num,num*vFood[index].getPrice;vAccount[account].vltem.push backfoodi;vAccount[account].addShouldpaynum*vFood[index].getPrice;vFood[index].subRemaindernum;else{cout“对不起,商品剩余量缺乏!〃endl;return false;}ifchoice==2{ifvClothing[index].getRemainder=num{item clothing!vClothingEindex].getName,num,num*vClothingEindex].getPrice;vAccount[account].vltem.push_backclothingi;vAccount[account].addShouldpaynum*vClothing[index].getPrice;vClothing[indcx].subRemaindernum;else{cout〈〃对不起,商品剩余量缺乏!〃endl;return false;}ifchoice==3{if vBook[i ndex].getRemainder=num{item bookivBook[index].getName,num,num*vBook[index].getPrice;vAccount[account].vltem.push backbooki;vAccount[account].addShouldpay num*vBook[index].getPrice;vBook[index].subRemaindernum;else{cout”对不起,商品剩余量缺乏!〃endl;return false;ifchoicelchoice3return false;return true;void Platform::Bandint index,string cardnum,string password{WORD sockVersion=MAKEWORD2,2;WSADATA data;ifWSAStartupsockVersion,fedata!=0printf〃嵌套字未翻开!〃;return;SOCKET sclient=socketAF_INET,SOCK_STREAM,IPPR0T0_TCP;ifsclient=INVALID_SOCKETprintf^invalid socket!〃;return;sockaddrin serAddr;serAddr.sin_family=AF INET;serAddr.sin_port=htons8888;serAddr.sin addr.S un.S_addr=inet_addr〃127・
0.
0.1〃;if connectsclient,sockaddr*serAddr,sizeofserAddr=S0CKET_ERR0R printfconnect error!〃;closesocketsclient;return;}char choice
[2]={1,\0};sendsclient,choice,2,0;sendsclient,cardnum.c_str,5,0;sendsclient,password.c_str,30,0;char isok⑵;char bankname
[15];recvsclient,isok,2,0;if isok
[0]==,T{recvsclient,bankname,15,0;string Bankname;Bankname=bankname;bankcard cardcardnum,Bankname;vAccount[index].vBank.push backcard;vAccount[index].addCardnum1;cout〃绑定成功!z,«endl;else cout〃绑定失败!〃endl;closesocketsclient;WSACleanupO;bool Platform::Payint index,string card{WORD sockVersion=MAKEWORD2,2;WSADATA data;ifWSAStartupsockVersion,fedata!=0printf〃嵌套字未翻开!〃;return false;SOCKET sclient=socketAF_INET,SOCK_STREAM,IPPROTO_TCP;ifsclient==INVALID_SOCKETprintfinvalid socket!〃;return false;sockaddr_in serAddr;serAddr.sin family=AF INET;serAddr.sin_port=htons8888;serAddr.sin addr.S un.S_addr=inet_addr〃127・
0.
0.1〃;if connectsclient,sockaddr*serAddr,sizeofserAddr==S0CKET_ERR0R printfconnecterror!〃;closesocketsclient;return false;char choice
[2]={,2,\0};sendsclient,choice,2,0;string password;cout«〃请输入银行卡密码〃;cinpassword;send sclient,card,c str,5,0;sendsclient,password,c str,30,0;char isok
[2];recvsclient,isok,2,0;if isok
[0]=1{char shouldpay
[20];sprintf shouldpay,vAccount[index].getShouldpay;sendsclient,shouldpay,30,0;〃发应付款char bankname
[15];recvsclient,isok,2,0;if isok
[0]==,f{return true;else{coul〈〃余额缺乏!〃;return false;else{cout«〃密码错误!〃;return false;closesocketsclient;WSACleanupO;五.实验总结
1.一开始总是忘记做非法输入处理,后来急急忙忙做了非法输入处理,但还是在验收的时候漏了对负数的处理,现在已经完善了
2.在保存银行账户以及平台账号时,一开始想用链表,不过后来发现C++自带vector类型,vector的功能与链表类似,而且有很多方便的函数如remove,push_back
3.一开始不会写派生类的构造函数,导致了很多BUG,后来知道了用food…goods…{}
4.学到了一个简单的翻开读写文件的方式if streamfinAccount_;和of streamfont Account_;还有对《〈的重载
5.在做到socket通信的时候总是报错,后来才知道是因为string没有转化成char*,因为socket只能传送char*类型的数据,所以我得先把int和double已经string型先转成char*,前两个用sprintf,后者直接加・c_str而char*转string直接赋值即可return bankname;string bankcard::getName{return name;string bankcard::getID{return ID;double bankcard::getRemainder{return remainder;bool bankcard::matchPassportstring password{ifthis-password==passwordreturn true;else returnfalse;ofstreamfe bankcard::operatorofstreani ofs{ofs/z卡号/zcardnum;ofs,z密码”〈〈password;ofszz银行〈〈bankname;ofs,姓名“〈〈name;ofs«z/身份证号〃ID;ofsz,余额〃dec〈〈reniainder«endl;return ofs;#ifndef BANKPORTALH#define BANKPORTALJISinclude WINS0CK
2.H#include stdio.h#pragma commentlib,z/ws
232.lib〃#include vector#include string#include bankcard.h〃using std::vector;class BankPortal{private:vectorstring vBankN;vectorbankcard vBankC;void init;void registerBankCO;void loginBankC;void changePasswordint index;void depositint index;void withdrawintindex;int matchstring cardnum;bool checkstring cardnum;void output;void bankNamingO;double StrToDoublestring str,int start,int end;void Bind;bool paystring num,double money;int Matchstringusername,string password;public:BankPortal;;#endif#include iostreamttincludefstream#include string#include strstream#include cmath#include time.httinclude〃bankportal.h〃const stringbankname_file=banknamc.txt”;const stringoutput_file=output.txt”;BankPortal::BankPortal{init;cout«z/l进入银行门户系统2进入监听模式,请输入您的选择〃;int Choice;string Select;while!cinSelect||Select
[0]!=TSelect
[0]!=2cout〈〃输入错误,请重新输入〃*endl;cuul«endl;Choice二std::atoiSelect.c_str;if Choice==2BindO;while true{coutendl;cout«〃=======二二二二欢送来到银行门户==========〃«endl;cout//===================================================//endl;cout«〃=========您想进行什么操作?=========〃endl;cout«〃二二二二二二=二二二=二二二二二二1:操作账户二二二二二二二二二二二二二二二二〃endl;cout«〃二二二二二二二=二二二二二二二二二2:注册专艮行卡二二二二二二二二二二二二二二二二〃«endl;cout,,=================3:iR th================,z endl;cout«〃二=二二=========================〃endl endl;cout«〃请输入您要进行的操作〃;int select;string Select;while!cinSelect||Select
[0],T||Select
[0]3cout〈〃输入错误,请重新输入〃endl;cout«endl;select=std::atoiSelect.c_str;switchselectcase1:loginBankC;break;case2:registerBankCO;break;case3:output;return;default:break;void BankPortal::registerBankCO{cout〈〃请选择你要注册的银行:〃endl;for unsignedinti=0;ivBankN.size;i++{ifi%4=3cout«〃〃«i«〃〃«vBankNCi]«endl;elsecouti,z z,vBankN[i];coutendl〈〃请输入您的选择〃;unsignedintnum;while!cinnum||num0num=vBankN.size cout«endl〈〃输入错误,请重新输入〃;coutendl;stringbank=vBankN[num];int cardnum;strstream t;stringcard;string password;string temp;stringname;string ID;inti;cout〃姓名〃;cinname;cout〈〃身份证号〃;cin»ID;srandint timeO;docardnum=1000+rand%9000;tcardnum;tcard;whilecheckcard;do{cout«〃设置密码〃;cinpassword;cout〈〃再次确认密码〃;cintemp;while password!=tempcoutendl〃前后输入的密码不同,请重新输入!〃endl;coutendl;bankcard beardcard,password,bank,name,ID,
0.0;vBankC.push backbeard;cout〈〃注册银行账号成功!〃〈〈endl;cout〈〃卡号为,zcardendl;void BankPortal::loginBankCO{stringcardnum;string password;unsignedintnum;docout〃请输入要操作的银行卡号〃Gendl;cincardnum;while num=match cardnum=vBankC.size账号输入错误!,,endl;while true{cout/,========================,/«vBankC[num].getNameO〃先生/女士二二二二二二二二二二二二二二二二二二〃endl;cout〃=二==二二二二二二二二二二二二二卡内余额共〃vBankC[num].getRemainder〃RMB=========〃endl;cout«〃===========================〃endl;cout«〃=======二二=二二二请选择您的操作=========〃«endl;cout«〃===========1存款=================/zendl;cout«〃===========2:取款===========〃endl;cout«〃二二二二二二二二二二二二二二二二二二二3:修改银行卡密码二二二二二二二二二二二二二二二二二〃endl;cout«〃==========4:返回上一级菜单=========〃endl;cout«〃二二二二二二二二二二二二二二二二二二二二二二二二二=二二二二一二二二二二二二二二二二二二二==二〃endl endl;cout〃请选择您的操作〃;int select;string Select;while!cinSelect||Select
[0],T||Select
[0]4cout«〃输入错误,请重新输入〃endl;select=std::atoiSelect,c str;cout«endl;switchselectcase1:depositnum;break;case2:withdrawnum;break;case3:changePasswordnum;break;case4:return;default:break;void BankPortal::depositint num{double money;cout«〃请输入您要存入的金额〃Gendl;cinmoney;vBankC[num].addRemainder money;void BankPortal::withdrawint num{double money;cout〃请输入您要取出的金额〃endl;cinmoney;ifvBankC[num],getRemainder=money{vBankC[num].subRemainder money;else cout«〃对不起,余额缺乏!〃endl;bool BankPortal::paystringnum,doublemoney{intindex;index=matchnum;if vBankC[index].getRemainder=money{vBankC[index].subRemaindermoney;returntrue;}else returnfalse;void BankPortal::changePasswordint num{string password;string temp;do{cout〃请输入新密码〃endl;cinpassword;cout〃再次确认新密码〃endl;cintemp;while password!=tempcout〈〃前后输入的密码不同,请重新输入!〃endl;vBankC[num].setPasswordpassword;int BankPortal::matchstring cardnum{unsignedinti=0;for;ivBankC.size;i++{if vBankC[i].getCardnum==cardnuni{。