星期三四五
實訓(xùn)內(nèi)容
1、模擬網(wǎng)上購書結(jié)賬功能;
實訓(xùn)要求
1、給出詳細的需求分析和描述,并給出實際需求分析文檔和UML描述;
2、給出詳細設(shè)計文檔及UML描述;
3、根據(jù)詳細設(shè)計的結(jié)果,寫出完整的C++代碼。要注意編碼風(fēng)格與編碼規(guī)范。
4、根據(jù)測試理論,寫出測試數(shù)據(jù)并給出詳細測試報告;
5、對該系統(tǒng)的可擴充性、可修改性和可維護性給出客觀的評價。
、賳栴}分析與功能定義:
本體是一個非常實用的題目。
隨著互聯(lián)網(wǎng)技術(shù)的飛速發(fā)展,網(wǎng)上購物越來越受到大家的喜愛,坐在家中,點點鼠標,就有人把你要的東西送到手中,多愜意呀。
現(xiàn)在要解決的問題是:用戶網(wǎng)上購書以后,系統(tǒng)根據(jù)購書人的不同類型計算出購書人的費用。
網(wǎng)上購書的一般過程是:用戶首先輸入他的會員號,然后,選擇想買的書籍放到購書筐中,選擇結(jié)束以后,用戶要求系統(tǒng)結(jié)賬,系統(tǒng)編輯算出費用通知用戶。本例并不像模擬網(wǎng)上購書的全部過程,所以會把選擇書的過程省去,假設(shè)用戶已經(jīng)選定了兩本書。
根據(jù)實際情況,確定了購書人可分為三類:普通人、會員、貴賓。
②對象(類)設(shè)計
根據(jù)上面的分析,需要設(shè)計一個基類buyer和它的3個派生類member(會員)、layfolk(普通人)、honoured_guest(貴賓)。基類中包含的數(shù)據(jù)成員是姓名、購書人編號、地址、購書金額。Member類中除了繼承了buyer的數(shù)據(jù),還增加了會員級別:honoured_guest則增加了折扣率一項。
在基類中定義了構(gòu)造函數(shù)和對所有類型的購書人相同的操作,getbuyname()負責(zé)取出購書者的姓名;getaddress()負責(zé)取出購書者的地址;getpay()負責(zé)取出購書者應(yīng)付的金額;getid()負責(zé)取出購書者的編號。
有關(guān)購書者的定義類如下:
class buyer
{
protected:
string name;
int buyerID;
string address;
double pay;
public:
buyer();
buyer(string n,int b,string a,double p);
string getbuyername();
string getaddress();
double getpay();
int getid();
virtual void display()=0;
virtual void setpay(double=0)=0;
};
class member:public buyer
{
public:
member(string n,int b,int l,string a,double p):buyer(n,b,a,p)
{leaguer_grade=1;}
void display();
void setpay(double p);
private:
int leaguer_grade;
};
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);
};
由于在計算購書金額的時要知道用戶買了那些書以及書的原價,所以必須建立一個類book,幫助完成對數(shù)的有關(guān)操作。
類book的定義如下:
class book
{
protected:
string book_ID;
string book_name;
string auther;
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 getauther();
string getpublishing();
double getprice();
};
從book到buyer類的箭頭表示:book對象要傳消息給buyer對象。
class string
{
friend ostream& operator<<(ostream& S,const string& Str);
friend istream& operator>>(istream& S,string& Str);
public:
string();
string(const string& Str);
void operator=(const string& Str);
~string();
string(char * p);
private:
short m_Length;
char * m_Data;
};
、酆诵目刂圃O(shè)計
在主函數(shù)中我們要做的操作包括:
(1) 建立繼承了基類buyer的3各類對象。
(2) 建立兩個book對象。
(3) 請用戶輸入購書人的編號。
(4) 通過編號查詢到相應(yīng)的對象。
(5) 用對象的計算金額的方法計算購書金額。
、芫幋a與測試
完成前幾階段的工作,現(xiàn)在就可以編碼實現(xiàn)程序了。
程序中包括了4個文件:buy.h、book..h、strclass.h和buy_book.cpp文件。
Buy_book.cpp程序中有main()函數(shù)
完整程序及相應(yīng)的說明如下:
//buy.h文件
class buyer
{
protected:
string name;
int buyerID;
string address;
double pay;
public:
buyer();
buyer(string n,int b,string a,double p);
string getbuyername();
string getaddress();
double getpay();
int getid();
virtual void display()=0;
virtual void setpay(double=0)=0;
};
class member:public buyer
{
public:
member(string n,int b,int l,string a,double p):buyer(n,b,a,p)
{leaguer_grade=1;}
void display();
void setpay(double p);
private:
int leaguer_grade;
};
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::getbuyername()
{return name;}
int buyer::getid()
{return buyerID;}
void member::display()
{
cout<<"購書人姓名:"<
cout<<"購書人編號:"<
cout<<"購書人為會員,級別:"<
cout<<"地址 :"<
}
void member::setpay(double p)
{
if(leaguer_grade==1)
pay=.95*p+pay;
else if(leaguer_grade==2)
pay=.90*p+pay;
else if(leaguer_grade==3)
pay=.85*p+pay;
else if(leaguer_grade==4)
pay=.8*p+pay;
else if(leaguer_grade==5)
pay=.7*p+pay;
else
cout<<"級別錯誤!";
}
void honoured_guest::display()
{
cout<<"購書人姓名:"<
cout<<"購書人編號:"<
cout<<"購書人為貴賓,折扣率為:"<
cout<<"地址 :"<
}
void honoured_guest::setpay(double p)
{ pay=pay+(1-discount_rate)*p; }
void layfolk::display()
{
cout<<"購書人姓名:"<
cout<<"購書人編號:"<
cout<<"購書人為普通人"<<"\n";
cout<<"地址:"<
}
void layfolk::setpay(double p)
{pay=pay+p;}
//buy.h結(jié)束
//book.h開始
class book
{
protected:
string book_ID;
string book_name;
string auther;
string publishing;
double price;
public:
book();