UML
申请
public class Request {
private String type;
private String content;
private int num;
public Request() {
}
public Request(String type, String content, int num) {
this.type = type;
this.content = content;
this.num = num;
}
@Override
public String toString() {
return "Request{" +
"type='" + type + '\'' +
", content='" + content + '\'' +
", num=" + num +
'}';
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
经理
public class CommonManager extends Manager {
public CommonManager(String name) {
super(name);
}
@Override
public void requestApplications(Request request) {
if ("请假".equals(request.getType()) && request.getNum() <= 2) {
System.out.println("经理 " + name + " 准许 " + request.toString());
} else if (superior != null) {
superior.requestApplications(request);
}
}
}
总监
public class Majordomo extends Manager {
public Majordomo(String name) {
super(name);
}
@Override
public void requestApplications(Request request) {
if ("请假".equals(request.getType()) && request.getNum() <= 5) {
System.out.println("总监 " + name + " 准许 " + request.toString());
} else if (superior != null) {
superior.requestApplications(request);
}
}
}
总经理
public class GeneraManager extends Manager {
public GeneraManager(String name) {
super(name);
}
@Override
public void requestApplications(Request request) {
if ("请假".equals(request.getType())) {
System.out.println("总经理 " + name + " 准许 " + request.toString());
} else if ("加薪".equals(request.getType()) && request.getNum() <= 500) {
System.out.println("总经理 " + name + " 准许 " + request.toString());
} else if ("加薪".equals(request.getType()) && request.getNum() > 500) {
System.out.println("总经理 " + name + " 这个请求我再考虑考虑 " + request.toString());
}
}
}
测试类
public class Main {
public static void main(String[] args) {
CommonManager commonManager = new CommonManager("王经理");
Majordomo majordomo = new Majordomo("李总监");
GeneraManager generaManager = new GeneraManager("张总经理");
commonManager.setSuperior(majordomo);
majordomo.setSuperior(generaManager);
Request request1 = new Request("请假", "我感冒了要请假!", 2);
commonManager.requestApplications(request1);
Request request2 = new Request("请假", "我感冒了要请假!", 3);
commonManager.requestApplications(request2);
Request request3 = new Request("加薪", "我要加薪!!!", 500);
commonManager.requestApplications(request3);
Request request4 = new Request("加薪", "我要加薪!!!", 1000);
commonManager.requestApplications(request4);
}
}
运行结果