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

C++自动糖果贩卖机的更多相关文章

  1. 自动贩卖机VS无人门店:谁是真正的零售新风口?

    ​ ​原本在线上不断发力,让实体店几乎凋敝的电商,却忽然对线下兴趣大增.阿里疯狂入股.收购线下商超:京东要在全国范围内开设百万家便利店,仅在农村就将开设50万家--这一股浪潮,或将直接改变整个百货零售 ...

  2. 【BZOJ-4590】自动刷题机 二分 + 判定

    4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 156  Solved: 63[Submit][Status ...

  3. BZOJ4590 自动刷题机

    Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题机刷题的方式非常简单:首先会瞬间得出题目的正确做法,然后开始写 ...

  4. 有限状态机FSM(自动售报机Verilog实现)

    有限状态机FSM(自动售报机Verilog实现) FSM 状态机就是一种能够描述具有逻辑顺序和时序顺序事件的方法. 状态机有两大类:Mealy型和Moore型. Moore型状态机的输出只与当前状态有 ...

  5. AOI自动光学检测机技术在电路板检查中的应用

    1.简述 AOI技术在许多不同的制造业领域使用,自从电子影像技术开始发展,就被各种人利用在不同的应用领域.大家最熟悉的数字相机.数字摄影机是大家生活中最常用到的器材之一,而工业产品的生产也大量使用这些 ...

  6. 做一个自动修改本机IP和mac的bat文件

    原文:做一个自动修改本机IP和mac的bat文件 1.ip bat修改理论探讨 前两天我突然萌生了一个念头:能不能做一个小程序来实现自动配置或修改IP和mac,达到一键搞定的目的,这样尤其适合那些带着 ...

  7. BZOJ_4590_[Shoi2015]自动刷题机_二分答案

    BZOJ_4590_[Shoi2015]自动刷题机_二分答案 Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题 ...

  8. SMD 自动点料机维修

    SMD 自动点料机维修 这个工具是一个好帮手,但是过完年回来发现坏了. 设置了数量不会自动停,按停止键没有反应,一定要按打印键才能停止. 这可愁死我了. 正常情况下开机设置好数量,然后开始点数,点到数 ...

  9. PXE 自动安装物理机 (DHCP服务由路由提供, 不能再配置)

    目录 1. PXE 自动安装物理机 (DHCP服务由路由提供, 不能再配置) 1.1. 需要的软件 1.2. 启动 proxy dhcp 服务 1.3. 关键的几个配置文件 PXE 自动安装物理机 ( ...

随机推荐

  1. 阿里云对象存储OSS

    阿里云的产品种类繁多,今天让我们一起来了解下对象存储(Object Storage Service,简称OSS)吧! 什么是对象存储呢? 简单来说,对象存储OSS是阿里云提供的海量.安全和高可靠的云存 ...

  2. python __dict__ 跟 dir()的区别

    __dict__:要是对象的话返回的是一个对象自身的实例属性.不包括类的属性:要是类的__dict__则不包括父类的属性,只包含自身类属性[方法.类变量],不包括实例属性.正是这样.每个实例的实例属性 ...

  3. python computer info look

    计算机信息查看-. import platform import platform def TestPlatform(): print("---------SYSTEM INFO------ ...

  4. 使用curl访问https

    在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具.然而在使用cr ...

  5. npm操作命令

    查看所有高级的npm moudles npm list --depth= 查看所有全局安装的模块 npm list --depth= -global 查找npm全局安装模块路径 npm config ...

  6. @babel/traverse 使用方法小记

    @babel/traverse 官网: https://babeljs.io/docs/en/babel-traverse github:https://github.com/babel/babel/ ...

  7. 第三篇 HTML 表单及表格

    表单及表格 表单,常用在登录.注册等地方,这也是一个最基本的.   我们就用登录,来学习什么是表单!   表单 form 标签,在某些好用的编辑工具,比如:WebStorm  你在上面写出form再按 ...

  8. SuperMap webgl对接iportal托管的三维服务

    在webgl中对接iportal加密的三维服务时,需要提前注册key值.Cesium.Credential.CREDENTIAL = new Cesium.Credential("你的key ...

  9. JRebel最新破解激活版(IDEA自动部署插件)

    参考: https://www.52pojie.cn/thread-906163-1-1.html https://blog.csdn.net/xingbaozhen1210/article/deta ...

  10. easyui datagrid连续删除问题

    如果在datagrid中直接将index传给easyui自带的deletRow方法来删除当前点击行,一开始并没有问题,但是当连续删除的时候就或出问题了. 原因是datagrid行是根据datagrid ...