首页 > 其他分享 >对某网上图书购买系统的优化

对某网上图书购买系统的优化

时间:2023-03-05 18:12:36浏览次数:30  
标签:cout void next book squence 网上 优化 class 图书

前言

在一个朋友那找到了一个网上图书购买系统,在经过一番阅读理解后发现了一些不足之处,现对其进行改良优化。


对原代码的理解

主要功能有:

  • 查询客户信息(管理员)
  • 查询图书信息
  • 查询订单信息(管理员)
  • 购书
  • 查询个人订单信息

在经过调试后发现基础功能相对比较完善,但是作为一个管理员只能在文件里修改库存的图书信息而无法通过程序来修改库存信息,这显然不符合一个完整的购书系统的要求。
所以我准备在程序中额外加入可以修改图书库存信息的功能,这样管理员就不用再找到后台的数据文件去修改图书库存信息了。

点击查看源代码

```cpp
#include 
#include
#include
#include
#include
#include
using namespace std;

void print_Begin() { //打印开头
system("color 0B");
cout << "请输入操作选项【0-5】:";
}
void cls() { //清屏
system("cls");
}
void loading() { //等待
printf("\n\n\t\tLoading......\n\n\n");
printf(" ");
for (int i = 1; i <= 40; ++i) {
printf(".");
Sleep(50); //延迟输出
}
printf(" ");
}
void error() {
cout << "\n\n输入错误!x_x\n\n请输入正确选项!\n\n";
loading();
}

class squence {
public:
string a;
string No1;

string b;
string No2;

string c;
int number;

string d;
double squence_pay;

public:
void show_squence();
class squence* first;
class squence* next;
friend void read_wb(class squence* h);
friend void squence_book(class squence* A);
};

class book {
protected:
string book_ID;
string book_name;
string author;
string publishing;
double price;
public:

void display();
class book* first;
class book* next;
friend void read_wb(class book* h);
friend void buy_book(class book* B, class customer* U);

};

class customer {

protected:
string bianhao; //购书人编号
string name; //姓名
string address; //购书人地址
string identity; //购书人身份
double pay;
public:
void show();
class customer* first;
class customer* next;
friend void read_wb(class customer* h);
friend void buy_book(class book* B, class customer* U);
void setpay();
};

class member :public customer {
private:
int leaguer_grade;
public:
void display();
void setpay(double p);
};

class vip :public customer {
double discount_rate;
public:

void display();
void setpay(double p);

};

class normol :public customer {
public:

void display();
void setpay(double p);

};

void squence::show_squence()
{
cout << "" << a << '\t';
cout << "" << No1 << '\t';

cout << "" << b << '\t';
cout << "" << No2 << '\t';

cout << "" << c << '\t';
cout << "" << number << '\t';

cout << "" << d << '\t';
cout << "" << squence_pay << endl;

}

void read_wb(class squence* h)
{
squence* x = h;

ifstream f("squences.txt", ios::in);
if (!f)
{
	cout << "打开文件夹失败!" << endl;
	exit(0);
}


while (!f.eof())
{
	squence* newsquence = new squence;

	f >> newsquence->a;
	if (newsquence->a.length() == 0)
	{
		delete newsquence;
		break;
	}
	f >> newsquence->No1;

	f >> newsquence->b;
	f >> newsquence->No2;

	f >> newsquence->c;
	f >> newsquence->number;

	f >> newsquence->d;
	f >> newsquence->squence_pay;
	newsquence->first = h;
	x->next = newsquence;
	x = x->next;
}
x->next = NULL;

f.close();

}

void book::display()
{
cout << "书号:" << book_ID << '\t';
cout << "书名:" << book_name << "\n";
cout << "作者:" << author << "\n";
cout << "出版社:" << publishing << "\n";
cout << "定价:" << price << "\n";
}

void read_wb(class book* h)
{
book* x = h;

ifstream f("book.txt", ios::in);
if (!f)
{
	cout << "打开文件夹失败!" << endl;
	exit(0);
}


while (!f.eof())
{
	book* newbook = new book;

	f >> newbook->book_ID;
	if (newbook->book_ID.length() == 0)
	{
		delete newbook;
		break;
	}
	f >> newbook->book_name;
	f >> newbook->author;
	f >> newbook->publishing;
	f >> newbook->price;
	newbook->first = h;
	x->next = newbook;
	x = x->next;
}
x->next = NULL;

f.close();

}

void customer::show()
{

cout << "编号: " << bianhao << '\t';
cout << "姓名: " << name << '\t';
cout << "地址: " << address << '\t';
cout << "身份: " << identity << endl;

}

void read_wb(class customer* h)
{
customer* x = h;

ifstream f("customers.txt", ios::in);
if (!f)
{
	cout << "打开文件夹失败!" << endl;
	exit(0);
}


while (!f.eof())
{
	customer* newcustomer = new customer;

	f >> newcustomer->bianhao;
	f >> newcustomer->name;
	f >> newcustomer->address;
	f >> newcustomer->identity;
	newcustomer->first = h;
	x->next = newcustomer;
	x = x->next;
}
x->next = NULL;
f.close();

}

void buy_book(class book* B, class customer* U) //根据会员等级,优惠不同(购书)
{
string b;
int c;
double d;

book* x = B;
customer* y = U;
string card_id;
string book_id;
cout << "请输入你的卡号:";
cin >> card_id;
string a = card_id;//建立临时变量保存卡号,方便输入文件中。 
cout << endl;
while (y->next != NULL)
{
	if (y->next->bianhao == card_id)
	{
		y->next->show();
		cout << endl << "***************欢迎光临***************!" << endl;
		break;
	}
	y = y->next;
}
if (y->next != NULL)
{
	int number;
	cout << endl << "请输入图书编号:";
	cout << endl << "(tip:编号为001~010)";
	cin >> book_id;
	cout << endl << "请输入您要购买的数量:";
	cin >> number;
	cls();
	b = book_id;//
	c = number;//建立临时变量,储存编号和数量。 
	cout << endl;
	while (x->next != NULL)
	{
		if (x->next->book_ID == book_id)
		{
			x->next->display();
			cout << endl;
			if (y->next->identity == "普通顾客")
			{
				cout << "您需要付款:" << x->next->price * number << "元" << endl;
				cout << "您是普通顾客哦,不享有优惠价格,快来办张卡吧!^_^";
				cout << endl;
				d = x->next->price * number;
				break;
			}
			else if (y->next->identity == "vip1")
			{
				cout << "您需要付款:" << x->next->price * 0.95 * number << "元" << endl;
				cout << "您是vip_1哦,享有九五折,升级vip可享有更高优惠!欢迎下次光临^_^";
				cout << endl;
				d = x->next->price * 0.95 * number;
				break;
			}
			else if (y->next->identity == "vip2")
			{
				cout << "您需要付款:" << x->next->price * 0.9 * number << "元" << endl;
				cout << "您是vip_2哦,享有九折,升级vip可享有更高优惠!欢迎下次光临^_^";
				cout << endl;
				d = x->next->price * 0.9 * number;
				break;
			}
			else if (y->next->identity == "vip3")
			{
				cout << "您需要付款:" << x->next->price * 0.85 * number << "元" << endl;
				cout << "您是vip_3哦,享有八五折,升级vip可享有更高优惠!欢迎下次光临^_^";
				cout << endl;
				d = x->next->price * 0.85 * number;
				break;
			}
			else if (y->next->identity == "vip4")
			{
				cout << "您需要付款:" << x->next->price * 0.8 * number << "元" << endl;
				cout << "您是vip_4哦,享有八折,升级vip可享有更高优惠!欢迎下次光临^_^";
				cout << endl;
				d = x->next->price * 0.8 * number;
				break;
			}
			else if (y->next->identity == "vip5")
			{
				cout << "您需要付款:" << x->next->price * 0.7 * number << "元" << endl;
				cout << "您是vip_1哦,享有七折,升级vip可享有更高优惠!欢迎下次光临^_^";
				cout << endl;
				d = x->next->price * 0.7 * number;
				break;
			}
			else
			{
				cout << "您需要付款:" << x->next->price * 0.5 * number << "元" << endl;
				cout << "您是尊贵用户!,享有五折优惠!欢迎下次光临^_^";
				cout << endl;
				d = x->next->price * 0.5 * number;
				break;
			}
			cout << "请及时支付!" << endl;
		}
		x = x->next;
	}

	if (x->next == NULL)
	{
		cout << "该书号不存在!" << endl;
	}
}
else
{
	cout << "该卡号不存在!" << endl;
}

ofstream fp;
fp.open("squences.wb", ios::app);

if (!fp.is_open())
{
	cout << "打开文件失败!!!" << endl;
}

fp << "购书人编号   " << a << '\t' << "图书编号   " << b << '\t' << "购买数量   " << c << '\t' << "订单金额   " << d << endl;

}

void squence_book(class squence* A)
{
squence* x = A;
string squence_id;
cout << "请输入您的卡号:";
cin >> squence_id;
string linshi = squence_id;
cout << endl;
cout << "您的订单信息为:" << endl;
while (x->next != NULL)
{
if (x->next->No1 == squence_id)
{
x->next->show_squence();
}
x = x->next;
}

}
void opt(customer* n, book* m, squence* t, book* gx, customer* sh, squence* wh) { //功能区
while (1)
{
cls(); //清屏
cout << "\t\t\t" << endl;
cout << "\t\t\t
1.客户信息 " << endl;
cout << "\t\t\t
2.图书信息 " << endl;
cout << "\t\t\t
3.订单信息 " << endl;
cout << "\t\t\t
4.购书 " << endl;
cout << "\t\t\t
5.查询个人订单信息 " << endl;
cout << "\t\t\t
0.退出程序 " << endl;
cout << "\t\t\t
\n\n\n" << endl;
int o;
print_Begin();
cin >> o;
cout << endl;
switch (o)
{
case 1:
cls();
n = sh;
cout << "请输入管理员密码(提示:20010827):" << endl;
int mima;
cin >> mima;
if (mima == 20010827)
{
cls();
cout << "客户信息:\n\n";
while (n->next != NULL)
{
n->next->show();
n = n->next;
cout << endl;

			}
			system("pause");
			break;
		}
		else
		{
			cls();
			cout << "密码错误,请重试!" << endl;
			system("pause");
			break;

		}



	case 2:
		cls();
		m = gx;
		cout << "图书信息:\n\n";
		while (m->next != NULL)
		{
			m->next->display();
			m = m->next;
			cout << endl;
		}
		system("pause");
		break;

	case 3:
		cls();
		t = wh;
		cout << "请输入管理员密码(为了方便演示,密码为:20010827):" << endl;
		int mima2;
		cin >> mima2;
		if (mima2 == 20010827)
		{

			cout << "订单信息:\n\n";
			while (t->next != NULL)
			{
				t->next->show_squence();
				t = t->next;
				cout << endl;
			}
			system("pause");
			break;
		}
		else {
			cls();
			cout << "密码错误,请重试!" << endl;
			Sleep(50);			//
			system("pause");
			break;
		}

	case 4:
		cls();
		buy_book(gx, sh);

		system("pause");
		break;

	case 5:
		cls();
		squence_book(wh);
		system("pause");
		break;


	defaut:
		cls();
		error();
		loading();
		system("pause");
		break;
	}
	if (o == 0)
	{
		cls();
		cout << "请按任意键退出!" << endl;
		break;
	}
}

}
int main() //先检测文件打开是否成功
{
book* gx = new book;
book* m = gx;
read_wb(gx);

customer* sh = new customer;
customer* n = sh;
read_wb(sh);

squence* wh = new squence;
squence* t = wh;
read_wb(wh);

print_Begin();		//改颜色 
loading();
opt(n, m, t, gx, sh, wh);
return 0;

}

</code></pre>
</details>

标签:cout,void,next,book,squence,网上,优化,class,图书
From: https://www.cnblogs.com/waibiwaibii/p/17181175.html

相关文章