北邮C++银行门户与电商平台

更新时间:2023-05-08 04:42:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

C++电商交易平台设计

班级:2013211306 姓名:严浩学号:2013211321

一.实验目的

1.锻炼我们用c++编程的能力

2.培养我们考虑问题是否全面的能力

3.训练我们的代码优化能力

二.基本内容

题目一:银行门户的设计

该门户是一个单独的程序,程序要求至少支持以下功能:

1)注册银行卡,银行可选;

2)修改银行卡密码;

3)存款取款;

银行卡至少需要有卡号,密码,所属银行名称,持卡人身份证号,卡内金额等内容。

要求1:请把所有的银行名写入文件(或数据库),注册银行卡的时候,要求只能选择已经存在的银行进行注册。

要求2:请做好错误场景的处理,例如读银行文件错误,输入数据不合法等等。

题目二:电商平台的设计

该平台是银行门户之外的一个单独的程序,程序要求至少支持以下功能:

1)注册&登录:支持新用户注册平台账号,已注册用户用平台账号登录平台。(要求已注册用户的信息长

久保留。)

2)浏览平台产品信息。

3)优惠活动:支持对同一品类下所有产品打折的活动,支持单笔订单满X减Y的活动。

4)购买产品:支持用户添加产品到购物车,查看实际应付的产品价格,提交订单。

在题目二我们暂时不考虑提交订单后支付等后续需求。

要求1:电商平台上至少有三类产品:如食物、服装、图书等,每类产品中至少有三个具体的产品(如图书中可以有《C++ Primer》、《Effertive C++》等),每个具体的产品请至少包含产品描述,产品原价,产品剩余量等数据。所有的产品信息需要存储在数据库或文件中,不能写在代码中,平台管理员通过直接修改数据库或文件,管理本平台上的产品,包括产品的增加和删除,修改数量以及具体产品的属性信息等。

要求2:请至少设计一层继承体系(产品基类-产品子类),设计一个产品基类,然后让图书类、电子产品类和服装类等产品子类类继承它,具体的产品是产品子类的实例对象(<> 是图书类的实例对象)。产品基类请至少具有一个虚函数getPrice()用于计算具体产品的价格。

要求3:请通过为每个产品子类定义“品类折扣系数”来支持对同产品子类下所有产品打折扣的活动(如图书全场5折,则图书类这一产品子类的折扣系数为0.5)。

要求4:请做好错误场景的处理。

题目三:网上支付的设计

实现题目三会让你设计的平台像个真正的运行在网络上的平台。题目三要求在题目一、二的基础上支持通过网上支付在电商平台上购物,请至少实现以下功能:

1)绑定银行卡:支持电商平台账号绑定银行卡。同一账号可以绑定多张银行卡,而且银行卡可以属于不

同银行。

2)网上支付:选择绑定的任一银行卡支付电商平台上的订单。

要求1:绑定银行卡和选择银行卡进行支付时都要求提供对应的银行卡密码。

要求2:当在电商平台上绑定银行卡或者进行网上支付的时候,请不要直接打开某个文件查找银行卡的信息,因为银行卡信息文件是银行门户系统私有的,电商平台系统无法直接访问,而应该由电商平台系统去向银行门户系统对接数据。请用socket通信来传送数据。

要求3:请做好错误场景的处理,如绑定银行卡失败,支付失败等。

三.实验方法

题目一:

创建了银行卡类、银行门户类,银行卡里包含了卡号、姓名、密码、身份证号、银行名称、余额几个属性。

银行门户有操作银行卡,注册银行卡两个功能,操作银行卡又包括存取款,修改密码等功能,账户信息和银行名称保存在文件里,银行卡用vector类型保存。

题目二:

创建了电商平台类、账户类、商品类(商品类有3个派生类)、绑定银行卡类、购物车项目类。其中账户类中包含了用户名、密码、手机号、购物车、应付款、绑定银行卡、绑定银行卡数量这几个属性,其中购物车里的项目用vector类型保存。

电商平台中的账户也用vector类型保存,平台的功能有:登录、注册,登录后可以绑定银行卡、查看商品并添加购物车、查看购物车和应付金额、结算购物车、退出登录等功能。

题目三:

在绑定银行卡和付款时用socket通信来传送数据。

绑定银行卡时,先向银行门户发送绑定银行卡标志位表示绑定功能,然后发送卡号密码,验证通过后返回成功标志位,电商平台收到后绑定成功。

