Goods类

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

class Goods
{
private:
	
public:
	const string name;
	float Weight;
	static float totalWeight;	
	Goods();
	Goods(string, float);
	static float getTotalWeight();
	void Sell(float );
	void Buy(float );
	void Print();
};
void Goods::Print()
{
	cout<<name<<": \t"<<Weight<<endl;
}

float Goods::totalWeight = 0;

Goods::Goods() 
{
	totalWeight = 0;
}

Goods::Goods(string str, float w) : name(str)
{
	Weight = 0;
}

float Goods::getTotalWeight()
{
	return totalWeight;
}

void Goods::Buy(float buy)
{
	Weight += buy;
}

void Goods::Sell(float sell)
{
	totalWeight -= sell;
}

int main()
{
	vector<Goods> goods;
	int flag;
	Goods a;
	goods.push_back(a);
as:
	cout	<<"\t╔══════════════════════════════╗"<<endl
			<<"\t║            1.购进了货物            ║"<<endl
			<<"\t║            2.卖出了货物            ║"<<endl
			<<"\t║            3.查看现库存            ║"<<endl
			<<"\t║            4.退出本程序            ║"<<endl
			<<"\t╚══════════════════════════════╝"<<endl;
	cin>>flag;
	switch(flag)
	{
	case 1:
		{
			string name;
			float weight;
			cout<<"购买了哪种商品?";
			cin>>name;
			cout<<"买了多少?";
			cin>>weight;
			for(int i=0; i<goods.size(); i++)
			{
				if(goods[i].name == name)
				{
					goods[i].Weight += weight;
					Goods::totalWeight += weight;
				}
					
				else if(i == goods.size()-1 && goods[i].name != name)
				{
					Goods g(name, weight);
					goods.push_back(g);
				}
			}
			break;
		}
	case 2:
		{
			string name;
			float weight;
			cout<<"卖出了哪种货物?";
			cin>>name;
			cout<<"卖了多少?";
			cin>>weight;
			for(int i=0; i<goods.size(); i++)
			{
				if(goods[i].name == name)
				{
					goods[i].Weight -= weight;
					Goods::totalWeight -= weight;
					if(goods[i].Weight < 0)
					{
						cout<<"卖成负数了!有错吧?";
						system("pause");
						goods[i].Weight += weight;
						Goods::totalWeight += weight;
					}
				}
			}
			break;
		}
	case 3:
		{
			cout<<"总重量: "<<Goods::getTotalWeight()<<endl;
			for(int i=1; i<goods.size(); i++)
				goods[i].Print();
			system("pause");
			break;
		}
	case 4:
		{
			exit (0);
		}
	}
	system("cls");
	goto as;
	system("pause");
	return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值