#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;
}
Goods类
最新推荐文章于 2023-01-15 22:44:57 发布