#include<Windows.h>
#include<iostream>
#include<conio.h>
#include<string>
#include<list>
using namespace std;
#define NUM 100
//****用链表实现商品信息系统****
struct GoodInfo
{
string ID;//商品编号
string name;//商品名称
float price;//价格
};
struct NodeList
{
GoodInfo data;
NodeList* next;
NodeList(GoodInfo val):data(val),next(NULL)
{
}
};
class Controller
{
public:
bool login();
void showCopy();
void initData();
void menu();
void setWindow();
void addInfo();
void updateInfo();
void viewInfo();
void deleteInfo();
void searchInfo();
};
list<GoodInfo> goods;
NodeList* Head=NULL;
NodeList* pCur=NULL;
void Controller::showCopy()
{
cout<<"*******************商品信息管理系统*************************"<<endl;
cout<<"*******************版权归虾米所有***************************"<<endl;
}
bool Controller::login()
{
int count=0;
int index=0;
char pwd[20];
string account;
char ch;
setWindow();
showCopy();
do
{
index=0;
cout<<"please enter your accout:";
cin>>account;
cout<<"please enter your password:";
//密码不能显示在屏幕上,要用*
while((ch=getch())!=13)//不是回车
{
if(ch==8) //backspace
{
if(index<=0)
index=0;
else
{
index--;
cout<<"\b\b";
}
}
else
{
pwd[index++]=ch;
putch('*');
}
}
pwd[index]='\0';
//检查账号密码是否正确
count++;
if(account=="xiaoxm" && strcmp(pwd,"123")==0)
{
initData();
return true;
}
else
{
if(count<4)
{
cout<<"\n\t账号或密码错误,请重新输入"<<endl;;
cout<<"\n\t你还有"<<3-count<<"次机会"<<endl;
}
else
{
cout<<"你无权登陆,系统将退出"<<endl;
exit(0);
}
}
}while(count<4);
return false;
}
void Controller::setWindow()
{
system("cls");
system("mode con:cols=80 lines=40");//设置窗口大小
system("color 2f"); //设置窗口字体的颜色
}
void Controller::initData()
{
Head=NULL;
pCur=Head;
}
void Controller::menu()
{
int option=0,count=0;
again:setWindow(); //again表示标签,标签+:组成一个带标签语句,可以作为goto的目标
showCopy();
cout<<"\n\t***************main menu**************\n";
cout<<"\n\t*************1.浏览商品信息***********\n";
cout<<"\n\t*************2.查询商品信息***********\n";
cout<<"\n\t*************3.添加商品信息***********\n";
cout<<"\n\t*************4.修改商品信息***********\n";
cout<<"\n\t*************5.删除商品信息***********\n";
cout<<"\n\t*************6.退出系统***************\n";
cout<<"\n\t****请输入您的选择(1-6):";
cin>>option;
switch(option)
{
case 1:viewInfo();break;
case 2:searchInfo();break;
case 3:addInfo();break;
case 4:updateInfo();break;
case 5:deleteInfo();break;
case 6:exit(0);break;
}
goto again;
}
void Controller::addInfo()
{
GoodInfo good;
cout<<"添加商品信息"<<endl;
setWindow();
showCopy();
cout<<"请输入您要添加的商品的编号:";
cin>>good.ID;
cout<<"请输入您要添加的商品的名称:";
cin>>good.name;
cout<<"请输入您要添加的商品的价格:";
cin>>good.price;
int result=MessageBox(NULL,LPCWSTR("Are you sure to add"),LPCWSTR("确认提示"),MB_YESNO|MB_ICONWARNING);
if(result==6)
{
if(Head==NULL)
{
Head=new NodeList(good);
pCur=Head;
}
else
{
NodeList* newNode=new NodeList(good);
pCur->next=newNode;
pCur=newNode;
}
cout<<"\n添加成功"<<endl;
}
cout<<"请按任意键返回主菜单"<<endl;
getch();
}
void Controller::updateInfo()
{
setWindow();
showCopy();
GoodInfo good;
string ID;
cout<<"\n**********修改指定商品的信息*********"<<endl;
cout<<"请输入要修改商品的编号:";
cin>>ID;
if(Head==NULL)
cout<<"暂无商品信息可修改"<<endl;
else
{
NodeList* pSearch=Head;
while(pSearch)
{
if(pSearch->data.ID==ID)
{
cout<<"请输入您要修改的商品的名称:";
cin>>pSearch->data.name;
cout<<"请输入您要修改的商品的价格:";
cin>>pSearch->data.price;
cout<<"修改成功"<<endl;
break;
}
else
{
pSearch=pSearch->next;
}
}
if(pSearch==NULL)
{
cout<<"未查到您要修改的商品"<<endl;
}
}
cout<<"\n请按任意键返回主菜单"<<endl;
getch();
}
void Controller::deleteInfo()
{
setWindow();
showCopy();
GoodInfo good;
string ID;
cout<<"\n**********删除指定商品的信息*********"<<endl;
cout<<"请输入要删除商品的编号:";
cin>>ID;
if(Head==NULL)
cout<<"暂无商品信息可删除"<<endl;
else
{
NodeList* pSearch=Head;
NodeList* pPre=NULL;
while(pSearch)
{
if(pSearch->data.ID==ID)
{
if(pSearch==Head)
{
Head=Head->next;
delete pSearch;
}
else
{
pPre->next=pSearch->next;
delete pSearch;
}
cout<<"删除成功"<<endl;
break;
}
else
{
pPre=pSearch;
pSearch=pSearch->next;
}
}
if(pSearch==NULL)
{
cout<<"未查到您要删除的商品"<<endl;
}
}
cout<<"\n请按任意键返回主菜单"<<endl;
getch();
}
void Controller::viewInfo()
{
setWindow();
showCopy();
cout<<"\n*******浏览商品信息********"<<endl;
NodeList* pView=Head;
if(Head==NULL)
cout<<"暂无商品信息可浏览"<<endl;
else
{
cout<<"\t编号"<<"\t名称"<<"\t价格"<<endl;
while(pView)
{
cout<<'\t'<<pView->data.ID<<'\t'<<pView->data.name<<'\t'<<pView->data.price<<endl;
pView=pView->next;
}
}
cout<<"\n请按任意键返回主菜单"<<endl;
getch();
}
void Controller::searchInfo()
{
setWindow();
showCopy();
GoodInfo good;
string ID;
cout<<"\n*******查找指定商品的信息********"<<endl;
cout<<"请输入要查找商品的编号:";
cin>>ID;
if(Head==NULL)
cout<<"暂无商品信息可查询"<<endl;
else
{
NodeList* pSearch=Head;
while(pSearch)
{
if(pSearch->data.ID==ID)
{
cout<<"\t编号"<<"\t名称"<<"\t价格"<<endl;
cout<<'\t'<<pSearch->data.ID<<'\t'<<pSearch->data.name<<'\t'<<pSearch->data.price<<endl;
break;
}
else
{
pSearch=pSearch->next;
}
}
if(pSearch==NULL)
{
cout<<"未查到您要找的商品"<<endl;
}
}
cout<<"\n请按任意键返回主菜单"<<endl;
getch();
}
int main()
{
Controller controller;
if(controller.login())
{
controller.menu();
}
return 0;
}