// CSE 143, Winter 2011, Marty Stepp // This program hires and fires teaching assistants based on their experience. // It demonstrates the use of the PriorityQueue data structure. import java.io.*; import java.util.*; public class FiringSquad { public static void main(String[] args) throws IOException { // read all current TAs from a file into a priority queue Queue queue = readTAs("tas.txt"); System.out.println(queue); System.out.println(); int oldSize = queue.size(); // FIRE all TAs with <= 2 quarters of experience while (!queue.isEmpty() && queue.peek().getExperience() <= 2) { queue.remove(); // Donald Trump: "You're fired." } int toHire = oldSize - queue.size(); System.out.println("fired " + toHire + " TAs."); System.out.println(queue); // hire an equivalent number of new replacement TAs Queue newTAs = readTAs("replacements.txt"); for (int i = 0; i < toHire; i++) { queue.add(newTAs.remove()); } // print all TAs in increasing order of experience while (!queue.isEmpty()) { TA ta = queue.remove(); System.out.println(ta); } } // Reads all TAs from the file with the given name and returns them // as a priority queue of TA objects. // Each line contains a name and quarters of experience, such as: Lisa 12 // Precondition: filename != null and refers to a file that exists, // can be read, and is in the proper format public static Queue readTAs(String filename) throws FileNotFoundException { Queue queue = new PriorityQueue(); Scanner in = new Scanner(new File(filename)); while (in.hasNext()) { String name = in.next(); int exp = in.nextInt(); TA ta = new TA(name, exp); queue.add(ta); } return queue; } }