java程序之出差补助计算

来源:互联网 发布:胶州搭建网络 编辑:程序博客网 时间:2024/04/29 11:38


    某公司为其它公司做技术服务,人员按照客户要求出差外派。补贴是在人员出差前预先派发的。需要计算出每个人的补贴数值,并且需要派出日期先后排序,以便于安排进行统一借款并进行补贴的派发。如果派出日期相同,则按照补贴金额从少到多排序。

    按照出差时间长短,补贴的标准是不同的。具体规定是:
    30天以内,每日补贴50元;超出31而在60天以内部分,每日补贴多10元,即60元;超出61而在90天以内部分,每日补贴再多0即70元,……以30日为周期以此类推。

    出差的天数以自然日计算,不需要考虑节假日。

    举例说明:
    张三2010-9-16外派出差,到2010-9-30回到公司,计算出差时间为15天,因为少于30天,出差补贴为50*15=750元。
    李四2010-9-1外派出差,到2010-10-20回到公司,计算出差时间为50天,50*30+60*20=2700元。

    为了方便后期调整出差补贴标准,需要采用config.properties对上面的补贴标准进行配置,程序运行时从C:\test\下读取。
    配置文件的内容为:
base=50
step=10

    给出的输入文件为C:\test\src.txt,每行内容为3部分,姓名 派出日期 释放日期
    其中:每个字段中间以一个空格分隔,日期的形式为2010-9-17。

    结果请写入C:\test\result.txt中,每行内容为5部分:姓名 派出日期 释放日期 出差天数 补助金额。
    其中:每个字段中间以一个空格分隔,日期的形式为2010-9-17(注:月份或日期位数不满2位的,不需要以0补全2位,即2010-9-1不需要输出为2010-09-01);出差天数、金额保留到整数位。

    输入、输出文件编码方式都使用GBK。

    提示:编程过程中,可以使用apache commons包中的api (这个建议与考查的内容无关,至少便于对处理文件关闭进行处理,评分是不会有任何影响)
除以上包以外,请使用j2se5.0或6.0的标准内容。引入其他第3方库并不符合考试要求。

src.txt文件的内容:

张三 2010-9-17 2010-10-15
李四 2010-9-5 2010-10-30
王五 2010-9-20 2010-11-2
赵六 2010-10-2 2010-10-30
阿童木 2010-10-15 2010-12-31

 

result.txt文件的内容:

李四 2010-9-5 2010-10-30 56 3060
张三 2010-9-17 2010-10-15 29 1450
王五 2010-9-20 2010-11-2 44 2340
赵六 2010-10-2 2010-10-30 29 1450
阿童木 2010-10-15 2010-12-31 78 4560

 

config.properties文件的内容:

base=50
step=10

 

package com.neusoft.exam;import java.io.File;import java.util.Collections;import java.util.List;/** *  * <b>Application name:</b>exam<br> * <b>Application describing:</b>主程序类<br> */public class ExamMain{    /**     *      * {主函数}     *       */    public static void main(String[] args)    {        String srcFilePath = "c:" + File.separator + "test" + File.separator + "src.txt";        String dstFilePath = "c:" + File.separator + "test" + File.separator + "result.txt";        List<PeopleOut> list = FileOperation.readFile(srcFilePath);        Collections.sort(list);        FileOperation.writeFile(list, dstFilePath);    }}


 