付款时,先向银行门户发送付款标志位表示付款功能,,然后发送卡号密码,验证通过后返回成功标志位,电商平台收到后发送应付金额,银行门户收到后调用付款函数,余额不足返回余额不足标志位,余额够则返回付款成功标志位。

四.源代码

银行门户代码:

#ifndef _BANKCARD_H_

#define_BANKCARD_H_

#include

#include

usingnamespace std;

class bankcard{

private:

string cardnum;

string password;

string bankname;

string name;

string ID;

double remainder;

public:

bankcard(){}

bankcard(string num,string password,string bankname,string name,string ID,double remainder);

void setPassword(string password);

void addRemainder(double money);

void subRemainder(double money);

string getCardnum();

string getPassword();

string getBankname();

string getName();

string getID();

double getRemainder();

bool matchPassport(string password);

ofstream& operator <<(ofstream& ofs);

};

#endif

#include"bankcard.h"

using std::string;

bankcard::bankcard(string cardnum,string passport,string bankname,string name,string ID,doub le remainder){

this->cardnum=cardnum;

this->password=passport;

this->bankname=bankname;

this->name=name;

this->ID=ID;

this->remainder=remainder;

}

void bankcard::setPassword(string newpassport){ this->password=newpassport;

}

void bankcard::addRemainder(double money){ this->remainder+=money;

}

void bankcard::subRemainder(double money){ this->remainder=this->remainder-money;

}

stringbankcard::getCardnum(){

return cardnum;

}

stringbankcard::getPassword(){

return password;

}

stringbankcard::getBankname(){

return bankname;

}

stringbankcard::getName(){

return name;

}

stringbankcard::getID(){

return ID;

}

double bankcard::getRemainder(){

return remainder;

}

bool bankcard::matchPassport(string password){ if(this->password==password)

returntrue;

elsereturnfalse;

}

ofstream&bankcard::operator <<(ofstream&ofs){ ofs<<" 卡号:"<

ofs<<" 密码:"<

ofs<<" 银行:"<

ofs<<" 姓名:"<

ofs<<" 身份证号:"<

ofs<<" 余额:"<

return ofs;

}

#ifndef BANKPORTAL_H

#define BANKPORTAL_H

#include

#include

#pragmacomment(lib,"ws2_32.lib")

#include

#include

#include"bankcard.h"

using std::vector;

class BankPortal{

private:

vector vBankN;

vector vBankC;

void init();

void registerBankC();

void loginBankC();

void changePassword(int index);

void deposit(int index);

void withdraw(int index);

int match(string cardnum);

bool check(string cardnum);

void output();

void bankNaming();

double StrToDouble(string str, int start, int end);

void Bind();

bool pay(string num,double money);

int Match(string username,string password);

public:

BankPortal();

};

#endif

#include

#include

#include

#include

#include

#include

#include"bankportal.h"

const string bankname_file = "bankname.txt";

const string output_file = "output.txt";

BankPortal::BankPortal(){

init();

cout<<"1:进入银行门户系统 2:进入监听模式,请输入您的选择:";

int Choice;

string Select;

while(!(cin >> Select) || (Select[0] != '1'&& Select[0] != '2'))

cout<<"输入错误,请重新输入:"<< endl;

cout<< endl;

Choice=std::atoi(Select.c_str());

if(Choice==2)

Bind();

while(true){

cout<< endl;

cout <<"=================欢迎来到银行门户=================="<< endl;

cout<<"==================================================="<< endl;

cout <<"=================您想进行什么操作?================"<< endl;

cout <<"=================1:操作账户 ================"<< endl;

cout <<"=================2:注册银行卡 ================"<< endl;

cout <<"=================3:退出 ================"<< endl;

cout<<"==================================================="<< endl <

cout <<"请输入您要进行的操作:";

int select;

string Select;

while(!(cin >> Select) || Select[0] <'1' || Select[0] >'3')

cout<<"输入错误,请重新输入:"<< endl;

cout<< endl;

select=std::atoi(Select.c_str());

switch(select)

{

case 1:

loginBankC();

break;

case 2:

registerBankC();

break;

case 3:

output();

return;

default:

break;

}

}

}

