C++自动糖果贩卖机

#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>

class Users{
private:
	std::map<std::string,std::string> m;
public:
	Users(){m["boss"]=std::string("123");}
	bool addUser(std::string username,std::string password)
	{
		auto it=m.find(username);
		if(it!=m.end()) return false;
		m[username]=password;
		return true;
	}
	bool check(std::string username,std::string password)
	{
		auto it = m.find(username);
		if(it==m.end()) return false;
		if(it->second!=password) return false;
		return true;
	}
	bool login()
	{
		puts("输入用户名:");
		std::string uname;
		std::cin >> uname;
		puts("输入密码:");
		std::string passw;
		std::cin >> passw;
		if (!check(uname, passw))
		{
			puts("用户名或密码错误");
			system("sleep 0.6");
			return false;
		}
		puts("登录成功");
		system("sleep 0.6");
		system("cls");
		return true;
	}
}users;

class CandyList{
private:
	std::map<std::string,int> list;//名字到id的映射
	std::vector<std::string> nameList;
public:
	CandyList(){nameList.resize(1);}
	int queryId(std::string name)//传入名字,返回id,不存在返回0
	{
		auto it = list.find(name);
		if(it!=list.end())
			return it->second;
		return 0;
	}
	std::string queryName(int id)
	{
		if(id>nameList.size()) return std::string("");
		return nameList[id];
	}
	int addCandyKind(std::string name)//无论有没有,都返回id
	{
		int id=queryId(name);
		if(id) return id;
		id=list.size()+1;
		// printf("&&DEBUG&&\n%s %d\n\n",name.c_str(),id);
		list[name]=id;
		nameList.push_back(name);
		return id;
	}
}candyList;

class Store{//仓库
private:
	std::vector<std::pair<int, int> > s; //数量、单价
	int sum;
public:
	Store()
	{
		s.clear();
		s.resize(100);
		sum=0;
	}
	int getSum(){return sum;}
	int getPrice(int id){return s[id].second;}
	void displayCandy()
	{
		// std::cout<<s.size()<<" ***"<<std::endl;
		std::cout << "编号\t品名\t剩余数量\t单价" << std::endl;
		for(int i=0;i<s.size();i++)
		{
			if(s[i].first>0)
			{
				std::cout<<i<<"\t"<<candyList.queryName(i)<<"\t"<<s[i].first<<"\t"<<s[i].second<<std::endl;
			}
		}
	}
	void addCandy(std::string name,int num,int price=-1)
	{
		bool ok=1;
		if(num<1)
			ok=0,puts("数量错误");
		if(price<-1||price==0)
			ok=0,puts("价格错误");
		if(!ok) return;
		int id=candyList.addCandyKind(name);
		// if(s.capacity()<id-1)
		// {
		// 	// std::cout<<"haha"<<std::endl;
		// 	if(price==-1)
		// 	{
		// 		puts("新品必须定价");
		// 		return;
		// 	}
		// 	s.resize(id+1);//仓库扩容
		// 	s[id]=std::make_pair(num,price);
		// 	return;
		// }
		s[id].first+=num;
		sum+=num;
		if(~price) s[id].second=price;//更新价格
	}
	int rmCandy(int id,int num,int pay)//卖糖
	{
		if(!id)
		{
			puts("查无此糖");
			return 0;
		}
		if(s[id].first<num)
		{
			puts("数量不足");
			return 0;
		}
		int totPrice=num*s[id].second;
		if(pay<totPrice)
		{
			puts("钱不够");
			return 0;
		}
		s[id].first-=num;
		sum-=num;
		return pay-totPrice;
	}
}store;

class FrontPage{//前端
private:
	inline void read(int &x)
	{
		int s = 0, w = 1;
		char ch = getchar();
		while (ch < '0' || ch > '9')
		{
			if (ch == '-')
				w = -1;
			ch = getchar();
		}
		while (ch >= '0' && ch <= '9')
			s = s * 10 + ch - '0', ch = getchar();
		x = s * w;
	}

public:
	FrontPage(){store.addCandy(std::string("默认糖"),10,10);}
	void run()
	{
		printf("启动中");
		for(int i=1;i<=5;i++)
			system("sleep 0.1"),printf(".");
		while(1)
		{
			system("cls");
			if(store.getSum()) printf("营业中!\n买糖请输入1,");
			else printf("缺货,");
			puts("加糖请输入2");
			int type=0;
			read(type);//提高容错性
			if(type==2)
			{
				if(!users.login()) continue;
				puts("请输入品名");
				std::string name;
				std::cin>>name;
				int id=candyList.queryId(name);
				puts("请输入数量");
				int num,price;
				bool transPrice=0;
				read(num);
				if(!id)
					puts("输入定价(分/个)"),read(price),transPrice=1;
				else
				{
					printf("要更改该商品定价请输入1,保持原定价%d 分/个 请输入0\n",store.getPrice(id));
					int temp;
					read(temp);
					if(temp)
					{
						puts("输入新定价(分/个)"),read(price),transPrice=1;
					}
				}
				if(transPrice) store.addCandy(name,num,price);
				else store.addCandy(name,num);
				continue;
			}
			if(type==1)
			{
				puts("欢迎光临");
				store.displayCandy();
				puts("输入编号");
				int id;
				read(id);
				puts("输入数量");
				int num;
				read(num);
				puts("输入付款数量");
				int pay;
				read(pay);
				int temp=store.rmCandy(id,num,pay);
				if(temp) std::cout<<"给你找零"<<temp<<"分"<<std::endl<<"欢迎再次光临";
				system("sleep 2");
				system("cls");
				continue;
			}
			puts("输入错误");
			system("sleep 2");
		}
	}
};

int main()
{
	FrontPage temp;
	temp.run();
	return 0;
}

posted @ 2019-12-15 20:19  wawcac  阅读(854)  评论(0编辑  收藏  举报