【设计模式】java设计模式实现随机生成员工并应用反射

Java员工薪资管理系统

利用java生成HR类,Employee类,以及给这些随机生成的员工进行涨工资的条件类。在这些类中,我们依靠反射,将随机生成的筛选条件传参进去。


Employee类如下:

package seu.sub;

/**
 * Created by Administrator on 2016/10/24 0024.
 */
public class Employee {

    public Employee(){}

    public Employee(String numCode,String name,String sex,Integer age,String birthDay,float salary,String subDay){

        this.numCode = numCode;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.birthDay = birthDay;
        this.salary = salary;
        this.subDay = subDay;

    }

    private String numCode;
    private String name;
    private String sex;
    private Integer age;
    private String birthDay;
    private float salary;
    private String subDay;
    private float old_salary;
    private Integer work_y;

    public String getNumCode() {
        return numCode;
    }

    public void setNumCode(String numCode) {
        this.numCode = numCode;
    }

    public Integer getWork_y() {
        return work_y;
    }

    public void setWork_y(Integer work_y) {
        this.work_y = work_y;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(String birthDay) {
        this.birthDay = birthDay;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }

    public String getSubDay() {
        return subDay;
    }

    public void setSubDay(String subDay) {
        this.subDay = subDay;
    }

    public float getOld_salary() {
        return old_salary;
    }

    public void setOld_salary(float old_salary) {
        this.old_salary = old_salary;
    }
}


HR 类: 

package seu.sub;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2016/10/24 0024.
 */
public class HR extends Random{

    private static HR hr = new HR();
    private static List<Employee> listEmploy = new ArrayList<Employee>();
    private static List<Rule> listRules = new ArrayList<Rule>();

    public static void main(String[] args) {

        int size = 1000;
        hr.register(size);
        hr.changeSalary();
        hr.printEmploy();
    }

    private void changeSalary() {

        float salary;
        //先将所有员工的工龄更新,调整工资
        for (Employee e : listEmploy){

            e.setWork_y(getYear(e.getSubDay()));
            salary = e.getSalary();
            e.setOld_salary(salary);
            e.setSalary(Math.round(salary*(1+getRate(e))*100)/100);
        }
    }


    private void register(int size) {

        Random ran = new Random();
        int age;
        int sex;
        String name;
        String birthDay;
        String subDay;
        float salary;

        for(int i = 1 ; i <= size ; i++){
            name = String.format("%05d",i);
            sex = ran.nextInt(2);
            age = 18 + ran.nextInt(47);
            birthDay = hr.getDate(age);
            subDay = hr.getDate(ran.nextInt(age-17)+1);
            salary = hr.getSalary(age);
            listEmploy.add(new Employee("E"+name,getChineseName(ran.nextInt(3)+2),Sex.values()[sex].toString(),age,birthDay,salary,subDay));
        }

        //初始化涨工资规则
        Rule rule1 = new Rule();
        rule1.setRate(0.1f);
        rule1.setListSubRules(Arrays.asList(
                new SubRule(1,"Salary",2500f,5000f),
                new SubRule(1,"Work_y",1,5),
                new SubRule(1,"Sex","F","F")
        ));

        Rule rule2 = new Rule();
        rule2.setRate(0.5f);
        rule2.setListSubRules(Arrays.asList(
                new SubRule(1,"Salary",5000f,(float)Integer.MAX_VALUE),
                new SubRule(1,"Work_y",6,10)
        ));
        listRules = Arrays.asList(rule1,rule2);

    }

    long nowTime;
    Date date;
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    public String getDate(int y){

        nowTime = System.currentTimeMillis();
        long time = y*365*24*60*60*1000l;
        date = new Date(nowTime-time);
        return sf.format(date);
    }


