/*在實現功能中,主要使用迭代器(迭代器(iterator)是一種對象,它能夠用來遍歷標準模板庫容器中的部分或全部元素,每個迭代器對象代表容器中的確定的地址。迭代器修改了常規指針的接口,所謂迭代器是一種概念上的抽象:那些行爲上像迭代器的東西都可以叫做迭代器。然而迭代器有很多不同的能力,它可以把抽象容器和通用算法有機的統一起來。
迭代器提供一些基本操作符:*、++、==、!=、=。這些操作和C/C++“操作array元素”時的指針接口一致。不同之處在於,迭代器是個所謂的複雜的指針,具有遍歷複雜數據結構的能力。其下層運行機制取決於其所遍歷的數據結構。因此,每一種容器型別都必須提供自己的迭代器。事實上每一種容器都將其迭代器以嵌套的方式定義於內部。因此各種迭代器的接口相同,型號卻不同。這直接導出了泛型程序設計的概念:所有操作行爲都使用相同接口,雖然它們的型別不同。)*/
//頭文件
#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
//源文件
#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();
}
商品銷售系統
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.