vs 2017的第一个C++项目(自动售货机)---笔记

vs 2017的第一个C++项目(自动售货机)—笔记

  1. 项目结构

在这里插入图片描述

  1. Cash

    #pragma once
    class Cash
    {
    public:
    	Cash(void); //无参构造
    	Cash(int c,int t_income); //有参构造
    	int getCash(); //Get函数
    	void dealCash(int money); //商品销售后对余额的处理
    	void dealIncome(int money);
    	void setCash(int money);
    	void setIncome(int money);
    	int getIncome();
    	~Cash(void); //析构
    private:
    	int cash; //余额
    	int income; //收入
    };
    
  2. Goods

    #pragma once
    
    class Goods
    {
    public:
    	Goods(void); //无参构造
    	Goods(string goodsName, int goodsCount, int goodsPrice); //有参构造
    	string getName(); //获取名字
    	void setCount(int t_count);
    	void setPrice(int t_price);
    	int getCount();//获取数量
    	int getPrice();//获取价格
    	void sellGoods(int num); //销售商品
    	~Goods(void); //析构
    private:
    	string name; //商品名称
    	int count; //库存数量
    	int price; //商品单价
    };
    
  3. 主函数

    #include "Cash.h"
    #include "Goods.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    int AddGoods(Goods[],Cash&);//添加商品信息
    void ShowInput(); //显示添加商品界面
    void Show(Goods[],int index); //显示货架上的商品
    void doSell(Goods[], Cash&, int); //售货行为
    int main()
    {
    	Cash cash;
    	Goods goodsList[6];
    	int number = AddGoods(goodsList,cash);
    	doSell(goodsList, cash, number);
    	return 0;
    }
    void ShowInput() {
    	cout << "*****************************************" << endl;
    	cout << "****************售货管理*****************" << endl;
    	cout << "*****************************************" << endl;
    	cout << "1.添加新商品  2.设置找零总额  3.退出设置" << endl;
    	cout << "请输入:";
    }
    
    void Show(Goods goodsList[],int index) {
    	cout << "****************************************" << endl;
    	cout << "******************售货******************" << endl;
    	cout << "****************************************" << endl;
    	for (int i = 0; i < index; i++) {
    		if (goodsList[i].getCount() > 0) {
    			cout << i + 1 <<  ". " << goodsList[i].getName() << "  ";
    		}
    		else {
    			cout << i + 1 << ". " << goodsList[i].getName() << "(售空)  ";
    		}
    	}
    	cout << endl;
    	cout << "8.查看机器剩余找零  9.查看钱柜金额  0.退出" << endl;
    	cout << "请输入:";
    }
    
    int AddGoods(Goods goodsList[],Cash &cash) {
    	ShowInput();
    	int index = 0;
    	int num;
    	while (cin >> num) {
    		if (num == 1) {
    			cout << "添加格式:商品名  商品数量  商品金额" << endl;
    			cout << "例:可乐 5 3 代表添加五瓶单价位3的可乐(最多可添加5件商品)" << endl;
    			cout << "请输入添加商品:";
    			string goodsName;
    			int price, number;
    			cin >> goodsName >> number >> price;
    			goodsList[index++] = Goods(goodsName,number,price);
    		}
    		else if (num == 2) {
    			cout << "设置找零总金额(元):";
    			int t_cash;
    			cin >> t_cash;
    			cash.setCash(t_cash);
    		}
    		else if (num == 3) {
    			if (cash.getCash() == 0) {
    				ShowInput();
    				cout << "总找零初始不能为0\n输入2设置找零总金额" << endl;
    				continue;
    			}
    			if (index == 0) {
    				cout << "请至少添加一件商品\n输入1进行商品添加" << endl;
    				ShowInput();
    				continue;
    			}
    			return index;
    		}
    		else {
    			cout << "请按提示输入!!!" << endl;
    		}
    		ShowInput();
    	}
    	return index;
    }
    
    void doSell(Goods goodsList[], Cash &cash, int index) {
    	Show(goodsList, index);
    	int num;
    	while (cin >> num) {
    		if (num <= index && num > 0) {
    			num -= 1;
    			cout << goodsList[num].getName() << " 剩余 " << goodsList[num].getCount() << " 件" << endl;
    			cout << goodsList[num].getName() << " 的价格为 " << goodsList[num].getPrice() << " 元/件" << endl;
    			cout << "请输入投放的金额:";
    			int price;
    			cin >> price;
    			while (price < goodsList[num].getPrice()) {
    				cout << "投放金额不足,请重新投放金额:" << endl;
    				cin >> price;
    			}
    			if (goodsList[num].getCount() == 0) {
    				cout << "商品存货不足" << endl;
    			}
    			else if (price == goodsList[num].getPrice()) {
    				cout << "购买成功,请取走您的商品" << endl;
    				goodsList[num].setCount(goodsList[num].getCount() - 1);
    				cash.dealIncome(price);
    			}
    			else if(cash.getCash() < (price - goodsList[num].getPrice()) )
    			{
    				cout << "找零不足,请下次购买0.0" << endl;
    			}
    			else {
    				cout << "购买成功,找零 " << price - goodsList[num].getPrice() << " 元" << endl;
    				cout << "请取走您的商品和零钱" << endl;
    				goodsList[num].setCount(goodsList[num].getCount() - 1);
    				cash.dealIncome(price);
    				cash.dealCash(price - goodsList[num].getPrice());
    			}
    			
    		}
    		else if (num == 8) {
    			cout << "剩余找零为:" << cash.getCash() << endl;
    		}
    		else if (num == 9) {
    			cout << "今日总收入:" << cash.getIncome() << endl;
    		}
    		else if (num == 0) {
    			break;
    		}
    		else {
    			cout << "请按提示输入!!!" << endl;
    		}
    		Show(goodsList, index);
    	}
    }
    
    
  4. 运行结果
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述

发布了14 篇原创文章 · 获赞 1 · 访问量 245

猜你喜欢

转载自blog.csdn.net/qq_41454682/article/details/105205644