展示商品ID、名字,进价,售价,储量
功能,购入商品,售出商品,添加新商品,查询商品,购买记录,售出记录,退出。
且通过文件写入txt文本
交易类 trade.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "trade.h"
#include<fstream>
using namespace std;
#define TAIL 3
#define CAP 5
#define TLEN 10
#define ALEN 30
bool trade::GetInformation(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::GetIndex()
{
for (auto iter = dataList.begin(); iter != dataList.end(); iter++)
{
printf("\t 商品编号: %i 商品名称:%s\n", iter->getID(), iter->getName());
}
}
bool trade::init()
{
int flag = 0;
ifstream file("stock.txt");
if (!file.fail())
{
char titles[CAP][TLEN] = { "ID:","NAME:","COST:","VALUE:","STOCK:" };
char saves[CAP][TLEN] = {};
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++)
{
file.get(temp[i]);
if (strcmp(temp, titles[tIndex]) == 0)
{
for (int j = 0; j < ALEN && !file.eof(); j++)
{
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;
}
else
{
dataList.push_back(commodity(atoi(saves[0]) ,saves[1],
atof(saves[2]), atof(saves[3]), atoi(saves[4])));
tIndex = 0;
kinds++;
break;
}
if (j == ALEN - 1)//超过参数长度,失败
{
return false;
}
}
break;
}
if (i == TLEN - 1)//到字符串结束符了,没匹配到参数名称,失败
{
return false;
}
}
}
commodity::setKinds(kinds);
flag++;
}
file.close();
file.open("sellrecord.txt");
if (file.is_open())
{
//printf("open ok");
//system("pause");
char titles[TAIL][TLEN] = { "NAME:","TIME:","COUNT:" };
char saves[TAIL][ALEN] = {};
int tIndex = 0;
char buf[128];
int times = 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++)
{
file.get(temp[i]);
if (strcmp(temp, titles[tIndex]) == 0)//读到了titles里的其中一个
{
for (int j = 0; j < ALEN && !file.eof(); j++)
{
char c;
file.get(c);
if (c != '\t' && c != '\n')
{
saves[tIndex][j] = c;
}
else if (c != '\n')
{
if (tIndex > 2)
{
return false;
}//行参数结尾
tIndex++;
break;//跳出j循环
}
else
{
sellRecordList.push_back(record(saves[0], saves[1],atoi(saves[2])));
tIndex = 0;
times++;
break;
}
if (j == ALEN - 1)
{
return false;
}
}
break;
}
if (i == TLEN - 1)
{
return false;
}
}
}
flag++;
}
file.close();
file.open("buyrecord.txt");
if (file.is_open())
{
//printf("open ok");
//system("pause");
char titles[TAIL][TLEN] = { "NAME:","TIME:","COUNT:" };
char saves[TAIL][ALEN] = {};
int tIndex = 0;
char buf[128];
int times = 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++)
{
file.get(temp[i]);
if (strcmp(temp, titles[tIndex]) == 0)
{
for (int j = 0; j < ALEN && !file.eof(); j++)
{
char c;
file.get(c);
if (c != '\t' && c != '\n')
{
saves[tIndex][j] = c;
}
else if (c != '\n')
{
if (tIndex > 2)
{
return false;
}
tIndex++;
break;
}
else
{
buyRecordList.push_back(record(saves[0],saves[1],atoi(saves[2])));
tIndex = 0;
times++;
break;
}
if (j == ALEN - 1)
{
return false;
}
}
break;
}
if (i == TLEN - 1)
{
return false;
}
}
}
flag++;
}
//printf("flag=%d", flag);
if (flag == 3)
return true;
else
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("购入文件创建失败");
}
file.close();
}
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[30];
strftime(temp, sizeof(temp), "%Y-%m-%d %H:%M:%S", localtime(&t));
buyRecordList.push_back(record(iter->getName(), temp, count));
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[30];
strftime(temp, sizeof(temp), "%Y-%m-%d %H:%M:%S", localtime(&t));
sellRecordList.push_back(record(iter->getName(), temp, count));
return true;
}
}
return false;
}
void trade::AddNew(char name[], float buyValue, float sellValue)
{
dataList.push_back(commodity(name, buyValue, sellValue));
}
void trade::getSellRecord()
{
int temp=0;
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");
temp++;
}
printf("一共有%d条销售记录", temp);
}
void trade::getBuyRecord()
{
int temp = 0;
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");
temp++;
}
printf("一共有%d条购买记录", temp );
}
交易类头文件 trade.h
//#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include<list>
#include"commodity.h"
using namespace std;
class trade
{
public:
struct record
{
char name[30] ;
char sTime[30];
int count;
record(char* name, char* time, int count)
{
strncpy_s(this->name, sizeof(this->name), name, _TRUNCATE);
this->count = count;
strncpy_s(sTime, sizeof(sTime), time, _TRUNCATE);
}
};
bool GetInformation(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();
private:
list<commodity>dataList;//商品数据链表
list<record>sellRecordList;
list<record>buyRecordList;
};
商品类 commodity.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "commodity.h"
#include<iostream>
commodity::commodity(char name[], float buyValue, float sellValue)
{
kinds++;
ID = kinds;
this->stock = 0;
this->buyValue = buyValue;
this->sellValue = sellValue;
strcpy_s(this->name, name);
}
commodity::commodity(int ID, char name[], float buyValue, float sellValue, int stock)
{
this->ID = ID;
this->stock = stock;
strcpy(this->name, name);
this->sellValue = sellValue;
this->buyValue = buyValue;
}
int commodity::kinds = 0;
int commodity::getID()
{
return ID;
}
int commodity::getStock()
{
return stock;
}
float commodity::getBuyValue()
{
return buyValue;
}
float commodity::getsellValue()
{
return sellValue;
}
char* commodity::getName()
{
return name;
}
void commodity::UpdateStock(int n)
{
stock += n;
}
void commodity::setKinds(int value)
{
kinds = value;
}
商品类头文件 commodity.h
#pragma once
class commodity
{
public:
commodity(char name[], float buyValue, float sellValue);
commodity(int ID,char name[], float buyValue, float sellValue,int stock);
int getID();
int getStock();
float getBuyValue();
float getsellValue();
char* getName();
void UpdateStock(int n);
static void setKinds(int value);
private:
int static kinds;//商品种类
int ID;//商品货号
int stock;//库存量
float buyValue;//进货的价格
float sellValue;//卖出的价格
char name[30];//商品名称
};
main.cpp
#include <iostream>
#include"trade.h"
#include <tchar.h>
using namespace std;
void ToBuy(int& ID, int& count)
{
char ch;
cout << "请输入购买商品的编号" << endl;
cin >> ID;
ch=getchar();
cout << "请输入购买商品的数量" << endl;
cin >> count;
ch=getchar();
}
void ToSell(int& ID, int& count)
{
char ch;
cout << "请输入卖出的商品的编号" << endl;
cin >> ID;
ch=getchar();
cout << "请输入卖出商品的数量" << endl;
cin >> count;
ch=getchar();
}
void operate()
{
char ch;
printf("按任意键继续");
ch=getchar();
system("cls");
}
void drawIndex()
{
system("cls");
cout << "\t===========================================\n";
cout << "\t *****商品信息***** \n";
cout << "\t===========================================\n";
}
void drawLine()
{
cout << "\t===========================================\n";
}
void drawBuyRecord()
{
system("cls");
cout << "\t===========================================\n";
cout << "\t *****采购记录***** \n";
cout << "\t===========================================\n";
}
void drawSellRecord()
{
system("cls");
cout << "\t===========================================\n";
cout << "\t *****销售记录***** \n";
cout << "\t===========================================\n";
}
void drawMainMenu()
{
cout << "\t===========================================\n";
cout << "\t 欢迎使用商品销售系统 \n";
cout << "\t===========================================\n";
cout << "\t 1 - 购进商品 \n";
cout << "\t 2 - 卖出商品 \n";
cout << "\t 3 - 添加商品 \n";
cout << "\t 4 - 查看商品信息 \n";
cout << "\t 5 - 查看采购记录 \n";
cout << "\t 6 - 查看销售记录 \n";
cout << "\t 7 - 退出 \n";
cout << "\t===========================================\n";
}
int main(int argc, _TCHAR* argv[])
{
trade myTrade;
if (!myTrade.init())
{
myTrade = trade();
}
bool quitFlag = false;
while (!quitFlag)
{
char ch;
drawMainMenu();
printf("请输入你的选项:");
int selection;
cin >> selection;
ch = 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;
ch = getchar();
cout << "请输入购入的价格" << endl;
cin >> cost;
ch = getchar();
cout << "请输入出售的价格" << endl;
cin >> value;
ch = getchar();
myTrade.AddNew(name, cost, value);
system("cls");
printf("操作成功,");
operate();
break;
}
case 4: {
drawIndex();
myTrade.GetIndex();
drawLine();
cout << "请输入商品编号:";
int index;
cin >> index;
ch = getchar();
system("cls");
if (!myTrade.GetInformation(index))
{
cout << "无效的商品编号,";
operate();
}
else
{
operate();
}
break;
}
case 5: {
drawBuyRecord();
myTrade.GetIndex();
drawLine();
myTrade.getBuyRecord();
ch = getchar();
system("cls");
break;
}
case 6: {
drawSellRecord();
myTrade.GetIndex();
drawLine();
myTrade.getSellRecord();
ch = getchar();
system("cls");
break;
}
case 7: {
quitFlag = true;
break;
}
default: {
system("cls");
printf("无效的选项,");
operate();
}
}
}
myTrade.save();
return 0;
}
演示: