标签:
#ifndef GOODS_H
#define GOODS_H
#include <iostream>
#include <string>
#include <list>
#include <fstream>
using namespace std;
class Goods{
protected:
int number;
string name;
float price;
list<Goods> goodslist;
list<Goods>::iterator iter;
public:
Goods(){};
Goods(int n,string nam,float p);
void addGoods();
void showGoods();
void menu();
void returnmenu();
void saveGoodsFile();
void eraseGoodsFile();
int getnumber(){return number;};
string getname(){return name;};
float getprice(){return price;};
void modifyGoods();
void saveGoodsfile();
void readGoodsfile();
};
#endif
// testapp.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "gq.h"
Goods::Goods(int n,string nam,float p)
{
number=n;
name=nam;
price=p;
}
void Goods::addGoods()
{
system("cls");
// cout<<"请输入商品编号:"<<endl;
// cin>>number;
number=goodslist.size();
cout<<"请输入商品名称:"<<endl;
cin>>name;
cout<<"请输入商品价格:"<<endl;
cin>>price;
goodslist.push_back(Goods(++number,name,price));
cout<<"添加商品成功!输入Q继续,输入W返回主菜单。"<<endl;
char h=0;
while ("h!=Q"&&"h!=W")
{
cin>>h;
if (h==‘Q‘)
addGoods();
else
if (h==‘W‘)
menu();
else
cout<<"您的输入有误,请重新输入:"<<endl;
}
}
void Goods::showGoods()
{
system("cls");
cout<<"商品信息"<<endl;
cout<<"编号\t"<<"商品名称\t"<<"价格"<<endl;
for (iter=goodslist.begin();iter!=goodslist.end();++iter)
cout<<iter->getnumber()<<"\t"<<iter->getname()<<"\t\t"<<iter->getprice()<<endl;
cout<<"按W返回主菜单..."<<endl;
returnmenu();
}
void Goods::menu()
{
system("cls");
cout<<"欢迎使用销售系统"<<endl;
cout<<"1.添加商品"<<endl;
cout<<"2.显示商品"<<endl;
cout<<"3.退出系统"<<endl;
cout<<"4.删除商品"<<endl;
cout<<"5.修改商品"<<endl;
cout<<"6.购买商品"<<endl;
cout<<"7.显示库存"<<endl;
cout<<"8.销售商品"<<endl;
cout<<"请输入您的选项:";
}
void Goods::eraseGoodsFile()
{ system("cls");
int ID;
cout<<"请输入要删除的ID:"<<endl;
cin>>ID;
for (iter=goodslist.begin();iter!=goodslist.end();++iter)
if (ID==iter->getnumber())
{
iter=goodslist.erase(iter);
cout<<"您已删除成功!按W返回主菜单..."<<endl;
break;
}
returnmenu();
}
void Goods::modifyGoods()
{
system("cls");
int X;
string N;
float P;
cout<<"请输入您需要更改的商品编号:"<<endl;
cin>>X;
for(iter=goodslist.begin();iter!=goodslist.end();++iter)
if (X==iter->getnumber())
{
iter=goodslist.erase(iter);
cout<<"请输入新商品名称"<<endl;
cin>>N;
cout<<"请输入新商品价格:"<<endl;
cin>>P;
goodslist.insert(iter,Goods(X,N,P));
cout<<"您已修改成功!按W返回主菜单..."<<endl;
break;
}
returnmenu();
}
void Goods::saveGoodsfile()
{
ofstream outfile;
outfile.open("gq.txt");
if(outfile.fail())
cout<<"您未保存成功..."<<endl;
else
for(iter=goodslist.begin();iter!=goodslist.end();++iter)
outfile<<iter->getnumber()<<"\t"<<iter->getname()<<"\t"<<iter->getprice()<<endl;
outfile.close( );
}
void Goods::readGoodsfile()
{
int nX;string nN;float nP;
ifstream fin;
fin.open("gq.txt");
if(fin.fail())
cout<<"读入文件失败!"<<endl;
else
while(!fin.eof())
{
fin>>nX>>nN>>nP;
goodslist.push_back(Goods(nX,nN,nP));
}
}
void Goods::returnmenu()
{
char h=0;
while (h!=‘W‘)
{
cin>>h;
if (h==‘W‘)
menu();
else
cout<<"您的输入有误,请重新输入:"<<endl;
}
}
继承类:
#ifndef BuyGoods_H
#define BuyGoods_H
#include <time.h>
#include "gq.h"
class BuyGoods:public Goods
{
protected:
long goodsamount;
long double totalmoney;
string Stime;
int sellID;
list<BuyGoods> buygoodslist;
list<BuyGoods>::iterator itera;
public:
BuyGoods(){};
BuyGoods(int sID,int num,string nam,float pri,long ga,long double tm,string tim);
int getgoodsamount(){return goodsamount;};
long double gettotalmoney(){return totalmoney;};
int getSellID(){return sellID;};
string getTime(){return Stime;};
void switch_while();
void showbuygoods();
void savebuyGoodsFile();
void readbuyGoodsfile();
string setTime();
void purchaseGoods();
void sellbuygoods();
};
#endif
#include "stdafx.h"
#include "jc.h"
BuyGoods::BuyGoods(int sID,int num,string nam,float pri,long ga,long double tm,string tim)
{
sellID=sID;
number=num;
name=nam;
price=pri;
goodsamount=ga;
totalmoney=tm;
Stime=tim;
}
void BuyGoods::switch_while()
{
while (1)
{
cout<<"请输入选项:"<<endl;
int x;
cin>>x;
switch (x)
{
case 1:addGoods();
break;
case 2:showGoods();
break;
case 3: saveGoodsfile();
savebuyGoodsFile();
exit(0);
break;
case 4:eraseGoodsFile();
break;
case 5:modifyGoods();
break;
case 6:purchaseGoods();
break;
case 7:showbuygoods();
break;
case 8:sellbuygoods();
break;
default:cout<<"您的输入有误!请重新输入。"<<endl;
break;
}
}
}
void BuyGoods::showbuygoods()
{
system("cls");
cout<<"库存信息"<<endl;
cout<<"单号\t编号\t名称\t价格\t数量\t金额\t时间"<<endl;
for (itera=buygoodslist.begin();itera!=buygoodslist.end();++itera)
cout<<itera->getSellID()<<"\t"<<itera->getnumber()<<"\t"<<itera->getname()<<"\t"<<itera->getprice()
<<"\t"<<itera->getgoodsamount()<<"\t"<<itera->gettotalmoney()<<"\t"<<
itera->getTime()<<endl;
cout<<"按W返回主菜单..."<<endl;
char h=0;
while (h!=‘W‘)
{
cin>>h;
if (h==‘W‘)
menu();
else
cout<<"您的输入有误,请重新输入:"<<endl;
}
}
void BuyGoods::savebuyGoodsFile()
{
ofstream outfile;
outfile.open("jc.txt");
if(outfile.fail())
cout<<"保存文件失败!"<<endl;
else
for(itera=buygoodslist.begin();itera!=buygoodslist.end();++itera)
outfile<<itera->getSellID()<<"\t"<<itera->getnumber()<<"\t"<<itera->getname()<<"\t"<<itera->getprice()
<<"\t"<<itera->getgoodsamount()<<"\t"<<itera->gettotalmoney()<<"\t"<<itera->getTime()<<endl;
outfile.close( );
}
void BuyGoods::readbuyGoodsfile()
{
int nsID;int nnum;string nnam;float npri;long nga;long double ntm;string ntim,ntim1;
ifstream fin;
fin.open("jc.txt");
if(fin.fail())
cout<<"打开文件失败!"<<endl;
else
while(!fin.eof())
{
fin>>nsID>>nnum>>nnam>>npri>>nga>>ntm>>ntim>>ntim1;
ntim.append(ntim1);
buygoodslist.push_back(BuyGoods(nsID,nnum,nnam,npri,nga,ntm,ntim));
}
}
void BuyGoods::purchaseGoods()
{
system("cls");
cout<<"商品信息"<<endl;
cout<<"编号\t"<<"商品名称\t"<<"价格"<<endl;
for (iter=goodslist.begin();iter!=goodslist.end();++iter)
cout<<iter->getnumber()<<"\t"<<iter->getname()<<"\t\t"<<iter->getprice()<<endl;
cout<<"请输入您要购买的货物编号:"<<endl;
int z,sID;
string nam,tim;
float pri;
long ga;
bool k=0;
while(!k)
{ cin>>z;
if (z>=0&&z<=goodslist.size())
k=1;
else
cout<<"您的输入有误,请重新输入..."<<endl;
}
for (iter=goodslist.begin();iter!=goodslist.end();++iter)
if (z==iter->getnumber())
{
nam=iter->getname();
pri=iter->getprice();
break;
}
cout<<"请输入您要购买的数量:"<<endl;
cin>>ga;
sID=buygoodslist.size()+1;
tim=setTime();
buygoodslist.push_back(BuyGoods(sID,z,nam,pri,ga,pri*ga,tim));
cout<<"购买"<<nam<<"数量为"<<ga<<"成功!"<<endl;
cout<<"按W返回主菜单..."<<endl;
returnmenu();
}
void BuyGoods::sellbuygoods()
{
system("cls");
cout<<"库存信息"<<endl;
cout<<"编号\t名称\t价格\t数量\t金额"<<endl;
for (itera=buygoodslist.begin();itera!=buygoodslist.end();++itera)
cout<<itera->getnumber()<<"\t"<<itera->getname()<<"\t"<<itera->getprice()
<<"\t"<<itera->getgoodsamount()<<"\t"<<itera->gettotalmoney()<<endl;
cout<<"请输入您要购买的货物编号:"<<endl;
int z,sID;
string nam,tim;
float pri;
long ga,nga;
bool k=0;
while(!k)
{
cin>>z;
if (z>=0&&z<=goodslist.size())
k=1;
else
cout<<"您的输入有误,请重新输入..."<<endl;
}
for (itera=buygoodslist.begin();itera!=buygoodslist.end();++itera)
if (z==itera->getnumber())
{
sID=itera->getSellID();
nam=itera->getname();
pri=itera->getprice();
nga=itera->getgoodsamount();
tim=itera->getTime();
cout<<"请输入您要购买的数量:"<<endl;
bool l=0;
while (!l)
{
cin>>ga;
if(ga>0&&ga<=nga)
l=1;
else
cout<<"您的输入有误..."<<endl;
}
if (ga==nga)
itera=buygoodslist.erase(itera);
else
{
itera=buygoodslist.erase(itera);
buygoodslist.push_back(BuyGoods(sID,z,nam,pri,nga-ga,pri*(nga-ga),tim));
}
cout<<"购买"<<nam<<"数量为"<<ga<<"成功!"<<endl;
break;
}
else
cout<<"库存内没有您需要的商品..."<<endl;
cout<<"按W返回主菜单..."<<endl;
returnmenu();
}
std::string BuyGoods::setTime()
{
time_t t=time(0);
char temp[70];
strftime(temp,sizeof(temp),"%Y年%m月%d日%c",localtime(&t));
string Dtime=temp;
return Dtime;
}
主函数
#include "stdafx.h"
#include "gq.h"
#include "jc.h"
void main()
{
BuyGoods A;
A.menu();
A.readGoodsfile();
A.readbuyGoodsfile();
A.switch_while();
}
标签:
原文地址:http://www.cnblogs.com/Lolita917/p/4319079.html