Goods类

Goods.h

#include<iostream>
using namespace std;
#include<string>

class Goods{
private:
	static float weight;
	const string name;
public:
	Goods(string);
	static void buy(float);
	static void sell(float);
	static float get();	
};

Goods::Goods(string s):name(s){
	weight=0;
}  

float Goods::weight=0;

void Goods::buy(float a){
	weight=weight+a;
}

void Goods::sell(float b){
	weight=weight-b;
	if(weight<0)
	{
		cout<<"没那么多东西卖了~请确定输入正确?\n";
		weight=weight+b;
	}
}

float Goods::get(){
	return weight;
}


main.cpp
#include<iostream>
using namespace std;
#include<string>
#include"Goods.h"

int main(){
	string name;
	int flag1;
	cout<<"建立一个商品吧:\n名称:";
	cin>>name;
	Goods goods(name);
	as:
	cout<<"\t╔══════════════════════════════╗"<<endl;
	cout<<"\t║            1.购进了货物            ║"<<endl;
	cout<<"\t║            2.卖出了货物            ║"<<endl;
	cout<<"\t║            3.查看现库存            ║"<<endl;	
	cout<<"\t║            4.退出本程序            ║"<<endl;
	cout<<"\t╚══════════════════════════════╝"<<endl;
	cin>>flag1;
	switch(flag1){
	case 1: {
					float buy;
					cout<<"买了多少?";
					cin>>buy;
					goods.buy(buy);
					break;
				}
	case 2: {
					float sell;
					cout<<"卖了多少?";
					cin>>sell;
					goods.sell(sell);
					break;
				}
	case 3: {
					cout<<goods.get();
					break;
				}
	case 4: {
					exit(0);
				}
	}
	system("pause");
	system("cls");
	goto as;
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值