Contents

  1. ./Employee.java
  2. ./EmployeeNameComparator.java

./Employee.java 1/2

[
top][prev][next]
package employee;

import java.util.Arrays;

/**
 * Represents an employee with an id, name, and age
 * 
 * @author sprenkle
 * 
 */
public class Employee implements Comparable<Employee> {

	private int id;
	private String name;
	private int age;

	/**
	 * Creates an employee with the given id, name, and age
	 * 
	 * @param id
	 *            the employee's given unique id
	 * @param name
	 *            the employee's full name
	 * @param age
	 *            the employee's age in whole years
	 */
	public Employee(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder rep = new StringBuilder(getName());
		rep.append("\n\t Employee ID: " + this.getId());
		rep.append("\n\t Age:" + this.getAge());
		return rep.toString();
	}

	/**
	 * Returns the employee's id
	 * 
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * Sets the employee's id
	 * 
	 * @param id
	 *            the id to set
	 */
	public void setEmpId(int empId) {
		this.id = empId;
	}

	/**
	 * @return the employee's name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            -- the name of the employee
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * @param age
	 *            -- the age of the employee
	 */
	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * Compares employees by their ids
	 */
	@Override
	public int compareTo(Employee o) {
		// note how this simple statement does what we want
		return this.id - o.id;
	}

	public static void main(String[] args) {
		Employee a = new Employee(12, "Homer J. Simpson", 38);
		Employee b = new Employee(1, "Charles Montgomery Burns", 104);
		Employee c = new Employee(3, "Waylon Smithers", 43);
		Employee d = new Employee(14, "Carl Carlson", 38);
		Employee e = new Employee(15, "Lenny Leonard", 38);

		System.out.println("Comparing " + a.getName() + " with " + b.getName()
				+ ": " + a.compareTo(b));

		System.out.println("\nSorted: ");
		Employee[] employees = { a, b, c, d, e };
		Arrays.sort(employees);
		for (Employee emp : employees) {
			System.out.println(emp);
		}
	}
}

./EmployeeNameComparator.java 2/2

[
top][prev][next]
package employee;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Sort the Employees by their names, instead of the default setting.
 * 
 * @author sprenkle
 * 
 */
public class EmployeeNameComparator implements Comparator<Employee> {

	/**
	 * Compare Employees by their names
	 */
	@Override
	public int compare(Employee o1, Employee o2) {
		return o1.getName().compareTo(o2.getName());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Employee a = new Employee(12, "Homer J. Simpson", 38);
		Employee b = new Employee(1, "Charles Montgomery Burns", 104);
		Employee c = new Employee(3, "Waylon Smithers", 43);
		Employee d = new Employee(14, "Carl Carlson", 38);
		Employee e = new Employee(15, "Lenny Leonard", 38);

		System.out.println("Comparing " + a.getName() + " with " + b.getName()
				+ ": " + a.compareTo(b));

		List<Employee> employees = new ArrayList<Employee>();
		employees.add(a);
		employees.add(b);
		employees.add(c);
		employees.add(d);
		employees.add(e);

		System.out.println("\nSorted natural order: ");
		Collections.sort(employees);

		for (Employee emp : employees) {
			System.out.println(emp);
		}

		System.out.println("\nSorted by name: ");

		Collections.sort(employees, new EmployeeNameComparator());

		for (Employee emp : employees) {
			System.out.println(emp);
		}
	}
}

Generated by GNU Enscript 1.6.6.