Employee.java
package test.score;
/**
* @author U100926
*/
public class Employee {
private String name;
private int japanese;
private int english;
private int math;
private int science;
private int sociology;
private int total;
public Employee(String name, int japanese, int english, int math, int science, int sociology, int total) {
this.name = name;
this.japanese = japanese;
this.english = english;
this.math = math;
this.science = science;
this.sociology = sociology;
this.total = total;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getJapanese() {
return japanese;
}
public void setJapanese(int japanese) {
this.japanese = japanese;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getScience() {
return science;
}
public void setScience(int science) {
this.science = science;
}
public int getSociology() {
return sociology;
}
public void setSociology(int sociology) {
this.sociology = sociology;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
Rank.java
package test.score;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Rank {
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee("中島", 80, 90, 30, 50, 70, 320));
list.add(new Employee("清水", 90, 94, 45, 50, 60, 339));
list.add(new Employee("住石", 30, 40, 40, 100, 100, 310));
list.add(new Employee("谷岡", 70, 40, 0, 100, 100, 310));
rank(list);
}
private static void rank(ArrayList<Employee> list) {
Collections.sort(list, new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return o2.getTotal() - o1.getTotal();
}
});
for (Employee e:list) {
System.out.println(e.getName()+" "+e.getTotal());
}
}
}