商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,单价不一样,因此商店需要记录下目前库存的货物的总重量和总价值。编写一个程序,通过定义类Carlo来模拟商店货物购进和卖出的情况。
(本题目主要练习静态数据成员的使用,定义私有变量存每件货物的价格和重量,用静态数据成员存货物的总重量和总价钱,定义构造函数和析构函数,当定义新的对象完成初始化的功能和删除对象时,从总重量和总价钱中减去对象的重量和价格)
// Menu.h
#ifndef MENU_H
#define MENU_H
class Menu
{ public:
int show();
};
#endif;
// Carlo.h
#include <iostream>
#include <windows.h>
using namespace std;
class Carlo
{public:
Carlo(double Weight = 0,double Price = 0);
virtual ~Carlo();
void SetCarlo(double = 0,double = 0);
const double &GetCurrentTotalWeight() const;
const double &GetCurrentTotalPrice() const;
void BuyBox();
void SellBox();
void ShowBoxInfor() const;
protected:
static double TotalWeight;
static double TotalPrice;
private:
double BoxWeight;
double BoxPrice;
};
//Carlo.cpp
//#include <iostream>
//#include "Carlo.h"
double Carlo::TotalPrice=0.0;
double Carlo::TotalWeight=0.0;
Carlo::Carlo(double Weight, double Price)
{
BoxWeight = Weight;
BoxPrice = Price;
TotalPrice += Weight * Price;
TotalWeight += Weight;
}
void Carlo::BuyBox()
{
double Weight,Price;
cout<<"Please input the Weight and Price of buy: ";
cin>>Weight>>Price;
TotalWeight+=BoxWeight;
TotalPrice+=BoxPrice*BoxWeight;
}
const double &Carlo::GetCurrentTotalPrice() const
{
return TotalPrice;
}
const double &Carlo::GetCurrentTotalWeight() const
{
return TotalWeight;
}
void Carlo::SetCarlo(double Weight, double Price)
{
double Weight1,Price1;
cout<<"Weight,Price: ";
cin>>Weight1>>Price1;
BoxWeight = Weight1;
BoxPrice= Price1;
}
void Carlo::SellBox()
{
double Weight,Price;
cout<<"Please input the Weight and Price of sell:"<<endl;
cin>>Weight>>Price;
TotalWeight-=BoxWeight;
TotalPrice-=BoxPrice*BoxWeight;
}
void Carlo::ShowBoxInfor() const
{
system("cls");
cout<<"库存总重量:"<<TotalWeight<<endl;
cout<<"库存总价:"<<TotalPrice<<endl;
cin.get();
}
Carlo::~Carlo()
{
}
//Menu.cpp
//#include <iostream>
//#include "Menu.h"
int Menu::show()
{ system("cls");
int choice;
cout<<"1.购进货物"<<endl;
cout<<"2.卖出货物"<<endl;
cout<<"3.库存信息"<<endl;
cout<<"4.退出"<<endl;
cin>>choice;
cin.get();
return (choice);
}
//CarloDemo.cpp
//#include "Menu.h"
//#include "Carlo.h"
int main()
{ Menu menu;
Carlo carlo;
int chioce;
while( (chioce = menu.show()) - 4)
{ switch(chioce)
{ case 1: carlo.BuyBox();
break;
case 2: carlo.SellBox();
break;
case 3: carlo.ShowBoxInfor();
break;
case 4:
return 0;
}
}
return 0;
}