C++——商品销售系统测试

商品销售系统测试

用的是VS2020
过年疫情宅家闲着无聊看到书上的一个商品销售系统挺有意思的就把他拖出来重构了一下,优化了一下写入逻辑的部分。参考书籍是《C++开发实战》

设计思路是一个主程序main,一个trade类负责交互,一个commodity类负责储存数据和容器填装。trade跟commodity类高度耦合,当初设计上的缺陷吧,所以调用trade类的时候会默认构造一个新的commodity类作为友元。

直接看程序吧
首先是commodity类的头文件

#include<iostream>
#include<vector>
#ifndef commodity_H
#define commodity_H
using namespace std;
class trade;
class commodity{
private:
	char name[32];//商品名称
	char buyValue[32];//进货的价格
	char sellValue[32];//卖出的价格
	char stock[32];//库存量
public:
	//从commodity中提取数据时暂存
	commodity(char* name, char* buyvalue,char* sellvalue, char* stock ) {
		strcpy_s(this->buyValue, buyvalue);
		strcpy_s(this->sellValue, sellvalue);
		strcpy_s(this->name, name);
		strcpy_s(this->stock, stock);
	}

	commodity() { /*cout << "commodity类被调用" << endl; */}
	~commodity() { /*cout << "commodity类被释放" << endl;*/ }
public:
	
	void newcommodity(char* name, char* buyvalue, char* sellvalue);//新建数据时候调用
	bool storage();//将信息保存为文本
	char* getStock();//获取库存
	char* getBuyValue();//获取进货价
	char* getsellValue();//获取卖出价
	char* getName();//获取商品名称
	int UpdateStock(int n); //改变库存量
	template<class T>int change(T temp);//将数据转换为int类型
	friend class trade;//友元trade类

	vector<commodity> data;//用容器存储提取出来的商品信息
	int init(); //从本地文件提取信息到容器
	int number();//获取容器大小
};
#endif 

再然后是commodity类的cpp

#include "commodity.h"
#include<iostream>
#include<fstream>


char* commodity::getStock(){
	return this->stock;
}
char* commodity::getBuyValue() {
	return this->buyValue;
}
char* commodity::getsellValue() {
	return this->sellValue;
}
char* commodity::getName() {
	return this->name;
}

template<class T>
int commodity::change(T temp) {
	int old;
	old = atoi(temp);
	return old;
}

bool commodity::storage() {
	fstream file;
	file.open("data.txt",  ios::app);
	try{
		file << this->name << '\t';
		file << this->buyValue << '\t';
		file << this->sellValue << '\t';
		file << this->stock << '\n';
		file.close();
	}
	catch (...){
		throw"数据写入失败";
		file.close();
	}
	return true;
}


void commodity::newcommodity(char* name, char* buyvalue, char* sellvalue) {
	strcpy_s(this->buyValue, buyvalue);
	strcpy_s(this->sellValue, sellvalue);
	strcpy_s(this->name, name);
	strcpy_s(this->stock, "0");
}

int commodity::init() {
	int total = 0;
	fstream file;
	file.open("data.txt", ios::in);
	file.seekp(0, ios::beg);
	if(!file.fail()){
		while (!file.eof()) {
			file >> this->name;
			file >> this->buyValue;
			file >> this->sellValue;
			file >> this->stock;
			total++;
			data.push_back(commodity(this->name,this->buyValue,this->sellValue,this->stock));//数据载入容器
		}
		file.close();
	}
	else{
		cout << "容器载入未知错误" << endl;;
		file.close();
	}
	return total;
}

int commodity::number() {
	int temp = 0;
	temp = data.size()-1;//-1忽略文本结束空格
	return temp;
}

int commodity::UpdateStock(int n) {
	int temp = 0;
	temp=change<char*>(this->stock);
	temp = temp + n;
	return temp;
}

然后是trade类的头文件

#include<vector>
#include<list>
#include "commodity.h"
#include<Windows.h>
using namespace std;
class commodity;
class trade {
	struct record //商品交易记录结构体
	{
		char name[30]; //商品名称
		int volume; //交易数量
		char sTime[70]; //交易时间

