某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
实验要求:
1. 提交类图;
2. 提交源代码;
3. 注意编程规范。
1、类图
2、源代码
#include<iostream>
#include<string>
using namespace std;
class PurchaseRequest {
private:
int type;
float price;
int id;
public:
PurchaseRequest(int type, float price, int id) {
this->type = type;
this->price = price;
this->id = id;
}
float GetPrice() {
return this->price;
}
int GetId() {
return this->id;
}
void Print() {
cout << this->GetId() << "号物资" << this->GetPrice() << "元";
}
};
class Approver {
protected:
Approver* approver;
string position;
public:
Approver(string position) {
this->position = position;
}
void SetApprover(Approver* approver) {
this->approver = approver;
}
virtual void ProcessRequest(PurchaseRequest* purchaseRequest) = 0;
};
class Director :public Approver {
public:
Director(string position) :Approver(position) {}
void SetApprover(Approver *approver) {
this->approver = approver;
}
void ProcessRequest(PurchaseRequest* purchaseRequest) {
if (purchaseRequest->GetPrice() <= 10000.0)
{
cout << "请求编号id =>" << purchaseRequest->GetId() << "被" << this->position << "处理了" << endl;
}
else {
approver->ProcessRequest(purchaseRequest);
}
}
};
class DepartmentManager :public Approver {
public:
DepartmentManager(string position) :Approver(position) {}
void SetApprover(Approver* approver) {
this->approver = approver;
}
void ProcessRequest(PurchaseRequest* purchaseRequest) {
if (purchaseRequest->GetPrice() > 10000.0 && purchaseRequest->GetPrice() <= 50000.0) {
cout << "请求编号id =>" << purchaseRequest->GetId() << "被" << this->position << "处理了" << endl;
}
else {
approver->ProcessRequest(purchaseRequest);
}
}
};
class ViceManager :public Approver {
public:
ViceManager(string position) :Approver(position) {}
void SetApprover(Approver* approver) {
this->approver = approver;
}
void ProcessRequest(PurchaseRequest* purchaseRequest) {
if (purchaseRequest->GetPrice() > 50000.0 && purchaseRequest->GetPrice() <= 100000.0) {
cout << "请求编号id =>" << purchaseRequest->GetId() << "被" << this->position << "处理了" << endl;
}
else {
approver->ProcessRequest(purchaseRequest);
}
}
};
class GeneralManager :public Approver {
public:
GeneralManager(string position) :Approver(position) {}
void SetApprover(Approver* approver) {
this->approver = approver;
}
void ProcessRequest(PurchaseRequest* purchaseRequest) {
if (purchaseRequest->GetPrice() > 100000.0 && purchaseRequest->GetPrice() <= 200000.0) {
cout << "请求编号id =>" << purchaseRequest->GetId() << "被" << this->position << "处理了" << endl;
}
else {
cout << "需要开员工大会决定" << endl;
}
}
};
int main() {
PurchaseRequest* purchaseRequest1 = new PurchaseRequest(1, 5600, 1);
PurchaseRequest* purchaseRequest2 = new PurchaseRequest(1, 25620, 2);
PurchaseRequest* purchaseRequest3 = new PurchaseRequest(1, 96541, 3);
PurchaseRequest* purchaseRequest4 = new PurchaseRequest(1, 152345, 4);
PurchaseRequest* purchaseRequest5 = new PurchaseRequest(1, 235685, 5);
Director* director = new Director("主任");
DepartmentManager* departmentManager = new DepartmentManager("部门经理");
ViceManager* viceManager = new ViceManager("副总经理");
GeneralManager* generalManager = new GeneralManager("总经理");
director->SetApprover(departmentManager);
departmentManager->SetApprover(viceManager);
viceManager->SetApprover(generalManager);
cout << purchaseRequest1->GetId() << "金额" << purchaseRequest1->GetPrice() << endl;
director->ProcessRequest(purchaseRequest1);
cout << "---------------------------------------" << endl;
cout << purchaseRequest2->GetId() << "金额" << purchaseRequest2->GetPrice() << endl;
director->ProcessRequest(purchaseRequest2);
cout << "---------------------------------------" << endl;
cout << purchaseRequest3->GetId() << "金额" << purchaseRequest3->GetPrice() << endl;
director->ProcessRequest(purchaseRequest3);
cout << "---------------------------------------" << endl;
cout << purchaseRequest4->GetId() << "金额" << purchaseRequest4->GetPrice() << endl;
director->ProcessRequest(purchaseRequest4);
cout << "---------------------------------------" << endl;
cout << purchaseRequest5->GetId() << "金额" << purchaseRequest5->GetPrice() << endl;
director->ProcessRequest(purchaseRequest5);
cout << "---------------------------------------" << endl;
}