C++实践项目:简易收银台程序

C++实践项目:简易收银台程序

  1. 项目开发背景
  2. 收银台简介
  3. 主要功能
  4. 数据库设计和封装
  5. 界面功能实现
  6. 可扩展

1.项目开发背景

在学校参与勤工助学岗位期间接触过收银员工作,由于当时学习了C语言和数据结构觉得可以简单的实现一个收银台程序。其次由于传统收银台缺陷

  • 收款结算速度慢,容易出现营业差错,
  • 不宜进行商品调价,盘点效率底
  • 用户体验不好
  • 收银台的优点:快捷方便,节省大量人力成本,不容易出错,能够快速反馈出商品的详细信息。
    因此:开发这个系统可以方便快捷地查出顾客结帐情况,商品信息情况,每天的售货情况,方便了对超市商 品管理、人员管理,大大提高了超市的售货速度。进而加速了社会的发展速度,提高了人民的生活水平。

2.收银台简介

收银台作为商场超市所必不可少的配套设施,越来越多的被客户所关注。收银台俗称付款处,是顾客付款交 易的地方,也是顾客在商店最后停留的地方。
收银台除了收银这一主要用途外,将在吸引顾客视线的同时发挥出特殊功效。事实上,收银作业不只是单纯 地为顾客提供结账服务而已,收银员收款工作完成后也并不代表卖场的销售行为就此结束,这其中还包括了 对顾客的礼仪态度。

3.主要功能

C++实践项目:简易收银台程序_第1张图片

  1. 登录模块

C++实践项目:简易收银台程序_第2张图片

  1. 后台管理员和前台售货员需要根据自己的用户名以及密码进行登录。
    用户输入名户名以及密码后,根据不同身份,显示不同界面,用户进行其相应操作。

  2. 管理员界面: 后台管理员 员工操作 查询员工基本信息 添加新员工 员工离职后,删除员工信息
    员工信息变更时,更新员工信息,比如:更新员工薪资 商品操作 按照条件查询商品的信息 商品入库 过期商品的删除 商品信息更新,比如:价格发生变动 按照日期查询商品销售情况

C++实践项目:简易收银台程序_第3张图片
4. 售货员界面:
C++实践项目:简易收银台程序_第4张图片
6. 售货员模块 售货 录入商品信息 出售:会员出售和普通用户出售 如果客户不满意,退货

4.数据库设计

设计

1.所有表格
C++实践项目:简易收银台程序_第5张图片
2.员工表

C++实践项目:简易收银台程序_第6张图片

3.商品表
C++实践项目:简易收银台程序_第7张图片

4.销售记录
C++实践项目:简易收银台程序_第8张图片

封装MySQL

1.配置Windows下mysql数据库环境
2.MySQL.h

#pragma once
#include
#include
#include
#include
#include
using namespace std;

class MySQL
{
public:
	MySQL();
	bool ConnectMySQL(const char* host, const char* user, const char* passward, const char* dbName);
	~MySQL();
	
	bool Insert(const string& strSQL);
	bool UpDate(const string& strSQL);
	bool Delete(const string& strSQL);
	vector> Select(const string& strSQL);
private:
	MYSQL * _mySQL;

};

3.mysql.cpp

#include"MySQL.h"
#include
#include
using namespace std;
MySQL::MySQL()
{
	_mySQL = mysql_init(nullptr);
}

bool MySQL::ConnectMySQL(const char* host, const char* user, const char* passward, const char* dbName)
{
	if (!mysql_real_connect(_mySQL, host, user, passward, dbName, 3306, nullptr, 0))
	{

		cout << "数据库连接失败" << endl;
		return false;
	}
	mysql_query(_mySQL,"set names 'gbk'");
	return true;
}

vector> MySQL::Select(const string& strSQL)
{
	vector> vvRet;
	if (mysql_query(_mySQL, strSQL.c_str()))
	{
		//SQL命令响应失败
		cout << mysql_error (_mySQL)<< endl;
		return vvRet;
	}

	//获取查询的记录集
	MYSQL_RES* mySQLRes = mysql_store_result(_mySQL);
	if (nullptr == mySQLRes)
	{
		cout << mysql_error(_mySQL) << endl;
		return vvRet;
	}
	//获取记录中有多少个字段
	int itemCount = mysql_num_fields(mySQLRes);
	MYSQL_ROW mysqlROW;

	while (mysqlROW = mysql_fetch_row(mySQLRes))
	{
		//已获取到一条记录
		vector vItem;
		for (size_t i = 0; i < itemCount; i++)
		{
			vItem.push_back(mysqlROW[i]);
		}
		vvRet.push_back(vItem);
	}
	mysql_free_result(mySQLRes);

	return vvRet;
}

