资源描述
《面向对象技术实训》课程实训报告
超市物品管理系统
姓 名:崔召杰
班 级:物联网一班
学 号:121210216
指导教师:王小辉
成 绩:
完成时间:2013年12月
完成地点:数据库实验室
6
1设计题目(问题)描述和要求
某超市需要对物品信息进行保存,包含物品姓名、工号、工龄、工作车间以及工资的各项信息。
要求:
(1) 物品号
(2) 生产单位
(3) 物品名称
(4) 购入时间
(5) 出售价格
2系统分析
根据问题描述和要求,系统要求能够编写程序实现输入、输入、查询、增加、删除等功能
根据案例需求可以定义一个smarket类,smarket的基本信息有物品号、生产厂家、购入时间、物品名称和出售价格。对物品的管理包含有输入、查询、增加、删除等功能,这些功能需要调用smarket类的成员函数实现。
由于需要对物品资料进行读入和存储,在程序中需要文件的输入输出操作。先从相应的文件中读入物品资料,用户可以对资料处理后,在退出系统的时候将已修改资料重新存储到原文件中去。
3设计
3.1 类的设计
smarket0类的设计
3.1.1数据成员
int Num[StrNum]; //物品条码
char WName[StrNum]; //物品名姓名
char production[StrNum]; //生产单位
char Date[StrNum]; //购入时间
float price; //出售价格
3.1.2函数成员
SMarket(); //构造函数
SMarket(const int *Num,const char *WName,const char *production,const char *Date, const float *price );//构造函数
char *get_Num(); //获取物品条码
char *get_WName();//获取物品名
char *get_production();
char *get_Date();
float get_price();
void changeNum(const int *newNum); //更改物品条码
void changeWName(const char *newWName); //更改物品名
void changeproduction(const char *newproduction); //更改生产单位
void changeDate(const char *newDate); //更改生产日期
void changeprice(const float *newprice); //更改价格
int cmpNum(const int *Num); //比较查询词和物品条码,如果相等返回1
3.1.3主要函数成员的实现思路
int *SMarket::get_Num() //获取物品条码
{
return Num;
};
char *SMarket::get_WName() //获取物品名
{
return WName;
};
char *SMarket::get_production() //获取物品条码
{
return production;
};
char *SMarket::get_Date() //获取物品条码
{
return Date;
};
float SMarket::get_price() //获取物品条码
{
return price;
};
void SMarket::changeNum(const int *newNum) //更改物品条码
{
strcpy(Num,newNum);
};
void SMarket::changeWName(const char *newWName) //更改物品名
{
strcpy(WName,newWName);
};
void SMarket::changeproduction(const int *newproduction) //更改生产单位
{
strcpy(production,newproduction);
};
void SMarket::changeDate(const char *newDate) //更改购入时间
{
strcpy(Date,newDate);
};
int SMarket::cmpNum(const char *Num) //比较查询词和物品条码,如果相等返回1
{
return strcmp(Num,Num);
};
3.2主程序设计
3.2.1 函数设计
(1)修改物品信息功能函数
修改功能的设计思路是:
询问用户要修改物品的姓名:
等待用户输入、并获取要修改物品的姓名:
查询该物品在物品列表中是否存在
若不存在,输出该物品不存在的提示信息
若存在,则等待用户输入、并获取要修改物品的姓名,修改物品列表中该物品的相应信息项
(2)删除物品信息功能函数
删除功能的设计思路是:
询问用户要删除物品的姓名:
等待用户输入、并获取要删除物品的名称:
查询该物品在物品列表中是否存在
若不存在,输出该物品不存在的提示信息
若存在,则修删除物品列表中该物品的相应信息项
删除过程。
(3)查找物品信息功能函数
设计思路如下:
询问查找的物品的名称;
等待用户输入;
若查找不存在,输出该物品不存在的提示信息;
若存在,输出该物品的相关信息;
(4)输出一名物品的信息。
设计思路如下:
询问要输出的物品的姓名;
调用该对象的函数,获取物品信息;
3.2.2 主函数设计
在主函数中创建了一个smarket类的对象数组,对物品资料的操作都将通过该类所创建对象的函数成员完成。
在主函数中通过调用函数showsmarket()和showsmarket_title()和showAll()实现物品资料的显示功能。
主程序调用流程图
4调试分析、测试结果
4.1.1 分析
1.设计的时候,会出现中英字符的错误,调试时,出现”0xa1”,空格导致的,重新书写,就可以消除错误。
2.在简单地基础上,进行添加数据成员,进而添加相应功能的成员函数,实现设计预期的结果。
3.编程就是一个不断完善的过程,先搭建框架、逐步扩充,由简到繁,最后完善。边编程,边调试,边扩充。
4.1.2 测试结果:
执行 1:
(1)
(2)
(3)
(4)
5小结
通过对图书馆管理系统的学习,自己进行了对物品信息管理系统的调查,程序的设计及实现。
在设计之初,首先要构思分条列出要查看的信息(数据成员),比如物品姓名、工号和薪资等,根据要得到的信息,设计要实现的操作,即对这些信息的使用(成员函数)。准备好之后,根据C++的知识,进行数据的类型选择,根据定义好的类型,选择相应的类型的函数,进行操作。如输入、输出、查找、删除、修改等。在简单地程序之上,逐渐扩充,实现更多的功能。在调试的过程会遇到不少大大小小的问题,书写认真很重要,细心更重要,即便是检查错误,也要细心,经常见到的就是输入法导致的中英字符混用。
编程时一个细心的过程,书写的规范,使得查看更方便。
6 附录
//*****************************************************************
// * good.h 类声明头文件
//*****************************************************************
#include<iostream.h >
#include<fstream.h> //进行文件操作需包含此头文件
#include<iomanip.h> //进行输出格式化设置需包含此头文件
#include "good.h" //用包含命令将类定义头文件包含进来
# define maxGoodNum 1000 //案例假设存储商品最大不超过1000件
void showGood(Good good) //输出商品名
{
cout<<setw(15)<<good.get_code()<<setw(15)<<good.get_name()<<setw(15)<<good.get_producer()<<setw(15)<<good.get_date()<<setw(15)<<good.get_price()<<endl;
}
void showGood_title()//(Good good) //输出商品名
{
cout<<setw(15)<<"商品的编码"<<setw(15)<<"商品名"<<setw(15)<<"生产厂家"<<setw(15)<<"生产日期"<<setw(15)<<"单价"<<endl;
//cout<<setw(15)<<good.get_code()<<setw(15)<<good.get_name()<<setw(15)<<good.get_producer()<<setw(15)<<good.get_date()<<setw(15)<<good.get_price()<<endl;
//cout<<setiosflags(ios::left)<<setw(20)<<"商品名"<<setw(20)<<"生产厂家"<<endl;
//cout<<setiosflags(ios::left)<<setw(20)<<good.get_name()<<setw(20)<<good.get_producer()<<endl;
}
void showAll(Good *good,int Num) //输出所有商品的商品名
{
showGood_title();
for (int i=0;i<Num;i++)
showGood(*(good+i));
cout<<"共有商品"<<Num<<"件"<<endl;
}
void main()
{
Good goodList[maxGoodNum]; //生成Good类对象数组
bool End=0; //程序终止信号
char Temp[StrNum]; //char数组Temp存储临时数据
int goodNum=0,lastNum=0,i=0,x=0,y=0;
//goodNum商品数目,lastNum上次文件中的商品数目
ifstream infile("Goodlist.txt");
//定义文件指针infile指向GoodList.txt
if ( ! infile ) // 打开失败
{
cerr << "cannot open Goodlist.txt for output\n";
exit( -1 );
}
while((infile>>Temp)) //infile读入数据为空,则循环终止
{
infile>>y;
goodList[i].changeCode(y); //更改编码
goodList[i].changeName(Temp); //更改商品名
infile>>Temp;
goodList[i].changeProducer(Temp); //更改生产厂家
infile>>Temp;
goodList[i].changeDate(Temp); //更改生产日期
infile>>x;
goodList[i].changePrice(x); //更改价格
i++;
}
goodNum=i;
lastNum=i;
cout<<"从GoodList.txt中读出已有商品如下:"<<endl;
showAll(goodList,goodNum); //显示所有商品名
while (End==0) //显示操作界面
{
cout<<"\n\n1、输入新商品\n2、查询旧商品\n3、显示所有商品\n4、修改商品生产厂家\n5、删除商品\n6、退出\n\n";
cin>>i;
if (i==1)
{
cout<<setw(20)<<"请输入新商品名称:";
cin>>Temp;
int biName=0;
for(i=0;i<goodNum;i++)
{
if(!goodList[i].cmpName(Temp))
biName=1;
}
if(!biName) //以前没有此商品则加入商品库
{
goodList[goodNum].changeName(Temp);
cout<<setw(20)<<"请输入生产厂家:";
cin>>Temp;
goodList[goodNum].changeProducer(Temp);
cout<<setw(20)<<"请输入新商品编码:";
cin>>y;
goodList[goodNum].changeCode(y);
cout<<setw(20)<<"请输入新商品生产日期:";
cin>>Temp;
goodList[goodNum].changeDate(Temp);
cout<<setw(20)<<"请输入新商品单价:";
int x;
cin>>x;
goodList[goodNum].changePrice(x);
goodNum++;
}
else //操作失败
cout<<"重复商品名,操作失败"<<endl;
}
else if(i==2) //查询操作
{
cout<<"请输入所查询商品目名称:"<<endl;
cin>>Temp;
for(i=0;i<goodNum;i++)
{
if(!goodList[i].cmpName(Temp))
{
//cout<<"编号"<<i+1<<endl;
//cout<<"生产厂家:"<<goodList[i].get_producer()<<endl;
cout<<"查询结果如下:"<<endl;
showGood_title();
showGood(goodList[i]);
cout<<endl;
break;
}
}
if(i==goodNum)
{
cout<<"未找到所需商品目."<<endl;
}
}
else if(i==6) //终止操作
{
End=1;
}
else if(i==3){ //显示所有商品目的操作
cout<<"显示所有商品目如下:"<<endl;
showAll(goodList,goodNum);
}
else if (i==4) //修改商品信息
{
cout<<"请输入所修改商品的名称:"<<endl;
float price;
cin>>Temp;
for(i=0;i<goodNum;i++)
{
if(!goodList[i].cmpName(Temp))
{
cout<<"请输入该商品新的价格:";
cin>>price;
goodList[i].changePrice(price);
cout<<endl<<"该商品修改后的信息如下:"<<endl;
showGood_title();
showGood(goodList[i]);
break;
}
}
if(i==goodNum)
{
cout<<"未找到所需商品目."<<endl;
}
}
else if (i==5) //删除商品
{
cout<<"请输入所删除商品的名称:"<<endl;
cin>>Temp;
for(i=0;i<goodNum;i++)
{
if(!goodList[i].cmpName(Temp))
{
cout<<"删除此商品前所有商品目如下:"<<endl;
showAll(goodList,goodNum);
for(int j=i;j<(goodNum-1);j++)
{
goodList[j].changeName(goodList[j+1].get_name());
goodList[j].changeProducer(goodList[j+1].get_producer());
}
goodNum--;
cout<<"删除此商品后所有商品目如下:"<<endl;
showAll(goodList,goodNum);
break;
}
}
if(i==goodNum)
{
cout<<"未找到所需商品目."<<endl;
}
}
}
infile.close(); //输入文件关闭
ofstream outfile("Goodlist.txt",ios::out); //输出文件打开,添加件尾
if ( ! outfile ) // 打开失败
{
cerr << "cannot open Goodlist.txt for output\n";
exit( -1 );
}
for(i=0;i<goodNum;i++) //新商品输出
{
//outfile<<goodList[i].get_code()<<endl<<goodList[i].get_name()<<endl<<goodList[i].get_producer()<<endl<<goodList[i].get_date()<<endl<<goodList[i].get_price()<<endl<<endl;
outfile<<setw(15)<<goodList[i].get_code()<<setw(15)<<goodList[i].get_name()<<setw(15)<<goodList[i].get_producer()<<setw(15)<<goodList[i].get_date()<<setw(15)<<goodList[i].get_price()<<endl<<endl;
}
/*ofstream outfile("Goodlist.txt",ios::app); //输出文件打开,添加件尾
if ( ! outfile ) // 打开失败
{
cerr << "cannot open Goodlist.txt for output\n";
exit( -1 );
}
for(i=lastNum;i<goodNum;i++) //新商品输出
{
outfile<<goodList[i].get_name()<<endl<<goodList[i].get_producer()<<endl<<endl;
}*/
outfile.close(); //输出文件关闭
}
// **********************************************************
// * main.cpp 系统主文件
// **********************************************************
#include<iostream.h >
#include<fstream.h> //进行文件操作需包含此头文件
#include<iomanip.h> //进行输出格式化设置需包含此头文件
#include "good.h" //用包含命令将类定义头文件包含进来
# define maxGoodNum 1000 //案例假设存储商品最大不超过1000件
void showGood(Good good) //输出商品名
{
cout<<setw(15)<<good.get_code()<<setw(15)<<good.get_name()<<setw(15)<<good.get_producer()<<setw(15)<<good.get_date()<<setw(15)<<good.get_price()<<endl;
}
void showGood_title()//(Good good) //输出商品名
{
cout<<setw(15)<<"商品的编码"<<setw(15)<<"商品名"<<setw(15)<<"生产厂家"<<setw(15)<<"生产日期"<<setw(15)<<"单价"<<endl;
//cout<<setw(15)<<good.get_code()<<setw(15)<<good.get_name()<<setw(15)<<good.get_producer()<<setw(15)<<good.get_date()<<setw(15)<<good.get_price()<<endl;
//cout<<setiosflags(ios::left)<<setw(20)<<"商品名"<<setw(20)<<"生产厂家"<<endl;
//cout<<setiosflags(ios::left)<<setw(20)<<good.get_name()<<setw(20)<<good.get_producer()<<endl;
}
void showAll(Good *good,int Num) //输出所有商品的商品名
{
showGood_title();
for (int i=0;i<Num;i++)
showGood(*(good+i));
cout<<"共有商品"<<Num<<"件"<<endl;
}
void main()
{
Good goodList[maxGoodNum]; //生成Good类对象数组
bool End=0; //程序终止信号
char Temp[StrNum]; //char数组Temp存储临时数据
int goodNum=0,lastNum=0,i=0,x=0,y=0;
//goodNum商品数目,lastNum上次文件中的商品数目
ifstream infile("Goodlist.txt");
//定义文件指针infile指向GoodList.txt
if ( ! infile ) // 打开失败
{
cerr << "cannot open Goodlist.txt for output\n";
exit( -1 );
}
while((infile>>Temp)) //infile读入数据为空,则循环终止
{
infile>>y;
goodList[i].changeCode(y); //更改编码
goodList[i].changeName(Temp); //更改商品名
infile>>Temp;
goodList[i].changeProducer(Temp); //更改生产厂家
infile>>Temp;
goodList[i].changeDate(Temp); //更改生产日期
infile>>x;
goodList[i].changePrice(x); //更改价格
i++;
}
goodNum=i;
lastNum=i;
cout<<"从GoodList.txt中读出已有商品如下:"<<endl;
showAll(goodList,goodNum); //显示所有商品名
while (End==0) //显示操作界面
{
cout<<"\n\n1、输入新商品\n2、查询旧商品\n3、显示所有商品\n4、修改商品生产厂家\n5、删除商品\n6、退出\n\n";
cin>>i;
if (i==1)
{
cout<<setw(20)<<"请输入新商品名称:";
cin>>Temp;
int biName=0;
for(i=0;i<goodNum;i++)
{
if(!goodList[i].cmpName(Temp))
biName=1;
}
if(!biName) //以前没有此商品则加入商品库
{
goodList[goodNum].changeName(Temp);
cout<<setw(20)<<"请输入生产厂家:";
cin>>Temp;
goodList[goodNum].changeProducer(Temp);
cout<<setw(20)<<"请输入新商品编码:";
cin>>y;
goodList[goodNum].changeCode(y);
cout<<setw(20)<<"请输入新商品生产日期:";
cin>>Temp;
goodList[goodNum].changeDate(Temp);
cout<<setw(20)<<"请输入新商品单价:";
int x;
cin>>x;
goodList[goodNum].changePrice(x);
goodNum++;
}
else //操作失败
cout<<"重复商品名,操作失败"<<endl;
}
else if(i==2) //查询操作
{
cout<<"请输入所查询商品目名称:"<<endl;
cin>>Temp;
for(i=0;i<goodNum;i++)
{
if(!goodList[i].cmpName(Temp))
{
//cout<<"编号"<<i+1<<endl;
//cout<<"生产厂家:"<<goodList[i].get_producer()<<endl;
cout<<"查询结果如下:"<<endl;
showGood_title();
showGood(goodList[i]);
cout<<endl;
break;
}
}
if(i==goodNum)
{
cout<<"未找到所需商品目."<<endl;
}
}
else if(i==6) //终止操作
{
End=1;
}
else if(i==3){ //显示所有商品目的操作
cout<<"显示所有商品目如下:"<<endl;
showAll(goodList,goodNum);
}
else if (i==4) //修改商品信息
{
cout<<"请输入所修改商品的名称:"<<endl;
float price;
cin>>Temp;
for(i=0;i<goodNum;i++)
{
if(!goodList[i].cmpName(Temp))
{
cout<<"请输入该商品新的价格:";
cin
展开阅读全文