void BankPortal::registerBankC(){

cout <<"请选择你要注册的银行:"<< endl;

for(unsignedint i=0;i

if(i % 4 == 3)

cout<<" "<< i <<":"<< vBankN[i] << endl;

else

cout<<" "<< i <<":"<< vBankN[i];

}

cout << endl <<"请输入您的选择:";

unsignedint num;

while(!(cin >> num) || num < 0 && num >= vBankN.size()) cout << endl <<"输入错误,请重新输入:";

cout<< endl;

string bank = vBankN[num];

int cardnum;

strstream t;

string card;

string password;

string temp;

string name;

string ID;

int i;

cout <<"姓名:";

cin>> name;

cout <<"身份证号:";

cin>> ID;

srand((int)time(0));

do{

cardnum=1000+(rand()%9000);

t<

t>>card;

}

while(check(card));

do{

cout <<"设置密码:";

cin>> password;

cout <<"再次确认密码:";

cin>> temp;

}

while(password != temp && cout << endl <<"前后输入的密码不同,请重新输入!"<< endl);

cout<< endl;

bankcard bcard(card, password, bank, name, ID,0.0);

vBankC.push_back(bcard);

cout <<"注册银行账号成功!"<

cout <<"卡号为:"<

}

void BankPortal::loginBankC(){

string cardnum;

string password;

unsignedint num;

do{

cout<<"请输入要操作的银行卡号:"<

cin>>cardnum;

}

while((num=match(cardnum))>=vBankC.size()&&cout<<"账号输入错误!"<

while(true){

cout <<"========================"<< vBankC[num].getName() <<"先生/女士

=================="<< endl;

cout <<"=====================卡内余额共 "<< vBankC[num].getRemainder()

<<"RMB================"<< endl;

cout<<"===================================================="<< endl;

cout <<"===================请选择您的操作:================="<< endl;

cout <<"===================1:存款 ================="<< endl;

cout <<"===================2:取款 ================="<< endl;

cout <<"===================3:修改银行卡密码================="<< endl;

cout <<"===================4:返回上一级菜单================="<< endl;

cout<<"===================================================="<< endl << endl;

cout <<"请选择您的操作:";

int select;

string Select;

while(!(cin >> Select) || Select[0] <'1' || Select[0] >'4')

cout<<"输入错误,请重新输入:"<< endl;

select=std::atoi(Select.c_str());

cout<< endl;

switch(select)

{

case 1:

deposit(num);

break;

case 2:

withdraw(num);

break;

case 3:

changePassword(num);

break;

case 4:

return;

default:

break;

}

}

}

void BankPortal::deposit(int num){

double money;

cout<<"请输入您要存入的金额:"<

cin>>money;

vBankC[num].addRemainder(money);

}

void BankPortal::withdraw(int num){

double money;

cout<<"请输入您要取出的金额:"<

cin>>money;

if(vBankC[num].getRemainder()>=money){

vBankC[num].subRemainder(money);

}

else cout<<"对不起,余额不足!"<

}

bool BankPortal::pay(string num,double money){

int index;

index=match(num);

if(vBankC[index].getRemainder()>=money){

vBankC[index].subRemainder(money);

returntrue;

}

elsereturnfalse;

}

void BankPortal::changePassword(int num){

string password;

string temp;

do{

cout<<"请输入新密码:"<

cin>>password;

cout<<"再次确认新密码:"<

cin>>temp;

}

while(password!=temp&&cout <<"前后输入的密码不同,请重新输入!"<< endl);

vBankC[num].setPassword(password);

}

int BankPortal::match(string cardnum){

unsignedint i=0;

for(;i

if(vBankC[i].getCardnum()==cardnum){

return i;

}

}

return i;

}

bool BankPortal::check(string cardnum){

unsignedint i=0;

for(;i

if(vBankC[i].getCardnum()==cardnum){

returntrue;

}

}

returnfalse;

}

void BankPortal::output(){

ofstream fout(output_file.c_str());

for(unsignedint i=0;i

vBankC[i]<<(fout);

}

fout.close();

}

void BankPortal::bankNaming(){

ifstream fin(bankname_file.c_str());

string input;

while(getline(fin,input)){

vBankN.push_back(input);

}

fin.close();

}

double BankPortal::StrToDouble(string str, int start, int end){//把字符串转化成double类型int state = 0;

int i = 0;

double num = 0;

for(; start

if(state == 0 &&str[start] != '.'&&isdigit(str[start]))

num = num * 10 + str[start] - '0';

elseif(state == 0 &&str[start] == '.')

state = 1;

elseif(state == 1 && isdigit(str[start])){

num = num * 10 + str[start] - '0';

++i;

}

}

return num/pow(10, i);

}

void BankPortal::init(){

ifstream fin(output_file.c_str());

string cardnum;

string password;

string bankname;

string name;

string ID;

string remainders;

double remainder;

while(fin>>cardnum){

cardnum=cardnum.substr(6);

fin>>password;

password=password.substr(6);

fin>>bankname;

bankname=bankname.substr(6);

fin>>name;

name=name.substr(6);

fin>>ID;

ID=ID.substr(10);

fin>>remainders;

remainder=StrToDouble(remainders,6,remainders.size());

bankcard bankc(cardnum,password,bankname,name,ID,remainder);

vBankC.push_back(bankc);

}

fin.close();

bankNaming();

}

int BankPortal::Match(string cardnum,string password){

unsignedint i=0;

for(;i

if(vBankC[i].getCardnum()==cardnum&&vBankC[i].getPassword()==password) return i;

}

return i;

}

void BankPortal::Bind()

{

WORD sockVersion = MAKEWORD(2,2);

WSADATA wsaData;

if(WSAStartup(sockVersion, &wsaData)!=0)

{

printf("嵌套字未打开!");

return;

}

//创建套接字

SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(slisten == INVALID_SOCKET)

{

printf("socket error !");

return;

}

//绑定IP和端口

sockaddr_in sin;

sin.sin_family = AF_INET;

sin.sin_port = htons(8888);

sin.sin_addr.S_un.S_addr = INADDR_ANY;

if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)

{

printf("bind error !");

}

//开始监听

if(listen(slisten, 5) == SOCKET_ERROR)

{

printf("listen error !");

return;

}

//循环接收数据

SOCKET sClient;

sockaddr_in remoteAddr;

int nAddrlen = sizeof(remoteAddr);

char sendBuf[2];

char receiveChoice[2];

int choice;

while (true)

{

printf("等待连接...\n");

sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);

if(sClient == INVALID_SOCKET)

{

printf("accept error !");

continue;

}

printf("接受到一个连接:%s ", inet_ntoa(remoteAddr.sin_addr));

recv(sClient,receiveChoice,2,0); //监听选择

choice=(receiveChoice[0]-'0');

if(choice==1)

{

char receiveCard[5];

char receivePassword[30];

recv(sClient,receiveCard,5,0); //接受账号密码

recv(sClient,receivePassword,30,0);

string cardnum;

cardnum=receiveCard;

string password;

password=receivePassword;

int index;

if((index=Match(cardnum,password))

sprintf(sendBuf,"1");

send(sClient,sendBuf,strlen(sendBuf)+1,0); //发送绑定成功 sprintf(sendBuf,vBankC[index].getBankname().c_str());//发送银行类型

send(sClient,sendBuf,strlen(sendBuf)+1,0);

}

else{

sprintf(sendBuf,"0");

send(sClient,sendBuf,strlen(sendBuf)+1,0);

}

}

elseif(choice==2){

char receiveCard[5];

char receiveShouldpay[20];

char receivePassword[30];

int index;

recv(sClient,receiveCard,5,0); //接受账号密码

recv(sClient,receivePassword,30,0);

string cardnum;

cardnum=receiveCard;

string password;

password=receivePassword;

index=Match(cardnum,password);

if(index

sprintf(sendBuf,"1");

send(sClient,sendBuf,strlen(sendBuf)+1,0); //发送验证成功

recv(sClient,receiveShouldpay,20,0);

double shouldpay;

shouldpay=std::atof(receiveShouldpay);

if(pay(cardnum,shouldpay)){

sprintf(sendBuf,"1");

send(sClient,sendBuf,strlen(sendBuf)+1,0); //发送付款成功output();

}

else{

cout<<"余额不足!";

sprintf(sendBuf,"0");

send(sClient,sendBuf,strlen(sendBuf)+1,0); //发送付款成功}

}

else{

sprintf(sendBuf,"0");

send(sClient,sendBuf,strlen(sendBuf)+1,0); //发送验证失败 }

}

}

closesocket(slisten);

WSACleanup();

}

电商平台代码:

#ifndef _ACOUNT_H_

#define_ACOUNT_H_

#include

#include

#include

#include"item.h"

#include"bankcard.h"

usingnamespace std;

class Account{

private:

string username;

string password;

string phonenum;

double shouldpay;

int cardnum;

public:

vector vBank;

vector vItem;

double getShouldpay(){

return shouldpay;

}

int getCardnum(){

return cardnum;

}

void addCardnum(int num)

{

cardnum+=num;

}

void clshouldpay(){

shouldpay=0.0;

}

void addShouldpay(double price){

shouldpay+=price;

}

void subShouldpay(double price){

shouldpay-=price;

}

Account();

Account(string username,string password,string phonenum,vector scart,double shouldpay,int cardnum);

void setPassword(string password);

void setPhone(string phonenum);

string getUsername();

string getPassword();

string getPhonenum();

ofstream& operator <<(ofstream& ofs);

};

#endif

#ifndef _BANKCARD_H_

#define_BANKCARD_H_

#include

#include

usingnamespace std;

class bankcard{

private:

string cardnum;

string bankname;

public:

bankcard(){}

bankcard(string cardnum,string bankname){

this->cardnum=cardnum;

this->bankname=bankname;

}

string getCardnum(){

return cardnum;

}

string getBankname(){

return bankname;

}

};

#endif

#ifndef _GOODS_H_

#define_GOODS_H_

#include

#include

usingnamespace std;

//登录后,1.查看现有商品{1.查看哪一类商品(记得显示产品描述,产品原价,产品剩余量,优惠信息)}|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 subRemainder(int num){

if(remainder>=num)

{

remainder=remainder-num;

returntrue;

}

returnfalse;

}

