1. 程式人生 > >軟件綜合實踐專題

軟件綜合實踐專題

單繼承 sin alt color 微軟雅黑 權限 rtu double 根據

訂單系統簡單分析

這是一個訂單系統軟件,是我的同學在小學期設計的一段代碼,這段代碼的主要功能是對於圖書的管理,通過order類的訂單管理,其中包含圖書信息,購書人信息,應付金額信息等,不同的信息由不同的類掌控,我根據代碼信息也畫了一個相應的簡易流程圖

技術分享圖片

這段代碼有優點也有缺點,優點是對於圖書管理系統中一個個功能模塊分的非常清楚,無論是用戶的信息還是圖書的信息或者是訂單的信息都在一個個相應的類裏,包括售後評價等也是齊全,但是我覺得有些代碼顯得太過繁瑣,或者說使用的方法一直是那麽幾個,函數方法沒有改變,繼承的時候也是普通單繼承方式,看多了會讓其他人覺得代碼太過冗長,如果還要加以完善的話,在功能上建議添加用戶權限這種功能,分為總管理員系統,讀者系統,不同的權限可以訪問不同的功能,當然,就我們現在的知識和能力而言,這段代碼在c++中已經是較為優秀和完善的代碼了。

#include<string>
using namespace std;
class buyer
{ protected:
string name;
int buyerID;
string address;
double pay;
public:
buyer();
buyer(string n,int b,string a,double p);
string getbuyname();
string getaddress();

double getpay();
int getid();
virtual void display()=0;
virtual void setpay(double=0)=0;
};


class member:public buyer
{ int leaguer_grade;
public:
member(string n,int b,int l,string a,double p):buyer(n,b,a,p)
{leaguer_grade=l;}
void display();
void setpay(double p);
};

class honoured_guest:public buyer
{ double discount_rate;
public:
honoured_guest(string n,int b,double r,string a,double p):buyer(n,b,a,p)
{discount_rate=r;}
void display();
void setpay(double p);
};
class order
{protected:
string orderID;
int buyerID;
string orderlist;
int listcount;
double price;

public:
order();
order(string o,int b,string ol,int l,int pr);
string getorderID();
int getbuyerID();
string getorderlist();
int getlistcount();
double getprice();
void display();
void getlist();
};

order::order(string o,int b,string ol,int l,int pr)
{	orderID=o;
buyerID=b;
orderlist=ol;
listcount=l;
price=pr;
}

order::order()
{	orderID="";
buyerID=0;
orderlist="";
listcount=0;
price=0;
}


void order::getlist(){
int i;
cout<<"\n\n訂單信息:\n\n";
ofstream fout("buying.dat",ios::out);
for(i=0;i<3;i++)
{
order* o[3];
order o1("2016-06-27-001",1,"7-302-04504-6",100,2500);
order o2("2016-06-27-002",3,"7-402-03388-9",10,200);
order o3("2016-06-27-003",2,"7-302-04504-6",40,1000);
o[0]=&o1;
o[1]=&o2;
o[2]=&o3;
o[i]->display();
fout<<"訂單編號:"<<o[i]->getorderID()<<endl;
fout<<"購書人編號:"<<o[i]->getbuyerID()<<endl;
fout<<"圖書編號:"<<o[i]->getorderlist()<<endl;
fout<<"購書數量:"<<o[i]->getlistcount()<<endl;
fout<<"總價格:"<<o[i]->getprice()<<endl<<endl;
}
fout.close();
cout<<"訂單已保存到buying.dat中"<<endl;}

void order::display()
{	cout<<"訂單編號:"<<orderID<<endl;
cout<<"購書人編號:"<<buyerID<<endl;
cout<<"圖書編號:"<<orderlist<<endl;
cout<<"購書數量:"<<listcount<<endl;
cout<<"總價格:"<<price<<endl<<endl;
}

