/*
*File:Product.h
*Author:88250
*
*CreatedonMay2,2008,5:51PM
*/
#ifndef_PRODUCT_H
#define_PRODUCT_H
#include<string>
classProduct{
public:
Product(std::stringproductName){
name=productName;
}
std::stringname;
};
#endif/*_PRODUCT_H*/
*File:Product.h
*Author:88250
*
*CreatedonMay2,2008,5:51PM
*/
#ifndef_PRODUCT_H
#define_PRODUCT_H
#include<string>
classProduct{
public:
Product(std::stringproductName){
name=productName;
}
std::stringname;
};
#endif/*_PRODUCT_H*/
/*
*File:RecordRow.h
*Author:88250
*
*CreatedonMay2,2008,5:54PM
*/
#ifndef_RECORDROW_H
#define_RECORDROW_H
#include<string>
classRecordRow{
public:
RecordRow(std::stringn,std::stringdir,intam,floatp,intm,intd){
name=n;
direction=dir;
amount=am;
price=p;
month=m;
day=d;
}
std::stringname;
std::stringdirection;
intamount;
floatprice;
intmonth;
intday;
};
#endif/*_RECORDROW_H*/
*File:RecordRow.h
*Author:88250
*
*CreatedonMay2,2008,5:54PM
*/
#ifndef_RECORDROW_H
#define_RECORDROW_H
#include<string>
classRecordRow{
public:
RecordRow(std::stringn,std::stringdir,intam,floatp,intm,intd){
name=n;
direction=dir;
amount=am;
price=p;
month=m;
day=d;
}
std::stringname;
std::stringdirection;
intamount;
floatprice;
intmonth;
intday;
};
#endif/*_RECORDROW_H*/

/**//*
*File:ProductManager.h
*Author:88250
*
*CreatedonMay2,2008,5:57PM
*/
#ifndef_PRODUCTMANAGER_H
#define_PRODUCTMANAGER_H
#include<vector>
#include"Product.h"
#include"RecordRow.h"
usingnamespacestd;

