链表实现的商品信息系统

#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;
}

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值