问题是:
一家公司销售5种不同的产品零售价分别是:产品1,2.98美元;产品2,4.50美元;产品3,9.98美元
;产品4,4.49美元;产品5,6.87美元。请编写一个程序,要求用户输入一系列如下所示的数据:
a)产品编号
b)销售量
您的程序应计算并显示所有销售产品的总零售价值。如果销售数量为负数,则将其设置为0。当程序读取产品数等于-l时,停止循环并显示最终结果。
实现代码如下,仅供参考:
#include<iostream>
using namespace std;
class HovertreeProduct
{
public:
HovertreeProduct() {
h_productId = 0;
h_price = 0;
h_name = "产品0";
}
HovertreeProduct(int htId) {
h_productId = htId;
h_price = 0;
h_name = "产品" + htId;
}
void SetPrice(double htPrice) {
if (htPrice < 0)
{
h_price = 0;
}
h_price = htPrice;
}
double GetPirce()
{
return h_price;
}
private :
int h_productId;
double h_price;
string h_name;
};
class HovertreeProductInfo
{
public :
HovertreeProductInfo(HovertreeProduct htProduct){
h_htProduct = htProduct;
h_totalCount = 0;
}
void HtAddCount(int htCount) {
h_totalCount = h_totalCount + htCount;
if (h_totalCount < 0)
h_totalCount = 0;//如果销售数量为负数,则将其设置为0。 by 何问起
}
double GetTotalPrice()
{
return h_htProduct.GetPirce()* h_totalCount;
}
/*string GetProcuctInfo()
{
string m_info = "";
}*/
private:
int h_totalCount;
HovertreeProduct h_htProduct;
};
int main() {
HovertreeProduct m_htProduct1(1);
m_htProduct1.SetPrice(2.98);
HovertreeProduct m_htProduct2(2);
m_htProduct2.SetPrice(4.50);
HovertreeProduct m_htProduct3(3);
m_htProduct3.SetPrice(9.98);
HovertreeProduct m_htProduct4(4);
m_htProduct4.SetPrice(4.49);
HovertreeProduct m_htProduct5(5);
m_htProduct5.SetPrice(6.87);
HovertreeProductInfo m_htProductInfo1(m_htProduct1);
HovertreeProductInfo m_htProductInfo2(m_htProduct2);
HovertreeProductInfo m_htProductInfo3(m_htProduct3);
HovertreeProductInfo m_htProductInfo4(m_htProduct4);
HovertreeProductInfo m_htProductInfo5(m_htProduct5);
int m_productId, m_count;
cout << "请输入产品编号(1,2,3,4,5)" << endl;
cin >> m_productId;
cout << "请输入产品销售量(整数,输入-1完成)" << endl;
cin >> m_count;
while (m_count != -1)//程序读取产品数等于-l时,停止循环并显示最终结果。 by 何问起
{
switch (m_productId)
{
case 1:
m_htProductInfo1.HtAddCount(m_count);
break;
case 2:
m_htProductInfo2.HtAddCount(m_count);
break;
case 3:
m_htProductInfo3.HtAddCount(m_count);
break;
case 4:
m_htProductInfo4.HtAddCount(m_count);
break;
case 5:
m_htProductInfo5.HtAddCount(m_count);
break;
default:
cout << "没有该编号的产品" << endl;
break;
}
cout << "请输入产品编号(1,2,3,4,5)" << endl;
cin >> m_productId;
cout << "请输入产品销售量" << endl;
cin >> m_count;
}
double m_totalPrice = m_htProductInfo1.GetTotalPrice()
+ m_htProductInfo2.GetTotalPrice()
+ m_htProductInfo3.GetTotalPrice()
+ m_htProductInfo4.GetTotalPrice()
+ m_htProductInfo5.GetTotalPrice();
cout << "总零售价值:" << m_totalPrice << endl;
cout << "何问起提示:输入任何内容,回车退出。 "<<endl;
string m_temp;
cin >> m_temp;
return 0;
}
运行效果如下图:
