使用STL模板库map容器的小型超市管理系统

一、项目要求
使用map容器模拟超市的购物流程
二、项目步骤(使用迭代器访问)
1、创建三个变量实体:
超市货架类:保存商品的名称和单价;---------模拟超市

map<string ,double> goods = {{"可乐",3.0},{"薯片",5.2},{"瓜子",4.5},{"口香糖",5.0},{"饼干",4.5}};
map<string ,double>::iterator git;

购物车类:保存选择的商品和购买的数目;---------模拟购物车

map<string ,int> shopping_car = {}; 
map<string ,int>::iterator sit;

用户类:用户姓名和账户余额;---------模拟超市会员

map<string, double> people = {{"amy",100},{"angle",102},{"kitty",88},{"cindy",99}};
map<string ,double>::iterator pit;

2、使用用户名进入该系统,没有账号先注册账号,充值再进入系统;
进入超市:从键盘输入要购买的东西,如果有则加入购物车,没有显示没有此种商品,重新选择;
可以取消之前购买的商品,若是输入相同的商品,第二次输入的商品数量会覆盖之前的输入。
3、结账环节:通过对购物车的商品进行结算及小票的打印,计算用户账户的余额。
三、代码实现

#include <map>
#include <iostream>
#include <ctime>

using namespace std;

double count_car(string, double);
void print_paper();
void remove_from_car(string, double);
void get_datetime();

//货架
map<string ,double> goods = {{"可乐",3.0},{"薯片",5.2},{"瓜子",4.5},{"口香糖",5.0},{"饼干",4.5}};
map<string ,double>::iterator git;
//购物车:物品  买几个 
map<string ,int> shopping_car = {}; 
map<string ,int>::iterator sit;
//买家
map<string, double> people = {{"thorns",100},{"andrea",102},{"jessica",88},{"cidy",99}};
map<string ,double>::iterator pit;

void insert_into_car(string name,double money)
{
	char ch;
	string goods_name;
	int goods_num = 0;
	while(1)
	{
		cout << "请输入你要购买的商品:" << endl;
		cin >> goods_name;
		cout << "请输入你要购买的数量:" << endl;
		cin  >> goods_num;
		git = goods.find(goods_name);
		if(git != goods.end())   //加一个修改购物车中数量的判断  
		{
			if(shopping_car.count(goods_name) == 0)
			{
				shopping_car.insert(map<string ,int>::value_type(goods_name,goods_num));

			} 
			else{
				shopping_car.at(goods_name) = goods_num;
			}
			
			shopping:cout << "是否继续购买;(Y/y  or  N/n) 按任意键可取消之前的商品:";
			cin >> ch;
			if(ch == 'Y' or ch == 'y')
			{
				continue;
			}
			else if(ch == 'N' or ch == 'n'){//结算 
				count_car(name,money);
				return ;
			}
			else{
				remove_from_car(name, money);
				goto shopping;
			}
		}
		else{
			cout <<  "没有这种商品,请重新选择:" << endl;
		}
	}	
}

void remove_from_car(string name, double money)
{
	char ch; 
	string cancel_goods;
	double cancel_money;
	while(1)
	{
		cout << "请输入您要取消的商品:";
		cin >> cancel_goods;
		sit = shopping_car.find(cancel_goods);
		if(sit != shopping_car.end())
		{
			shopping_car.erase(cancel_goods);
		}
		else{
			cout << "您没有购买过此商品!" << endl;
		}
		cancel:cout << "是否继续取消购买;(Y/y  or  N/n) 按任意键可重新购买:";
		cin >> ch;
		if(ch == 'Y' or ch == 'y')
		{
			continue;
		}
		else if(ch == 'N' or ch == 'n'){//结算 
			count_car(name,money);
			return ;
		}
		else{
			insert_into_car(name, money);
			goto cancel;
		} 
	}
	return ;
}

//结算 
double count_car(string name, double money)
{
	string goods_name;
	double goods_num;
	double price = 0;
	double price_sum = 0;
	for(sit = shopping_car.begin(); sit != shopping_car.end();sit++)
	{
		cout << sit->first << ":" << sit->second << endl;
		goods_name = sit->first;
		goods_num = sit->second;
		git = goods.find(goods_name); 
		if(git != goods.end())
		{
			price = git->second * goods_num; 
			cout << "你需要支付" << price << "元" << endl;
			
		}
		price_sum += price;
	} 
	pit = people.find(name);
	if(pit != people.end())
	{
		cout << name << "先生,您共支付" << price_sum << "元," << "您的余额" << money-price_sum << endl;
		people[name] = money-price_sum; 
		print_paper();
		cout << "本次消费:" << price_sum << "元,账户余额:" << people[name] << endl;  
		get_datetime();
	} 
	return price;
}
//打印小票 
void print_paper()
{
	cout<< "-------------荆棘便利店欢迎您--------------" << endl;
	cout << "本次购买商品:"<< endl;
	cout << "商品名\t" << "数量\t" << endl;  
	for(sit = shopping_car.begin(); sit != shopping_car.end();sit++)
	{
		cout << sit->first << ":\t" << sit->second << endl;
	}
	 
}

void get_datetime()
{
	time_t t;
	struct tm* it;
	time(&t);
	it = localtime(&t);
	printf("%d/%d/%d   %d:%d:%d\n",it->tm_year+1900,it->tm_mon+1,it->tm_mday, \
							it->tm_hour, it->tm_min, it->tm_sec);
}

int main()
{
	string name;
	string reg_name;
	double reg_money;
	cout << "请输入用户名:"<< endl;
	while(1)
	{
		cin >> name;
		pit = people.find(name); 
		if(pit != people.end())
		{
			cout << "欢迎光临荆棘便利店" << endl; 
			cout << pit->first << "您的余额还有" << pit->second <<"元"<< endl;	
			break;	
		}
		else{
			cout << "请先注册本店会员" << endl; 
			cout << "=======注册会员=======" << endl;
			cout << "输入您的姓名:";
			cin >> reg_name;
			cout << "输入您的会员余额:";
			cin >> reg_money;
			people.insert(map<string ,double>::value_type(reg_name,reg_money));  
			cout <<"登录:请输入您的姓名:"; 
		}
	}
	
	int ch;
	cout << "=======以下是本店商品=======" << endl; 
	for(git = goods.begin();git != goods.end();git++)
	{
		cout << git->first << ":" << git->second << endl;
	}
	cout << "=======请输入你需要的操作=======" << endl;
	cout << "=========1、加入购物车==========" << endl;
	cout << "=========2、取消购买============" << endl;
	cout << "=========3、结算================" << endl;
	cout << "=========4、打印小票============" << endl;
	cout << "=========5、退出系统============" << endl; 
	cin >> ch;
	switch(ch)
	{
		case 1:insert_into_car(pit->first,pit->second);break;
		case 2:remove_from_car(pit->first,pit->second);break;
		case 3:count_car(pit->first,pit->second);break;
		case 4:print_paper();break;
		case 5:break; 
		default:break; 
	}
	return 0;
}

四、结果预览
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值