1、模拟超市的收银系统 一、 系统分析与设计。 随着计算机的发展,计算机技术已经融入到社会生活的各个角落,把人们从以前繁琐的手工操作中解放出来,从而使信息的管理大大简便起来。超市日常有大量的数据需要进行处理,包括收银员收银时输入的消费者购买信息、管理员输入的入库商品信息、管理员输入的人员信息、日常销售额的统计等等。面对如此大的信息量,就需要有相应的计算机管理系统来提高工作的效率和系统管理的安全性。通过这样的系统,超市就可以由收银员方便的在收银柜台进行销售额的录入,管理员也可以方便地进行管理,从而减小手工操作的工作量,本设计就是为了模拟超市的收银系统。 从需求分析的角度来看,这个超市管理系
2、统的需求如下所示: 该系统的功能主要是模拟超市的收银的过程,所以不需要设计数据库,当进入系统,就会显示仓库里的所有货物,当管理员输入货物编号(索引号)时,就会提示你需要买的数量,输入完成就会提示你购买成功。再就是付款。 根据系统需求,这个版本的超市管理系统是一个简单的管理系统, 三、主要程序清单(见附录) //************************************************************************ //*main.cpp 系统主文件 //******************************************
3、 #include "counter.h" //创建一个商店 CStore* PrepareStore(){ //创建一个空商店 CStore *pStore = new CStore(); //为商店配货 CGoods *pGoods = new CGoods(1, "西瓜", 4.50); pStore->AddGoods(pGoods, 1000); pGoods = new CGoods(2, "鸡蛋", 5.00); pStore->AddGoods(pGood
4、s, 1000); pGoods = new CGoods(3, "牛肉", 12.00); pStore->AddGoods(pGoods, 1000); pGoods = new CGoods(4, "香蕉", 4.10); pStore->AddGoods(pGoods, 1000); pGoods = new CGoods(5, "空调", 3000.00); pStore->AddGoods(pGoods, 100); pGoods = new CGoods(6, "大米", 1.00); pStore->AddGoods(pGoods
5、 10000); pGoods = new CGoods(7, "葡萄", 5.00); pStore->AddGoods(pGoods, 1000); pGoods = new CGoods(8, "面包", 6.00); pStore->AddGoods(pGoods, 1000); pGoods = new CGoods(9, "火腿", 15.00); pStore->AddGoods(pGoods, 1000); return pStore; } void usage(){ cout<<"\n\n"; cout.width
6、35);
cout.fill('*');
cout< 7、
}
void main()
{
//准备超市储藏室、购物篮、收银台
CStore *pStore = PrepareStore(); //给超市配货
CStore *pBasket = new CStore();
CCounter Counter(pBasket);
double dCash = 0.0;
cout<<"------------------------欢迎光临 \"美特好\" 超市------------------------\n\n"< 8、有以下商品,欢迎您的选购*******************"< 9、 }
//购买完毕,退出
if (iIndex == -1) {
break;
}
//获取iIndex所对应的商品
PSTOREELEMENT pSE = pStore->GetGoods( iIndex );
//没有此类商品
if (pSE == NULL) {
cout<<"\n很抱歉,本商店没有索引号为 \""< 10、
//非法输入
if ( !(cin>>iNum) ) {
cout<<"\n\n你键入了非法的购买数目,程序即将退出\n\n";
goto CELEAN;
}
//商品查询
int iStoreNum = pStore->QueryGoods( iIndex );
if (iStoreNum <= 0) {
cout<<"很抱歉,你要购买的\""< 11、um > iStoreNum ) {
cout<<"很抱歉,本商店目前仅有\""< 12、品: "< 13、out<<"\n\n嗨,什么都没买到,白来一趟;(\n\n";
goto CELEAN;
}
else{
cout<<"\n\n购买完毕,今天收获不小啊,购买了如下商品:\n\n";
pBasket->Print();
}
cout<<"\n\n您好,您本次共计消费 "< 14、"元\n";
cout<<"请交钱:";
cin >> dCash;
}
cout<<"\n\n共收取您现金:"< 15、te pBasket;
return;
// counter.cpp: implementation of the supermarket class.
//
//////////////////////////////////////////////////////////////////////
#include "counter.h"
#include 16、
CGoods::CGoods( int iIndex, string strName, double dUnitPrice )
{
// 初始化商品
m_iIndex = iIndex;
m_strName = strName;
m_dUnitPrice = dUnitPrice;
}
CGoods::~CGoods()
{
}
//attributes and operations
in 17、t CGoods::GetIndex(void) const {
return m_iIndex;
}
string CGoods::GetName(void) const{
return m_strName;
}
double CGoods::GetUnitPrice(void) const {
return m_dUnitPrice;
}
void CGoods::SetUnitPrice(double dUnitPrice){
m_dUnitPrice = dUnitPrice;
}
CGoods *CGoods::Clone(){
r 18、eturn new CGoods(m_iIndex, m_strName, m_dUnitPrice);
}
//************************************************************************
//*商品类的成员函数定义结束
//************************************************************************
//****************************************************************** 19、
//*储藏室类的成员函数定义
//************************************************************************
CStore::CStore()
{
}
CStore::~CStore()
{
DeleteAllGoods();
}
//往储藏室种添加货物
bool CStore::AddGoods(CGoods *pGoods, int iNum){
assert( pGoods != NULL );
int size = m_vStore.size();
20、
bool bIsExist = false;
for(int i=0; i 21、
//储藏室没有同类商品,直接增加此类商品
if ( !bIsExist ){
PSTOREELEMENT p = new STOREELEMENT;
p->pGoods = pGoods;
p->iNum = iNum;
m_vStore.push_back( p );
}
return true;
}
//清空整个储藏室
void CStore::DeleteAllGoods(){
int size = m_vStore.size();
for(int i=0; i 22、
PSTOREELEMENT pSE = m_vStore[i];
assert(pSE != NULL);
if ( pSE->pGoods != NULL ){
delete pSE->pGoods; //删除商品
pSE->pGoods = NULL;
}
delete pSE; //删除储藏室元素
pSE = NULL;
}
}
//删除名为strName的货物,iNum为删除数量
int CStore::DeleteGoods(const string &strName, int i 23、Num){
int size = m_vStore.size();
for(int i=0; i 24、 //删除储藏室元素
pSE = NULL;
return 0;
}
else{
pSE->iNum -= iNum;
return pSE->iNum;
}
}
}
return 0;
}
//删除索引号为iIndex的货物,iNum为删除数量
int CStore::DeleteGoods(int iIndex, int iNum){
int size = m_vStore.size();
for(int i=0; i 25、PSTOREELEMENT pSE = m_vStore[i];
assert(pSE != NULL);
if ( pSE->pGoods != NULL && pSE->pGoods->GetIndex() == iIndex ){
if ( iNum <= -1 || pSE->iNum <= iNum){
delete pSE->pGoods;
pSE->iNum = 0;
delete pSE; //删除储藏室元素
pSE = NULL;
return 0;
}
el 26、se{
pSE->iNum -= iNum;
return pSE->iNum;
}
}
}
return 0;
}
//获取货物清单
vector 27、MENT pSE = m_vStore[i];
assert(pSE != NULL);
if ( pSE->pGoods != NULL && pSE->pGoods->GetIndex() == iIndex ){
return pSE;
}
}
return NULL;
}
//获取某类货物
PSTOREELEMENT CStore::GetGoods(const string &strName){
int size = m_vStore.size();
for(int i=0; i 28、EELEMENT pSE = m_vStore[i];
assert(pSE != NULL);
if ( pSE->pGoods != NULL && pSE->pGoods->GetName() == strName ){
return pSE;
}
}
return NULL;
}
//查询货物
int CStore::QueryGoods(const string &strName){
int size = m_vStore.size();
for(int i=0; i 29、NT pSE = m_vStore[i];
assert(pSE != NULL);
if ( pSE->pGoods != NULL && pSE->pGoods->GetName() == strName ){
return pSE->iNum;
}
}
return 0;
}
//查询货物
int CStore::QueryGoods(int iIndex){
int size = m_vStore.size();
for(int i=0; i 30、ore[i];
assert(pSE != NULL);
if ( pSE->pGoods != NULL && pSE->pGoods->GetIndex() == iIndex ){
return pSE->iNum;
}
}
return 0;
}
//打印水单
void CStore::Print(){
string strTitle[4];
strTitle[0] = "索引号";
strTitle[1] = "商品名";
strTitle[2] = "数量";
strTitle[3] = "单价";
fo 31、r (int i=0; i<4; i++){
cout.width(16);
cout< 32、ut.width(16);
cout< 33、
//************************************************************************
//*收银台类的成员函数定义
//************************************************************************
CCounter::CCounter(CStore *pBasket)
{
m_pBasket = pBasket;
m_dCash = 0.0;
}
CCounter::~CCounter 34、)
{
}
//获取帐单
double CCounter::Bill(void){
double dTotalPrice = 0.0;
vector 35、Num ;
}
return dTotalPrice;
}
//收钱及找零
double CCounter::Count(double dCash){
m_dCash += dCash;
double dTotal = Bill();
return m_dCash - dTotal;
}
//收取的现金数
double CCounter::GetCash(){
return m_dCash;
}
//************************************************************************
//*收银台类的成员函数定义结束
//************************************************************************
}四、运行结果
五、试验体会
在这次的设计过程中,我采用的是VC++,不仅使我对课程有了进一步的强化,还使我的开发能力得到了提高。
在此感谢给予我帮助的老师和同学们。






