其他分享
首页 > 其他分享> > 对朋友已有的项目进行二次开发

对朋友已有的项目进行二次开发

作者:互联网

我的这篇博客的内容是对我朋友所做的图书管理系统的二次开发程序的介绍和对源代码的解读,然后有一些自己的感想。

原项目如图:

 

整个项目分为以下几个部分:

查看图书信息、查看购书人信息、添加订单、查看订单                

在我对此项目进行查看并使用的过程中发现了一些不足,如:

 

 在进行这个操作时(3号添加订单)购书人不知道购买的书籍信息,然后就是返回输入的如果不1会进入其他见面。还有添加订单的时候输出书本信息等bug,所以要添加一些代码进去,其中要添加一些选项。还有对源代码switch语句的整改。

 

 所以我在这一段添加了switch语句中的case1 如下:

 

 然后这是原项目主页面的代码:

 

 修改之后(消除了之前按1返回的bug):

 

 

 这是修改后的项目页面:

 

 然后进行操作4查看订单我添加了一些内容如图:

 

 及添加了购书人ID和书名信息。

再简单解读一下此项目:

  1. 查看图书信息:该模块继承原书代码
  2. 查看购书人信息:原书代码
  3. 增加订单:该模块首先创建order类,在该类里面设置ordercount静态变量,用来统计共有多少订单,同时用来表示订单号,它会随着订单的增加而逐渐增加。每创建一个订单,程序都会将相关信息记录到指定位置的文本文件中。

显示订单:该模块只用于显示文本信息,将指定位置的文本中的信息依次输出。注意,每次只有先增加订单,才能显示订单。

 

然后展示一下原项目代码:

#include <string>
#include <iostream>
#include<fstream>
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;

private:

};
class member :public buyer {
private:
int leaguer_grade;
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);
};
class honoured_guest :public buyer
{
double discount_rate;
public:
honoured_guest();

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);
private:

};

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 << "\n";
cout << "地址:" << address << "\n";
}
void member::setpay(double p)
{
if (leaguer_grade == 1)
{
pay = .95 * p + pay;
}
else if (leaguer_grade == 2)
{
pay = .95 * 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 << "购书人姓名:" << name << "\t";
cout << "购书人编号:" << buyerID << "\t";
cout << "购书人为贵宾!折扣率为:" << discount_rate * 100 << "%\n";
cout << "地址:" << address << "\n\n";

}
void honoured_guest::setpay(double p)
{
pay = pay + (1 - discount_rate) * p;
}
void layfolk::display()
{
cout << "购书人姓名:" << name << "\t";
cout << "购书人编号:" << buyerID << "\t";
cout << "购书人为普通人" << "\n";
cout << "地址:" << address << "\n\n";

}
void layfolk::setpay(double p)
{
pay = pay + p;
}
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();
private:

};
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 << "\n";
cout << "出版社:" << publishing << "\t";
cout << "定价:" << price << "\n";
}
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;
}
//订单类
class order
{
protected:
static int ordercount;
int orderID;
int buyerID;
int listcount;
string orderlist[2];
public:
order();
order(int bID, int lt, string olt[2]);
int getorderID();
int getbuyerID();
int getlistcount();
string* getorderlist();
private:

};
//订单静态变量,统计订单数
int order::ordercount = 0;
order::order()
{
orderID = 0;
buyerID = 0;
listcount = 0;
for (int i = 0; i < 2; i++)
{
orderlist[i] = "";
}
}

