1.来源
该项目来自同学以前的期末大作业:网上购书系统项目。
2.运行环境及运行结果
(1)运行环境:Dev C++
(2)运行结果:
代码如下:
点击查看代码
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include"buyer.h"
#include"book.h"
#include"order.h"
#include"member.h"
#include"layfolk.h"
#include"honored.h"
using namespace std;
class member;
class layfolk;
class honored;
void show_menu(){
cout << "\t\t ------------------------------------------------------\n";
cout << "\t\t| | \n";
cout << "\t\t| 网上购书系统 |\n";
cout << "\t\t| 说明:请输入功能前数字 |\n";
cout << "\t\t|******************************************************|\n";
cout << "\t\t| 1 查看书单 |\n";
cout << "\t\t|******************************************************|\n";
cout << "\t\t| 2 快速查找 |\n";
cout << "\t\t|******************************************************|\n";
cout << "\t\t| 3 购买书籍 |\n";
cout << "\t\t|******************************************************|\n";
cout << "\t\t| 4 查看订单 |\n";
cout << "\t\t|******************************************************|\n";
cout << "\t\t| 5 退出系统 |\n";
cout << "\t\t| |\n";
cout << "\t\t ------------------------------------------------------\n";
}
int main()
{
buyer bu;
book b1;
order o;
member m(0.95);
layfolk l(1.0);
honored h(0.60);
// 登录账户
bu.log_in();
//进入主界面
while(1)
{
show_menu();
int n;
cout<<"请输入数字"<<endl;
cin>>n;
switch (n) {
case 1:
//展示书单
b1.ReadBook();
system("pause");
system("cls");
break;
case 2:
//快速查找
b1.CheckBook();
system("pause");
system("cls");
break;
case 3:
//购买书籍
b1.CinOrder(bu,l,m,h);//购买并添加到账单中
system("pause");
system("cls");
break;
case 4:
//查看账单
o.ShowOrder();
system("pause");
system("cls");
break;
case 5:
//退出
exit(1);
break;
default:
//TODO
break;
}
}
return 0;
}
#include "book.h"
#include "member.h"
#include "layfolk.h"
#include "honored.h"
#include"buyer.h"
book::book(){}
book::book(int b_ID,string b_Nam,string a_name,double b_price)
{
book_ID=b_ID;
book_name=b_Nam;
book_price=b_price;
author_name=a_name;
}
book::book(double sum,int c_id)
{
sumpay=sum;
cin_ID=c_id;
}
double book::Member_discount(member&m)
{
return m.mdiscount;
}
double book::Layfolk_discount(layfolk&l)
{
return l.ldiscount;
}
double book::Honored_discount(honored&h)
{
return h.hdiscount;
}
void book::Read()
{
ifstream check;
check.open("C:\\Users\\26829\\Desktop\\book.txt",ios::in);
if(!check)
{
cout<<"文件打开失败\n";
system("pause");
exit(0);
}
int i,j=0;
string n,an;
double p;
books.resize(10);
while(check>>i>>n>>an>>p)
{
books[j]= new book(i,n,an,p);
j++;
}
check.close();
}
void book::checkcheck(double b)
{
Read();
ofstream write;
write.open("C:\\Users\\26829\\Desktop\\order.txt",ios::out|ios::app);
write<<"总金额为:"<<b<<"元"<<endl;
write.close();
}
void book::ReadBook()
{
system("cls");
ifstream read;
read.open("C:\\Users\\26829\\Desktop\\book.txt",ios::in);
char abooks[1024]={0};
while(read.getline(abooks,sizeof(abooks)))
{
cout<<abooks<<endl;
}
}
void book::Check(int C_ID)
{
Read();
for(vector<book*>::const_iterator it=books.begin();it!=books.end();it++)
{
if((*it)->book_ID==C_ID)
{
cout<<setw(5)<<(*it)->book_ID<<" "<<setw(30)<<(*it)->book_name<<" "<<setw(30)<<(*it)->author_name<<" "<<setw(10)<<(*it)->book_price<<"元"<<endl;
ofstream write;
write.open("C:\\Users\\26829\\Desktop\\order.txt",ios::out|ios::app);
write<<setw(5)<<(*it)->book_ID<<setw(30)<<(*it)->book_name<<setw(30)<<(*it)->author_name<<setw(30)<<(*it)->book_price<<"元"<<endl;
break;
}
else if(C_ID>1007||C_ID<1001)
break;
}
}
void book::CheckBook()
{
Read();
book c1;
cout<<"请输入书籍编号:";
cin>>c1.book_ID;
for(vector<book*>::iterator it=books.begin(); it!=books.end(); it++)
{
if((*it)->book_ID==c1.book_ID)
{
cout<<(*it)->book_ID<<" ";
cout<<(*it)->book_name<<" ";
cout<<(*it)->author_name<<" ";
cout<<(*it)->book_price<<endl;
break;
}
else if(c1.book_ID>1007||c1.book_ID<1001)
{
cout<<"未找到相关书籍!"<<endl;
break;
}
}
}
double book::CalBook(int C_ID)
{
double b_pay=0;
Read();
for(vector<book*>::iterator it=books.begin();it!=books.end();it++)
{
if((*it)->book_ID==C_ID)
{
b_pay=(*it)->book_price;
break;
}
else if(C_ID>1007||C_ID<1001)
break;
}
return b_pay;
}
void book::judge(buyer&bu,layfolk&l,member&m,honored&h)
{
double fincost=0;
if(bu.grade==1)
{
fincost=sumpay*Layfolk_discount(l);
checkcheck(fincost);
cout<<"由于您尚未开通会员,所以您的总金额为:"<<fincost<<"元"<<endl;
cout<<"感谢您的消费!"<<endl;
}
else if(bu.grade==2)
{
fincost=sumpay*Member_discount(m);
checkcheck(fincost);
cout<<"由于您是普通会员,所以您的总金额为:"<<fincost<<"元"<<endl;
cout<<"感谢您的消费!"<<endl;
}
else if(bu.grade==3)
{
fincost=sumpay*Honored_discount(h);
checkcheck(fincost);
cout<<"由于您是高级会员,所以您的总金额为:"<<fincost<<"元"<<endl;
cout<<"感谢您的消费!"<<endl;
}
}
void book::CinOrder(buyer&bu,layfolk&l,member&m,honored&h)
{
system("cls");
ReadBook();
sumpay=0;
cout<<"_______________________________________"<<endl;
cout<<"请输入您想要购买的书籍ID:"<<endl;
cout<<"(输入9999以停止选购)"<<endl;
int BOOKID;
while(cin>>BOOKID&&BOOKID!=9999)
{
Check(BOOKID);
sumpay+=CalBook(BOOKID);
}
judge(bu,l,m,h);
}
#include "layfolk.h"
layfolk::layfolk(){}
layfolk::layfolk(double l_discount)
{
ldiscount=l_discount;
}
#include "order.h"
order::order(){}
void order::ShowOrder()
{
ifstream reorder;
reorder.open("C:\\Users\\26829\\Desktop\\order.txt",ios::in);
char eorder[1024]={0};
while(reorder.getline(eorder,sizeof(eorder)))
{
cout<<eorder<<endl;
}
reorder.close();
}
#include "buyer.h"
buyer::buyer(){}
buyer::buyer(string b_nam, int ID,int b_grade)
{
name = b_nam;
buyer_ID = ID;
grade = b_grade;
}
void buyer::log_in()
{
ifstream log;
log.open("C:\\Users\\26829\\Desktop\\buyer.txt", ios::in);
buyers.resize(10);
int i, g, j = 0;
string n;
while (log >> n >> i >> g)
{
buyers[j] = new buyer(n,i,g);//XD
j++;
}
log.close();
buyer b1;
cout << "请输入您的用户名和ID号" << endl;
cout << "用户名: ";
cin >> b1.name;
cout << endl;
cout << "ID号: ";
cin >> b1.buyer_ID;
cout << endl;
fstream order;
order.open("C:\\Users\\26829\\Desktop\\order.txt",ios::out|ios::app);
order<<b1.name<<" "<<b1.buyer_ID<<" ";
int flag=0;
for(vector<buyer*>::iterator it=buyers.begin();it!=buyers.end();it++)
{
if((*it)->name==b1.name&&(*it)->buyer_ID==b1.buyer_ID)
{
flag=1;
if((*it)->grade==1)
{
order<<" 无会员"<<endl;
grade=1;
}
else if((*it)->grade==2)
{
order<<" 普通会员"<<endl;
grade=2;
}
else if((*it)->grade==3)
{
order<<" 高级会员"<<endl;
grade=3;
}
break;
}
}
order.close();
if(flag==1)
{
cout<<"登陆成功!"<<endl;
}
else if(flag==0)
{
cout<<"登陆失败!"<<endl;
}
}
#include "honored.h"
honored::honored(){}
honored::honored(double h_discount)
{
hdiscount=h_discount;
}
项目结构:
运行截图:
3.主要问题列表
(1)layfolk类、honored类、member类仅实现了各级别会员的折扣率的定义,book类实现功能过多,类功能分配不合理。
(2)订单查看功能没有做好对数据储存的分配,导致之前的订单既包含用户自己的订单,还有其他用户的订单。
(3)购买书籍总金额没有对小数点后位数进行控制,以至于小数点后位数超过两位。
4.新代码
(1)对于类功能分配问题,我将layfolk类、honored类、member类三个类合为一个discount类,并将book类中的购买书籍的部分功能函数移到discount类中,采用友元函数Discount()将折扣数据传递给book类
点击查看代码
class discount:public buyer{
friend double book::Disount(discount&l,discount&m,discount&h);
protected:
double ldisount;
double mdiscount;
double hdiscount;
public:
discount();
discount(double l_discount,double m_discount,double l_discount);
}
#include"discount.h"
discount::discount(){}
discount::discount(double l_discount,double m_discount,double l_discount)
{
mdiscount=m_discount;
ldiscount=l_discount;
hdiscount=h_discount;
}
点击查看代码
case 5:
//退出
fstream fsm;
fsm.open("C:\\Users\\26829\\Desktop\\order.txt",ios::trunc);
exit(1);
break;
点击查看代码
void book::judge(buyer&bu,layfolk&l,member&m,honored&h)
{
double fincost=0;
if(bu.grade==1)
{
fincost=sumpay*Layfolk_discount(l);
checkcheck(fincost);
cout<<"由于您尚未开通会员,所以您的总金额为:"<<round(fincost*100)/100.00<<"元"<<endl;
cout<<"感谢您的消费!"<<endl;
}
else if(bu.grade==2)
{
fincost=sumpay*Member_discount(m);
checkcheck(fincost);
cout<<"由于您是普通会员,所以您的总金额为:"<<round(fincost*100)/100.00<<"元"<<endl;
cout<<"感谢您的消费!"<<endl;
}
else if(bu.grade==3)
{
fincost=sumpay*Honored_discount(h);
checkcheck(fincost);
cout<<"由于您是高级会员,所以您的总金额为:"<<round(fincost*100)/100.00<<"元"<<endl;
cout<<"感谢您的消费!"<<endl;
}
}
5.重构的软件测试截图
6.总结
本次项目的二次开发中,由于代码功能分配杂乱,导致阅读难度较大,大部分时间用于阅读和理清代码关系,这让我更加重视代码功能的合理分配,而且一定要写好注释,这对于团队项目开发很重要。其次是要注重细节,很多时候就是一点点的疏忽就会导致bug的出现,在写代码时要更加严谨。最后就是要针对客户的需求进行项目的开发,多站在客户的角度去想功能的实现。总而言之,本次项目的二次开发让我对于逆向软件工程有了更深的理解,也对自己的代码编写能力和代码编写的严谨性有更多的启发。