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