把代码切出来供大家讨论:
[code="java"]
public interface DoSalary {
/**
* 发薪水/奖金
* @param emp
* @param salary
* @param reward
*/
void doSalaryForStaff(Employee emp,double salary,double reward);
/**
* 添加员工
* @param emp
*/
void addStaff(Employee emp);
/**
* 删除员工
* @param emp
*/
void removeStaff(Employee emp);
}
[/code]
[code="java"]
public class Employee {
/**
* 个人基本信息
*/
private EmployeeInfo baseInfo;
public Employee(EmployeeInfo baseInfo) {
this.baseInfo = baseInfo;
}
public EmployeeInfo getBaseInfo() {
return baseInfo;
}
}
[/code]
[code="java"]
public class Manager extends Employee implements DoSalary,Comparable{
/**
* 存储下属员工。
*/
private Map<String, Employee> staffs;
public Manager(EmployeeInfo baseInfo) {
super(baseInfo);
if(baseInfo.getPower()== EmployeeInfo.POWER.NORMAL) {
throw new IllegalArgumentException("普通员工不能作为经理。");
}
if(baseInfo.getPower()== EmployeeInfo.POWER.PRESIDENT) {
//总经理的薪水、奖金是固定值。
baseInfo.setSalary(10000);
baseInfo.setReward(2000);
System.out.println(baseInfo.getName()+"你是总经理你的工资和奖金是定值。工资:10000¥,奖金:2000");
}
}
public Map<String, Employee> getStaffs() {
if(staffs == null) {
staffs = new HashMap<String, Employee>();
}
return staffs;
}
/**
* 发薪水
*/
@Override
public void doSalaryForStaff(Employee emp,double salary,double reward) {
if(this == emp) {
System.out.println("你不能给自己发工资。");
return;
}
if(getBaseInfo().getPower() !=EmployeeInfo.POWER.PRESIDENT&&
!getStaffs().containsKey(emp.getBaseInfo().getNumber())) {
System.out.println(emp.getBaseInfo().getName()+"不是你的员工你不能给发工资。");
}
int power = compareTo(emp);
if(power > 0) {//权限大
emp.getBaseInfo().setSalary(salary);
emp.getBaseInfo().setReward(reward);
System.out.println(this.getBaseInfo().getName()
+"给"+emp.getBaseInfo().getName()+"发工资:"+salary+"¥"+
" 发奖金:"+reward+"¥。");
} else {//权限小
System.out.println("你不能添加比你级别高或者同级别的人发工资。");
}
}
@Override
public void addStaff(Employee emp) {
int power = compareTo(emp);
if(power == 1) {
getStaffs().put(emp.getBaseInfo().getNumber(), emp);
} else {
System.out.println("你不能添加比你级别高或者同级别的人作为你的下属员工。");
}
}
@Override
public void removeStaff(Employee emp) {
getStaffs().remove(emp.getBaseInfo().getNumber());
}
/**
* 比较权限
*/
@Override
public int compareTo(Employee emp) {
EmployeeInfo.POWER self = this.getBaseInfo().getPower();
EmployeeInfo.POWER other = emp.getBaseInfo().getPower();
return self.compareTo(other);
}
}
[/code]
[code="java"]
public class NormalEMP extends Employee {
public NormalEMP(EmployeeInfo baseInfo) {
super(baseInfo);
if(baseInfo.getPower()!= EmployeeInfo.POWER.NORMAL) {
throw new IllegalArgumentException("你是经理不是普通员工。");
}
}
}
[/code]
[code="java"]
/**
-
员工类
*
/
public class EmployeeInfo {
/*
- 姓名
*/
private String name;
/**
/**
- 工号
*/
private String number;
/**
*职位
*/
private String position;
/**
- 工资
*/
private double salary;
/**
- 奖金
*/
private double reward;
/**
- 权限
*/
private POWER power;
/**
- 权限:普通,经理,总经理
*/
public enum POWER{NORMAL,MANAGER,PRESIDENT};
/**
*性别:男、女
*/
public enum SEX{MALE,FEMALE};
public EmployeeInfo(String name,String number,POWER power) {
if(name==null||number==null||power == null) {
throw new IllegalArgumentException("员工不能没有姓名、工号、权限。");
}
this.name = name;
this.number = number;
this.power = power;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SEX getSex() {
return sex;
}
public void setSex(SEX sex) {
this.sex = sex;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getReward() {
return reward;
}
public void setReward(double reward) {
this.reward = reward;
}
public POWER getPower() {
return power;
}
public void setPower(POWER power) {
this.power = power;
}
}
[/code]
[code="java"]
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
/**
* 总经理(张三)
* |
* ---------------------------------
* | |
* 经理(李四) 经理(王五)
* | |
* ----------------- ----------------
* | | | |
* 员工(王六) 员工(王七) 员工(王八) 员工(王九)
*
*/
//总经理(张三)
EmployeeInfo baseinfo = new EmployeeInfo("张三","001",EmployeeInfo.POWER.PRESIDENT);
Manager president = new Manager(baseinfo);
//经理(李四)
baseinfo = new EmployeeInfo("李四","002",EmployeeInfo.POWER.MANAGER);
Manager manager_1 = new Manager(baseinfo);
//经理(王五)
baseinfo = new EmployeeInfo("王五","003",EmployeeInfo.POWER.MANAGER);
Manager manager_2 = new Manager(baseinfo);
//员工(王六)
baseinfo = new EmployeeInfo("王六","004",EmployeeInfo.POWER.NORMAL);
NormalEMP emp_1 = new NormalEMP(baseinfo);
//员工(王七)
baseinfo = new EmployeeInfo("王七","005",EmployeeInfo.POWER.NORMAL);
NormalEMP emp_2 = new NormalEMP(baseinfo);
//员工(王八)
baseinfo = new EmployeeInfo("王八","006",EmployeeInfo.POWER.NORMAL);
NormalEMP emp_3 = new NormalEMP(baseinfo);
//员工(王九)
baseinfo = new EmployeeInfo("王六","007",EmployeeInfo.POWER.NORMAL);
NormalEMP emp_4 = new NormalEMP(baseinfo);
president.addStaff(manager_1);
president.addStaff(manager_2);
manager_1.addStaff(emp_1);
manager_1.addStaff(emp_2);
manager_2.addStaff(emp_3);
manager_2.addStaff(emp_4);
//总经理给经理发工资
president.doSalaryForStaff(manager_1, 300, 10);
//总经理给员工发奖金
president.doSalaryForStaff(emp_1, 0, 1000);
//经理给员工发奖金
manager_1.doSalaryForStaff(emp_2, 10000, 100);
//经理(王五)给(王七)发工资
manager_2.doSalaryForStaff(emp_2, 10000, 100);
//经理(李四)给(王五)发工资
manager_1.doSalaryForStaff(manager_2, 10000, 100);
//经理(李四)给(李四)发工资
manager_1.doSalaryForStaff(manager_1, 10000, 100);
//emp_1,emp_2,emp_3,emp_4是普通员工,无法发工资。
}
}
[/code]