面向对象的程序设计-26-对于商品库存量计算的实例

来源:互联网 发布:安卓数据精灵 编辑:程序博客网 时间:2024/05/01 05:09
///*作业一:
//某商店经销一种货物。货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,因此,商店需要记录下目录库存的总重量。
//现在用C++模拟商店货物购进和卖出的情况,设计一个可以统计货物总重量的程序。要求定义一个Goods类,完成此功能。*/
//#include <iostream>
//using namespace std;
//class Goods
//{
// int mass;//质量
// static int i;//记录箱数
//public:
// Goods(){mass=0;}
// Goods(int m){mass=m;++i;}
// int read_mass(){return mass;}
// static int read_total(){return i;};
// int sell(){return -mass;}
// int buy(){return mass;}
//};
//int Goods::i=0;
//void main()
//{
// Goods g1(20),g2(30),g3(50);
// cout<<"经手的箱数:"<<Goods::read_total()<<endl;
// int total=0;
// total=g1.buy()+g2.sell()+g3.buy();
// cout<<"所有的存放质量:"<<total<<endl;
//
//};
/*
以前的例子,对于同一品种货物的买进与卖出的描述。建立该货物对象后,买进多少,再卖出多少,能够计算当下的库存量
*/
#include <iostream>
using namespace std;
class Goods
{
double total,remain,saled;
public:
Goods(){total=remain=saled=0;}
Goods(double i){total=remain=i;saled=0;}
void Buy(double i)
{
remain=total=i+remain;
cout<<"购入货物总量(kg):"<<i<<endl;
cout<<"目前库存剩余总量(kg):"<<remain<<endl;
}


void Sale()
{
double j;
cout<<"输入卖出货物重量(kg):";
cin>>j;
if(remain<j){cout<<"库存不足!"<<endl;}
else
{
remain-=j;saled+=j;
cout<<"已卖出货物(kg):"<<saled<<endl<<"目前库存剩余总量(kg):"<<remain<<endl;
}
}
};
void main()
{
Goods g(500);
g.Buy(300);g.Sale();g.Sale();
}
原创粉丝点击