		record(char* name, int total, char* time)
		{
			strcpy_s(this->name, name);
			this->volume = total;
			strcpy_s(sTime, time);
		}
	};

public:
	int buysell(int t,int number);//添加或减少商品库存量
	void time(char *thistime);//获取当前时间信息
	bool container(int number);//获取指定容器
	bool catalog();//获取数据目录

public: 
	commodity* cty;
	trade() { 
		cty = new commodity(); 
		//cout << "trade类被调用" << endl;
	}
	~trade() { 
		delete  cty;
		//cout << "trade类被释放" << endl;
	}
	 bool tardestorage(char* name, int total, char* sTime);//存储销售信息
	 bool displaybuy();//打印买入数据
	 bool displaysell();//打印卖出数据
};

然后是trade的cpp

#include<vector>
#include<iostream>
#include"trade.h"
#include<fstream>
#include<cstring>
#include <iomanip>
#include <windows.h>
#include <cstring>

using namespace std;
bool trade::container(int number) {
	cout << "名称" << setw(3) << "进货价" << setw(3) << "出售价" << setw(3) << "库存量" << endl;
	cout << cty->data[number - 1].getName()<< "   ";
	cout << cty->data[number - 1].getBuyValue() << "   ";
	cout << cty->data[number - 1].getsellValue() << "   ";
	cout << cty->data[number - 1].getStock() << "   ";
	cout << endl;
	return true;
}

bool trade::catalog() {
	int cemp = 1;//计数器
	vector<commodity>::iterator itr = cty->data.begin();
	for (; itr != cty->data.end(); itr++) {
		if (cemp <= cty->number()) { 
			cout << setw(4) << cemp << "    ";
			cemp++;
		}
		cout << itr->getName() << endl;
	}
	return true;
}

int trade::buysell(int number, int plus) {
	int temp = 0;
	temp=cty->change<char*>(cty->data[number - 1].stock);//取出指定容器内的库存变量转化为int类型运算
	temp = temp + plus;
	memset(cty->data[number - 1].stock, 0, sizeof(cty->stock));//清空原先数据,避免重复写入
	snprintf(cty->data[number - 1].stock, sizeof(cty->stock), "%d", temp);//将更新后的数据重新存入
	remove("data.txt");//清空TXT数据
	vector<commodity>::iterator itr = cty->data.begin();//重新将容器写进TXT
	for (int c=0; itr != cty->data.end(); itr++,c++) {
		strcpy_s(cty->name, cty->data[c].name);
		strcpy_s(cty->buyValue, cty->data[c].buyValue);
		strcpy_s(cty->sellValue, cty->data[c].sellValue);
		strcpy_s(cty->stock, cty->data[c].stock);
		cty->storage();
	}
	char* thistime=new char[64];
	time(thistime);
	tardestorage(cty->data[number - 1].name, plus, thistime);//销售信息存储为文本
	delete[]thistime;
	return 0;
}

void trade::time(char *thistime) {
	char time[64];//临时变量储存时间
	SYSTEMTIME sys;
	GetLocalTime(&sys);
	sprintf_s(time, "%4d/%02d/%02d %02d:%02d:%02d 星期%1d\n", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wDayOfWeek);
	memcpy_s(thistime,64, time, sizeof(time));//将时间拷贝给指针
}

bool trade::tardestorage(char* name, int total, char* sTime) {
	fstream file;
	if (total > 0) {

		file.open("buydata.txt", ios::app);
		//file.seekg(0, ios::beg);
		if (!file.fail()) {
			file << name << '\t';
			file << "  ";
			file << total << '\t';
			file << sTime;
			file.close();
		}

		else {
			throw"销售记录创建失败";
			file.close();
		}
	}

	if (total < 0) {
		file.open("selldata.txt", ios::app);
		//file.seekg(0, ios::beg);
		if (!file.fail()) {
				file << name << '\t';
				file << "  ";
				file << total << '\t';
				file << sTime;
				file.close();
		}
	else {
		throw"销售记录创建失败";
		file.close();
	}
			return true;
	}
}

bool trade::displaybuy() {
	int temp = 1;
	char display[64];
	fstream file;
	cout << "名称" << setw(3) << "进货价" << setw(3) << "出售价" << setw(3) << "库存量" << endl;
	file.open("buydata.txt", ios::in);
	if(!file.fail()){
		file.seekp(0, ios::beg);
		while (!file.eof()){
			file.getline(display,';');
			cout << temp << "    ";
			cout << display << endl;
			++temp;
			*display = NULL;
		}
		file.close();
	}
	else{
		throw"文件打开失败";
	}
}

