开发语言:C++
开发环境:VS2017
程序说明:商品销售系统程主要分为三部分:
commodity类的声明与实现(commodity.h、commodity.cpp)、
trade类的声明与实现(trade.h、trade.cpp)、
主函数及各功能模块设计(main.cpp)及相关的预编译头文件(stdafx.h、stdafx.cpp、tarverget.h)
程序的主界面及可实现的功能如下:
部分功能模块的界面如下:
程序会在当前目录下生成3个可见的txt文件,分别是stock.txt(商品记录)、sellRecord.cpp(销售记录)、buyRecord(进货记录)。
以下是代码部分:
commodity.h
#include <string.h>
class commodity
{
private:
int static kinds;//商品种类
int ID; //货品编号
int stock;//库存量
float buyValue;//进货的价格
float sellValue;//卖出的价格
char name[30];//商品名称
public:
int getID();//获取ID
int getStock();//获取库存
float getBuyValue();//获取进货价
float getsellValue();//获取卖出价
char* getName();//获取商品名称
void UpdateStock(int n);; //改变库存量
friend class trade;
commodity(char name[],float buyValue,float sellValue)
{
kinds++;
ID = kinds;
this->stock = 0;//默认库存为0
this->buyValue = buyValue;
this->sellValue = sellValue;
strcpy(this->name,name);
}
commodity(int ID,char name[],float buyValue,float sellValue,int stock)
{
kinds++;
this->ID = ID;
this->stock = stock;
this->buyValue = buyValue;
this->sellValue = sellValue;
strcpy(this->name,name);
}
};
commodity.cpp
#include "stdafx.h"
#include "commodity.h"
//初始化ID
int commodity::kinds = 0;
void commodity::UpdateStock(int n)
{
stock+=n;
}
int commodity::getStock()
{
return stock;
}
int commodity::getID()
{
return ID;
}
float commodity::getBuyValue()
{
return buyValue;
}
float commodity::getsellValue()
{
return sellValue;
}
char* commodity::getName()
{
return name;
}
trade.h
#include "stdafx.h"
#include <list>
#include "commodity.h"
using std::list;
class trade{
struct record
{
char name[30];//商品名称
int count;//交易数量
char sTime[70];//交易时间
record(char* name,int count,char* time)//c++中的struct类似class
{
strcpy(this->name,name);
this->count = count;
strcpy(sTime,time);
}
};
//成员变量
private:
list<commodity> dataList;//商品数据链表
list<record> sellRecordList;//卖出商品数据链表
list<record> buyRecordList;//买入商品数据链表
//成员函数
public:
bool GetInformathion(int index);//获取并输出商品信息
void GetIndex();//获取并输出商品目录
bool init();//从本地文件获取商品信息
void save();//将商品信息保存到本地文件中
bool Buy(int ID,int count);//买入商品的操作与数据检查
bool Sell(int ID, int count);//售出商品的操作与数据检查
void AddNew(char name[],float buyValue,float sellValue);//添加新的商品
/*获得商品的购入和销售记录*/
void getSellRecord();
void getBuyRecord();
};
trade.cpp
#include "stdafx.h"
#include "trade.h"
#include <fstream>
#include <time.h>
#define CAP 5
#define TLEN 10
#define ALEN 30
using std::ofstream;
using std::ifstream;
bool trade::Buy(int ID,int count)
{
for(auto iter = dataList.begin();iter!=dataList.end();iter++)
{
if(iter->getID() == ID)
{
iter->UpdateStock(count);
time_t t =time(0);//记录当前系统时间
char temp[50];
strftime(temp,sizeof(temp),"%Y年%m月%d日%X%A",localtime(&t));//指定系统时间的输出格式并存入temp变量中
buyRecordList.push_back(record(iter->name,count,temp));//将记录添加至买入商品数据链表尾部
return true;
}
}
return false;
}
bool trade::Sell(int ID,int count)
{
for(auto iter = dataList.begin();iter!=dataList.end();iter++)
{
if(iter->getID() == ID && !(iter->getStock()+count<0))//当前库存不小于要卖出的数量
{
iter->UpdateStock(-count);
time_t t=time(0);
char temp[50];
strftime(temp,sizeof(temp),"%Y年%m月%d日%X%A",localtime(&t));
sellRecordList.push_back(record(iter->name,count,temp));
return true;
}
}
return false;
}
void trade::AddNew(char name[],float buyValue,float sellValue)
{
dataList.push_back(commodity(name,buyValue, sellValue));
}
void trade::GetIndex()
{
for(auto iter = dataList.begin();iter!=dataList.end();iter++)
{
printf("\t 商品编号: %i 商品名称: %s\n",iter->getID(),iter->getName());
}
};
bool trade::GetInformathion(int index)
{
for(auto iter = dataList.begin();iter!=dataList.end();iter++)
{
if(iter->getID() == index )
{
printf("商品编号:%d\n商品名称:%s\n购入价格:%f\n出售价格:%f\n剩余:%d\n",
index,
iter->getName(),
iter->getBuyValue(),
iter->getsellValue(),
iter->getStock());
return true;
}
}
return false;
}
void trade::save()
{
ofstream file;
file.open("stock.txt");
if(!file.fail())
{
file<<" ════════════════════════════ \n";
file<<" ***** 商品信息 ***** \n";
file<<" ════════════════════════════ \n";
for(auto iter = dataList.begin();iter!=dataList.end();iter++)
{
file<<"ID:"<<iter->getID()
<<"\tNAME:"<<iter->getName()
<<"\tCOST:"<<iter->getBuyValue()
<<"\tVALUE:"<<iter->getsellValue()
<<"\tSTOCK:"<<iter->getStock()
<<"\n";
}
}
else
{
printf("记录文件创建失败");
}
file.close();
file.open("sellRecord.txt");
if(!file.fail())
{
file<<" ════════════════════════════ \n";
file<<" ***** 销售信息 ***** \n";
file<<" ════════════════════════════ \n";
for(auto iter = sellRecordList.begin();iter!=sellRecordList.end();iter++)
{
file<<"NAME:"<<iter->name
<<"\tTIME:"<<iter->sTime
<<"\tCOUNT:"<<iter->count
<<"\n";
}
}
else
{
printf("销售记录文件创建失败");
}
file.close();
file.open("buyRecord.txt");
if(!file.fail())
{
file<<" ════════════════════════════ \n";
file<<" ***** 购入信息 ***** \n";
file<<" ════════════════════════════ \n";
for(auto iter = buyRecordList.begin();iter!=buyRecordList.end();iter++)
{
file<<"NAME:"<<iter->name
<<"\tTIME:"<<iter->sTime
<<"\tCOUNT:"<<iter->count
<<"\n";
}
}
else
{
printf("购入记录文件创建失败");
}
}
bool trade::init()
{
ifstream file("stock.txt");
if(!file.fail())
{
char titles[CAP][TLEN]={"ID:","NAME:","COST:","VALUE:","STOCK:"};
char saves [CAP][ALEN]={};
int tIndex = 0;
char buf[128];
int kinds = 0;//商品种类计数
for(int i=0;i<3;i++) //忽略标题
{
file.getline(buf,128);
}
while(!file.eof())
{
char temp[TLEN]="";//读取文件内容的字符数组
for (int i=0;i<TLEN&&!file.eof();i++)//for NO.1
{
file.get(temp[i]);
if(strcmp(temp,titles[tIndex])==0)
{
for(int j=0;j<ALEN&&!file.eof();j++)//for NO.2
{
char c;
file.get(c);
if(c!='\t'&&c!='\n')
{
saves[tIndex][j] = c;
}
else if(c!='\n')
{
if(tIndex>4)
{
return false;//行参数结尾后仍然有字符存在,失效
}
tIndex++;
break; //break NO.2
}
else
{
dataList.push_back(commodity(atoi(saves[0]),saves[1],
atof(saves[2]),atof(saves[3]),atoi(saves[4])));
tIndex=0;
kinds++;
break; //break NO.2
}
if(j==ALEN-1)
{
return false;//超过了参数长度,初始化失败
}
}//end NO.2
break;//break NO.1
}
if(i==TLEN-1)
{
return false; //没有匹配到参数名称,初始化失败
}
}//end NO.1
}//end While,读取结束
commodity::kinds=kinds ;
return true;
}
return false;//文件不存在,初始化失败
}
void trade::getSellRecord()
{
for(auto iter = sellRecordList.begin();iter!=sellRecordList.end();iter++)
{
printf("\t出售商品名称:%s\n",iter->name);
printf("\t交易日期:%s\n",iter->sTime);
printf("\t出售数目:%d\n",iter->count);
printf("\t ════════════════════════════ \n");
}
}
void trade::getBuyRecord()
{
for(auto iter = buyRecordList.begin();iter!=buyRecordList.end();iter++)
{
printf("\t购入商品名称:%s\n",iter->name);
printf("\t交易日期:%s\n",iter->sTime);
printf("\t购入数目:%d\n",iter->count);
printf("\t ════════════════════════════ \n");
}
}
main.cpp
// 13.1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "trade.h"
#include <iostream>
using namespace::std;
void ToBuy(int& ID,int& count)
{
cout<<"请输入购买商品的编号"<<endl;
cin>>ID;
getchar();
cout<<"请输入购买商品的数量:"<<endl;
cin>>count;
getchar();
}
void ToSell(int& ID,int& count)
{
cout<<"请输入卖出商品的编号"<<endl;
cin>>ID;
getchar();
cout<<"请输入卖出商品的数量:"<<endl;
cin>>count;
getchar();
}
void operate()
{
printf("按任意键继续");
getchar();
system("cls");
}
void drawIndex()
{
system("cls");
printf("\t ════════════════════════════ \n");
printf("\t ***** 商品目录 ***** \n");
printf("\t ════════════════════════════ \n");
}
void drawLine()
{
printf("\t ════════════════════════════ \n");
}
void DrawMainMenu()
{
printf("\t--------------------------------------------------------\n");
printf("\t| 欢迎使用商品销售系统 |\n");
printf("\t|------------------------------------------------------|\n");
printf("\t| 1 - 购进商品 |\n");
printf("\t| 2 - 卖出商品 |\n");
printf("\t| 3 - 添加新品 |\n");
printf("\t| 4 - 查看商品信息 |\n");
printf("\t| 5 - 查看采购记录 |\n");
printf("\t| 6 - 查看销售记录 |\n");
printf("\t| 7 - 退出 |\n");
printf("\t--------------------------------------------------------\n");
}
void drawBuyRecord()
{
system("cls");
printf("\t ════════════════════════════ \n");
printf("\t ***** 采购记录 ***** \n");
printf("\t ════════════════════════════ \n");
}
void drawSellRecord()
{
system("cls");
printf("\t ════════════════════════════ \n");
printf("\t ***** 销售记录 ***** \n");
printf("\t ════════════════════════════ \n");
}
int main(int argc, _TCHAR* argv[])
{
trade myTrade;
if(!myTrade.init())
{
myTrade = trade();
}
bool quitFlag =false;
while(!quitFlag)
{
DrawMainMenu();
printf("请输入您的选项:");
int selection;
cin>>selection;
getchar();
int ID;
int count;
switch(selection)
{
case 1:
drawIndex();
myTrade.GetIndex();
drawLine();;
ToBuy(ID,count);
if(myTrade.Buy(ID,count))
{
system("cls");
printf("操作成功,");
}
else
{
system("cls");
printf("您的输入有误,");
}
operate();
break;
case 2:
drawIndex();
myTrade.GetIndex();
drawLine();
ToSell(ID,count);
if(myTrade.Sell(ID,count))
{
system("cls");
printf("操作成功,");
}
else
{
system("cls");
printf("您的输入有误,");
}
operate();
break;
case 3:
char name[30];
float value;
float cost;
system("cls");
cout<<"请输入新品的名称"<<endl;
cin>>name;
getchar();
cout<<"请输入购入价格"<<endl;
cin>>cost;
getchar();
cout<<"请输入出售价格"<<endl;
cin>>value;
getchar();
myTrade.AddNew(name,cost,value);
system("cls");
printf("操作成功,");
operate();
break;
case 4:
drawIndex();
myTrade.GetIndex();
drawLine();
cout<<"请输入商品编号:";
int index;
cin>>index;
getchar();
system("cls");
if(!myTrade.GetInformathion(index))
{
cout<<"无效的商品编号,";
operate();
}
else{
operate();
}
break;
case 5:
drawBuyRecord();
myTrade.getBuyRecord();
operate();
break;
case 6:
drawSellRecord();
myTrade.getSellRecord();
operate();
break;
case 7:
quitFlag = true;
break;
default:
system("cls");
printf("无效的选项,");
operate();
}
}
myTrade.save();
return 0;
}
stadfx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
stadfx.cpp
// stdafx.cpp : 只包括标准包含文件的源文件
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
targetver.h
#pragma once
// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。
// 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将
// WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。
#include <SDKDDKVer.h>
按照书上敲得
转载自原文链接, 如需删除请联系管理员。
原文链接:C++入门编程实战(二)商品销售系统,转载请注明来源!