/* Array8.java CIS 160 Daveid Klick 2004-11-07 using the Arrays class to sort an array of objects */ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.DecimalFormat; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; class Employee { int id; String fname; String lname; double rate; double hours; public String toString() { DecimalFormat d4 = new DecimalFormat("0000"); DecimalFormat p2 = new DecimalFormat("#.00"); return d4.format(id) + ": " + padRight(lname, 16) + padRight(fname, 13) + " rate = " + padLeft(p2.format(rate), 5) + ", hours = " + padLeft(p2.format(hours), 5); } private String padLeft(String s, int n) { int x = n - s.length(); while (x-- > 0) s = ' ' + s; return s; } private String padRight(String s, int n) { int x = n - s.length(); while (x-- > 0) s += ' '; return s; } } class EmployeeByID implements Comparator { public int compare(Employee e1, Employee e2) { return e1.id - e2.id; } } class EmployeeByLastFirstID implements Comparator { public int compare(Employee e1, Employee e2) { int c = e1.lname.compareToIgnoreCase(e2.lname); if (c != 0) return c; c = e1.fname.compareToIgnoreCase(e2.fname); if (c != 0) return c; return e1.id - e2.id; } } class EmployeeByHoursLastFirstID implements Comparator { public int compare(Employee e1, Employee e2) { double d = e1.hours - e2.hours; if (d < 0.0) return -1; if (d > 0.0) return 1; int c = e1.lname.compareToIgnoreCase(e2.lname); if (c != 0) return c; c = e1.fname.compareToIgnoreCase(e2.fname); if (c != 0) return c; return e1.id - e2.id; } } class EmployeeByRateLastFirstID implements Comparator { public int compare(Employee e1, Employee e2) { double d = e1.rate - e2.rate; if (d < 0.0) return -1; if (d > 0.0) return 1; int c = e1.lname.compareToIgnoreCase(e2.lname); if (c != 0) return c; c = e1.fname.compareToIgnoreCase(e2.fname); if (c != 0) return c; return e1.id - e2.id; } } public class Array8 { public static void main(String[] args) throws IOException { Employee[] recs = new Employee[1000]; int numRecs = 0; // open file BufferedReader in = new BufferedReader(new FileReader("array8data.txt")); // read records into array numRecs = readFile(in, recs); if (numRecs == -1) { System.out.println("Array not large enough to hold all records from file"); } else if (numRecs == -2) { System.out.println("Invalid record found in file"); } else if (numRecs < 0) { System.out.println("Error reading data from file"); } else if (numRecs == 0) { System.out.println("No records found in file"); } else { // display unsorted records System.out.println("Unsorted:"); displayArray(recs, numRecs); // sort aray by employee id and display System.out.println("Sorted by employee ID:"); Arrays.sort(recs, 0, numRecs, new EmployeeByID()); displayArray(recs, numRecs); // sort array by last name, first name, employee id and display System.out.println("Sorted by last name | first name | ID:"); Arrays.sort(recs, 0, numRecs, new EmployeeByLastFirstID()); displayArray(recs, numRecs); // sort aray by hours worked, last name, first name, employee id and display System.out.println("Sorted by hours | last name | first name | ID:"); Arrays.sort(recs, 0, numRecs, new EmployeeByHoursLastFirstID()); displayArray(recs, numRecs); // sort aray by hourly rate, last name, first name, employee id and display System.out.println("Sorted by rate | last name | first name | ID:"); Arrays.sort(recs, 0, numRecs, new EmployeeByRateLastFirstID()); displayArray(recs, numRecs); } } private static int readFile(BufferedReader in, Employee[] emp) throws IOException { int ndx = 0; String strIn = in.readLine(); while (strIn != null) { if (ndx+1 > emp.length) return -1; StringTokenizer tok = new StringTokenizer(strIn, ":"); if (tok.countTokens() != 5) return -2; try { emp[ndx] = new Employee(); emp[ndx].id = Integer.parseInt(tok.nextToken()); emp[ndx].fname = tok.nextToken(); emp[ndx].lname = tok.nextToken(); emp[ndx].rate = Double.parseDouble(tok.nextToken()); emp[ndx].hours = Double.parseDouble(tok.nextToken()); } catch (Exception e) { return -2; } ndx++; strIn = in.readLine(); } return ndx; } private static void displayArray(Employee[] e, int num) { for (int i=0; i