好的,所以到目前为止我有这个代码按工作位置排序,现在它还需要按姓氏和字母顺序排序。我设法按位置排序然后给出平均工资和总数。
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class faculty {
private static final String ASSISTANT = "assistant";
private static final String ASSOCIATE = "associate";
private static final String FULL = "full";
public static void main(String[] args) {
FileInputStream filestream;
BufferedReader reader;
PrintWriter writer;
String line;
double totalAssistant = 0;
double totalAssociate = 0;
double totalFull = 0;
try {
filestream = new FileInputStream("Faculty List.txt");
reader = new BufferedReader(new InputStreamReader(filestream));
writer = new PrintWriter("test.txt");
List<String> assistantList = new ArrayList<String>();
List<String> associateList = new ArrayList<String>();
List<String> fullList = new ArrayList<String>();
while ((line = reader.readLine()) != null) {
String[] split = line.split(" ");
double value = Double.parseDouble(split[split.length - 1]);
String type = split[split.length - 2];
if (ASSISTANT.equals(type)) {
assistantList.add(line);
} else if (ASSOCIATE.equals(type)) {
totalAssociate += value;
associateList.add(line);
} else if (FULL.equals(type)) {
totalFull += value;
fullList.add(line);
}
}
writeInFileOutput(writer, totalAssistant, assistantList);
writeInFileOutput(writer, totalAssociate, associateList);
writeInFileOutput(writer, totalFull, fullList);
reader.close();
writer.close();
} catch (IOException e) {
System.out.println(e);
} finally {
reader = null;
filestream = null;
writer = null;
}
}
private static void writeInFileOutput(PrintWriter writer, double totalSalary, List<String> listLines) {
for (String assistant : listLines) {
writer.append(assistant).append("\n");
}
writer.append("-------\n");
writer.append("Total Salary:$").append(String.valueOf(totalSalary)).append("\n");
writer.append("Average Salary: $")
.append(String.valueOf(totalSalary / listLines.size())).append(" \n\n");
}
}
输出
Pablo Bailey EDUC associate 68757.00
Lonnie Williamson GENS associate 134777.00
Raymond Page EDUC associate 120150.00
Wallace Fitzgerald BUSN associate 40889.00
Juana Robbins SOBL associate 93669.00
Steven Hall SOBL associate 117532.00
Melissa Davis EDUC associate 132186.00
Karla Valdez BUSN associate 16385.00
Melba Luna HLTH associate 70358.00
Sonja Washington HLTH associate 59302.00
Julio Diaz HLTH associate 102641.00
Virgil Briggs PAC associate 40936.00
Terrell Sherman EDUC associate 161595.00
Jorge Scott CSIS associate 124175.00
Tanya Duncan BUSN associate 178894.00
Troy Cannon BUSN associate 58890.00
-------
Total Salary: $3645049.0
Average Salary: $104144.25714285714
这就是我目前的输出,按排名排序。我现在必须添加到列表中按姓氏排序,然后按字母顺序排列名字。这只是输出的一小部分,因为文本文件很长。 新行可以添加到输出中。
答案 0 :(得分:1)
我会让你的东西更加面向对象。所以这意味着:引入一个新的对象员工,其中包含以下示例中的一些字段:
public class Employee {
private String firstName;
private String familyName;
private String department;
private Enum jobPosition;
private double salary;
// Put constructor and some getters and setters for your field here
}
public enum JobPosition {
assistant,
associate,
full
}
阅读输入时,请分成创建员工所需的字段,并为所有员工创建1个列表。因为您现在拥有对象而不是包含一个员工的多个属性的String,所以您可以轻松地在所需的每个属性上对对象进行排序。
答案 1 :(得分:0)
我使用Aster的方法,然后让员工实施Comparable<Employee>
,或者制作Comparator<Employee>
。
Comparable<Employee>
public class Employee implements Comparable<Employee> {
@Override
public int compareTo(Employee e) {
int deptResult = this.department.compareToIgnoreCase(e.department);
if (deptResult != 0) {
return deptResult;
}
familyNameResult = this.familyName.compareToIgnoreCase(e.familyName);
if (familyNameResult != 0) {
return familyNameResult;
}
return this.firstName.compareToIgnoreCase(e.firstName);
}
}
Comparator<Employee>
public class EmployeeComparator implements Comparator<Employee> {
@Override
public int compare(Employee e1, Employee e2) {
int deptResult = e1.getDepartment().compareToIgnoreCase(e2.getDepartment());
if (deptResult != 0) {
return deptResult;
}
familyNameResult = e1.getFamilyName().compareToIgnoreCase(e2.getFamilyName());
if (familyNameResult != 0) {
return familyNameResult;
}
return e1.getFirstName().compareToIgnoreCase(e2.getFirstName());
}
}