Goods类
2013-10-29 20:18
387 查看
#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; }
相关文章推荐
- spoj cot Count on a tree
- 遇到问题首先不要google---菜鸟的感悟
- mogoDB的安装
- Mongo 初涉
- 谷歌推出企业版地图服务Google Maps Engine Pro
- 谷歌推出企业版地图服务Google Maps Engine Pro
- Mongo服务器集群配置学习三——分片
- Needleman-Wunsch Algorithm
- Gof设计模式-创建模式
- ubuntu 13.10 & Ubuntu 14.04 64位安装及配置 google拼音输入法及Adobe flash player、mp3 插件安装、中文GBK编码等
- [LeetCode] Plus One
- mongo操作及相关资料
- [LeetCode] Rotate Image
- 关于django post表单
- mongo 语法 增删改查 - 群叔
- POJ1058 The Gourmet Club
- 注册get cookie get formhash go to register
- 【4】Golismero报表编写-ReportPlugin插件编写
- What are some good resources for learning about machine learning? Why
- Project Euler 题解 #45 Triangular, pentagonal, and hexagonal