码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3715:计算工作天数

时间:2014-08-15 01:25:46      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   数据   for   ar   2014   

代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // 获得员工数据
        ArrayList<Staff> staffs = getStaffs();
        
        // 计算相隔天数
        staffs = calStaffWorkingDays(staffs);
        
        // 打印
        printSortStaffs(staffs);
    }

    /**
     * 计算全部员工的工作天数
     * @param staffs
     * @return
     */
    private static ArrayList<Staff> calStaffWorkingDays(ArrayList<Staff> staffs) {
        for (int i = 0; i < staffs.size(); i++) {
            staffs.get(i).setDays(calWorkingDays(staffs.get(i)));
        }
        
        // 排序
        staffs = sortStaffs(staffs);
        
        return staffs;
    }
    
    private static ArrayList<Staff> sortStaffs(ArrayList<Staff> staffs) {
        for (int i = 0; i < staffs.size(); i++) {
            for (int j = i + 1; j < staffs.size(); j++) {
                if (staffs.get(i).getDays() < staffs.get(j).getDays()) {
                    Staff tempStaff = new Staff();
                    tempStaff = staffs.get(i);
                    staffs.set(i, staffs.get(j));
                    staffs.set(j, tempStaff);
                }
            }
        }
        
        return staffs;
    }
    
    /**
     * 获得全部输入的员工信息
     * @return
     */
    private static ArrayList<Staff> getStaffs() {
        ArrayList<Staff> staffs = new ArrayList<Staff>();
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        String temp = input.nextLine();
        for (int i = 0; i < n; i++) {
            Staff staff = getStaff(input.nextLine());
            staffs.add(staff);
        }
        return staffs;
    }
    
    /**
     * 对得到的一串字符串进行处理
     * @param worker
     * @return
     */
    private static Staff getStaff(String worker) {
        String[] info = worker.split(" ");
        Staff staff = new Staff();
        staff.setName(info[0]);
        staff.setComeYear(Integer.parseInt(info[1]));
        staff.setComeMonth(Integer.parseInt(info[2]));
        staff.setComeDay(Integer.parseInt(info[3]));
        staff.setGoYear(Integer.parseInt(info[4]));
        staff.setGoMonth(Integer.parseInt(info[5]));
        staff.setGoDay(Integer.parseInt(info[6]));
        return staff;   
    }
    
    /**
     * 计算员工的工作天数
     * @param staff
     * @return
     */
    private static int calWorkingDays(Staff staff) {
        int days = 0;
        int comeYear = staff.getComeYear();
        int comeMonth = staff.getComeMonth();
        int comeDay = staff.getComeDay();
        int goYear = staff.getGoYear();
        int goMonth = staff.getGoMonth();
        int goDay = staff.getGoDay();
        int[] daysOfMonth = {29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        
        for (int y = comeYear; y < goYear; y++) {
            if (isLeapYear(y)) {
                daysOfMonth[2] = 29;
                days += 366;
            } else {
                daysOfMonth[2] = 28;
                days += 365;
            }
        }
        
        days = days + (calDaysofCurrentYear(goYear, goMonth, goDay)) - calDaysofCurrentYear(comeYear, comeMonth, comeDay) + 1;
        
        return days;
    }
    
    /**
     * 计算得到当前日期是当前年的第几天
     * @param year
     * @param month
     * @param day
     * @return
     */
    private static int calDaysofCurrentYear(int year, int month, int day) {
        int[] daysOfMonth = getDaysofMonth(year);
        int days = 0;
        for (int i = 1; i < month; i++) {
            days += daysOfMonth[i];
        }
        days += day;
        
        return days;
    }
    
    /**
     * 根据年份获得当前年的每月的天数数组
     * @param year
     * @return
     */
    private static int[] getDaysofMonth(int year) {
        int[] daysOfMonth = {29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (isLeapYear(year)) {
            daysOfMonth[2] = 29;
            return daysOfMonth;
        }else {
            daysOfMonth[2] = 28;
            return daysOfMonth;
        }
    }
    
    /**
     * 判断是否是闰年
     * @param year
     * @return
     */
    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
    
    /**
     * 打印员工的工作天数
     * @param staffs
     */
    private static void printSortStaffs(ArrayList<Staff> staffs) {
        for (int i = 0; i < staffs.size(); i++) {
            System.out.println(staffs.get(i).getName() + " " + staffs.get(i).getDays());
        }
    }
    
    /**
     * 定义一个员工内部类,用于保存单条员工信息
     * @author Wenhai-Q
     * 开发日期:2014年8月13日
     */
    private static class Staff {
        private String name;
        private int comeYear;
        private int comeMonth;
        private int comeDay;
        private int goYear;
        private int goMonth;
        private int goDay;
        private int days;
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getComeYear() {
            return comeYear;
        }
        public void setComeYear(int comeYear) {
            this.comeYear = comeYear;
        }
        public int getComeMonth() {
            return comeMonth;
        }
        public void setComeMonth(int comeMonth) {
            this.comeMonth = comeMonth;
        }
        public int getComeDay() {
            return comeDay;
        }
        public void setComeDay(int comeDay) {
            this.comeDay = comeDay;
        }
        public int getGoYear() {
            return goYear;
        }
        public void setGoYear(int goYear) {
            this.goYear = goYear;
        }
        public int getGoMonth() {
            return goMonth;
        }
        public void setGoMonth(int goMonth) {
            this.goMonth = goMonth;
        }
        public int getGoDay() {
            return goDay;
        }
        public void setGoDay(int goDay) {
            this.goDay = goDay;
        }
        
        public int getDays() {
            return days;
        }
        public void setDays(int days) {
            this.days = days;
        }
    }
}

(PS:这里只提供一种想法,因为我的代码WA,只是没想到,哪里错了。如果好心的你看了,可以留言提出宝贵的意见。)

POJ 3715:计算工作天数,布布扣,bubuko.com

POJ 3715:计算工作天数

标签:style   blog   color   java   数据   for   ar   2014   

原文地址:http://www.cnblogs.com/Stone-sqrt3/p/3913758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!