处理流程:
 
  1、用IO流读取revSource.txt中的源数据。
2、将该数据保存到缓存区中。
3、开启三个线程来进行员工工资的计算,每计算一条时间必须大于2秒,这里用线程中的睡眠来模拟。
4、等全部的员工工资都处理完后进行排序,按税后工资的降序排序。
5、将员工数据保存高result.txt文件中,参考输出结果的格式。
源数据
工号,姓名,工资
输出结果
工号,姓名,,税前工资,个人所得税,税后工资工资计算方式(手工模拟例子):
(800-0)*0% + (2000-800)*10% + (6000-2000)*15% + (8600-6000)*20% + (50000-10000)*35%
工资的税率表:
上限 下限 税率
0 800 0%
801 2000 10%
2001 6000 15%
6001 10000 20%
10001 100000 35%

解决方案 »

  1.   

    这些还是java初级的吗??我怎么一点都看不懂,在北大青鸟一学期都学完了,哎
      

  2.   

    挺简单的嘛,随便写了一个
    思路
    读取文件并保存到缓存
    开三个线程分别从缓存的不同位置开始计算工资(取不同位置处理而不是交替处理是为了避免同步)
    按员工税后工资排序(可以采用多种文法,这里采用定义一个Staff类并实现Comparable接口用于排序)
    输出到文件
    import java.util.*;
    import java.io.*;
    class StaffMain { //主程序
        public static void main(String[] args) throws Throwable {
            List<Staff> list = readFile("revSource.txt"); //读文件并保留到缓存        int avg = list.size()/3; //按缓存的不同位置平均分配
            Thread[] t = new Thread[3];
            for (int i=0; i<3; i++) { //三个线程计算工资
                t[i] = new CaculateThread (list, i*avg, (i==2 ? list.size() : (i+1)*avg));
                t[i].start();
            }
            while (true) { //主线程无限循环直到线程执行结束为止
                boolean over = true;
                for (int i=0; i<3; i++) {
                    over &= (t[i].getState() == Thread.State.TERMINATED);
                }
                if (over) {break;}            Thread.yield();
            }
            
            Collections.sort(list); //排序
            
            writeFile(list, "result.txt"); //输出结果
            
        }    public static List<Staff> readFile(String filename) throws Throwable { //读文件
            RandomAccessFile raf = new RandomAccessFile(filename, "r");
            String buf;
            List<Staff> list = new ArrayList<Staff>();
            while ((buf=raf.readLine()) != null) {
                if (buf.matches("(.*?),(.*?),(\\d+([.]\\d+)?)")) {
                    String[] sa = buf.split(",");
                    Staff s = new Staff(sa[0], sa[1], Double.valueOf(sa[2]));
                    list.add(s);
                }
            }
            raf.close();
            return list;
        }    public static void writeFile(List<Staff> list, String filename) throws Throwable { //写文件
            PrintStream ps = new PrintStream(new FileOutputStream(filename));
            ps.println("工号,姓名,税前工资,个人所得税,税后工资");
            for (Staff staff : list) {
                ps.println(String.format("%s,%s,%.2f,%.2f,%.2f", staff.getId(), staff.getName(), 
                           staff.getSalary(), staff.getTax(), staff.getNetSalary()));
            }
            ps.close();
        }
    }class Staff implements Comparable<Staff> { //定义一个员工类
        String id;
        String name;
        double salary;
        double tax = 0.0;
        public Staff(String id, String name, double salary) {
            this.id = id;
            this.name = name;
            this.salary = salary;
        }
        public String getId() {return id;}
        public String getName() {return name;}
        public double getSalary() {return salary;}
        public void setTax(double tax) {this.tax = tax;}
        public double getTax() {return tax;}
        public double getNetSalary() {return (salary-tax);}    public int compareTo(Staff staff) { //税后工资排序
            if (staff == null) return -1;
            return (getNetSalary() > staff.getNetSalary() ? -1 : 
                   (getNetSalary() == staff.getNetSalary() ? 0 : 1));  
        }
    }class CaculateThread extends Thread { //线程
        List<Staff> list;
        int begin;
        int end;
        public CaculateThread (List<Staff> list, int begin, int end) {
            this.list = list;
            this.begin = begin;
            this.end = end;
        }
        public void run() {
            for (int i=begin; i<end; i++) {
                //long t1 = System.currentTimeMillis();
                Staff staff = list.get(i);
                double salary = staff.getSalary();
                double tax = 0.0;
                if (salary > 10000) {
                    tax += (salary - 10000) * 0.35;
                    salary = 10000;
                }
                if (salary > 6000) {
                    tax += (salary - 6000) * 0.20;
                    salary = 6000;
                }
                if (salary > 2000) {
                    tax += (salary - 2000) * 0.15;
                    salary = 2000;
                }
                if (salary > 800) {
                    tax += (salary - 800) * 0.10;
                    salary = 800;
                }
                staff.setTax(tax);
                //long t2 = System.currentTimeMillis();
                try {
                    sleep(2000); //模拟计算时间
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
      

  3.   

    不愧是大神阿宝,唯一缺陷姓名不能是中文,我小改了一下,可以输入中文了
    import java.util.*;
    import java.io.*;
    class StaffMain { //主程序
        public static void main(String[] args) throws Throwable {
            List<Staff> list = readFile("F:/JAVA/revSource.txt"); //读文件并保留到缓存        int avg = list.size()/3; //按缓存的不同位置平均分配
            Thread[] t = new Thread[3];
            for (int i=0; i<3; i++) { //三个线程计算工资
                t[i] = new CaculateThread (list, i*avg, (i==2 ? list.size() : (i+1)*avg));
                t[i].start();
            }
            while (true) { //主线程无限循环直到线程执行结束为止
                boolean over = true;
                for (int i=0; i<3; i++) {
                    over &= (t[i].getState() == Thread.State.TERMINATED);
                }
                if (over) {break;}            Thread.yield();
            }
            
            Collections.sort(list); //排序
            
            writeFile(list, "F:/JAVA/result.txt"); //输出结果
            
        }    public static List<Staff> readFile(String filename) throws Throwable { //读文件
            RandomAccessFile raf = new RandomAccessFile(filename, "r");
            String str;
            List<Staff> list = new ArrayList<Staff>();
            while ((str=raf.readLine()) != null) {
             String buf=new String(str.getBytes("iso8859-1"),"GBK");
                if (buf.matches("(.*?),([\\s\\S]*?),(\\d+([.]\\d+)?)")) {
                    String[] sa = buf.split(",");
                    Staff s = new Staff(sa[0], sa[1], Double.valueOf(sa[2]));
                    list.add(s);
                }
            }
            raf.close();
            return list;
        }    public static void writeFile(List<Staff> list, String filename) throws Throwable { //写文件
            PrintWriter ps = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename)));
            ps.println("工号,姓名,税前工资,个人所得税,税后工资");
            for (Staff staff : list) {
                ps.println(String.format("%s,%s,%.2f,%.2f,%.2f", staff.getId(), staff.getName(), 
                           staff.getSalary(), staff.getTax(), staff.getNetSalary()));
            }
            ps.close();
        }
    }class Staff implements Comparable<Staff> { //定义一个员工类
        String id;
        String name;
        double salary;
        double tax = 0.0;
        public Staff(String id, String name, double salary) {
            this.id = id;
            this.name = name;
            this.salary = salary;
        }
        public String getId() {return id;}
        public String getName() {return name;}
        public double getSalary() {return salary;}
        public void setTax(double tax) {this.tax = tax;}
        public double getTax() {return tax;}
        public double getNetSalary() {return (salary-tax);}    public int compareTo(Staff staff) { //税后工资排序
            if (staff == null) return -1;
            return (getNetSalary() > staff.getNetSalary() ? -1 : 
                   (getNetSalary() == staff.getNetSalary() ? 0 : 1));  
        }
    }class CaculateThread extends Thread { //线程
        List<Staff> list;
        int begin;
        int end;
        public CaculateThread (List<Staff> list, int begin, int end) {
            this.list = list;
            this.begin = begin;
            this.end = end;
        }
        public void run() {
            for (int i=begin; i<end; i++) {
                //long t1 = System.currentTimeMillis();
                Staff staff = list.get(i);
                double salary = staff.getSalary();
                double tax = 0.0;
                if (salary > 10000) {
                    tax += (salary - 10000) * 0.35;
                    salary = 10000;
                }
                if (salary > 6000) {
                    tax += (salary - 6000) * 0.20;
                    salary = 6000;
                }
                if (salary > 2000) {
                    tax += (salary - 2000) * 0.15;
                    salary = 2000;
                }
                if (salary > 800) {
                    tax += (salary - 800) * 0.10;
                    salary = 800;
                }
                staff.setTax(tax);
                //long t2 = System.currentTimeMillis();
                try {
                    sleep(2000); //模拟计算时间
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
      

  4.   

    不能用中文?那是文件编码的问题吧。
    改一下读文件用BufferedReader就好了
        public static List<Staff> readFile(String filename) throws Throwable { //读文件
            BufferedReader br = new BufferedReader(
                new InputStreamReader(new FileInputStream(filename))); //可以根据情况调整编码
            String buf;
            List<Staff> list = new ArrayList<Staff>();
            while ((buf=br.readLine()) != null) {
                if (buf.matches("(.*?),(.*?),(\\d+([.]\\d+)?)")) {
                    String[] sa = buf.split(",");
                    Staff s = new Staff(sa[0], sa[1], Double.valueOf(sa[2]));
                    list.add(s);
                }
            }
            br.close();
            return list;
        }
      

  5.   

    //第一个类
    //往文件里先随机插入一定的数
    package com.arithmetic.csdn;import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;public class WriteData {
    //要写入的记录数
    public static final int RECORDS=100;
    public void writeData(){
    BufferedWriter bfw=null;
    try {
    bfw =new BufferedWriter(new FileWriter("d:\\src.txt"));
    bfw.write("工号\t姓名\t工资");
    //要写入的记录数
    for(int i=0;i<RECORDS;i++){
    bfw.write("\r\n");
    bfw.write("g"+i+"\t");//工号
    bfw.write("x"+i+"\t");//姓名
    int temp=(int) (Math.random()*9000+1000);//1000-10000
    bfw.write(temp+"");//工资
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    if(bfw!=null){
    try {
    bfw.close();
    } catch (IOException e) {
    bfw=null;
    e.printStackTrace();
    }
    }
    }

    }

    public static void main(String[] args) {
    new WriteData().writeData();
    }
    }
    //第二个类
    package com.arithmetic.csdn;import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.List;public class DealData { private List<String> cache = null;// 缓存 private List<String> finalSala = new ArrayList<String>();// 最终工资 // 线程内部类,负责启动多线程
    private class ThreadDeal extends Thread { private int start;
    private int end; public ThreadDeal(int start, int end) {
    super();
    this.start = start;
    this.end = end;
    } @Override
    public void run() {
    deal(start, end);
    } }
    public static void main(String[] args) {
    DealData d = new DealData();
    d.readData();
    d.writeData();
    System.out.println(d.finalSala.size());
    }
    /**
     * 预算数据
     */
    public void budgetData() {
    int ageCount = cache.size() / 3;// 平均每条线程处理的个数,同时也是分截数
    // first thread
    Thread t1 = new ThreadDeal(0, ageCount);
    t1.start();
    // second thread
    Thread t2 = new ThreadDeal(ageCount, ageCount * 2);
    t2.start();
    // third thread
    Thread t3 = new ThreadDeal(ageCount * 2, cache.size());
    t3.start();
    // 排序时,要等待所有工资信息处理完批,否则会抛异常
    while (true) {
    if (t1.getState() != Thread.State.TERMINATED) {
    continue;
    }
    if (t2.getState() != Thread.State.TERMINATED) {
    continue;
    }
    if (t3.getState() != Thread.State.TERMINATED) {
    continue;
    }
    // 所有线程执行完毕,开始排序
    subSort(0, finalSala.size() - 1);
    // 跳出手环
    break;
    }
    } /**
     * 开始记录,结束记录
     * 
     * @param start
     * @param end
     */
    private void deal(int start, int end) { DecimalFormat df = new DecimalFormat("0.00");// 设置小数位数
    String[] strArr = null;// 存放编号,姓名,工资
    double tax = 0;// 税后工资
    double salary = 0;// 税前工资
    // 开始计算工资
    for (int i = start; i < end; i++) {
    String employee = cache.get(i);// 得到第几个人的工资
    strArr = employee.split("\t"); salary = Integer.parseInt(strArr[2]);// 税前工资
    tax = 0.0;// 税后工资
    if (salary > 10000) {
    tax += (salary - 10000) * 0.35;
    }
    if (salary > 6000) {
    tax += (salary - 6000) * 0.20;
    }
    if (salary > 2000) {
    tax += (salary - 2000) * 0.15;
    }
    if (salary > 800) {
    tax += (salary - 800) * 0.10;
    }
    // 将计算后的工资放进finalSala
    StringBuffer strbf = new StringBuffer();
    strbf.append(strArr[0] + "\t").append(strArr[1] + "\t")
    .append(strArr[2] + "\t\t").append((salary - tax) + "\t\t")
    .append(df.format(tax));
    finalSala.add(strbf.toString());
    } } /**
     * 得到税后工资
     * 
     * @return
     */
    private double getTax(String record) {
    String[] arr = record.split("\t");
    return Double.parseDouble(arr[arr.length - 1]);
    } /**
     * 读取数据
     */
    public void readData() {
    BufferedReader bfr = null;
    try {
    cache = new ArrayList<String>();// 初始化缓存 bfr = new BufferedReader(new InputStreamReader(new FileInputStream(
    "d:\\src.txt")));
    String line = null;
    // 第一行不要
    bfr.readLine();
    while ((line = bfr.readLine()) != null) {
    cache.add(line);
    }
    budgetData();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    bfr.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    } /**
     * 快速排序
     * 
     * @param low
     * @param end
     */
    void subSort(int low, int end) {
    if (low < end) {
    int i = low;
    int j = end;
    String keyStr = finalSala.get(i);
    Double key = getTax(keyStr);
    while (i < j) {
    while (i < j && getTax(finalSala.get(j)) > key) {
    j--;
    }
    // 找到中间值,进行替换
    if (i < j) {
    finalSala.set(i, finalSala.get(j));
    i++;
    }
    while (i < j && getTax(finalSala.get(i)) < key) {
    i++;
    }
    if (i < j) {
    finalSala.set(j, finalSala.get(i));
    j--;
    }
    }
    // 此时i==j
    finalSala.set(i, keyStr);
    subSort(low, j - 1);
    subSort(j + 1, end);
    }
    } /**
     * 往文件里写入数据
     */
    public void writeData(){
    BufferedWriter bfw=null;
    try {
    bfw =new BufferedWriter(new FileWriter("d:\\res.txt"));
    bfw.write("工号\t姓名\t税前工资\t个人所得税\t税后工资");
    //要写入的记录数
    for(int i=0;i<finalSala.size();i++){
    bfw.write("\r\n");
    bfw.write(finalSala.get(i));
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    if(bfw!=null){
    try {
    bfw.close();
    } catch (IOException e) {
    bfw=null;
    e.printStackTrace();
    }
    }
    }

    }
    }
    //这个程序没有上边那一位设计的较合理,很多是采用低层的,本来这是前一天该做完的,但是由于其他原因,今天做了,没有想到别人已经做好了。贴出来吧,大家一起参考参考,
    顺便我也想问大家一个问题
    一个文件里包含
    aaa河南机电高等专科学校 天天0001         tt
    bb河南科技学院     马六六0002 xpy
    c河南师范大学 王0003 w
    这样的对齐方式,如何在控制台里也保持这个格式,,,这也是我考试的一个题,因为Myeclisep会吃掉一些空格,导致对齐较难实现,希望有人贴出程序