order::order(int bID, int lt, string olt[2])
{
orderID = ordercount;
buyerID = bID;
listcount = lt;
for (int i = 0; i < 2; i++)
{
orderlist[i] = olt[i];
}
ordercount++;

}
int order::getorderID() {
return orderID;
}
int order::getbuyerID() {
return buyerID;
}
int order::getlistcount() {
return listcount;
}
string* order::getorderlist() {
return orderlist;
}
int main()
{
while (1)
{
int i = 0, buyerid, flag;
book* c[2];
layfolk b1("林晓查", 1, "北京", 0);
honoured_guest b2("王瑶瑶", 2, .6, "上海", 0);
member b3("赵红艳", 3, 5, "广州", 0);
buyer* b[3] = { &b1,&b2,&b3 };

book c1("7-302-04504-6", "C++程序设计", "谭浩强", "清华", 25);
book c2("7-402-03388-9", "数据结构", "许倬群", "北大", 20);
c[0] = &c1;
c[1] = &c2;
int Interface;
cout << " ***书籍订单管理系统*** " << endl;
cout << "*******************************************************************************" << endl;
cout << "* 请选择操作: *" << endl;
cout << "* 1.查看图书信息 *" << endl;
cout << "* 2.查看购书人信息 *" << endl;
cout << "* 3.添加订单 *" << endl;
cout << "* 4.查看订单 *" << endl;
cout << "*******************************************************************************" << endl;
cout << "请输入操作项:";
cin >> Interface;
switch (Interface)
{
case 1:
{//显示图书信息
cout << "\n图书信息:\n\n";
for (i = 0; i < 2; i++)
{
c[i]->display();
}
cout << "*******************************************************************************" << endl;
cout << "返回(输入1):" << endl;
int op;
cin >> op;
if (op == 1)
{
system("cls");
break;
}
}
case 2:
{//显示购书人信息
cout << "购书人信息:\n\n";
for (i = 0; i < 3; i++)
{
b[i]->display();
}
cout << "*******************************************************************************" << endl;
cout << "返回(输入1):" << endl;
int op2;
cin >> op2;
if (op2 == 1)
{
system("cls");
break;
}
}
case 3:
{
cout << "\n\n请输入购书人编号:";
cin >> buyerid;
flag = 0;//用来判断输入编号是否有效的标志
for (int i = 0; i < 3; i++) {
if (buyerid == b[i]->getid())
{
flag = 1;//说明输入的编号有效
}
}
if (flag)
{
for (i = 0; i < 3; i++)
{
if (b[i]->getid() == buyerid) {
cout << "请输入购书数量:" << endl;
int s = 1;
cin >> s;
string ol[2];
ol[0] = c[0]->getbook_name();
ol[1] = c[1]->getbook_name();
order od(i + 1, s, ol);//创建订单
b[i]->setpay(c[0]->getprice());
b[i]->setpay(c[1]->getprice());
cout << endl << "购书人需要付费:" << b[i]->getpay() * s << "\n\n";
//将订单信息写入文件
ofstream out("D:\\订单.txt", ios::app);//app表示每次操作前均定位到文件末尾
if (out.fail()) {
cout << "文件打开失败!\n";
}
out << od.getorderID() << "\n" << od.getbuyerID() << "\n" << od.getlistcount() << "\n" << od.getorderlist()[0] << "\n" << od.getorderlist()[1] << endl;
out.close();
}
}
}
else
{
cout << "编号不存在" << endl;
}
cout << "*******************************************************************************" << endl;
cout << "返回(输入1):" << endl;
int op3;
cin >> op3;
if (op3 == 1)
{
system("cls");
break;
}
}
case 4:
{
//读取文件
ifstream ifs;
ifs.open("D:\\订单.txt", ios::in);
if (!ifs.is_open()) {
cout << "文件打开失败!" << endl;

}
string buf;
int j = 1;
while (getline(ifs, buf)) {
switch (j)
{
case 1:
{ cout << "订单号:" << buf << "\t";
j = 2;
break;
}
case 2:
{ cout << "购书人ID:" << buf << "\t";
j = 3;
break;
}
case 3:
{ cout << "订单数量:" << buf << "\t";
j = 4;
break;
}
case 4:
{ cout << "书名:" << buf << "\t";
j = 5;
break;
}
case 5:
{ cout << "书名:" << buf << endl;
j = 1;
break;
}
}
}
ifs.close();
cout << "*******************************************************************************" << endl;
cout << "返回(输入1):" << endl;
int op4;
cin >> op4;
if (op4 == 1)
{
system("cls");
break;
}
}
default:
break;
}
}
return 0;
}

优化后的代码:

#include <string>
#include <iostream>
#include<fstream>
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;

private:

};
class member :public buyer {
private:
int leaguer_grade;
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);
};
class honoured_guest :public buyer
{
double discount_rate;
public:
honoured_guest();

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);
private:

};

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 << "\n";
cout << "地址:" << address << "\n";
}
void member::setpay(double p)
{
if (leaguer_grade == 1)
{
pay = .95 * p + pay;
}
else if (leaguer_grade == 2)
{
pay = .95 * 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 << "购书人姓名:" << name << "\t";
cout << "购书人编号:" << buyerID << "\t";
cout << "购书人为贵宾!折扣率为:" << discount_rate * 100 << "%\n";
cout << "地址:" << address << "\n\n";

}
void honoured_guest::setpay(double p)
{
pay = pay + (1 - discount_rate) * p;
}
void layfolk::display()
{
cout << "购书人姓名:" << name << "\t";
cout << "购书人编号:" << buyerID << "\t";
cout << "购书人为普通人" << "\n";
cout << "地址:" << address << "\n\n";

}
void layfolk::setpay(double p)
{
pay = pay + p;
}
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();
private:

};
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 << "\n";
cout << "出版社:" << publishing << "\t";
cout << "定价:" << price << "\n";
}
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;
}
//订单类
class order
{
protected:
static int ordercount;
int orderID;
int buyerID;
int listcount;
string orderlist[2];
public:
order();
order(int bID, int lt, string olt[2]);
int getorderID();
int getbuyerID();
int getlistcount();
string* getorderlist();
private:

};
//订单静态变量,统计订单数
int order::ordercount = 0;
order::order()
{
orderID = 0;
buyerID = 0;
listcount = 0;
for (int i = 0; i < 2; i++)
{
orderlist[i] = "";
}
}