bool MySQL::Insert(const string& strSQL)
{
	if (mysql_query(_mySQL, strSQL.c_str()))
	{
		//SQL命令响应失败
		cout << mysql_error(_mySQL) << endl;
		return false;
	}
	return true;
}

bool MySQL::UpDate(const string& strSQL)
{
	if (mysql_query(_mySQL, strSQL.c_str()))
	{
		//SQL命令响应失败
		cout << mysql_error(_mySQL) << endl;
		return false;
	}
	return true;
}


MySQL::~MySQL()
{

}

界面功能实现

管理员界面部分功能实现

C++实践项目:简易收银台程序_第9张图片
该界面点击获取

void MainWnd::Notify(TNotifyUI& msg)
{
	CDuiString strNAME = msg.pSender->GetName();
	if (msg.sType == _T("click"))
	{
		if (strNAME == _T("BTN_CLOSE"))
			Close();
		else if (strNAME == _T("BTN_MIN"))
			::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
		else if (strNAME == _T("BTN_SELECT"))
			//MessageBox(NULL, _T("SSS"), _T("Cashier"), IDOK);
			SelectEmployeeInfo();
		else if (strNAME == _T("BTN_INSERT"))
			//MessageBox(NULL, _T("III"), _T("Cashier"), IDOK);
			InsertEmployeeInfo();
		else if (strNAME == _T("BTN_UPDATE"))
			MessageBox(NULL, _T("UUU"), _T("Cashier"), IDOK);
		else if (strNAME == _T("BTN_DELETE"))
			//MessageBox(NULL, _T("DDD"), _T("Cashier"), IDOK);
			DeleteEmployeeInfo();
		else if (strNAME == _T("BTN_SELL_RECORD"))
			MessageBox(NULL, _T("SR"), _T("Cashier"), IDOK);


	}
	else if (msg.sType == _T("selectchanged"))
	{
		CTabLayoutUI* pTab = (CTabLayoutUI*)m_PaintManager.FindControl(_T("tablayout"));
		if (strNAME == _T("OPTION_EMPLOYEE"))
			pTab->SelectItem(0);
		else
			pTab->SelectItem(1);
	}
	
	else if (msg.sType == _T("windowinit")) {
		//窗口在创建期间,将一些空间初始化
		
		CComboBoxUI* pGender = (CComboBoxUI*)m_PaintManager.FindControl(_T("usergender"));
		pGender->SelectItem(0);

		CComboBoxUI* pPosition = (CComboBoxUI*)m_PaintManager.FindControl(_T("position"));
		pGender->SelectItem(0);

		CComboBoxUI* pSelect = (CComboBoxUI*)m_PaintManager.FindControl(_T("COMOB_SELECT"));
		pGender->SelectItem(0);
	
	}
	
}

1.插入员工信息

void MainWnd::InsertEmployeeInfo()
{
	//从界面中获取员工信息
	CDuiString strName = ((CEditUI*)m_PaintManager.FindControl(_T("username")))->GetText();
	CDuiString strgender = ((CComboBoxUI*)m_PaintManager.FindControl(_T("usergender")))->GetText();
	CDuiString strBirthday = ((CEditUI*)m_PaintManager.FindControl(_T("userbirthday")))->GetText();
	CDuiString strPos = ((CComboBoxUI*)m_PaintManager.FindControl(_T("position")))->GetText();
	CDuiString strTel = ((CEditUI*)m_PaintManager.FindControl(_T("telphone")))->GetText();
	CDuiString strSalary = ((CEditUI*)m_PaintManager.FindControl(_T("salary")))->GetText();


	CListUI* pListUI = (CListUI*)m_PaintManager.FindControl(_T("ListDemo1"));
	char szCount[32] = { 0 };
	_itoa(pListUI->GetCount()+1,szCount,10);
	//构造SQL命令g
	string strSQL("insert into employee values(");
	strSQL +=szCount;
	strSQL += ",'";
	strSQL += UnicodeToANSI(strName);
	strSQL += "','";
	strSQL += UnicodeToANSI(strgender);
	strSQL += "','";
	strSQL += UnicodeToANSI(strBirthday);
	strSQL += "','0000','";
	strSQL += UnicodeToANSI(strPos);
	strSQL += "','";
	strSQL += UnicodeToANSI(strTel);
	strSQL += "','";
	strSQL += UnicodeToANSI(strSalary);
	strSQL += "');";

	//响应SQL命令
	p_mysql->Insert(strSQL);
	//将该员工插入到List
	CListTextElementUI* pItem = new CListTextElementUI;
	
	pListUI->Add(pItem);
	pItem->SetText(0, strName);
	pItem->SetText(1, strgender);
	pItem->SetText(2, strBirthday);
	pItem->SetText(3, strPos);
	pItem->SetText(4, strTel);
	pItem->SetText(5, strSalary);
}


2.查询员工信息