bool trade::displaysell() {
	int temp = 1;
	char display[64];
	fstream file;
	cout << "名称" << setw(3) << "进货价" << setw(3) << "出售价" << setw(3) << "库存量" << endl;
	file.open("selldata.txt", ios::in);
	if (!file.fail()) {
		file.seekp(0, ios::beg);
		while (!file.eof()) {
			file.getline(display, ';');
			cout << temp << "    ";
			cout << display << endl;
			temp++;
		}
		file.close();
	}
	else {
		throw"文件打开失败";
	}
}

最后是主程序

#include<vector>
#include<iostream>
#include"trade.h"
#include<fstream>
#include "commodity.h"
#include<cstring>
using namespace std;

int inputnumber();//数字接收
char *inputwords();//文字接收
void displaymenu();//开始菜单栏
void wait();

int inputnumber() {
	char buf[32];
	gets_s(buf, 32);
	while (atoi(buf) == 0) {
		cout << "请输入数字" << endl;
		gets_s(buf, 32);
	}
	return atoi(buf);
}

char *inputwords() {
	char* buf=new char[32];
	gets_s(buf, 32);
	return buf;

}

void displaymenu() {
	printf("\t╔═══════════════════╗ \n");
	printf("\t║ 销售系统测试文件  ║ \n");
	printf("\t╠═══════════════════╣ \n");
	printf("\t║ 1 -   购进商品    ║ \n");
	printf("\t║ 2 -   卖出商品    ║ \n");
	printf("\t║ 3 -   添加新品    ║ \n");
	printf("\t║ 4 -   查看商品信息║ \n");
	printf("\t║ 5 -   查看采购记录║ \n");
	printf("\t║ 6 -   查看销售记录║ \n");
	printf("\t║ 7 -   退出        ║ \n");
	printf("\t╚═══════════════════╝ \n");
}

void wait() {
	char buf;
	cout << "输入任意键继续" << endl;
	cin >> buf;
}

int main(void) {
	while (1) {
		system("cls");
		displaymenu();
		trade s_trade;//初始化

		switch(inputnumber()) {
		case 1: {
			system("cls");
			if (s_trade.cty->init() == 0) { break; }
			s_trade.catalog();
			int number, plus;
			cout << "输入要购进商品的编号" << endl;
			number = inputnumber();
			if (number > s_trade.cty->number()) {
				cout << "超出限制范围" << endl;
				number = inputnumber();
			}
			s_trade.container(number);
			cout << "输入要购进的数量" << endl;
			plus = inputnumber();
			s_trade.buysell(number, plus);
			wait();
			break;
		}
		case 2: {
			system("cls");
			if (s_trade.cty->init() == 0) { break; }
			s_trade.catalog();
			int number, plus;
			cout << "输入要卖出商品的编号" << endl;
			number = inputnumber();
			if (number > s_trade.cty->number()) {
				cout << "超出限制范围" << endl;
				number = inputnumber();
			}
			s_trade.container(number);
			cout << "输入要卖出的数量" << endl;
			plus = -inputnumber();
			s_trade.buysell(number, plus);
			wait(); 
			break;
		}
		case 3: {
			system("cls");
			char name[32], buyvalue[32], sellvalue[32];
			cout << "请输入商品名称" << endl;
			strcpy_s(name,inputwords());
			cout << "请输入购入价格" << endl;
			strcpy_s(buyvalue, inputwords());
			cout << "请输入卖出价格" << endl;
			strcpy_s(sellvalue, inputwords());
			s_trade.cty->newcommodity(name, buyvalue, sellvalue);
			s_trade.cty->storage();
			wait(); 
			break;
		}
		case 4: {
			system("cls");
			if (s_trade.cty->init() == 0) { break; }
			s_trade.catalog();
			s_trade.container(inputnumber());
			wait();
			break;
		}
		case 5: {
			system("cls");
			s_trade.displaybuy();
			wait();
			break;
		}
		case 6: {
			system("cls");
			s_trade.displaysell();
			wait();
			break;
		}
		case 7:
			return 0;
			break;

		}
	}
}

基本上设计思路就是通过commodity类接受数据存为txt文档,然后进行各种添加减少操作,销售记录是利用了时间函数进行记录。原理很简单。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值