string order::getorderID()
{return orderID;}
int order::getbuyerID()
{return buyerID;}
string order::getorderlist()
{return orderlist;}
int order::getlistcount()
{return listcount;}
double order::getprice()
{return price;}
class layfolk:public buyer
{public:
layfolk(string n,int b,string a,double p):buyer(n,b,a,p)
{ }
void display();
void setpay(double p);
};
#include<string>
using namespace std;
class buyer
{ protected:
string name;
int buyerID;
string address;
double pay;
public:
buyer();
buyer(string n,int b,string a,double p);
string getbuyname();
string getaddress();

double getpay();
int getid();
virtual void display()=0;
virtual void setpay(double=0)=0;
};


class member:public buyer
{ int leaguer_grade;
public:
member(string n,int b,int l,string a,double p):buyer(n,b,a,p)
{leaguer_grade=l;}
void display();
void setpay(double p);
};

class honoured_guest:public buyer
{ double discount_rate;
public:
honoured_guest(string n,int b,double r,string a,double p):buyer(n,b,a,p)
{discount_rate=r;}
void display();
void setpay(double p);
};

class layfolk:public buyer
{public:
layfolk(string n,int b,string a,double p):buyer(n,b,a,p)
{ }
void display();
void setpay(double p);
};

buyer::buyer()
{	name="";
buyerID=0;
address="";
pay=0;
}

buyer::buyer(string n,int b,string a,double p)
{	name=n;
buyerID=b;
address=a;
pay=p;
}

double buyer::getpay()
{return pay;}
string buyer::getaddress()
{return address;}
string buyer::getbuyname()
{return name;}
int buyer::getid()
{return buyerID;}

void member::display()
{	cout<<"購書人姓名:"<<name<<"\t";
cout<<"購書人編號:"<<buyerID<<"\t";
cout<<"購書人為會員,級別:"<<leaguer_grade<<endl;
cout<<"地址:"<<address<<endl;
}

void member::setpay(double p)
{	if(leaguer_grade==1)
pay=0.95*p+pay;
else if(leaguer_grade==2)
pay=0.9*p+pay;
else if(leaguer_grade==3)
pay=0.85*p+pay;
else if(leaguer_grade==4)
pay=0.8*p+pay;
else if(leaguer_grade==5)
pay=0.7*p+pay;
else
cout<<"級別錯誤!";
}

void honoured_guest::display()
{	cout<<"購書人姓名:"<<name<<"\t";
cout<<"購書人編號:"<<buyerID<<"\t";
cout<<"購書人為貴賓,折扣率為:"<<discount_rate*100<<"%\n";
cout<<"地址:"<<address<<endl<<endl;
}

void honoured_guest::setpay(double p)
{	pay=pay+(1-discount_rate)*p; }

void layfolk::display()
{	cout<<"購書人姓名:"<<name<<"\t";
cout<<"購書人編號:"<<buyerID<<"\t";
cout<<"購書人為普通人"<<endl;
cout<<"地址:"<<address<<endl<<endl;
}

void layfolk::setpay(double p)
{	pay=p+pay; }
//buy.h文件結束
class book
{protected:
string book_ID;
string book_name;
string author;
string publishing;
double price;
public:
book();
book(string b_id,string b_n,string au,string pu,double pr);
void display();
string getbook_ID();
string getbook_name();
string getauthor();
string getpublishing();
double getprice();
void getall(); 
};

book::book(string b_id,string b_n,string au,string pu,double pr)
{	book_ID=b_id;
book_name=b_n;
author=au;
publishing=pu;
price=pr;
}
book::book()
{	book_ID="";
book_name="";
author="";
publishing="";
price=0;
}

