本帖最后由 Meiyouren123 于 2013-01-30 10:13:38 编辑

解决方案 »

  1.   

    .... 我竟然看完了。  
    按 行 获取 txt中的数据,根据空格 拆分数组。 把时间 转换成Data型。 支持 直接比较,可以直接求出出差了总天数, 
    总天数/30 获得出去了n个周期。 
    int x=50; //工资基数
    int sum=0;
    for(int i=1;i<n;i++){ 
       sum+=x*30;
       x+=10;
    }
    sum+=(总天数%30*x);
    sum 就是 补贴的钱了。   感觉没什么考点,最多就是读取txt,也不算什么考点
      

  2.   


    初始i应该是0  比如 31天 31/30 == 1 ==> n=1 ;i= 1 则 无法进入for   结果只是 31%30 * 50
     
      

  3.   

    笔误  i从0开始  就ok了
      

  4.   

    其实就是一些文件操作而已,直接上代码:
    PersonBusinessTripInfo类,表示一个员工的出差信息:
    public class PersonBusinessTripInfo {
    private String name;
    private String startDate;
    private String endDate;
    private int tripDays;
    private int bonus;

    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getStartDate() {
    return startDate;
    }
    public void setStartDate(String startDate) {
    this.startDate = startDate;
    }
    public String getEndDate() {
    return endDate;
    }
    public void setEndDate(String endDate) {
    this.endDate = endDate;
    }
    public int getTripDays() {
    return tripDays;
    }
    public void setTripDays(int tripDays) {
    this.tripDays = tripDays;
    }
    public int getBonus() {
    return bonus;
    }
    public void setBonus(int bonus) {
    this.bonus = bonus;
    }}Main.java:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;public class Main { private static final String SETUP_FILE = "c:\\test\\config.properties";
    private static final String SRC_FILE = "c:\\test\\src.txt";
    private static final String RESULT_FILE = "c:\\test\\result.txt";

    private List<PersonBusinessTripInfo> personInfoList = new LinkedList<PersonBusinessTripInfo>();

    private int base = 50;
    private int step = 10;
    private static final int PERIOD = 30 ; //30为周期
    private static final String NEWLINE = System.getProperty("line.separator");

        /**
         * 读取输入文件
         * @param srcFile
         */

    private void readSrcFile(String srcFile)
    {
    BufferedReader is = null;
    try {

    is = new BufferedReader(new FileReader(new File(srcFile)));
    String line = null;
    while((line = is.readLine())!=null)
    {
    String[] infoArray = line.split(" ");
    PersonBusinessTripInfo personInfo = new PersonBusinessTripInfo();
    personInfo.setName(infoArray[0]);
    personInfo.setStartDate(infoArray[1]);
    personInfo.setEndDate(infoArray[2]);
    personInfoList.add(personInfo);
    }
    }catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch(IOException e){
    e.printStackTrace();
    }finally
    {
    if(is!=null)
    {
    try {
    is.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    is = null;
    }
    }

    }

    /**
     * 读取配置文件,设置base和step的值
     * @param setupFile
     */
    private void readSetup(String setupFile)
    {
    BufferedReader is = null;
    try {

    is = new BufferedReader(new FileReader(new File(setupFile)));
    String line = null;
    if((line = is.readLine())!=null)
    {
    if(line.startsWith("base"))
    {
    String[] infoArray = line.split("=");
    this.base = Integer.parseInt(infoArray[1]);
    }
    else if (line.startsWith("step"))
    {
    String[] infoArray = line.split("=");
    this.step = Integer.parseInt(infoArray[1]);
    }
    }
    }catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch(IOException e){
    e.printStackTrace();
    }finally
    {
    if(is!=null)
    {
    try {
    is.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    is = null;
    }
    }
    }

    public int dateDiff(String startTime, String endTime, String format) 
    {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000*24*60*60;//一天的毫秒数
        long diff;
        long day = 0;
        try {
        //获得两个时间的毫秒时间差异
        diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        day = diff/nd;//计算差多少天  
        } catch (ParseException e) {
         e.printStackTrace();
        }
        
        return (int)day+1; //包括当天
        
    }

    public void run()
    {
    readSetup(SETUP_FILE);
    readSrcFile(SRC_FILE);

    Iterator<PersonBusinessTripInfo> iter = personInfoList.iterator();
    while(iter.hasNext())
    {
    PersonBusinessTripInfo pbti = iter.next();

    int bonus = 0;
    int index = 0;
    int tripDays = dateDiff(pbti.getStartDate(),pbti.getEndDate(),"yyyy-MM-dd");

    for(int i=tripDays;i>0;i-=30)
    {
    if(i>=30)
    {
    bonus += 30*(base+step*index);
    }else
    {
    bonus += i*(base+step*index);
    }
    index++;

    }

    pbti.setTripDays(tripDays);
    pbti.setBonus(bonus);
    }

    showResult();
    writeToResultFile();
    }

    private void writeToResultFile()
    {
    FileWriter fw = null;
    try {
    fw = new FileWriter(new File(RESULT_FILE));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    if(null != fw)
    {
    try {
    Iterator<PersonBusinessTripInfo> iter = personInfoList.iterator();
    while(iter.hasNext())
    {
    PersonBusinessTripInfo pbti = iter.next();
    fw.write(pbti.getName()+" "+pbti.getStartDate()+" "+pbti.getEndDate()+" "+pbti.getTripDays()+" "+pbti.getBonus()+ NEWLINE);
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally
    {
    if(null != fw)
    {
    try {
    fw.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    fw = null;
    }
    }
    }


    }

    private void showResult()
    {
    Iterator<PersonBusinessTripInfo> iter = personInfoList.iterator();
    while(iter.hasNext())
    {
    PersonBusinessTripInfo pbti = iter.next();
    System.out.println(pbti.getName()+" "+pbti.getStartDate()+" "+pbti.getEndDate()+" "+pbti.getTripDays()+" "+pbti.getBonus());
    }
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new Main().run();
    }
    }
    运行完成后将会在c:\test\目录下生成一个result.txt文件,里面存放了运行结果。
      

  5.   

    Sorry,Main.java中的readSetup函数有误,应把if改为while,修改如下:private void readSetup(String setupFile)
    {
    BufferedReader is = null;
    try {

    is = new BufferedReader(new FileReader(new File(setupFile)));
    String line = null;
    while((line = is.readLine())!=null)
    {
    if(line.startsWith("base"))
    {
    String[] infoArray = line.split("=");
    this.base = Integer.parseInt(infoArray[1]);
    }
    else if (line.startsWith("step"))
    {
    String[] infoArray = line.split("=");
    this.step = Integer.parseInt(infoArray[1]);
    }
    }
    }catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch(IOException e){
    e.printStackTrace();
    }finally
    {
    if(is!=null)
    {
    try {
    is.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    is = null;
    }
    }
    }
    同时,把run函数里面的30改成用static final常量,避免出现magic number:public void run()
    {
    readSetup(SETUP_FILE);
    readSrcFile(SRC_FILE);

    Iterator<PersonBusinessTripInfo> iter = personInfoList.iterator();
    while(iter.hasNext())
    {
    PersonBusinessTripInfo pbti = iter.next();

    int bonus = 0;
    int index = 0;
    int tripDays = dateDiff(pbti.getStartDate(),pbti.getEndDate(),"yyyy-MM-dd");

    for(int i=tripDays;i>0;i-=PERIOD)
    {
    if(i>=PERIOD)
    {
    bonus += PERIOD*(base+step*index);
    }else
    {
    bonus += i*(base+step*index);
    }
    index++;

    }

    pbti.setTripDays(tripDays);
    pbti.setBonus(bonus);
    }

    showResult();
    writeToResultFile();
    }
      

  6.   

    package com.xun.io;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;import javax.swing.JFileChooser;public class ReaderFile{
    public static void main(String[] args) {
    //获取文件
    OpenFile openFile = new OpenFile();
    File currentFile = null;
    boolean flag = true;
    while(flag) {
    currentFile = openFile.getFile();
    if(currentFile == null) {
    System.out.println("请选择有效的文件!!");
    }else {
    System.out.println("你已经选择的有效文件,正在等待处理....");
    flag = false;
    }
    }

    //操作文件
    FileHandler fh = new TxtFileHandler();
    fh.update(currentFile);
    }
    }
    class OpenFile {
    private JFileChooser fileChooser;

    public OpenFile(){
    fileChooser = new  JFileChooser();
    };

    public File getFile() {
    File file = null;

    int response = fileChooser.showOpenDialog(null);
    if(response == JFileChooser.APPROVE_OPTION) {
    file = fileChooser.getSelectedFile();
    }
    return file;
    }

    public String saveFile() {
    int response = fileChooser.showSaveDialog(null);
    String filePath = "";

    if(response == JFileChooser.APPROVE_OPTION) {
    filePath = fileChooser.getSelectedFile().getAbsolutePath();
    }
    return filePath;
    }
    }interface FileHandler {
    public void save(File file);
    public void read(File file);
    public void update(File file);
    public void delete(File file);
    }class TxtFileHandler implements FileHandler {
    private BufferedReader br;
    private DataHandler dateHangler;
    private Subsidies subsidies;
    private OpenFile openFile;
    private PrintWriter pw;
    private File createFile;

    public TxtFileHandler() {
    dateHangler = DataHandler.getInstence();
    subsidies = new TravelAllowance();
    openFile = new OpenFile();
    }

    @Override
    public void save(File file) {
    // TODO Auto-generated method stub

    } @Override
    public void read(File File) {
    try {
    br = new BufferedReader(new FileReader(File));

    while(br.ready()) {
    System.out.println(br.readLine());
    }
    }catch(IOException e) {
    e.printStackTrace();
    }catch(Exception e) {
    e.printStackTrace();
    }finally {
    try {
    if(br != null) {
    br.close();
    }
    br = null;
    }catch(IOException e) {
    e.printStackTrace();
    }
    }
    } @Override
    public void update(File file) {
    try {
    //得到源文件
    br = new BufferedReader(new FileReader(file));

    //保存文件
    String filePath = openFile.saveFile();
    createFile = new File(filePath);
    if(!(file.exists())) {
    file.createNewFile();
    }

    pw = new PrintWriter(new FileWriter(createFile),true);

    while(br.ready()) {
    String info = br.readLine();
    String[] infoArray = info.split(" ");

    long days = dateHangler.getDays(dateHangler.parse(infoArray[1]), dateHangler.parse(infoArray[2]));
    double money = subsidies.getSubsidies(days);

    String message = infoArray[0] + "\t" + infoArray[1] + "\t" + infoArray[2] + "\t" + days + "天" + "\t" + money + "$";
    pw.write(message);
    pw.println();
    pw.flush();
    }

    }catch(IOException e) {
    e.printStackTrace();
    }catch(Exception e) {
    e.printStackTrace();
    }finally {
    try {
    if(br != null) {
    br.close();
    }
    br = null;
    }catch(IOException e) {
    e.printStackTrace();
    }
    }
    } @Override
    public void delete(File file) {
    // TODO Auto-generated method stub

    }
    }
    interface Subsidies {
    public double getSubsidies(long days);
    }class TravelAllowance implements Subsidies { @Override
    public double getSubsidies(long days) {
    double money = 0;
    money = (50 + (days/30)*10)*days;
    return money;
    }
    }class DataHandler {
    private static DataHandler dateHandler = null;
    private SimpleDateFormat sdf;

    private DataHandler(){}

    public static synchronized DataHandler getInstence() {
    if(dateHandler == null) {
    return new DataHandler();
    }else {
    return dateHandler;
    }
    }

    public Date parse(String dateStr) {
    Date date = null;
    sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
    date = sdf.parse(dateStr);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return date;
    }

    public long getDays(Date beforeDate, Date afterDate) {
    long days = 0;

    Calendar beforeCalendar = Calendar.getInstance();
    beforeCalendar.setTime(beforeDate);
    long beforeTimeMillis =  beforeCalendar.getTimeInMillis();

    Calendar afterCalendar = Calendar.getInstance();
    afterCalendar.setTime(afterDate);
    long afterTimeMillis = afterCalendar.getTimeInMillis();

    days = (afterTimeMillis - beforeTimeMillis)/(1*1000*60*60*24);

    return days;
    }
    }无聊 写了下!!