void MainWnd::SelectEmployeeInfo()
{
	string strSQL("select* from employee");

	CComboBoxUI* pCombo=(CComboBoxUI*)m_PaintManager.FindControl(_T("ListDemo1"));
	CDuiString strStyle = pCombo->GetText();
	if (strStyle == _T("无"))
			strSQL += ";";
	else if (strStyle==_T("名字"))
	{
		strSQL += "where Name = '";
		CDuiString strName = ((CEditUI*)m_PaintManager.FindControl(_T("")))->GetText();
		if (strName.IsEmpty())
		{
			MessageBox(m_hWnd, _T("请输入查询用户的名字"), _T("Cashier"), IDOK);
			return;
		}
		strSQL += UnicodeToANSI(strName);
		strSQL += "';";
	}
	else if(strStyle == _T("性别"))
		;
	else if (strStyle == _T("Salary"))
		;
	vector> vRet = p_mysql->Select(strSQL);
	if (vRet.empty())
		return;
	CListUI* pListUI= (CListUI*)m_PaintManager.FindControl(_T("ListDemo1"));
	pListUI->RemoveAll();
	for (size_t i=0;i& strItem = vRet[i];
		CListTextElementUI* pData = new CListTextElementUI;
		pData->SetAttribute(_T("align"), _T("center"));

		pListUI->Add(pData);
		//
		pData->SetText(0, ANSIToUnicode(strItem[1]));
		pData->SetText(1, ANSIToUnicode(strItem[2]));
		pData->SetText(2, ANSIToUnicode(strItem[3]));
		pData->SetText(3, ANSIToUnicode(strItem[5]));
		pData->SetText(4, ANSIToUnicode(strItem[6]));
		pData->SetText(5, ANSIToUnicode(strItem[7]));
	}
}

3.删除员工信息

void MainWnd::DeleteEmployeeInfo()
{
	//获取当前选中信息
	CListUI* pListUI = (CListUI*)m_PaintManager.FindControl(_T("ListDemo1"));
	pListUI->RemoveAll();
	int lineNo = pListUI->GetCurSel();
	CListTextElementUI* pRow = (CListTextElementUI*)pListUI->GetItemAt(lineNo);
	//从数据库中将该员工的信息删除
	//构造SQL命令
	string strSQL("delete");

    //CDuiString strName = pRow->GetText(0);

	//从数据库中将该条记录删除
	//p_mysql->Delete(strSQL);

	//从list里删除
	pListUI->RemoveAt(lineNo);

}

售货员界面部分功能实现

1.查询商品剩余

void CashierWnd::SelectGoods()
{
	//1.获取商品名称EDIT_GOODS_NAME	
	CDuiString strGoodsName= ((CEditUI*)m_PaintManager.FindControl(_T("EDIT_GOODS_NAME")))->GetText();

	//2.构造SQL语句,到数据库中查找此商品
	string strSQL("select * from goods where GoodsName='");
	strSQL += UnicodeToANSI(strGoodsName);
	strSQL += "';";
	vector>vRet = m_pMySQL->Select(strSQL);
	if(vRet.empty())
	{
		MessageBox(m_hWnd, _T("暂无此商品!"), _T("Cashier"), IDOK);
		return;
	}
	//3.将商品剩余个数显示到界面编辑框中
	((CEditUI*)m_PaintManager.FindControl(_T("EDIT_GOODS_LEFT")))->SetText(ANSIToUnicode(vRet[0][7]));
	
}

项目中遇到的问题

1.首先环境搭建,如果连接文件有误,或者编译文件出现错误没有生成目标文件
可以已通过报错返回的错误类型如果外部库或者数据库头文件等就是存在环境搭建问题
2.类型转化问题?
很多时候需要将Unicode格式的编码转化为二进制或者将二进制转化为Uicode我们在网上查阅了相关资料编写了相关函数
3.SQL语句编写出错

项目总结

首先给我的收获就是学会了Duillib这套库的使用,可以制作简单的界面,让我学到了很多界面设计的知识其次我们学会了将自己的在一个项目中,自己会的语言并不是简单的进行组合,更要有大局观,要用语言将所有模块统一组织,更加注重模块之间的关系,而不思简单的对于语言的利用整个项目到这里就差不多了,整个项目从开始到结束历时一个周左右,部分功能可能还有待扩展,但是节本框架完善,可以进行基本操作,在这次项目中让我们学到的知识更加能够利用起来,更加明了,纸上得来终觉浅绝知此事要躬行,在今天我们应届大学生将会是今后社会上的中流砥柱,更加是社会未来的主人,不能只学课本上的知识,更加要将书本与实际相结合,做一个理论素质强,同时兼具动手能力的新时代大学生,才能更好面对将来的挑战。

项目源代码参考:https://github.com/AYUEJR/Cashier

你可能感兴趣的:(c++,mysql)