void book::display()
{	cout<<"書號:"<<book_ID<<"\t";
cout<<"書名:"<<book_name<<"\t";
cout<<"作者:"<<author<<endl;
cout<<"出版社:"<<publishing<<"\t";
cout<<"定價:"<<price<<"\n";
}
void book::getall(){
book* c[5];	
book c1("7-302-04504-6","c++程序設計","譚浩強","清華",25);
book c2("7-402-03388-9"," 數據結構","徐卓群","北大",20);	
book c3("7-547-82783-3"," 海洋大數據","黃冬梅","海洋出版社",40);
book c4("7-302-32507-9"," 離散數學","屈婉玲","清華大學出版社",33);
book c5("7-040-37346-2"," 數學分析","哈工大數學系","高等教育出版社",26);
c[0]=&c1;
c[1]=&c2;
c[2]=&c3;
c[3]=&c4;
c[4]=&c5;
cout<<"圖書信息"<<endl;
ofstream outf("book.txt",ios::out);
int i; 
for(i=0;i<5;i++){
outf.write((char*)&c[i],sizeof(c[i]));
c[i]->display();
}
outf.close(); 
}
string book::getbook_ID()
{return book_ID;}
string book::getbook_name()
{return book_name;}
string book::getauthor()
{return author;}
string book::getpublishing()
{return publishing;}
double book::getprice()
{return price;}
//book.h 文件結束
//buy_book.cpp文件開始 #include<iostream> #include<fstream> #include<string> using namespace std; #include"buy.h" #include"book.h" #include"order.h" #include"orderank.h" #include"strclass.h" const string people[4][2] = { "chenbx", "1234", "zhaoyt", "3456", "wangj", "5678", "zhujh","7890" }; void pingjia(); int main() { string name,pw; int m,n; cout<<" 歡迎來到網上購書查詢系統"<<endl; cout<<"----------------請登錄------------------"<<endl; cout<<"請輸入用戶名:"; while(cin>>name) { for(m = 0; m < 3; m ++) { if(people[m][0]==name)break; } if(m == 3) { cout << "未找到該用戶\n"; return 0; } n=m; break; } cout<<"請輸入密碼:"; cin >> pw; if(people[n][1]!=pw) { cout << "密碼錯誤\n"; return 0; } cout << "登陸成功,歡迎你"<< name << endl; int i=0,buyerid,flag; layfolk b1("林小茶",1,"北京",0); honoured_guest b2("王遙遙",2,.6,"上海",0); member b3("趙紅艷",3,5,"廣州",0); buyer*b[3]={&b1,&b2,&b3}; book* c[5]; book c1("7-302-04504-6","c++程序設計","譚浩強","清華",25); book c2("7-402-03388-9"," 數據結構","徐卓群","北大",20); book c3("7-547-82783-3"," 海洋大數據","黃冬梅","海洋出版社",40); book c4("7-302-32507-9"," 離散數學","屈婉玲","清華大學出版社",33); book c5("7-040-37346-2"," 數學分析","哈工大數學系","高等教育出版社",26); c[0]=&c1; c[1]=&c2; c[2]=&c3; c[3]=&c4; c[4]=&c5; order* o[3]; order o1("2016-06-27-001",1,"7-302-04504-6",100,2500); order o2("2016-06-27-002",3,"7-402-03388-9",10,200); order o3("2016-06-27-003",2,"7-302-04504-6",40,1000); o[0]=&o1; o[1]=&o2; o[2]=&o3; orderank* r[3]; orderank r1("2016-06-27-001",1,"7-302-04504-6",100,2500); orderank r2("2016-06-27-002",3,"7-402-03388-9",10,200); orderank r3("2016-06-27-003",2,"7-302-04504-6",40,1000); r[0]=&r1; r[1]=&r2; r[2]=&r3; char k; while(k!=‘6‘) { cout<<endl<<endl; cout<<"---------------------------------------------"<<endl; cout<<"----1.查看購書人信息-------------------------"<<endl; cout<<"----2.查看圖書信息---------------------------"<<endl; cout<<"----3.訂單查詢-------------------------------"<<endl; cout<<"----4.查詢應付金額---------------------------"<<endl; cout<<"----5.訂單排序打印---------------------------"<<endl; cout<<"----6.進行評價-------------------------------"<<endl; cout<<"---------------------------------------------"<<endl; cout<<"----請選擇功能:"; cin>>k; switch(k) { case ‘1‘: { cout<<"購書人信息:\n\n"; for(i=0;i<3;i++) b[i]->display(); }break; case ‘2‘: { book A; A.getall(); }break; case ‘3‘:{ order B; B.getlist(); }break; case ‘4‘:{ cout<<"\n\n請輸入購書人編號:"; cin>>buyerid; flag=0; for(i=0;i<3;i++) if(b[i]->getid()==buyerid) { flag=1;break; } if(!flag) {cout<<"編號不存在"<<endl;} else { b[i]->setpay(c[0]->getprice()); b[i]->setpay(c[1]->getprice()); cout<<endl<<"購書人需要付費:"<<b[i]->getpay()<<"\n\n"; }}break; case ‘5‘: { cout<<"\n\n訂單按總價格排序為:"<<endl<<endl; int w,s,l=3; orderank* tmp; ofstream sout("order.txt",ios::out); for(s=0; s<l-1; s++) for(w=0; w<l-1-i; w++) if(r[w+1]->getprice() > r[w]->getprice()) { tmp = r[w]; r[w] = r[w+1]; r[w+1] = tmp; } for(w=0;w<l;w++) { r[w]->display(); sout<<"訂單編號:"<<r[w]->getorderID()<<endl; sout<<"購書人編號:"<<r[w]->getbuyerID()<<endl; sout<<"圖書編號:"<<r[w]->getorderlist()<<endl; sout<<"購書數量:"<<r[w]->getlistcount()<<endl; sout<<"總價格:"<<r[w]->getprice()<<endl<<endl; } sout.close(); cout<<"\n訂單已按總價格排序並打印到order.txt中"<<endl; }break; case ‘6‘: pingjia();break; } } return 0; } void pingjia( ){ cout<<"\n\n\n\n\n\n\t\t\t\t *********************待評價*******************\n\n\n"; cout<<"\t\t\t\t 交易成功,請對賣家作出評價\n\n\n"; cout<<"\t\t\t\t 1.☆ 2.☆☆ 3.☆☆☆ 4.☆☆☆☆ 5.☆☆☆☆☆\n"; int a; cout<<"\n\t\t\t\t\t 描述相符:"; cin>>a; switch(a) { case 1:cout<<"\t\t\t\t\t 描述相符:☆\n";break; case 2:cout<<"\t\t\t\t\t 描述相符:☆☆\n";break; case 3:cout<<"\t\t\t\t\t 描述相符:☆☆☆\n";break; case 4:cout<<"\t\t\t\t\t 描述相符:☆☆☆☆\n";break; case 5:cout<<"\t\t\t\t\t 描述相符:☆☆☆☆☆\n";break; } int b; cout<<"\n\t\t\t\t\t 物流服務:"; cin>>b; switch(b) { case 1:cout<<"\t\t\t\t\t 物流服務:☆\n";break; case 2:cout<<"\t\t\t\t\t 物流服務:☆☆\n";break; case 3:cout<<"\t\t\t\t\t 物流服務:☆☆☆\n";break; case 4:cout<<"\t\t\t\t\t 物流服務:☆☆☆☆\n";break; case 5:cout<<"\t\t\t\t\t 物流服務:☆☆☆☆☆\n";break; } int c; cout<<"\n\t\t\t\t\t 服務態度:"; cin>>c; switch(c) { case 1:cout<<"\t\t\t\t\t 服務態度:☆\n";break; case 2:cout<<"\t\t\t\t\t 服務態度:☆☆\n";break; case 3:cout<<"\t\t\t\t\t 服務態度:☆☆☆\n";break; case 4:cout<<"\t\t\t\t\t 服務態度:☆☆☆☆\n";break; case 5:cout<<"\t\t\t\t\t 服務態度:☆☆☆☆☆\n";break; } cout<<"\n\t\t\t\t\t 感謝您的評價(*^◎^*)\n\n\n"; } //buy_book.cpp文件結束

  

圖書信息

軟件綜合實踐專題