本系统基于c++面向对象以及一些基础的算法来实现 。
不多说,直接上代码。
头文件以及初始数据:
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string.h>
#include<time.h>
#include<stdio.h>
using namespace std;
int n;
int logCount=0;
数据结构:
//商品结构体
struct commodity
{
string name;//商品名称
char location[200];//商品产地
float price;//商品单价
int amount;//商品数量
int year;//商品生产日期
int month;
int day;
long long int address;//商品代码
int shelflife; //商品保质期大小
string date;//商品保质期单位
int inyear,inmonth, inday, inhour, inminute, insecond;//入库详细时间
};
//日志模块
struct log {
int address2;
string name1;
char location1[200];
int amount1;
int exyear, exmonth, exday, exhour, exminute, exsecond;
};
类的定义以及功能函数:
class Goods
{
private:
struct commodity goodslist[100000];//商品结构体数组
struct log logs[100000];//商品日志结构体数组
public:
void init();//初始化
void adds();//商品数量添加
void addl();//商品种类添加
void delete1();//商品删除
int login();//登陆部分
void CTRLmenu();//管理员界面
void SWGmenu();//进货员界面
void goodsmenu();//商品展示
int user();//用户选择
void purchase();//模拟购买系统
void buylist();//商品列表
void logshow();//日志展示
int freshnessdate();//保质期
int judgedate(int x,int y,int z);//判断生产日期是否合理
int limitnumber(int x, int y); //限制程序崩溃模块
void change();//改变商品信息
};
各个函数功能模块:
//身份选择:
int Goods::user()
{
int identity;
cout << "-----请选择你的身份:-----" << endl;
cout << "-----1.管理员-----" << endl;
cout << "-----2.进货员-----" << endl;
time_t nowtime;
time(&nowtime);
tm *p = localtime(&nowtime);
printf("%04d:%02d:%02d %02d:%02d:%02d\n", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min,p->tm_sec);//显示时间;
cin >> identity;
return identity;
}
//管理员菜单:
void Goods::CTRLmenu()
{
cout<<" 管理者界面 ";
cout<<endl;
cout<<"-------------------------------------------------------------------------------------------------------------------";
cout<<endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~1.商品初始化~~~~~~~~~~~~~~~~~~"<<" |";
cout << endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~2.商品数量添加~~~~~~~~~~~~~~~~"<<" |";
cout << endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~3.商品种类添加~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~4.商品删除~~~~~~~~~~~~~~~~~~~~"<<" |";
cout << endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~5.查看商品信息~~~~~~~~~~~~~~~~"<<" |";
cout << endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~6.模拟购买系统~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~7.查看销售日志~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~8.商品信息更改~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~9.退出系统~~~~~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout<<"-------------------------------------------------------------------------------------------------------------------";
cout<<endl;
cout << "输入你的选择:";
cout << endl;
}
//进货员界面
void Goods::SWGmenu()
{
cout<<" 进货员界面 ";
cout<<endl;
cout<<"-------------------------------------------------------------------------------------------------------------------";
cout<<endl;
cout<<"|"<<'\t'<<'\t'<<'\t'<<'\t'<<"~~~~~~~~~~~~~~~~~~~~1.商品数量添加~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout<<"|"<<'\t'<<'\t'<<'\t'<<'\t'<<"~~~~~~~~~~~~~~~~~~~~2.模拟购买系统~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout<<"|"<<'\t'<<'\t'<<'\t'<<'\t'<<"~~~~~~~~~~~~~~~~~~~~3.商品种类添加~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout<<"|"<<'\t'<<'\t'<<'\t'<<'\t'<<"~~~~~~~~~~~~~~~~~~~~4.退出系统~~~~~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout<<"-------------------------------------------------------------------------------------------------------------------";
cout<<endl;
cout << "输入你的选择:";
cout << endl;
}
//限制数字功能,防止输入错误使程序崩溃
int Goods::limitnumber(int x, int y)
{
int X;//存储用户的输入
while (1)
{
cin >> X;
if (cin.fail())//如果输入的不是整型
{
cout << "非整数!请重新输入:";
cin.clear();
cin.ignore(1024, '\n');//清除缓冲区
}
else if (X<x || X>y)//整数不在范围内
{
cout << "非范围内!请重新输入:";
}
else
return X;
}
}
//删除商品
void Goods::delete1()
{
string name;
cout<<"请输入你要删除的商品名称:";
cin>>name;
int flag=0;
int i,id;
int j;
for(i=0;i<n;i++)
{
if(name==goodslist[i].name)
{
flag=1;
id=i;
break;
}
}
if(id!=n-1&&flag==1)
{
for(j=id;j<n;j++)
{
goodslist[j]=goodslist[j+1];
}
--n;
cout<<"已删除完成!!"<<endl;
}
else if(id==n-1&&flag==1)
{
--n;
cout<<"已删除完成!!"<<endl;
}
else
{
cout<<"未找到商品,无法删除!!"<<endl;
}
cout<<endl;
system("pause");
system("cls");
}
//初始化商品
void Goods::init()
{
cout << "请输入你要添加商品种类数量:" << endl;
int i,m,l;
cin >> n;
cout <<"商品代码------"<< "商品名称------" << "商品产地------" << "商品单价------" << "商品数量------" << "商品生产日期------" <<"保质期"<<
endl;
for (i = 0; i < n; i++) {
cin>>goodslist[i].address >> goodslist[i].name >> goodslist[i].location >> goodslist[i].price >> goodslist[i].amount >> goodslist[i].year >>
goodslist[i].month >> goodslist[i].day;
while(1)
{
l=judgedate(goodslist[i].year,goodslist[i].month,goodslist[i].day);
if(l==0)
{
cout<<"日期输入有误,请重新输入!"<<endl;
cin>>goodslist[i].year >>goodslist[i].month >> goodslist[i].day;
}
else
{
break;
}
}
m=freshnessdate();
if(m==1)
{
cin>>goodslist[i].shelflife;
goodslist[i].date="年";
}
else if(m==2)
{
cin>>goodslist[i].shelflife;
goodslist[i].date="月";
}
else
{
cin>>goodslist[i].shelflife;
goodslist[i].date="天";
}
time_t nowtime;
time(&nowtime);
tm *p = localtime(&nowtime);
goodslist[i].inyear=p->tm_year+1900;
goodslist[i].inmonth=p->tm_mon + 1;
goodslist[i].inday=p->tm_mday;
goodslist[i].inhour=p->tm_hour;
goodslist[i].inminute=p->tm_min;
goodslist[i].insecond=p->tm_sec;
}
cout << '\t' << '\t' << "已初始化完成" << '\t' << '\t' << endl;
system("pause");
system("cls");
}
//判断日期是否合理
int Goods::judgedate(int x,int y,int z)
{
if(y==1||y==3||y==5||y==7||y==8||y==10||y==12)
{
if(z>31||z<1)
{
return 0;
}
}
if(y==4||y==6||y==9||y==11)
{
if(z<1||z>30)
{
return 0;
}
}
if((x % 4 == 0 && x % 100 != 0 || x % 400 == 0)&&y==2)
{
if(z<1||z>29)
{
return 0;
}
}
if((x % 4 != 0 && x % 100 == 0 || x % 400 != 0)&&y==2)
{
if(z<1||z>28)
{
return 0;
}
}
}
//商品数量添加
void Goods::adds()
{
long long int address1;
string name;
int i;
int id;
int flag=0;
int addnumber;
int choice1;
cout<<"请选择添加方式:"<<endl;
cout<<"1.通过商品代码操作"<<endl;
cout<<"2.通过商品名称操作"<<endl;
cin>>choice1;
if(choice1==1)
{
cout << "请输入你要添加的商品代码:" << endl;
cin>>address1;
cout << "请输入你要添加的商品的数量:" << endl;
cin>>addnumber;
for(i=0;i<n;i++)
{
if(goodslist[i].address==address1)
{
flag=1;
id=i;
break;
}
}
if(flag==1)
{
goodslist[id].amount+=addnumber;
cout<<"添加完成!!!"<<endl;
}
else
{
cout<<"未找到此商品,请在商品种类添加模块进行添加"<<endl;
}
}
if(choice1==2)
{
cout << "请输入你要添加的商品名称:" << endl;
cin>>name;
cout << "请输入你要添加的商品的数量:" << endl;
cin>>addnumber;
for(i=0;i<n;i++)
{
if(goodslist[i].name==name)
{
flag=1;
id=i;
break;
}
}
if(flag==1)
{
goodslist[id].amount+=addnumber;
cout<<"添加完成!!!"<<endl;
}
else
{
cout<<"未找到此商品,请在商品种类添加模块进行添加"<<endl;
}
}
system("pause");
system("cls");
}
//商品种类添加,其实就是改变商品总数量n
void Goods::addl()
{
int i,m;
cout<<"请输入需要添加的商品种类的数量:"<<endl;
int num;
int j;
cin>>num;
j=n;
n=n+num;
cout <<"商品代码------"<< "商品名称------" << "商品产地------" << "商品单价------" << "商品数量------" << "商品生产日期------" <<"保质期"<<
endl;
for (i = j; i < n; i++) {
cin>>goodslist[i].address >> goodslist[i].name >> goodslist[i].location >> goodslist[i].price >> goodslist[i].amount >> goodslist[i].year >>
goodslist[i].month >> goodslist[i].day;
m=freshnessdate();
if(m==1)
{
cin>>goodslist[i].shelflife;
goodslist[i].date="年";
}
else if(m==2)
{
cin>>goodslist[i].shelflife;
goodslist[i].date="月";
}
else
{
cin>>goodslist[i].shelflife;
goodslist[i].date="天";
}
time_t nowtime;
time(&nowtime);
tm *p = localtime(&nowtime);
goodslist[i].inyear=p->tm_year+1900;
goodslist[i].inmonth=p->tm_mon + 1;
goodslist[i].inday=p->tm_mday;
goodslist[i].inhour=p->tm_hour;
goodslist[i].inminute=p->tm_min;
goodslist[i].insecond=p->tm_sec;
}
cout<<"已添加完成!!"<<endl;
system("pause");
system("cls");
}
//保质期单位
int Goods::freshnessdate()
{
cout<<"请选择商品保质期单位:"<<endl;
cout<<"1.年";
cout<<endl;
cout<<"2.月";
cout<<endl;
cout<<"3.天";
cout<<endl;
int choice;
choice=limitnumber(1,3);
cout<<"请输入保质期具体大小:"<<endl;
return choice;
}
//商品查看
void Goods::goodsmenu()
{
int i;
int id;
cout<<"请选择你要查看商品的方式:";
cout<<endl;
cout<<"1.单独查看";
cout<<endl;
cout<<"2.整体查看";
cout<<endl;
int choice;
cout<<"请输入:";
cin>>choice;
if(choice==1)
{
while(1)
{
int flag=0;
cout<<"请选择查看方式:"<<endl;
cout<<"1.商品代码查看"<<endl;
cout<<"2.商品名称查看"<<endl;
cout<<"3.退出."<<endl;
int choice1;
choice1=limitnumber(1,3);
if(choice1==3)
{
break;
}
if(choice1==1)
{
cout<<"请输入要查看商品的代码:";
long long int address2;
cin>>address2;
for(i=0;i<n;i++)
{
if(address2==goodslist[i].address)
{
flag=1;
id=i;
break;
}
}
//
if(flag==1)
{
cout<<"-----------------------------------------------------------------商品信息查看---------------------------------------------------------------------------"<<endl;
cout <<"商品代码"<<"--"<< "商品名称"<<"--" << "商品产地"<<"--"<< "商品单价" <<"--"<< "商品数量"<<"--" << "商品生产日期"<<"--"<<"保质期"<<"--"<<"最新入库时间"<<"--"<<"总价值"<<endl;
cout<<goodslist[id].address<<'\t'<<goodslist[id].name <<'\t'<< goodslist[id].location <<'\t'<< goodslist[id].price <<'\t'<< goodslist[id].amount<<'\t' << goodslist[id].year<<"/" <<
goodslist[id].month <<"/"<< goodslist[id].day<<'\t'<<goodslist[id].shelflife<<goodslist[id].date<<'\t'<<goodslist[id].inyear<<"/"<<goodslist[id].inmonth<<"/"<<goodslist[id].inday<<" "<<goodslist[id].inhour<<":"<<goodslist[id].inminute<<":"<<goodslist[id].insecond<<'\t'<<goodslist[id].amount*goodslist[id].price;
cout<<endl;
}
else
{
cout<<"未找到此商品!!!";
}
//
}
if(choice1==2)
{
string name;
cout<<"请输入你要查看的商品的名称:";
cin>>name;
for(i=0;i<n;i++)
{
if(name==goodslist[i].name)
{
flag=1;
id=i;
break;
}
}
//
if(flag==1)
{
cout<<"----------------------------------------------------------------------商品信息查看-----------------------------------------------------------------------------------"<<endl;
cout <<"商品代码"<<"--"<< "商品名称"<<"--" << "商品产地"<<"--"<< "商品单价" <<"--"<< "商品数量"<<"--" << "商品生产日期"<<"--"<<"保质期"<<"--"<<"最新入库时间"<<"--"<<"总价值"<<endl;
cout<<goodslist[id].address<<'\t'<<goodslist[id].name <<'\t'<< goodslist[id].location <<'\t'<< goodslist[id].price <<'\t'<< goodslist[id].amount<<'\t' << goodslist[id].year<<"/" <<
goodslist[id].month <<"/"<< goodslist[id].day<<'\t'<<goodslist[id].shelflife<<goodslist[id].date<<'\t'<<goodslist[id].inyear<<"/"<<goodslist[id].inmonth<<"/"<<goodslist[id].inday<<" "<<goodslist[id].inhour<<":"<<goodslist[id].inminute<<":"<<goodslist[id].insecond<<'\t'<<goodslist[id].amount*goodslist[id].price;
cout<<endl;
}
else
{
cout<<"未找到此商品!!!";
}
//
}
}
}
if(choice==2)
{
cout<<"----------------------------------------------------------------------商品信息查看-----------------------------------------------------------------------------------"<<endl;
cout <<"商品代码"<<"--"<< "商品名称"<<"--" << "商品产地"<<"--"<< "商品单价" <<"--"<< "商品数量"<<"--" << "商品生产日期"<<"--"<<"保质期"<<"--"<<"最新入库时间"<<"--"<<"总价值"<<endl;
for(i=0;i<n;i++)
{
cout<<goodslist[i].address<<'\t'<<goodslist[i].name <<'\t'<< goodslist[i].location <<'\t'<< goodslist[i].price <<'\t'<< goodslist[i].amount<<'\t' << goodslist[i].year<<"/" <<
goodslist[i].month <<"/"<< goodslist[i].day<<'\t'<<goodslist[i].shelflife<<goodslist[i].date<<'\t'<<goodslist[i].inyear<<"/"<<goodslist[i].inmonth<<"/"<<goodslist[i].inday<<" "<<goodslist[i].inhour<<":"<<goodslist[i].inminute<<":"<<goodslist[i].insecond<<'\t'<<goodslist[i].amount*goodslist[i].price;
cout<<endl;
}
cout<<endl;
}
system("pause");
system("cls");
}
//登陆系统
int Goods::login()
{
char code1[1024] = "";
char pwd1[1024] = "";
cout<<"当前时间:";
time_t nowtime;
time(&nowtime);
tm *p = localtime(&nowtime);
printf("%04d:%02d:%02d %02d:%02d:%02d\n", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min,
p->tm_sec);//显示时间;
while (1)
{
cout<<"1.登录 2.注册 3.退出"<<endl;;
int m;
m=limitnumber(1,3);
char code2[1024];
char pwd2[1024];
if (m == 1)
{
if (code1[0] == '\0')
{
cout<<"账号为空,请先注册账号"<<endl;;
continue;
}
cout<<"请输入登录的账号"<<endl;
scanf("%s", code2);
cout<<"请输入登录的密码"<<endl;
scanf("%s", pwd2);
if ((strcmp(code1, code2) == 0) && (strcmp(pwd1, pwd2) == 0))
{
printf("登录成功\n");
system("pause");
system("cls");
return 1;
}
else
{
if (strcmp(code1, code2) != 0)
{
cout<<"登录失败,该账号不存在,请重新输入"<<endl;;
}
if (strcmp(pwd1, pwd2) != 0)
{
cout<<"登录失败,账号密码错误,请重新输入"<<endl;;
}
}
}
else if (m == 2)
{
cout<<"请输入注册的账号"<<endl;
scanf("%s", code1);
cout<<"请输入注册的密码"<<endl;
scanf("%s", pwd1);
cout<<"注册成功!!"<<endl;;
}
else if (m == 3)
{
cout<<"感谢使用,再见!";
return 0;
}
else
{
continue;
}
}
}
//购买时展示商品信息
void Goods::buylist()
{
cout<<"正在进入模拟购买系统......"<<endl;
int i;
cout<<"商品列表"<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout<<'\t'<<"序号"<<'\t'<<"名称"<<'\t'<<"产地"<<'\t'<<"单价"<<'\t'<<"数量"<<endl;
for(i=0;i<n;i++)
{
cout<<'\t'<<i+1<<'\t'<<goodslist[i].name<<'\t'<<goodslist[i].location<<'\t'<<goodslist[i].price<<'\t'<<goodslist[i].amount<<endl;;
}
cout<<"---------------------------------------------------------------"<<endl;
}
//模拟购买模块
void Goods::purchase()
{
int choice;
float count;
float pay, back;
float total,temp;
while (1) {
count = pay = back = total = back = 0;
buylist();
cout<<"请输入您购买的商品序号(输入0退出商品售卖):";
cin >> choice;
if (choice == 0)
{
return;
}
else if (choice < 0)
{
cout<<"您的输入有误!请重试!"<<endl;
cout<<"请输入您购买的商品序号(输入0退出商品售卖):";
cin >> choice;
if (choice < 0 || choice > n)
{
cout<<"您的输入连续有误,已退出商品售卖!"<<endl;
return;
}
}
else if (choice > n)
{
cout<<"未找到该商品!请重试!"<<endl;
cout<<"请输入您购买的商品序号(输入0退出商品售卖):"<<endl;
cin >> choice;
if (choice < 0 || choice > n)
{
cout<<"您的输入连续有误,已退出商品售卖!"<<endl;
return;
}
}
cout<<"请输入您购买的数量:";
cin >> count;
if (count > goodslist[choice - 1].amount)
{
cout<<"购买数量超过库存,购买失败! "<<endl;;
system("pause");
return;
}
pay = (goodslist[choice - 1].price) * count;
cout<<"应付款:"<<fixed<<setprecision(2)<<pay;
cout << endl;
{
cout << "请选择支付方式"<<endl;;
cout << "1.微信"<<endl;
cout << "2.支付宝"<<endl;
cout << "3.现金"<<endl;
cout << "4.刷卡"<<endl;
cout << "5.刷脸支付"<<endl;
cout << "6.刷手支付"<<endl;;
cout << "-------------\n";
cout << "请输入您的支付选择:";
}
int buychoice;
cin >> buychoice;
switch (buychoice)
{
case 1:
cout << "请打开微信支付:\n";
break;
case 2:
cout << "请打开支付宝支付:\n";
break;
case 3:
cout << "请支付现金:\n";
break;
case 4:
cout << "请刷卡支付:\n";
break;
case 5:
cout << "请刷脸支付: \n";
break;
case 6:
cout << "请刷手支付 \n";
break;
default:
cout<<"您未选择任何支付,请重试!已自动为您退出商品售卖";
return;
}
cout<<"应付款:"<<fixed<<setprecision(2)<<pay<<endl;
cout <<"请付款:";
cin>>temp;
if (temp >= pay)
{
back = temp - pay;
total = temp;
}
else
{
cout << "余额不足,支付失败!\n";
system("pause");
return;
}
cout<<"购买成功!找零"<<fixed<<setprecision(1)<<back<<"请取走您的商品。"<<endl;
goodslist[choice-1].amount-=count;
if(goodslist[choice-1].amount<10)
{
cout<<"商品即将卖完,请及时补货!!"<<endl;
}
logs[logCount].name1=goodslist[choice - 1].name;
strcpy(logs[logCount].location1, goodslist[choice - 1].location);
logs[logCount].address2=goodslist[choice-1].address;
logs[logCount].amount1 = count;
time_t nowtime;
time(&nowtime);
tm *p = localtime(&nowtime);
logs[logCount].exyear=p->tm_year+1900;
logs[logCount].exmonth=p->tm_mon + 1;
logs[logCount].exday=p->tm_mday;
logs[logCount].exhour=p->tm_hour;
logs[logCount].exminute=p->tm_min;
logs[logCount].exsecond=p->tm_sec;
logCount++;
system("pause");
system("cls");
cout << endl;
cout << " 超市发票 \n";
cout<<"\t商品名称:"<<goodslist[choice - 1].name;
cout<<endl;
cout<<"\t商品单价:"<<fixed<<setprecision(1)<< goodslist[choice - 1].price;
cout<<endl;
cout<<"\t商品数量:"<<count;
cout<<endl;
cout<<"\t应付款额:"<<pay;
cout<<endl;
cout<<"\t实际付款:"<<fixed<<setprecision(1)<< total;
cout<<endl;
cout<<"\t应找零钱:"<<fixed<<setprecision(1)<<back;
cout << endl;
printf("如果产品存在问题请在七日内进行调换或者退款 \n");
cout << "\t联系电话12345678900 \t\n";
cout<<"\t当前时间:";
time_t notime;
time(¬ime);
tm* q = localtime(¬ime);
printf("%04d:%02d:%02d %02d:%02d:%02d\n", q->tm_year + 1900, q->tm_mon + 1, q->tm_mday, q->tm_hour, q->tm_min, q->tm_sec);
cout << endl;
cout << endl;
printf("\t 谢谢惠顾! \t\n");
printf("\t欢迎下次光临 \t\n");
system("pause");
system("cls");
}
}
//商品信息改变
void Goods::change()
{
int choice;
int i,m;
long long int address1;
string name;
int flag=0;
int id;
cout<<"请选择你要更改商品信息的方式:";
cout<<endl;
cout<<"1.通过商品代码改变";
cout<<endl;
cout<<"2.通过商品名称改变";
cout<<endl;
cin>>choice;
if(choice==1)
{
cout<<"请输入你要改变的商品的代码:";
cin>>address1;
for(i=0;i<n;i++)
{
if(address1==goodslist[i].address)
{
flag=1;
id=i;
break;
}
}
if(flag==1)
{
cout<<"请选择你要改变的信息:";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~1.商品代码~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~2.商品名称~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~3.商品产地~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~4.商品单价~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~5.商品数量~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~6.商品生产日期~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~7.商品保质期~~~~~~~~~~~~~~~~~";
cout<<endl;
int choice1;
choice1=limitnumber(1,7);
if(choice1==1)
{
cout<<"请输入新代码:";
cin>>goodslist[id].address;
}
else if(choice1==2)
{
cout<<"请输入新名称:";
cin>>goodslist[id].name;
}
else if(choice1==3)
{
cout<<"请输入新产地:";
cin>>goodslist[id].location;
}
else if(choice1==4)
{
cout<<"请输入新单价:";
cin>>goodslist[id].price;
}
else if(choice1==5)
{
cout<<"请输入新数量:";
cin>>goodslist[id].amount;
}
else if(choice1==6)
{
cout<<"请输入新的生产日期:";
cin>>goodslist[id].year>>goodslist[id].month>>goodslist[id].day;
while(1)
{
int judge;
judge=judgedate(goodslist[id].year,goodslist[id].month,goodslist[id].day);
if(judge==0)
{
cout<<"输入的日期有误,请重新输入!";
cin>>goodslist[id].year>>goodslist[id].month>>goodslist[id].day;
}
else
{
break;
}
}
}
else if(choice1==7)
{
m=freshnessdate();
cout<<"请输入新的保质期:";
if(m==1)
{
cin>>goodslist[id].shelflife;
goodslist[id].date="年";
}
else if(m==2)
{
cin>>goodslist[id].shelflife;
goodslist[id].date="月";
}
else
{
cin>>goodslist[id].shelflife;
goodslist[id].date="天";
}
}
}
}
if(choice==2)
{
cout<<"请输入你要改变商品的名称:";
cin>>name;
for(i=0;i<n;i++)
{
if(name==goodslist[i].name)
{
flag=1;
id=i;
break;
}
}
if(flag==1)
{
cout<<"请选择你要改变的信息:";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~1.商品代码~~~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~2.商品名称~~~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~3.商品产地~~~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~4.商品单价~~~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~5.商品数量~~~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~6.商品生产日期~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~7.商品保质期~~~~~~~~~~~~~~~~~~~";
cout<<endl;
int choice2;
choice2=limitnumber(1,7);
if(choice2==1)
{
cout<<"请输入新的代码:";
cin>>goodslist[id].address;
}
else if(choice2==2)
{
cout<<"请输入新的名称:";
cin>>goodslist[id].name;
}
else if(choice2==3)
{
cout<<"请输入新的产地:";
cin>>goodslist[id].location;
}
else if(choice2==4)
{
cout<<"请输入新的单价:";
cin>>goodslist[id].price;
}
else if(choice2==5)
{
cout<<"请输入新的数量:";
cin>>goodslist[id].amount;
}
else if(choice2==6)
{
cout<<"请输入新的生产日期:";
cin>>goodslist[id].year>>goodslist[id].month>>goodslist[id].day;
while(1)
{
int judge;
judge=judgedate(goodslist[id].year,goodslist[id].month,goodslist[id].day);
if(judge==0)
{
cout<<"输入的日期有误,请重新输入!";
cin>>goodslist[id].year>>goodslist[id].month>>goodslist[id].day;
}
else
{
break;
}
}
}
else if(choice2==7)
{
m=freshnessdate();
cout<<"请输入新的保质期:";
if(m==1)
{
cin>>goodslist[id].shelflife;
goodslist[id].date="年";
}
else if(m==2)
{
cin>>goodslist[id].shelflife;
goodslist[id].date="月";
}
else
{
cin>>goodslist[id].shelflife;
goodslist[id].date="天";
}
}
}
}
cout<<"改变完成!!!";
system("pause");
system("cls");
}
void Goods::logshow()
{
int i;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~销售日志~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"**************************************************************************"<<endl;
cout<<'\t'<<"商品代码"<<'\t'<<"商品名称"<<'\t'<<"商品产地"<<'\t'<<"购买数量"<<'\t'<<"销售时间"<<endl;
for(i=0;i<logCount;i++)
{
cout<<'\t'<<logs[i].address2<<'\t'<<logs[i].name1<<'\t'<<logs[i].location1<<'\t'<<logs[i].amount1<<'\t'<<logs[i].exyear<<"/"<<logs[i].exmonth<<"/"<<logs[i].exday<<" "<<logs[i].exhour<<":"<<logs[i].exminute<<":"<<logs[i].exsecond<<endl;
}
cout<<"**************************************************************************"<<endl;
system("pause");
system("cls");
}
//主函数
Goods a;//定义一个商品对象
int main()
{
int choice, key, key1;
key1 = a.user();
if (key1 == 1)
{
cout << "——————————管理员登陆界面——————————" << endl;
key = a.login();
if (key == 1) {
while (1) {
a.CTRLmenu();
choice=a.limitnumber(1,9);
switch (choice)
{
case 1:
a.init();
break;
case 2:
a.adds();
break;
case 3:
a.addl();
break;
case 4:
a.delete1();
break;
case 5:
a.goodsmenu();
break;
case 6:
a.purchase();
break;
case 7:
a.logshow();
break;
case 8:
a.change();
break;
case 9:
cout << "感谢使用!!";
exit(0);
break;
default:
cout << "输入有误!!请重新输入" << endl;
break;
}
}
}
else
return 0; //if判断是否登陆成功
}//if判断身份
if (key1 == 2)
{
cout << "——————————进货员登陆界面——————————" << endl;
key = a.login();
if (key == 1)
{
while (1)
{
a.SWGmenu();
choice=a.limitnumber(1,4);
switch(choice)
{
case 1:
a.adds();
cout<<"已成功添加货物";
break;
case 2:
a.purchase();
break;
case 3:
a.addl();
break;
case 4:
cout<<"感谢使用!!";
exit(0);
break;
default:
cout << "输入有误,请重新输入!!" << endl;
}
}
}
else
return 0;
}
}
整个程序:
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string.h>
#include<time.h>
#include<stdio.h>
using namespace std;
int n;
int logCount=0;
//商品结构体模块
struct commodity
{
string name;//商品名称
char location[200];//商品产地
float price;//商品单价
int amount;//商品数量
int year;//商品生产日期
int month;
int day;
long long int address;//商品代码
int shelflife; //商品保质期大小
string date;//商品保质期单位
int inyear,inmonth, inday, inhour, inminute, insecond;//入库详细时间
};
//日志模块
struct log {
int address2;
string name1;
char location1[200];
int amount1;
int exyear, exmonth, exday, exhour, exminute, exsecond;
};
class Goods
{
private:
struct commodity goodslist[100000];
struct log logs[100000];
public:
void init();//初始化
void adds();//商品数量添加
void addl();//商品种类添加
void delete1();//商品删除
int login();//登陆部分
void CTRLmenu();//管理员界面
void SWGmenu();//进货员界面
void goodsmenu();//商品展示
int user();//用户选择
void purchase();//模拟购买系统
void buylist();//商品列表
void logshow();//日志展示
int freshnessdate();//保质期
int judgedate(int x,int y,int z);//判断生产日期是否合理
int limitnumber(int x, int y); //限制程序崩溃模块
void change();//改变商品信息
};
int Goods::user()
{
int identity;
cout << "-----请选择你的身份:-----" << endl;
cout << "-----1.管理员-----" << endl;
cout << "-----2.进货员-----" << endl;
time_t nowtime;
time(&nowtime);
tm *p = localtime(&nowtime);
printf("%04d:%02d:%02d %02d:%02d:%02d\n", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min,p->tm_sec);//显示时间;
cin >> identity;
return identity;
}
void Goods::CTRLmenu()
{
cout<<" 管理者界面 ";
cout<<endl;
cout<<"-------------------------------------------------------------------------------------------------------------------";
cout<<endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~1.商品初始化~~~~~~~~~~~~~~~~~~"<<" |";
cout << endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~2.商品数量添加~~~~~~~~~~~~~~~~"<<" |";
cout << endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~3.商品种类添加~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~4.商品删除~~~~~~~~~~~~~~~~~~~~"<<" |";
cout << endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~5.查看商品信息~~~~~~~~~~~~~~~~"<<" |";
cout << endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~6.模拟购买系统~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~7.查看销售日志~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~8.商品信息更改~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout <<"|"<<'\t'<<'\t'<<'\t'<<'\t'<< "~~~~~~~~~~~~~~~~~~~~9.退出系统~~~~~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout<<"-------------------------------------------------------------------------------------------------------------------";
cout<<endl;
cout << "输入你的选择:";
cout << endl;
}
void Goods::SWGmenu()
{
cout<<" 进货员界面 ";
cout<<endl;
cout<<"-------------------------------------------------------------------------------------------------------------------";
cout<<endl;
cout<<"|"<<'\t'<<'\t'<<'\t'<<'\t'<<"~~~~~~~~~~~~~~~~~~~~1.商品数量添加~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout<<"|"<<'\t'<<'\t'<<'\t'<<'\t'<<"~~~~~~~~~~~~~~~~~~~~2.模拟购买系统~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout<<"|"<<'\t'<<'\t'<<'\t'<<'\t'<<"~~~~~~~~~~~~~~~~~~~~3.商品种类添加~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout<<"|"<<'\t'<<'\t'<<'\t'<<'\t'<<"~~~~~~~~~~~~~~~~~~~~4.退出系统~~~~~~~~~~~~~~~~~~~~"<<" |";
cout<<endl;
cout<<"-------------------------------------------------------------------------------------------------------------------";
cout<<endl;
cout << "输入你的选择:";
cout << endl;
}
int Goods::limitnumber(int x, int y)
{
int X;//存储用户的输入
while (1)
{
cin >> X;
if (cin.fail())//如果输入的不是整型
{
cout << "非整数!请重新输入:";
cin.clear();
cin.ignore(1024, '\n');//清除缓冲区
}
else if (X<x || X>y)//整数不在范围内
{
cout << "非范围内!请重新输入:";
}
else
return X;
}
}
/删除商品
void Goods::delete1()
{
string name;
cout<<"请输入你要删除的商品名称:";
cin>>name;
int flag=0;
int i,id;
int j;
for(i=0;i<n;i++)
{
if(name==goodslist[i].name)
{
flag=1;
id=i;
break;
}
}
if(id!=n-1&&flag==1)
{
for(j=id;j<n;j++)
{
goodslist[j]=goodslist[j+1];
}
--n;
cout<<"已删除完成!!"<<endl;
}
else if(id==n-1&&flag==1)
{
--n;
cout<<"已删除完成!!"<<endl;
}
else
{
cout<<"未找到商品,无法删除!!"<<endl;
}
cout<<endl;
system("pause");
system("cls");
}
//初始化商品
void Goods::init()
{
cout << "请输入你要添加商品种类数量:" << endl;
int i,m,l;
cin >> n;
cout <<"商品代码------"<< "商品名称------" << "商品产地------" << "商品单价------" << "商品数量------" << "商品生产日期------" <<"保质期"<<
endl;
for (i = 0; i < n; i++) {
cin>>goodslist[i].address >> goodslist[i].name >> goodslist[i].location >> goodslist[i].price >> goodslist[i].amount >> goodslist[i].year >>
goodslist[i].month >> goodslist[i].day;
while(1)
{
l=judgedate(goodslist[i].year,goodslist[i].month,goodslist[i].day);
if(l==0)
{
cout<<"日期输入有误,请重新输入!"<<endl;
cin>>goodslist[i].year >>goodslist[i].month >> goodslist[i].day;
}
else
{
break;
}
}
m=freshnessdate();
if(m==1)
{
cin>>goodslist[i].shelflife;
goodslist[i].date="年";
}
else if(m==2)
{
cin>>goodslist[i].shelflife;
goodslist[i].date="月";
}
else
{
cin>>goodslist[i].shelflife;
goodslist[i].date="天";
}
time_t nowtime;
time(&nowtime);
tm *p = localtime(&nowtime);
goodslist[i].inyear=p->tm_year+1900;
goodslist[i].inmonth=p->tm_mon + 1;
goodslist[i].inday=p->tm_mday;
goodslist[i].inhour=p->tm_hour;
goodslist[i].inminute=p->tm_min;
goodslist[i].insecond=p->tm_sec;
}
cout << '\t' << '\t' << "已初始化完成" << '\t' << '\t' << endl;
system("pause");
system("cls");
}
int Goods::judgedate(int x,int y,int z)
{
if(y==1||y==3||y==5||y==7||y==8||y==10||y==12)
{
if(z>31||z<1)
{
return 0;
}
}
if(y==4||y==6||y==9||y==11)
{
if(z<1||z>30)
{
return 0;
}
}
if((x % 4 == 0 && x % 100 != 0 || x % 400 == 0)&&y==2)
{
if(z<1||z>29)
{
return 0;
}
}
if((x % 4 != 0 && x % 100 == 0 || x % 400 != 0)&&y==2)
{
if(z<1||z>28)
{
return 0;
}
}
}
//商品数量添加
void Goods::adds()
{
long long int address1;
string name;
int i;
int id;
int flag=0;
int addnumber;
int choice1;
cout<<"请选择添加方式:"<<endl;
cout<<"1.通过商品代码操作"<<endl;
cout<<"2.通过商品名称操作"<<endl;
cin>>choice1;
if(choice1==1)
{
cout << "请输入你要添加的商品代码:" << endl;
cin>>address1;
cout << "请输入你要添加的商品的数量:" << endl;
cin>>addnumber;
for(i=0;i<n;i++)
{
if(goodslist[i].address==address1)
{
flag=1;
id=i;
break;
}
}
if(flag==1)
{
goodslist[id].amount+=addnumber;
cout<<"添加完成!!!"<<endl;
}
else
{
cout<<"未找到此商品,请在商品种类添加模块进行添加"<<endl;
}
}
if(choice1==2)
{
cout << "请输入你要添加的商品名称:" << endl;
cin>>name;
cout << "请输入你要添加的商品的数量:" << endl;
cin>>addnumber;
for(i=0;i<n;i++)
{
if(goodslist[i].name==name)
{
flag=1;
id=i;
break;
}
}
if(flag==1)
{
goodslist[id].amount+=addnumber;
cout<<"添加完成!!!"<<endl;
}
else
{
cout<<"未找到此商品,请在商品种类添加模块进行添加"<<endl;
}
}
system("pause");
system("cls");
}
//商品种类添加
void Goods::addl()
{
int i,m;
cout<<"请输入需要添加的商品种类的数量:"<<endl;
int num;
int j;
cin>>num;
j=n;
n=n+num;
cout <<"商品代码------"<< "商品名称------" << "商品产地------" << "商品单价------" << "商品数量------" << "商品生产日期------" <<"保质期"<<
endl;
for (i = j; i < n; i++) {
cin>>goodslist[i].address >> goodslist[i].name >> goodslist[i].location >> goodslist[i].price >> goodslist[i].amount >> goodslist[i].year >>
goodslist[i].month >> goodslist[i].day;
m=freshnessdate();
if(m==1)
{
cin>>goodslist[i].shelflife;
goodslist[i].date="年";
}
else if(m==2)
{
cin>>goodslist[i].shelflife;
goodslist[i].date="月";
}
else
{
cin>>goodslist[i].shelflife;
goodslist[i].date="天";
}
time_t nowtime;
time(&nowtime);
tm *p = localtime(&nowtime);
goodslist[i].inyear=p->tm_year+1900;
goodslist[i].inmonth=p->tm_mon + 1;
goodslist[i].inday=p->tm_mday;
goodslist[i].inhour=p->tm_hour;
goodslist[i].inminute=p->tm_min;
goodslist[i].insecond=p->tm_sec;
}
cout<<"已添加完成!!"<<endl;
system("pause");
system("cls");
}
int Goods::freshnessdate()
{
cout<<"请选择商品保质期单位:"<<endl;
cout<<"1.年";
cout<<endl;
cout<<"2.月";
cout<<endl;
cout<<"3.天";
cout<<endl;
int choice;
choice=limitnumber(1,3);
cout<<"请输入保质期具体大小:"<<endl;
return choice;
}
//商品列表
void Goods::goodsmenu()
{
int i;
int id;
cout<<"请选择你要查看商品的方式:";
cout<<endl;
cout<<"1.单独查看";
cout<<endl;
cout<<"2.整体查看";
cout<<endl;
int choice;
cout<<"请输入:";
cin>>choice;
if(choice==1)
{
while(1)
{
int flag=0;
cout<<"请选择查看方式:"<<endl;
cout<<"1.商品代码查看"<<endl;
cout<<"2.商品名称查看"<<endl;
cout<<"3.退出."<<endl;
int choice1;
choice1=limitnumber(1,3);
if(choice1==3)
{
break;
}
if(choice1==1)
{
cout<<"请输入要查看商品的代码:";
long long int address2;
cin>>address2;
for(i=0;i<n;i++)
{
if(address2==goodslist[i].address)
{
flag=1;
id=i;
break;
}
}
//
if(flag==1)
{
cout<<"-----------------------------------------------------------------商品信息查看---------------------------------------------------------------------------"<<endl;
cout <<"商品代码"<<"--"<< "商品名称"<<"--" << "商品产地"<<"--"<< "商品单价" <<"--"<< "商品数量"<<"--" << "商品生产日期"<<"--"<<"保质期"<<"--"<<"最新入库时间"<<"--"<<"总价值"<<endl;
cout<<goodslist[id].address<<'\t'<<goodslist[id].name <<'\t'<< goodslist[id].location <<'\t'<< goodslist[id].price <<'\t'<< goodslist[id].amount<<'\t' << goodslist[id].year<<"/" <<
goodslist[id].month <<"/"<< goodslist[id].day<<'\t'<<goodslist[id].shelflife<<goodslist[id].date<<'\t'<<goodslist[id].inyear<<"/"<<goodslist[id].inmonth<<"/"<<goodslist[id].inday<<" "<<goodslist[id].inhour<<":"<<goodslist[id].inminute<<":"<<goodslist[id].insecond<<'\t'<<goodslist[id].amount*goodslist[id].price;
cout<<endl;
}
else
{
cout<<"未找到此商品!!!";
}
//
}
if(choice1==2)
{
string name;
cout<<"请输入你要查看的商品的名称:";
cin>>name;
for(i=0;i<n;i++)
{
if(name==goodslist[i].name)
{
flag=1;
id=i;
break;
}
}
//
if(flag==1)
{
cout<<"----------------------------------------------------------------------商品信息查看-----------------------------------------------------------------------------------"<<endl;
cout <<"商品代码"<<"--"<< "商品名称"<<"--" << "商品产地"<<"--"<< "商品单价" <<"--"<< "商品数量"<<"--" << "商品生产日期"<<"--"<<"保质期"<<"--"<<"最新入库时间"<<"--"<<"总价值"<<endl;
cout<<goodslist[id].address<<'\t'<<goodslist[id].name <<'\t'<< goodslist[id].location <<'\t'<< goodslist[id].price <<'\t'<< goodslist[id].amount<<'\t' << goodslist[id].year<<"/" <<
goodslist[id].month <<"/"<< goodslist[id].day<<'\t'<<goodslist[id].shelflife<<goodslist[id].date<<'\t'<<goodslist[id].inyear<<"/"<<goodslist[id].inmonth<<"/"<<goodslist[id].inday<<" "<<goodslist[id].inhour<<":"<<goodslist[id].inminute<<":"<<goodslist[id].insecond<<'\t'<<goodslist[id].amount*goodslist[id].price;
cout<<endl;
}
else
{
cout<<"未找到此商品!!!";
}
//
}
}
}
if(choice==2)
{
cout<<"----------------------------------------------------------------------商品信息查看-----------------------------------------------------------------------------------"<<endl;
cout <<"商品代码"<<"--"<< "商品名称"<<"--" << "商品产地"<<"--"<< "商品单价" <<"--"<< "商品数量"<<"--" << "商品生产日期"<<"--"<<"保质期"<<"--"<<"最新入库时间"<<"--"<<"总价值"<<endl;
for(i=0;i<n;i++)
{
cout<<goodslist[i].address<<'\t'<<goodslist[i].name <<'\t'<< goodslist[i].location <<'\t'<< goodslist[i].price <<'\t'<< goodslist[i].amount<<'\t' << goodslist[i].year<<"/" <<
goodslist[i].month <<"/"<< goodslist[i].day<<'\t'<<goodslist[i].shelflife<<goodslist[i].date<<'\t'<<goodslist[i].inyear<<"/"<<goodslist[i].inmonth<<"/"<<goodslist[i].inday<<" "<<goodslist[i].inhour<<":"<<goodslist[i].inminute<<":"<<goodslist[i].insecond<<'\t'<<goodslist[i].amount*goodslist[i].price;
cout<<endl;
}
cout<<endl;
}
system("pause");
system("cls");
}
//登陆系统
int Goods::login()
{
char code1[1024] = "";
char pwd1[1024] = "";
cout<<"当前时间:";
time_t nowtime;
time(&nowtime);
tm *p = localtime(&nowtime);
printf("%04d:%02d:%02d %02d:%02d:%02d\n", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min,
p->tm_sec);//显示时间;
while (1)
{
cout<<"1.登录 2.注册 3.退出"<<endl;;
int m;
m=limitnumber(1,3);
char code2[1024];
char pwd2[1024];
if (m == 1)
{
if (code1[0] == '\0')
{
cout<<"账号为空,请先注册账号"<<endl;;
continue;
}
cout<<"请输入登录的账号"<<endl;
scanf("%s", code2);
cout<<"请输入登录的密码"<<endl;
scanf("%s", pwd2);
if ((strcmp(code1, code2) == 0) && (strcmp(pwd1, pwd2) == 0))
{
printf("登录成功\n");
system("pause");
system("cls");
return 1;
}
else
{
if (strcmp(code1, code2) != 0)
{
cout<<"登录失败,该账号不存在,请重新输入"<<endl;;
}
if (strcmp(pwd1, pwd2) != 0)
{
cout<<"登录失败,账号密码错误,请重新输入"<<endl;;
}
}
}
else if (m == 2)
{
cout<<"请输入注册的账号"<<endl;
scanf("%s", code1);
cout<<"请输入注册的密码"<<endl;
scanf("%s", pwd1);
cout<<"注册成功!!"<<endl;;
}
else if (m == 3)
{
cout<<"感谢使用,再见!";
return 0;
}
else
{
continue;
}
}
}
void Goods::buylist()
{
cout<<"正在进入模拟购买系统......"<<endl;
int i;
cout<<"商品列表"<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout<<'\t'<<"序号"<<'\t'<<"名称"<<'\t'<<"产地"<<'\t'<<"单价"<<'\t'<<"数量"<<endl;
for(i=0;i<n;i++)
{
cout<<'\t'<<i+1<<'\t'<<goodslist[i].name<<'\t'<<goodslist[i].location<<'\t'<<goodslist[i].price<<'\t'<<goodslist[i].amount<<endl;;
}
cout<<"---------------------------------------------------------------"<<endl;
}
void Goods::purchase()
{
int choice;
float count;
float pay, back;
float total,temp;
while (1) {
count = pay = back = total = back = 0;
buylist();
cout<<"请输入您购买的商品序号(输入0退出商品售卖):";
cin >> choice;
if (choice == 0)
{
return;
}
else if (choice < 0)
{
cout<<"您的输入有误!请重试!"<<endl;
cout<<"请输入您购买的商品序号(输入0退出商品售卖):";
cin >> choice;
if (choice < 0 || choice > n)
{
cout<<"您的输入连续有误,已退出商品售卖!"<<endl;
return;
}
}
else if (choice > n)
{
cout<<"未找到该商品!请重试!"<<endl;
cout<<"请输入您购买的商品序号(输入0退出商品售卖):"<<endl;
cin >> choice;
if (choice < 0 || choice > n)
{
cout<<"您的输入连续有误,已退出商品售卖!"<<endl;
return;
}
}
cout<<"请输入您购买的数量:";
cin >> count;
if (count > goodslist[choice - 1].amount)
{
cout<<"购买数量超过库存,购买失败! "<<endl;;
system("pause");
return;
}
pay = (goodslist[choice - 1].price) * count;
cout<<"应付款:"<<fixed<<setprecision(2)<<pay;
cout << endl;
{
cout << "请选择支付方式"<<endl;;
cout << "1.微信"<<endl;
cout << "2.支付宝"<<endl;
cout << "3.现金"<<endl;
cout << "4.刷卡"<<endl;
cout << "5.刷脸支付"<<endl;
cout << "6.刷手支付"<<endl;;
cout << "-------------\n";
cout << "请输入您的支付选择:";
}
int buychoice;
cin >> buychoice;
switch (buychoice)
{
case 1:
cout << "请打开微信支付:\n";
break;
case 2:
cout << "请打开支付宝支付:\n";
break;
case 3:
cout << "请支付现金:\n";
break;
case 4:
cout << "请刷卡支付:\n";
break;
case 5:
cout << "请刷脸支付: \n";
break;
case 6:
cout << "请刷手支付 \n";
break;
default:
cout<<"您未选择任何支付,请重试!已自动为您退出商品售卖";
return;
}
cout<<"应付款:"<<fixed<<setprecision(2)<<pay<<endl;
cout <<"请付款:";
cin>>temp;
if (temp >= pay)
{
back = temp - pay;
total = temp;
}
else
{
cout << "余额不足,支付失败!\n";
system("pause");
return;
}
cout<<"购买成功!找零"<<fixed<<setprecision(1)<<back<<"请取走您的商品。"<<endl;
goodslist[choice-1].amount-=count;
if(goodslist[choice-1].amount<10)
{
cout<<"商品即将卖完,请及时补货!!"<<endl;
}
logs[logCount].name1=goodslist[choice - 1].name;
strcpy(logs[logCount].location1, goodslist[choice - 1].location);
logs[logCount].address2=goodslist[choice-1].address;
logs[logCount].amount1 = count;
time_t nowtime;
time(&nowtime);
tm *p = localtime(&nowtime);
logs[logCount].exyear=p->tm_year+1900;
logs[logCount].exmonth=p->tm_mon + 1;
logs[logCount].exday=p->tm_mday;
logs[logCount].exhour=p->tm_hour;
logs[logCount].exminute=p->tm_min;
logs[logCount].exsecond=p->tm_sec;
logCount++;
system("pause");
system("cls");
cout << endl;
cout << " 超市发票 \n";
cout<<"\t商品名称:"<<goodslist[choice - 1].name;
cout<<endl;
cout<<"\t商品单价:"<<fixed<<setprecision(1)<< goodslist[choice - 1].price;
cout<<endl;
cout<<"\t商品数量:"<<count;
cout<<endl;
cout<<"\t应付款额:"<<pay;
cout<<endl;
cout<<"\t实际付款:"<<fixed<<setprecision(1)<< total;
cout<<endl;
cout<<"\t应找零钱:"<<fixed<<setprecision(1)<<back;
cout << endl;
printf("如果产品存在问题请在七日内进行调换或者退款 \n");
cout << "\t联系电话12345678900 \t\n";
cout<<"\t当前时间:";
time_t notime;
time(¬ime);
tm* q = localtime(¬ime);
printf("%04d:%02d:%02d %02d:%02d:%02d\n", q->tm_year + 1900, q->tm_mon + 1, q->tm_mday, q->tm_hour, q->tm_min, q->tm_sec);
cout << endl;
cout << endl;
printf("\t 谢谢惠顾! \t\n");
printf("\t欢迎下次光临 \t\n");
system("pause");
system("cls");
}
}
void Goods::change()
{
int choice;
int i,m;
long long int address1;
string name;
int flag=0;
int id;
cout<<"请选择你要更改商品信息的方式:";
cout<<endl;
cout<<"1.通过商品代码改变";
cout<<endl;
cout<<"2.通过商品名称改变";
cout<<endl;
cin>>choice;
if(choice==1)
{
cout<<"请输入你要改变的商品的代码:";
cin>>address1;
for(i=0;i<n;i++)
{
if(address1==goodslist[i].address)
{
flag=1;
id=i;
break;
}
}
if(flag==1)
{
cout<<"请选择你要改变的信息:";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~1.商品代码~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~2.商品名称~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~3.商品产地~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~4.商品单价~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~5.商品数量~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~6.商品生产日期~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~7.商品保质期~~~~~~~~~~~~~~~~~";
cout<<endl;
int choice1;
choice1=limitnumber(1,7);
if(choice1==1)
{
cout<<"请输入新代码:";
cin>>goodslist[id].address;
}
else if(choice1==2)
{
cout<<"请输入新名称:";
cin>>goodslist[id].name;
}
else if(choice1==3)
{
cout<<"请输入新产地:";
cin>>goodslist[id].location;
}
else if(choice1==4)
{
cout<<"请输入新单价:";
cin>>goodslist[id].price;
}
else if(choice1==5)
{
cout<<"请输入新数量:";
cin>>goodslist[id].amount;
}
else if(choice1==6)
{
cout<<"请输入新的生产日期:";
cin>>goodslist[id].year>>goodslist[id].month>>goodslist[id].day;
while(1)
{
int judge;
judge=judgedate(goodslist[id].year,goodslist[id].month,goodslist[id].day);
if(judge==0)
{
cout<<"输入的日期有误,请重新输入!";
cin>>goodslist[id].year>>goodslist[id].month>>goodslist[id].day;
}
else
{
break;
}
}
}
else if(choice1==7)
{
m=freshnessdate();
cout<<"请输入新的保质期:";
if(m==1)
{
cin>>goodslist[id].shelflife;
goodslist[id].date="年";
}
else if(m==2)
{
cin>>goodslist[id].shelflife;
goodslist[id].date="月";
}
else
{
cin>>goodslist[id].shelflife;
goodslist[id].date="天";
}
}
}
}
if(choice==2)
{
cout<<"请输入你要改变商品的名称:";
cin>>name;
for(i=0;i<n;i++)
{
if(name==goodslist[i].name)
{
flag=1;
id=i;
break;
}
}
if(flag==1)
{
cout<<"请选择你要改变的信息:";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~1.商品代码~~~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~2.商品名称~~~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~3.商品产地~~~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~4.商品单价~~~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~5.商品数量~~~~~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~6.商品生产日期~~~~~~~~~~~~~~~~~";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~7.商品保质期~~~~~~~~~~~~~~~~~~~";
cout<<endl;
int choice2;
choice2=limitnumber(1,7);
if(choice2==1)
{
cout<<"请输入新的代码:";
cin>>goodslist[id].address;
}
else if(choice2==2)
{
cout<<"请输入新的名称:";
cin>>goodslist[id].name;
}
else if(choice2==3)
{
cout<<"请输入新的产地:";
cin>>goodslist[id].location;
}
else if(choice2==4)
{
cout<<"请输入新的单价:";
cin>>goodslist[id].price;
}
else if(choice2==5)
{
cout<<"请输入新的数量:";
cin>>goodslist[id].amount;
}
else if(choice2==6)
{
cout<<"请输入新的生产日期:";
cin>>goodslist[id].year>>goodslist[id].month>>goodslist[id].day;
while(1)
{
int judge;
judge=judgedate(goodslist[id].year,goodslist[id].month,goodslist[id].day);
if(judge==0)
{
cout<<"输入的日期有误,请重新输入!";
cin>>goodslist[id].year>>goodslist[id].month>>goodslist[id].day;
}
else
{
break;
}
}
}
else if(choice2==7)
{
m=freshnessdate();
cout<<"请输入新的保质期:";
if(m==1)
{
cin>>goodslist[id].shelflife;
goodslist[id].date="年";
}
else if(m==2)
{
cin>>goodslist[id].shelflife;
goodslist[id].date="月";
}
else
{
cin>>goodslist[id].shelflife;
goodslist[id].date="天";
}
}
}
}
cout<<"改变完成!!!";
system("pause");
system("cls");
}
void Goods::logshow()
{
int i;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~销售日志~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"**************************************************************************"<<endl;
cout<<'\t'<<"商品代码"<<'\t'<<"商品名称"<<'\t'<<"商品产地"<<'\t'<<"购买数量"<<'\t'<<"销售时间"<<endl;
for(i=0;i<logCount;i++)
{
cout<<'\t'<<logs[i].address2<<'\t'<<logs[i].name1<<'\t'<<logs[i].location1<<'\t'<<logs[i].amount1<<'\t'<<logs[i].exyear<<"/"<<logs[i].exmonth<<"/"<<logs[i].exday<<" "<<logs[i].exhour<<":"<<logs[i].exminute<<":"<<logs[i].exsecond<<endl;
}
cout<<"**************************************************************************"<<endl;
system("pause");
system("cls");
}
Goods a;
int main()
{
int choice, key, key1;
key1 = a.user();
if (key1 == 1)
{
cout << "——————————管理员登陆界面——————————" << endl;
key = a.login();
if (key == 1) {
while (1) {
a.CTRLmenu();
choice=a.limitnumber(1,9);
switch (choice)
{
case 1:
a.init();
break;
case 2:
a.adds();
break;
case 3:
a.addl();
break;
case 4:
a.delete1();
break;
case 5:
a.goodsmenu();
break;
case 6:
a.purchase();
break;
case 7:
a.logshow();
break;
case 8:
a.change();
break;
case 9:
cout << "感谢使用!!";
exit(0);
break;
default:
cout << "输入有误!!请重新输入" << endl;
break;
}
}
}
else
return 0; //if判断是否登陆成功
}//if判断身份
if (key1 == 2)
{
cout << "——————————进货员登陆界面——————————" << endl;
key = a.login();
if (key == 1)
{
while (1)
{
a.SWGmenu();
choice=a.limitnumber(1,4);
switch(choice)
{
case 1:
a.adds();
cout<<"已成功添加货物";
break;
case 2:
a.purchase();
break;
case 3:
a.addl();
break;
case 4:
cout<<"感谢使用!!";
exit(0);
break;
default:
cout << "输入有误,请重新输入!!" << endl;
}
}
}
else
return 0;
}
}