double getDiscount();

virtualdouble getPrice(){

return discount*originalPrice;

}

ofstream&goods::operator <<(ofstream& ofs);

};

class food : public goods{

public:

virtualdouble getPrice();

food(string name,string description,double originalPrice,int remainder,double discount): goods(name,description,originalPrice,remainder,discount){}

};

class clothing : public goods{

public:

virtualdouble getPrice();

clothing(string name,string description,double originalPrice,int remainder,double discou nt):goods(name,description,originalPrice,remainder,discount){}

};

class book : public goods{

public:

virtualdouble getPrice();

book(string name,string description,double originalPrice,int remainder,double discount): goods(name,description,originalPrice,remainder,discount){}

};

#endif

#ifndef _ITEM_H_

#define_ITEM_H_

#include

usingnamespace std;

class item{

protected:

string name;

int num;

double price;

public:

item(string name,int num,double price){

this->name=name;

this->num=num;

this->price=price;

}

string getName(){

return name;

}

int getNum(){

return num;

}

double getPrice(){

return price;

}

};

#endif

#ifndef _PLATFORM_H_

#define_PLATFORM_H_

#include

#include

#pragmacomment(lib,"ws2_32.lib")

#include

#include

#include"Account.h"

#include"goods.h"

using std::vector;

class Platform{

private:

vector vAccount;

vector vFood;

vector vClothing;

vector vBook;

void init();//初始化

void registerAccount();

bool loginAccount();

//void changePhone(string phonenum);

void output();

bool check(string username);

int match(string username,string password);

void checkgoods(int account);

double StrToDouble(string str, int start, int end);

bool addshoppingcart(int choice,int index,int num,int account);

void checkScart(int account);

void Band(int index,string cardnum,string password);

bool Pay(int index,string card);

public:

Platform();

};

#endif

#include"Account.h"

using std::string;

Account::Account(string username,string password,string phonenum,vectorscart,double s houldpay,int cardnum){

this->username=username;

this->password=password;

this->phonenum=phonenum;

this->vItem=scart;

this->shouldpay=shouldpay;

this->cardnum=cardnum;

}

void Account::setPassword(string password){

this->password=password;

}

void Account::setPhone(string phonenum){

this->phonenum=phonenum;

}

stringAccount::getUsername(){

return username;

}

stringAccount::getPassword(){

return password;

}

stringAccount::getPhonenum(){

return phonenum;

}

ofstream&Account::operator <<(ofstream&ofs){

ofs<<" 用户名:"<

ofs<<" 密码:"<

ofs<<" 手机号:"<

ofs<<" 银行卡数量:"<

//ofs<<" 购物车:";

//for(unsigned i=0;i

// ofs<

"<

//}

return ofs;

}

#include"goods.h"

using std::string;

//goods::goods(string name,string description,double originalPrice,int remainder,double discount)

stringgoods::getName(){

return name;

}

stringgoods::getDescription(){

return description;

}

double goods::getOriginalPrice(){

本文来源:https://www.bwwdw.com/article/3q2e.html

Top