package com.neusoft.exam;import java.io.File;import java.math.BigDecimal;import java.util.Calendar;/** *  * <b>Application name:</b>Exam<br> * <b>Application describing:</b> 外派员工信息类<br> */public class PeopleOut implements Comparable<PeopleOut>{    private String name;//姓名    private Calendar startDate;//外派开始日期    private Calendar endDate;//外派结束日期    private int days;//外派的天数    private BigDecimal buTie;//外出的补贴    /**     * {无参构造函数}     *         */    public PeopleOut()    {        super();        // TODO Auto-generated constructor stub        this.days = 0;        this.buTie = new BigDecimal("0");    }    /**     * {带3个参数的构造函数}     *      * @param name     * @param startDate     * @param endDate      */    public PeopleOut(String name, Calendar startDate, Calendar endDate)    {        super();        this.name = name;        this.startDate = startDate;        this.endDate = endDate;        this.days = 0;        this.buTie = new BigDecimal("0");        this.countBuTie();//调用自己的计算补贴的方法给days和buTie赋值    }    /**     *      * {排序方法}     * 需要派出日期先后排序,       如果派出日期相同,则按照补贴金额从少到多排序。     * @param o     * @return     * @author:developer     */    public int compareTo(PeopleOut o)    {        // if (this.startDate == o.getStartDate())        if (this.getStartDate().compareTo(o.getStartDate()) == 0)        {            //return o.getBuTie().compareTo(this.getBuTie());            return this.buTie.compareTo(o.getBuTie());        }        else        {            return this.startDate.compareTo(o.getStartDate());        }    }    /**     *      * {按照外派的天数计算每个人的补贴}     * 按照出差时间长短,补贴的标准是不同的。具体规定是:        30天以内,每日补贴50元;超出31而在60天以内部分,每日补贴多10元,即60元;        超出61而在90天以内部分,每日补贴再多0即70元,……以30日为周期以此类推。     *        */    public void countBuTie()    {        this.days = DateOperation.daysBetween(startDate, endDate);        String filePath = "c:" + File.separator + "test" + File.separator + "config.properties";        BigDecimal base = new BigDecimal(PropertyOperation.readProperty(filePath).getProperty("base"));        BigDecimal step = new BigDecimal(PropertyOperation.readProperty(filePath).getProperty("step"));        int m = days / 30;        int n = days % 30;        for (int i = 0; i <= m; i++)        {            if (i == m)            {                buTie = buTie.add(base.add(new BigDecimal(i).multiply(step)).multiply(new BigDecimal(n)));            }            else            {                buTie = buTie.add(base.add(new BigDecimal(i).multiply(step)).multiply(new BigDecimal("30")));            }        }        this.buTie = buTie.setScale(0, BigDecimal.ROUND_HALF_UP);    }    /**     * buTie的GET方法     * @return BigDecimal buTie.     */    public BigDecimal getBuTie()    {        return buTie;    }    /**     * days的GET方法     * @return int days.     */    public int getDays()    {        return days;    }    /**     * endDate的GET方法     * @return Calendar endDate.     */    public Calendar getEndDate()    {        return endDate;    }    /**     * name的GET方法     * @return String name.     */    public String getName()    {        return name;    }    /**     * startDate的GET方法     * @return Calendar startDate.     */    public Calendar getStartDate()    {        return startDate;    }    /**     * buTie的SET方法     * @param buTie The buTie to set.     */    public void setBuTie(BigDecimal buTie)    {        this.buTie = buTie;    }    /**     * days的SET方法     * @param days The days to set.     */    public void setDays(int days)    {        this.days = days;    }    /**     * endDate的SET方法     * @param endDate The endDate to set.     */    public void setEndDate(Calendar endDate)    {        this.endDate = endDate;    }    /**     * name的SET方法     * @param name The name to set.     */    public void setName(String name)    {        this.name = name;    }    /**     * startDate的SET方法     * @param startDate The startDate to set.     */    public void setStartDate(Calendar startDate)    {        this.startDate = startDate;    }    /**     *      * {重写toString()方法,便于输出}     *      * @return     * @author:developer     */    public String toString()    {        StringBuilder sb = new StringBuilder(80);        sb.append(name);        sb.append(" ");        sb.append(DateOperation.dateToStr(startDate));        sb.append(" ");        sb.append(DateOperation.dateToStr(endDate));        sb.append(" ");        sb.append(days);        sb.append(" ");        sb.append(buTie);        return sb.toString();    }}
package com.neusoft.exam;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.List;/** *  * <b>Application name:</b>Exam<br> * <b>Application describing:</b> 文件操作类<br> */public class FileOperation{    /**     *      * {写入文件操作}     *      * @param list     * @param filePath     */    public static void writeFile(List<PeopleOut> list, String filePath)    {        BufferedWriter bw = null;        try        {            bw = new BufferedWriter(new FileWriter(filePath));            for (PeopleOut p : list)            {                bw.write(p.toString());//向文件中写入一条数据                bw.newLine();//换行            }            bw.flush();//强制输出缓冲区的内容,避免数据缓存,造成文件写入不完整的情况。        }        catch (IOException e)        {            System.out.println("文件写入失败!");            e.printStackTrace();        }        finally        {            if (bw != null)            {                try                {                    bw.close();                }                catch (IOException e)                {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }    /**     *      * {读取文件信息操作}     *      * @param filePath     * @return      */    public static List<PeopleOut> readFile(String filePath)    {        BufferedReader br = null;        List<PeopleOut> list = new ArrayList<PeopleOut>();        try        {            br = new BufferedReader(new FileReader(filePath));            String str = null;            String[] params = null;            PeopleOut p = null;            while ((str = br.readLine()) != null)            {                params = str.split(" ");                p = new PeopleOut();                for (int i = 0; i < params.length; i++)                {                    if (i == 0)                    {                        p.setName(params[i]);                    }                    if (i == 1)                    {                        p.setStartDate(DateOperation.strToDate(params[i]));                    }                    if (i == 2)                    {                        p.setEndDate(DateOperation.strToDate(params[i]));                    }                }                //p = new PeopleOut(params[0], DateOperation.strToDate(params[1]), DateOperation.strToDate(params[2]));                p.countBuTie();                list.add(p);            }        }        catch (FileNotFoundException e)        {            System.out.println("文件未找到!");            e.printStackTrace();        }        catch (IOException e)        {            System.out.println("文件读取失败!");            e.printStackTrace();        }        finally        {            if (br != null)            {                try                {                    br.close();                }                catch (IOException e)                {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }        return list;    }}


 

package com.neusoft.exam;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;/** *  * <b>Application name:</b>Exam<br> * <b>Application describing:</b>日期操作工具类  <br> */public class DateOperation{    /**     * 日期格式转换格式     */    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");    /**     * 一天的毫秒数     */    public static final long interval = 1000L * 24 * 60 * 60;    /**     *      * {日期转换成字符串函数}     *      * @param calendar     * @return      */    public static String dateToStr(Calendar calendar)    {        String str = null;        str = sdf.format(calendar.getTime());        return str;    }    /**     *      * {计算两个日期之间相差的天数}     *      * @param startCal     * @param endCal     * @return       */    public static int daysBetween(Calendar startCal, Calendar endCal)    {        if (startCal != null && endCal != null)        {            if (startCal.after(endCal))            {                throw new IllegalArgumentException("参数非法,开始时间不能大于结束时间");            }            long start = startCal.getTimeInMillis();            long end = endCal.getTimeInMillis();            int res = (int) ((end - start) / interval + 1);            return res;        }        else        {            return 0;        }    }    /**     *      * {字符串转换成日期方法 }     *      * @param str     * @return       */    public static Calendar strToDate(String str)    {        Calendar calendar = null;        try        {            calendar = Calendar.getInstance();            calendar.setTime(sdf.parse(str));        }        catch (ParseException e)        {            //throw new RuntimeException(e);            System.out.println("格式解析错误!");            e.printStackTrace();        }        return calendar;    }}


 

package com.neusoft.exam;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Properties;/** *  * <b>Application name:</b>Exam<br> * <b>Application describing:</b>Properties文件操作工具类 <br> */public class PropertyOperation{    /**     *      * {读取磁盘上的properties文件信息}     *      * @param filePath     * @return     */    public static Properties readProperty(String filePath)    {        Properties properties = new Properties();        InputStream is = null;        try        {            is = new FileInputStream(filePath);            properties.load(is);        }        catch (FileNotFoundException e)        {            System.out.println("没有找到文件");            e.printStackTrace();        }        catch (IOException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }        finally        {            if (is != null)            {                try                {                    is.close();                }                catch (IOException e)                {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }        return properties;    }}