/* SortEmployeeFile2.java CIS 111 Dave Klick 2013-11-15 Reads in sequential file data, creates Employee objects, stores them in an ArrayList, and then sorts and displays the records. */ import java.util.ArrayList; import java.util.Collections; class Employee implements Comparable { public String name; public String dept; public double rate; public Employee(String nm, String dp, double rt) { name = nm; dept = dp; rate = rt; } public int compareTo(Employee e) { int n = dept.compareToIgnoreCase(e.dept); if (n == 0) n = name.compareToIgnoreCase(e.name); return n; } } public class SortEmployeeFile2 extends CIS111App { public static void main(String[] args) { SequentialFile infile = new SequentialFile("emp.txt", FileMode.INPUT); ArrayList list = new ArrayList(); // read records into ArrayList while (!infile.eof()) { list.add(new Employee(infile.readString(), infile.readString(), infile.readDouble())); } infile.close(); // sort records Collections.sort(list); // display sorted records println("Name Dept Rate"); println("---------- ------ -------"); for (Employee emp : list) { printf("%-10s %-6s %7.2f\n", emp.name, emp.dept, emp.rate); } } }