    public int getYear(String dateTime){

        nowTime = System.currentTimeMillis();
        Date date = new Date();
        try {
            date = sf.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int year = (int) ((nowTime-date.getTime())/(365*24*3600*1000l));

        return year;
    }

    Method m;
    String str;
    String typeName;
    public float getRate(Employee e){

        str = "";
        float rate = 0f;
        boolean returnBoolean = true;
        for (Rule r : listRules){
            for (SubRule s : r.getListSubRules()){
                try {
                    m = e.getClass().getMethod("get"+s.getKey());
                    str = m.invoke(e).toString();
                    typeName = m.getReturnType().getSimpleName();
                } catch (NoSuchMethodException e1) {
                    e1.printStackTrace();
                } catch (InvocationTargetException e1) {
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    e1.printStackTrace();
                }

                returnBoolean = getTrueOrFalse(returnBoolean,getObjectVal(typeName,str,s),s.getSymbol());
            }

            rate = returnBoolean ? r.getRate() : rate;

        }

        return rate;
    }

    public boolean getTrueOrFalse(boolean b1,boolean b2,int symbol){

        boolean b = false;
        switch (symbol){
            case 1:
                b = b1 && b2;
                break;
            case 2:
                b = b1 || b2;
                break;
            case 3:
                b = b1 && (!b2);
                break;
        }

        return b;
    }


    public boolean getObjectVal(String typeName,String val,SubRule s){

        if ("Integer".equals(typeName)) {

            int tempVal = Integer.parseInt(val);
            return tempVal >= (Integer) s.getMinVal() && tempVal <= (Integer)s.getMaxVal();
        }else if("float".equals(typeName)){

            float tempVal = Float.parseFloat(val);
            return tempVal >= (Float) s.getMinVal() && tempVal <= (Float)s.getMaxVal();
        }else if("String".equals(typeName)){

            return val.compareTo(s.getMinVal().toString()) == 0 ;
        }
        return false;
    }


    Random ran = new Random();
    float tempSalary;
    public float getSalary(int age){
        tempSalary = ran.nextFloat()*10000 + 2500;
        if(age < 25){
            while (tempSalary > 5000)
                tempSalary = ran.nextFloat()*10000 + 2500;
        }else if(age > 55) {
            while (tempSalary < 5000)
                tempSalary = ran.nextFloat() * 10000 + 2500;
        }

        return Math.round(tempSalary*100)/100;
    }

    public void printEmploy() {

        StringBuilder result = new StringBuilder();
        Employee e;
        for (int i = 0 ; i < listEmploy.size() ; i++){
            e = listEmploy.get(i);
            String str = (e.getSalary()-e.getOld_salary() > 0) ? "  \t| 工资涨了:"+(e.getSalary()-e.getOld_salary()) : "  \t| 工资没涨";
            result.append("\t工号:"+e.getNumCode()+" \t| 姓名:"+String.format("%7s",e.getName())+" \t| 性别:"+e.getSex()+" \t| 年龄:"+e.getAge()+" \t| 出生日期:"+e.getBirthDay()
                    +" \t| 工龄:"+String.format("%2d",e.getWork_y())+" \t| 入职时间:"+e.getSubDay()+" \t| 原工资:"+e.getOld_salary()
                    +"  \t| 现工资:"+e.getSalary()+str+"\n");
        }

        System.out.println(result.toString());
    }


    public String getChineseName(int sizeNum){

        int heightPos, lowPos;
        str = "";
        byte[] cByte = new byte[2];
        for (int i = 1 ; i <= sizeNum ; i++){
            heightPos = (176 + Math.abs(ran.nextInt(39)));
            lowPos = (161 + Math.abs(ran.nextInt(93)));

            cByte[0] = (new Integer(heightPos).byteValue());
            cByte[1] = (new Integer(lowPos).byteValue());
            try {
                str += new String(cByte,"GBK");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        return str;

    }


    public enum Sex{
        F,M
    }
}

Rule类: 

package seu.sub;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/24 0024.
 */
public class Rule {

    private float rate;
    private List<SubRule> listSubRules = new ArrayList<SubRule>();

    public float getRate() {
        return rate;
    }

    public void setRate(float rate) {
        this.rate = rate;
    }

    public List<SubRule> getListSubRules() {
        return listSubRules;
    }

    public void setListSubRules(List<SubRule> listSubRules) {
        this.listSubRules = listSubRules;
    }
}
SubRule 类:

package seu.sub;

/**
 * Created by Administrator on 2016/10/24 0024.
 */
public class SubRule {

    private int symbol;
    private Object key;
    private Object minVal;
    private Object maxVal;

    SubRule(int symbol,Object key,Object minVal,Object maxVal){
        this.symbol = symbol;
        this.key = key;
        this.minVal = minVal;
        this.maxVal = maxVal;
    }

    public int getSymbol() {
        return symbol;
    }

    public void setSymbol(int symbol) {
        this.symbol = symbol;
    }

    public Object getKey() {
        return key;
    }

    public void setKey(Object key) {
        this.key = key;
    }

    public Object getMinVal() {
        return minVal;
    }

    public void setMinVal(Object minVal) {
        this.minVal = minVal;
    }

    public Object getMaxVal() {
        return maxVal;
    }

    public void setMaxVal(Object maxVal) {
        this.maxVal = maxVal;
    }
}
运行后的结果如下: 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值