classProductManager...{
public:
voidinit();
voidsaveProduct(std::vector<Product>products);
voidreadProduct(std::vector<Product>&products);
voidsaveRecord(std::vector<RecordRow>records);
voidreadRecord(std::vector<RecordRow>&records);
voidnewProduct(stringname);
voidnewRecord(RecordRowrr);
voiddisplayName(void);
voiddisplayRecords(void);
voiddisplayStat(intproductNum,intstartM,intendM,intstartD,intendD);
voiddisplayAllStat(intstartM,intendM,intstartD,intendD);
vector<Product>productVector;
vector<RecordRow>recordVector;
private:
vector<RecordRow>getRRByName(stringname,vector<RecordRow>rrs);
vector<string>getNames(vector<RecordRow>rrs);
boolcontainsName(vector<string>names,stringname);
voiddisplayAProduct(vector<RecordRow>rr,float&mao,float&chun);
};
#endif/*_PRODUCTMANAGER_H*/
/*
*File:ProductManager.cpp
*Author:88250
*
*CreatedonMay2,2008,7:7PM
*/
#include"ProductManager.h"
#include<vector>
#include<string>
#include<fstream>
#include<iostream>
usingnamespacestd;
voidProductManager::displayAllStat(intstartM,intendM,intstarD,intendD){
vector<RecordRow>rrs;
for(inti=0;i<recordVector.size();i++){
RecordRowrr=recordVector[i];
if(rr.month>startM&&rr.month<endM){
rrs.push_back(rr);
continue;
}
if(rr.month==startM||rr.month==endM){
if(rr.day>=starD&&rr.day<=endD){
rrs.push_back(rr);
}
}
}
vector<string>names=getNames(rrs);
vector<vector<RecordRow>>rrss;
for(inti=0;i<names.size();i++){
rrss.push_back(getRRByName(names[i],rrs));
}
cout<<"品种 "<<"进总数 "<<"出总数 "<<"进总价 "
<<"出总价 "<<"阶段利润 "<<"总金额流量"<<endl;
floatmao=0.0;//毛利
floatchun=0.0;//纯利
for(inti=0;i<rrss.size();i++){
displayAProduct(rrss[i],mao,chun);
}
cout<<"当前的总毛收入:"<<mao<<endl;
cout<<"当前的总纯收入:"<<chun<<endl;
cout<<endl;
}
voidProductManager::displayStat(intproductNum,
intstartM,intendM,intstarD,intendD){
vector<RecordRow>rrs;
for(inti=0;i<recordVector.size();i++){
RecordRowrr=recordVector[i];
if(rr.name==productVector[productNum].name){
if(rr.month>startM&&rr.month<endM){
rrs.push_back(rr);
continue;
}
if(rr.month==startM||rr.month==endM){
if(rr.day>=starD&&rr.day<=endD){
rrs.push_back(rr);
}
}
}
}
cout<<"品种 "<<"进总数 "<<"出总数 "<<"进总价 "
<<"出总价 "<<"阶段利润 "<<"总金额流量"<<endl;
floatmao=0.0;
floatchun=0.0;
displayAProduct(rrs,mao,chun);
cout<<"当前的总毛收入:"<<mao<<endl;
cout<<"当前的总纯收入:"<<chun<<endl;
cout<<endl;
}
voidProductManager::displayAProduct(vector<RecordRow>rr,
float&mao,float&chun){
intiam=0;
intoam=0;
floatip=0.0;
floatop=0.0;
floatpro=0.0;
floatppro=0.0;
floatiavg=0.0;
intinum=0;
if(rr[0].name.length()>=8){
cout<<rr[0].name<<" ";
}else{
cout<<rr[0].name<<" ";
}
for(inti=0;i<rr.size();i++){
if(rr[i].direction=="进"){
iam+=rr[i].amount;
iavg+=rr[i].price;
ip+=rr[i].amount*rr[i].price;
inum++;
}else{
oam+=rr[i].amount;
op+=rr[i].amount*rr[i].price;
}
}
iavg=iavg/inum;
pro=op-ip;
ppro=op-oam*iavg;
cout<<iam<<" "<<oam<<" "<<ip<<" "<<op<<" "
<<ppro<<" "<<pro<<" "<<endl;
chun+=ppro;
mao+=op;
}
vector<RecordRow>ProductManager::getRRByName(stringname,
vector<RecordRow>rrs){
vector<RecordRow>ret;
for(inti=0;i<rrs.size();i++){
if(rrs[i].name==name){
ret.push_back(rrs[i]);
}
}
returnret;
}
vector<string>ProductManager::getNames(vector<RecordRow>rrs){
vector<string>ret;
for(inti=0;i<rrs.size();i++){
if(!containsName(ret,rrs[i].name)){
ret.push_back(rrs[i].name);
}
}
returnret;
}
boolProductManager::containsName(vector<string>names,stringname){
for(inti=0;i<names.size();i++){
if(names[i]==name){
returntrue;
}
}
returnfalse;
}
voidProductManager::init(){
std::vector<Product>products;
readProduct(products);
productVector=products;
std::vector<RecordRow>rrs;
readRecord(rrs);
recordVector=rrs;
}
voidProductManager::newProduct(stringname){
vector<Product>::iteratori;
boolisExisted=false;
for(i=productVector.begin();i!=productVector.end();i++){
if((*i).name==name){
isExisted=true;
}
}
if(!isExisted){
productVector.push_back(Product(name));
}
saveProduct(productVector);
}
voidProductManager::newRecord(RecordRowrr){
recordVector.push_back(rr);
saveRecord(recordVector);
}
voidProductManager::displayName(void){
for(inti=0;i<productVector.size();i++){
cout<<i<<"."<<productVector[i].name<<endl;
}
cout<<endl;
}
voidProductManager::displayRecords(void){
if(!recordVector.empty()){
cout<<"品种 "<<"流向 "<<"数量 "<<"单价 "<<"日期"<<endl;
for(inti=0;i<recordVector.size();i++){
if(recordVector[i].name.length()>=8){
cout<<recordVector[i].name<<" "<<recordVector[i].direction<<" "
<<recordVector[i].amount<<" "<<recordVector[i].price
<<" "<<recordVector[i].month<<"-"<<recordVector[i].day
<<endl;
}else{
cout<<recordVector[i].name<<" "<<recordVector[i].direction<<" "
<<recordVector[i].amount<<" "<<recordVector[i].price
<<" "<<recordVector[i].month<<"-"<<recordVector[i].day
<<endl;
}
}
cout<<endl;
}
}
voidProductManager::saveProduct(std::vector<Product>products){
ofstreamfout("name");
if(!fout){
cout<<"Cannotopenoutputfile! ";
return;
}
for(inti=0;i<products.size();i++){
fout<<products[i].name<<endl;
}
fout.close();
}
voidProductManager::saveRecord(std::vector<RecordRow>records){
ofstreamfout("data");
if(!fout){
cout<<"Cannotopenoutputfile! ";
return;
}
for(inti=0;i<records.size();i++){
fout<<"Record"<<endl
<<records[i].name<<endl
<<records[i].direction<<endl
<<records[i].amount<<endl
<<records[i].price<<endl
<<records[i].month<<endl
<<records[i].day<<endl;
}
fout.close();
}
voidProductManager::readProduct(std::vector<Product>&products){
ifstreamfin("name");
if(!fin){
cout<<"Cannotopeninputfile! ";
return;
}
stringaLine;
while(getline(fin,aLine)){
products.push_back(Product(aLine));
}
fin.close();
}
voidProductManager::readRecord(std::vector<RecordRow>&records){
ifstreamfin("data");
if(!fin){
cout<<"Cannotopeninputfile! ";
return;
}
stringaLine;
while(getline(fin,aLine)){
stringn,dir;
intam,m,d;
floatpr;
if(aLine.at(0)=='R'){
getline(fin,aLine);
n=aLine;
getline(fin,aLine);
dir=aLine;
getline(fin,aLine);
am=atoi(aLine.c_str());
getline(fin,aLine);
pr=atof(aLine.c_str());
getline(fin,aLine);
m=atoi(aLine.c_str());
getline(fin,aLine);
d=atoi(aLine.c_str());
records.push_back(RecordRow(n,dir,am,pr,m,d));
}
}
fin.close();
}
*File:ProductManager.cpp
*Author:88250
*
*CreatedonMay2,2008,7:7PM
*/
#include"ProductManager.h"
#include<vector>
#include<string>
#include<fstream>
#include<iostream>
usingnamespacestd;
voidProductManager::displayAllStat(intstartM,intendM,intstarD,intendD){
vector<RecordRow>rrs;
for(inti=0;i<recordVector.size();i++){
RecordRowrr=recordVector[i];
if(rr.month>startM&&rr.month<endM){
rrs.push_back(rr);
continue;
}
if(rr.month==startM||rr.month==endM){
if(rr.day>=starD&&rr.day<=endD){
rrs.push_back(rr);
}
}
}
vector<string>names=getNames(rrs);
vector<vector<RecordRow>>rrss;
for(inti=0;i<names.size();i++){
rrss.push_back(getRRByName(names[i],rrs));
}
cout<<"品种 "<<"进总数 "<<"出总数 "<<"进总价 "
<<"出总价 "<<"阶段利润 "<<"总金额流量"<<endl;
floatmao=0.0;//毛利
floatchun=0.0;//纯利
for(inti=0;i<rrss.size();i++){
displayAProduct(rrss[i],mao,chun);
}
cout<<"当前的总毛收入:"<<mao<<endl;
cout<<"当前的总纯收入:"<<chun<<endl;
cout<<endl;
}
voidProductManager::displayStat(intproductNum,
intstartM,intendM,intstarD,intendD){
vector<RecordRow>rrs;
for(inti=0;i<recordVector.size();i++){
RecordRowrr=recordVector[i];
if(rr.name==productVector[productNum].name){
if(rr.month>startM&&rr.month<endM){
rrs.push_back(rr);
continue;
}
if(rr.month==startM||rr.month==endM){
if(rr.day>=starD&&rr.day<=endD){
rrs.push_back(rr);
}
}
}
}
cout<<"品种 "<<"进总数 "<<"出总数 "<<"进总价 "
<<"出总价 "<<"阶段利润 "<<"总金额流量"<<endl;
floatmao=0.0;
floatchun=0.0;
displayAProduct(rrs,mao,chun);
cout<<"当前的总毛收入:"<<mao<<endl;
cout<<"当前的总纯收入:"<<chun<<endl;
cout<<endl;
}
voidProductManager::displayAProduct(vector<RecordRow>rr,
float&mao,float&chun){
intiam=0;
intoam=0;
floatip=0.0;
floatop=0.0;
floatpro=0.0;
floatppro=0.0;
floatiavg=0.0;
intinum=0;
if(rr[0].name.length()>=8){
cout<<rr[0].name<<" ";
}else{
cout<<rr[0].name<<" ";
}
for(inti=0;i<rr.size();i++){
if(rr[i].direction=="进"){
iam+=rr[i].amount;
iavg+=rr[i].price;
ip+=rr[i].amount*rr[i].price;
inum++;
}else{
oam+=rr[i].amount;
op+=rr[i].amount*rr[i].price;
}
}
iavg=iavg/inum;
pro=op-ip;
ppro=op-oam*iavg;
cout<<iam<<" "<<oam<<" "<<ip<<" "<<op<<" "
<<ppro<<" "<<pro<<" "<<endl;
chun+=ppro;
mao+=op;
}
vector<RecordRow>ProductManager::getRRByName(stringname,
vector<RecordRow>rrs){
vector<RecordRow>ret;
for(inti=0;i<rrs.size();i++){
if(rrs[i].name==name){
ret.push_back(rrs[i]);
}
}
returnret;
}
vector<string>ProductManager::getNames(vector<RecordRow>rrs){
vector<string>ret;
for(inti=0;i<rrs.size();i++){
if(!containsName(ret,rrs[i].name)){
ret.push_back(rrs[i].name);
}
}
returnret;
}
boolProductManager::containsName(vector<string>names,stringname){
for(inti=0;i<names.size();i++){
if(names[i]==name){
returntrue;
}
}
returnfalse;
}
voidProductManager::init(){
std::vector<Product>products;
readProduct(products);
productVector=products;
std::vector<RecordRow>rrs;
readRecord(rrs);
recordVector=rrs;
}
voidProductManager::newProduct(stringname){
vector<Product>::iteratori;
boolisExisted=false;
for(i=productVector.begin();i!=productVector.end();i++){
if((*i).name==name){
isExisted=true;
}
}
if(!isExisted){
productVector.push_back(Product(name));
}
saveProduct(productVector);
}
voidProductManager::newRecord(RecordRowrr){
recordVector.push_back(rr);
saveRecord(recordVector);
}
voidProductManager::displayName(void){
for(inti=0;i<productVector.size();i++){
cout<<i<<"."<<productVector[i].name<<endl;
}
cout<<endl;
}
voidProductManager::displayRecords(void){
if(!recordVector.empty()){
cout<<"品种 "<<"流向 "<<"数量 "<<"单价 "<<"日期"<<endl;
for(inti=0;i<recordVector.size();i++){
if(recordVector[i].name.length()>=8){
cout<<recordVector[i].name<<" "<<recordVector[i].direction<<" "
<<recordVector[i].amount<<" "<<recordVector[i].price
<<" "<<recordVector[i].month<<"-"<<recordVector[i].day
<<endl;
}else{
cout<<recordVector[i].name<<" "<<recordVector[i].direction<<" "
<<recordVector[i].amount<<" "<<recordVector[i].price
<<" "<<recordVector[i].month<<"-"<<recordVector[i].day
<<endl;
}
}
cout<<endl;
}
}
voidProductManager::saveProduct(std::vector<Product>products){
ofstreamfout("name");
if(!fout){
cout<<"Cannotopenoutputfile! ";
return;
}
for(inti=0;i<products.size();i++){
fout<<products[i].name<<endl;
}
fout.close();
}
voidProductManager::saveRecord(std::vector<RecordRow>records){
ofstreamfout("data");
if(!fout){
cout<<"Cannotopenoutputfile! ";
return;
}
for(inti=0;i<records.size();i++){
fout<<"Record"<<endl
<<records[i].name<<endl
<<records[i].direction<<endl
<<records[i].amount<<endl
<<records[i].price<<endl
<<records[i].month<<endl
<<records[i].day<<endl;
}
fout.close();
}
voidProductManager::readProduct(std::vector<Product>&products){
ifstreamfin("name");
if(!fin){
cout<<"Cannotopeninputfile! ";
return;
}
stringaLine;
while(getline(fin,aLine)){
products.push_back(Product(aLine));
}
fin.close();
}
voidProductManager::readRecord(std::vector<RecordRow>&records){
ifstreamfin("data");
if(!fin){
cout<<"Cannotopeninputfile! ";
return;
}
stringaLine;
while(getline(fin,aLine)){
stringn,dir;
intam,m,d;
floatpr;
if(aLine.at(0)=='R'){
getline(fin,aLine);
n=aLine;
getline(fin,aLine);
dir=aLine;
getline(fin,aLine);
am=atoi(aLine.c_str());
getline(fin,aLine);
pr=atof(aLine.c_str());
getline(fin,aLine);
m=atoi(aLine.c_str());
getline(fin,aLine);
d=atoi(aLine.c_str());
records.push_back(RecordRow(n,dir,am,pr,m,d));
}
}
fin.close();
}
/*
*File:Main.cpp
*Author:88250
*
*CreatedonMay2,2008,9:36PM
*/
#include"ProductManager.h"
#include<iostream>
usingnamespacestd;
intmain(intargc,char**argv){
ProductManagerpm;
pm.init();
intinput;
intnum=0;
stringname;
intamount=0;
floatprice=0.0;
intmonth=1;
intday=1;
intdir=1;
intsm=1;
intsd=1;
intem=2;
inted=1;
intpn=0;
stringd;
charconfirm='n';
while(true){
cout<<"1.查看现有品种"<<endl;
cout<<"2.增加新品种"<<endl;
cout<<"3.添加销售记录"<<endl;
cout<<"4.查看所有销售记录"<<endl;
cout<<"5.统计帐目"<<endl;
cout<<"请选择:"<<"";
cin>>input;
switch(input){
case1:
pm.displayName();
break;
case2:
cout<<"请输入品种名称(111返回):"<<"";
cin>>name;
if(name=="111"){
break;
}
pm.newProduct(name);
cout<<"新建品种成功!"<<endl;
break;
case3:
pm.displayName();
cout<<"请输入产品编号(111返回):";
cin>>num;
if(num==111){
break;
}
if(num>=0&&num<=pm.productVector.size()){
name=pm.productVector[num].name;
}else{
cout<<"输入有误!";
break;
}
cout<<"请选择流向(1.进;2.出):";
cin>>dir;
if(dir==1){
d="进";
cout<<"请输入进货数量:";
cin>>amount;
cout<<"请输入进货单价:";
cin>>price;
cout<<"请输入进货月:";
cin>>month;
cout<<"请输入进货日:";
cin>>day;
}elseif(dir==2){
d="出";
cout<<"请输入卖出数量:";
cin>>amount;
cout<<"请输入卖出单价:";
cin>>price;
cout<<"请输入销售月:";
cin>>month;
cout<<"请输入销售日:";
cin>>day;
}else{
cout<<"输入有误!";
break;
}
cout<<"确定?(y.确定n.放弃)"<<endl;
cin>>confirm;
if(confirm=='y'){
pm.newRecord(RecordRow(name,d,amount,price,month,day));
}
break;
case4:
pm.displayRecords();
break;
case5:
cout<<"开始月(111返回):";
cin>>sm;
if(sm==111){
break;
}
cout<<"开始日:";
cin>>sd;
cout<<"结束月:";
cin>>em;
cout<<"结束日:";
cin>>ed;
cout<<"请选择统计方式(1.单项品种2.所有品种):";
cin>>num;
if(num==1){
pm.displayName();
cout<<"请输入产品编号:";
cin>>pn;
pm.displayStat(pn,sm,em,sd,ed);
}elseif(num==2){
pm.displayAllStat(sm,em,sd,ed);
}else{
cout<<"选择有误,返回!"<<endl;
}
break;
default:
cout<<"选择有误!"<<endl;
}
}
return(EXIT_SUCCESS);
}
*File:Main.cpp
*Author:88250
*
*CreatedonMay2,2008,9:36PM
*/
#include"ProductManager.h"
#include<iostream>
usingnamespacestd;
intmain(intargc,char**argv){
ProductManagerpm;
pm.init();
intinput;
intnum=0;
stringname;
intamount=0;
floatprice=0.0;
intmonth=1;
intday=1;
intdir=1;
intsm=1;
intsd=1;
intem=2;
inted=1;
intpn=0;
stringd;
charconfirm='n';
while(true){
cout<<"1.查看现有品种"<<endl;
cout<<"2.增加新品种"<<endl;
cout<<"3.添加销售记录"<<endl;
cout<<"4.查看所有销售记录"<<endl;
cout<<"5.统计帐目"<<endl;
cout<<"请选择:"<<"";
cin>>input;
switch(input){
case1:
pm.displayName();
break;
case2:
cout<<"请输入品种名称(111返回):"<<"";
cin>>name;
if(name=="111"){
break;
}
pm.newProduct(name);
cout<<"新建品种成功!"<<endl;
break;
case3:
pm.displayName();
cout<<"请输入产品编号(111返回):";
cin>>num;
if(num==111){
break;
}
if(num>=0&&num<=pm.productVector.size()){
name=pm.productVector[num].name;
}else{
cout<<"输入有误!";
break;
}
cout<<"请选择流向(1.进;2.出):";
cin>>dir;
if(dir==1){
d="进";
cout<<"请输入进货数量:";
cin>>amount;
cout<<"请输入进货单价:";
cin>>price;
cout<<"请输入进货月:";
cin>>month;
cout<<"请输入进货日:";
cin>>day;
}elseif(dir==2){
d="出";
cout<<"请输入卖出数量:";
cin>>amount;
cout<<"请输入卖出单价:";
cin>>price;
cout<<"请输入销售月:";
cin>>month;
cout<<"请输入销售日:";
cin>>day;
}else{
cout<<"输入有误!";
break;
}
cout<<"确定?(y.确定n.放弃)"<<endl;
cin>>confirm;
if(confirm=='y'){
pm.newRecord(RecordRow(name,d,amount,price,month,day));
}
break;
case4:
pm.displayRecords();
break;
case5:
cout<<"开始月(111返回):";
cin>>sm;
if(sm==111){
break;
}
cout<<"开始日:";
cin>>sd;
cout<<"结束月:";
cin>>em;
cout<<"结束日:";
cin>>ed;
cout<<"请选择统计方式(1.单项品种2.所有品种):";
cin>>num;
if(num==1){
pm.displayName();
cout<<"请输入产品编号:";
cin>>pn;
pm.displayStat(pn,sm,em,sd,ed);
}elseif(num==2){
pm.displayAllStat(sm,em,sd,ed);
}else{
cout<<"选择有误,返回!"<<endl;
}
break;
default:
cout<<"选择有误!"<<endl;
}
}
return(EXIT_SUCCESS);
}
本文介绍了一个简单商品管理系统的实现,包括产品和交易记录的类定义、管理功能如添加新品种、记录销售过程等,并展示了如何通过命令行界面进行交互操作。
6512

被折叠的 条评论
为什么被折叠?