order::order(int bID, int lt, string olt[2])
{
orderID = ordercount;
buyerID = bID;
listcount = lt;
for (int i = 0; i < 2; i++)
{
orderlist[i] = olt[i];
}
ordercount++;

}
int order::getorderID() {
return orderID;
}
int order::getbuyerID() {
return buyerID;
}
int order::getlistcount() {
return listcount;
}
string* order::getorderlist() {
return orderlist;
}
int main()
{
while (1)
{
int i = 0, buyerid, flag;
book* c[2];
layfolk b1("林晓查", 1, "北京", 0);
honoured_guest b2("王瑶瑶", 2, .6, "上海", 0);
member b3("赵红艳", 3, 5, "广州", 0);
buyer* b[3] = { &b1,&b2,&b3 };

book c1("7-302-04504-6", "C++程序设计", "谭浩强", "清华", 25);
book c2("7-402-03388-9", "数据结构", "许倬群", "北大", 20);
c[0] = &c1;
c[1] = &c2;
int Interface;
system("cls");
cout << " ***书籍订单管理系统*** " << endl;
cout << "*******************************************************************************" << endl;
cout << "* 请选择操作: *" << endl;
cout << "* 1.查看图书信息 *" << endl;
cout << "* 2.查看购书人信息 *" << endl;
cout << "* 3.添加订单 *" << endl;
cout << "* 4.查看订单 *" << endl;
cout << "* 5.退出系统 *" << endl;
cout << "*******************************************************************************" << endl;
cout << "请输入操作项:";
cin >> Interface;
switch (Interface)
{
case 1:
{//显示图书信息
cout << "\n图书信息:\n\n";
for (i = 0; i < 2; i++)
{
c[i]->display();
}
cout << "*******************************************************************************" << endl;
system("pause");
break;
}
case 2:
{//显示购书人信息
cout << "购书人信息:\n\n";
for (i = 0; i < 3; i++)
{
b[i]->display();
}
cout << "*******************************************************************************" << endl;
system("pause");
break;
}
case 3:
{
cout << "\n图书信息:\n\n";
for (i = 0; i < 2; i++)
{
c[i]->display();
}
cout << "*******************************************************************************" << endl;
cout << "\n\n请输入购书人编号:";
cin >> buyerid;
flag = 0;//用来判断输入编号是否有效的标志
for (int i = 0; i < 3; i++) {
if (buyerid == b[i]->getid())
{
flag = 1;//说明输入的编号有效
}
}
if (flag)
{
for (i = 0; i < 3; i++)
{
if (b[i]->getid() == buyerid) {
cout << "请输入购书数量:" << endl;
int s = 1;
cin >> s;
string ol[2];
ol[0] = c[0]->getbook_name();
ol[1] = c[1]->getbook_name();
order od(i + 1, s, ol);//创建订单
b[i]->setpay(c[0]->getprice());
b[i]->setpay(c[1]->getprice());
cout << endl << "购书人需要付费:" << b[i]->getpay() * s << "\n\n";
//将订单信息写入文件
ofstream out("D:\\订单.txt", ios::app);//app表示每次操作前均定位到文件末尾
if (out.fail()) {
cout << "文件打开失败!\n";
}
out << od.getorderID() << "\n" << od.getbuyerID() << "\n" << od.getlistcount() << "\n" << od.getorderlist()[0] << "\n" << od.getorderlist()[1] << endl;
out.close();
}
}
}
else
{
cout << "编号不存在" << endl;
}
cout << "*******************************************************************************" << endl;
system("pause");
break;
}
case 4:
{
//读取文件
ifstream ifs;
ifs.open("D:\\订单.txt", ios::in);
if (!ifs.is_open()) {
cout << "文件打开失败!" << endl;

}
string buf;
int j = 1;
while (getline(ifs, buf)) {
switch (j)
{
case 1:
{ cout << "订单号:" << buf << "\t";
j = 2;
break;
}
case 2:
{ cout << "购书人ID:" << buf << "\t";
j = 3;
break;
}
case 3:
{ cout << "订单数量:" << buf << "\t";
j = 4;
break;
}
case 4:
{ cout << "书名:" << buf << "\t";
j = 5;
break;
}
case 5:
{ cout << "书名:" << buf << endl;
j = 1;
break;
}
}
}
ifs.close();
cout << "*******************************************************************************" << endl;
system("pause");
break;
}
case 5:
system("cls");
return 0;
break;
}
}
return 0;
}

最后说一下心得体会吧:第一次发表博客,还是挺奇妙挺新颖的。在优化项目过程当中呢也有不少感想,因为这次优化项目的机会,我也发现了自己很多代码知识变得生疏,所以这次也算是给自己复习了,其次在进行优化的时候对项目的思考,以及对程序的是错都费了不少功夫,对项目的创新更是考验了创新能力,最后对这次项目优化还算是满意的。希望自己再接再厉。加油!

标签:string,朋友,double,int,book,buyer,二次开发,已有,cout
来源: https://www.cnblogs.com/Bagzattt/p/15955332.html