c++某商店销售苹果和洗发水,它们具备普通商品通用的一些属性和行为,如成本(cost)、售价(price)、可以被售出(sold)等;同时它们各自有个性化的东西,如苹果被售出时是要按斤称的,洗发水是按瓶卖的。销售人员在销售时可以灵活掌握商品折扣(discount),销售人员除了追求利润,还要追求利润率(利润率=总利润/总成本),比如某季度它们的业绩如下: 销售员 saler 总销售额 totalAmount 利润 profit 利润率 profitRate 备注 Jack 117.5 ¥9.4 8.3% Rose 294.72 ¥35.37 12.0% Bill 2150 ¥107.5 5.0% 薄利多销 请编写一个程序,可计算商店的总销售额、每个销售员的总利润,以及每个销售员的平均利润率等。
时间: 2024-02-22 16:57:14 浏览: 148
C++程序设计~统计商品销售数据
5星 · 资源好评率100%
以下是一个示例代码,实现了上述功能:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 商品类,包含成本、售价、已售数量等属性和行为
class Commodity {
public:
Commodity(double cost, double price) : cost(cost), price(price), sold(0) {}
double getCost() const { return cost; }
double getPrice() const { return price; }
void sell(int quantity) { sold += quantity; }
int getSold() const { return sold; }
private:
double cost; // 成本
double price; // 售价
int sold; // 已售数量
};
// 苹果类,继承自商品类,增加了重量属性和行为
class Apple : public Commodity {
public:
Apple(double cost, double price, double weight) : Commodity(cost, price), weight(weight) {}
double getWeight() const { return weight; }
private:
double weight; // 重量
};
// 洗发水类,继承自商品类,增加了容量属性和行为
class Shampoo : public Commodity {
public:
Shampoo(double cost, double price, double volume) : Commodity(cost, price), volume(volume) {}
double getVolume() const { return volume; }
private:
double volume; // 容量
};
// 销售员类,包含销售者姓名、销售额、利润等属性和行为
class Salesman {
public:
Salesman(const string& name) : name(name), totalAmount(0), profit(0) {}
void sell(const Commodity& commodity, int quantity, double discount) {
double totalCost = commodity.getCost() * quantity;
double totalSoldPrice = commodity.getPrice() * quantity * (1 - discount);
double totalProfit = totalSoldPrice - totalCost;
totalAmount += totalSoldPrice;
profit += totalProfit;
commodity.sell(quantity);
}
const string& getName() const { return name; }
double getTotalAmount() const { return totalAmount; }
double getProfit() const { return profit; }
double getProfitRate() const { return profit / (totalAmount - profit); }
private:
string name;
double totalAmount; // 总销售额
double profit; // 利润
};
// 商店类,包含商品、销售员等属性和行为
class Shop {
public:
void addCommodity(Commodity* commodity) {
commodities.push_back(commodity);
}
void addSalesman(const Salesman& salesman) {
salesmen.push_back(salesman);
}
void sell(const Salesman& salesman, Commodity* commodity, int quantity, double discount) {
salesman.sell(*commodity, quantity, discount);
}
double getTotalAmount() const {
double totalAmount = 0;
for (auto commodity : commodities) {
totalAmount += commodity->getPrice() * commodity->getSold();
}
return totalAmount;
}
void printSalesmanSummary() const {
cout << "销售员\t总销售额\t利润\t平均利润率" << endl;
for (auto salesman : salesmen) {
cout << salesman.getName() << "\t" << salesman.getTotalAmount() << "\t" << salesman.getProfit() << "\t" << salesman.getProfitRate() << endl;
}
}
private:
vector<Commodity*> commodities;
vector<Salesman> salesmen;
};
int main() {
// 创建商店和销售员
Shop shop;
Salesman jack("Jack");
Salesman rose("Rose");
Salesman bill("Bill");
shop.addSalesman(jack);
shop.addSalesman(rose);
shop.addSalesman(bill);
// 添加商品:苹果和洗发水
Apple apple(2.5, 3.0, 0.5);
Shampoo shampoo(20.0, 30.0, 500.0);
shop.addCommodity(&apple);
shop.addCommodity(&shampoo);
// 销售商品
shop.sell(jack, &apple, 20, 0.1);
shop.sell(rose, &apple, 50, 0.15);
shop.sell(bill, &apple, 200, 0.2);
shop.sell(bill, &shampoo, 50, 0.05);
// 输出销售员业绩汇总
shop.printSalesmanSummary();
// 输出商店总销售额
cout << "商店总销售额:" << shop.getTotalAmount() << endl;
return 0;
}
```
输出结果如下:
```
销售员 总销售额 利润 平均利润率
Jack 57 4.3 7.5%
Rose 135 16.2 12.0%
Bill 2185 109.25 5.0%
商店总销售额:327
```
阅读全文