处理流程:
http://hi.csdn.net/attachment/201109/12/7977098_131581882246WJ.jpg
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%
http://hi.csdn.net/attachment/201109/12/7977098_131581882246WJ.jpg
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%
解决方案 »
- sitemesh3问题
- java.lang.ClassNotFoundException: org.logicalcobwebs.proxool.ProxoolDataSource
- 关于struts2的映射问题
- 求助:.net调用java cxf webservice服务日期类型
- 验证码的实现
- 为什么我的转换字符转换不了。
- hibernate中Expression和Restrictions的区别
- Sping下怎么配置oracle驱动阿?? 查了N多地方咋都是mysql和sqlserver的呢...
- 有没有struts的标签动态提示的工具软件?
- Weblogic8中配注JDBC出错了!!
- 重启TOMCAT 修改内容无法加载
- <S:select 選中值的問題 有關list
处理流程:
如图
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%
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;public class Calculate
{ /**
* @param args
*/
public static void main(String[] args)
{
List<Person> persons = null;
try
{
persons = FileUtil.read();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
CalculateThread.persons = persons;
Thread thread1 = new Thread((new CalculateThread()), "thread1");
Thread thread2 = new Thread((new CalculateThread()), "thread2");
Thread thread3 = new Thread((new CalculateThread()), "thread3");
thread1.start();
thread2.start();
thread3.start();
while(thread1.isAlive()||thread2.isAlive()||thread3.isAlive())
{
}
Collections.sort(persons,new Comparator<Person>(){
@Override
public int compare(Person o1, Person o2)
{
return (int) (o1.getGain_salary()-o2.getGain_salary());
}
});
for(Person person : persons)
{
System.out.println(person.getName()+": salary:"+person.getSalary()+" tax:"+person.getTax()+" gain:"+person.getGain_salary());
}
try
{
FileUtil.write(persons);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}}class CalculateThread implements Runnable
{
public static List<Person> persons; @Override
public void run()
{
for (int i = 0; i < persons.size(); i++)
{
Person person = persons.get(i);
if (Thread.currentThread().getName().equals("thread1")
&& person.getId() % 3 == 0)
{
// 线程1处理
makeResult(person);
}
else if (Thread.currentThread().getName().equals("thread2")
&& person.getId() % 3 == 1)
{
// 线程2处理
makeResult(person);
}
else if (Thread.currentThread().getName().equals("thread3")
&& person.getId() % 3 == 2)
{
// 线程3处理
makeResult(person);
}
else
{
continue;
}
} } private void makeResult(Person person)
{
System.out.println(Thread.currentThread().getName() + ":" + "person "
+ person.getName() + " " + new Date().getSeconds()); double salary = person.getSalary();
double tax = 0;
if (salary >= 0 && salary <= 800)
{
tax = 0;
}
if (salary >= 801)
{
tax += ((salary <= 2000 ? salary : 2000) - 800) * 0.1;
}
if (salary >= 2001)
{
tax += ((salary <= 6000 ? salary : 6000) - 2000) * 0.15;
}
if (salary >= 6001)
{
tax += ((salary <= 10000 ? salary : 10000) - 6000) * 0.2;
}
if (salary >= 10001)
{
tax += (salary - 10000) * 0.35;
}
person.setTax(tax);
person.setGain_salary(salary - tax);
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
FileUtil.javaimport java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;public class FileUtil
{
private static String inputName = "revSource.txt";
private static String outputName = "result.txt"; public static List<Person> read() throws IOException
{
List<Person> persons = new ArrayList<Person>(); BufferedReader reader = new BufferedReader(new FileReader(inputName)); String line = ""; while (null != (line = reader.readLine()))
{
String[] info = line.split(",");
if (info.length == 3)
{
String workNum = info[0];
String name = info[1];
double salary = 0.0;
try
{
salary = Double.parseDouble(info[2]);
}
catch (NumberFormatException e)
{
continue;
} int id = persons.size();
Person person = new Person(id, workNum, name, salary);
persons.add(person);
}
} return persons;
} public static void write(List<Person> persons) throws IOException
{
File file = new File(outputName);
if (!file.exists() || file.isDirectory())
{
if (!file.createNewFile())
{
throw new IOException("创建输出文件失败!");
}
}
Writer writer = new FileWriter(file);
StringBuffer buffer = new StringBuffer();
for (Person person : persons)
{
buffer.append(person.getWorkNum()).append("\t");
buffer.append(person.getName()).append("\t");
buffer.append(person.getSalary()).append("\t");
buffer.append(person.getTax()).append("\t");
buffer.append(person.getGain_salary()).append("\t");
writer.write(buffer.toString());
writer.append("\n");
writer.flush();
buffer = new StringBuffer();
}
}
}class Person
{
private int id;
private String workNum;
private String name;
private double salary;// 税前
private double tax;// 税
private double gain_salary;// 税后 public Person(int id, String workNum, String name, double salary)
{
this.id = id;
this.workNum = workNum;
this.name = name;
this.salary = salary;
} public int getId()
{
return id;
} public void setId(int id)
{
this.id = id;
} public String getWorkNum()
{
return workNum;
} public void setWorkNum(String workNum)
{
this.workNum = workNum;
} public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
} public double getSalary()
{
return salary;
} public void setSalary(double salary)
{
this.salary = salary;
} public double getTax()
{
return tax;
} public void setTax(double tax)
{
this.tax = tax;
} public double getGain_salary()
{
return gain_salary;
} public void setGain_salary(double gainSalary)
{
gain_salary = gainSalary;
}}
work2,lisi,3000
work3,wangwu,4000
work4,worker4,5000
work5,worker5,500
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();
}
}
}
}