// CSE 143, Winter 2010, Marty Stepp // This program hires and fires teaching assistants based on their experience. // It demonstrates the PriorityQueue class. import java.io.*; import java.util.*; public class FiringSquad { private static final int MIN_EXPERIENCE = 3; private static final String TAS_FILE = "tas.txt"; private static final String REPLACEMENTS_FILE = "replacements.txt"; public static void main(String[] args) throws IOException { // read TAs from file List taList = getTAsFromFile(TAS_FILE); System.out.println(taList); System.out.println(); // load TAs into a priority queue Queue queue = new PriorityQueue(); queue.addAll(taList); System.out.println(queue); // prompt for TAs to fire Scanner console = new Scanner(System.in); System.out.print("How many TAs to fire? "); int fire = console.nextInt(); // fire the given number of TAs for (int i = 0; i < fire; i++) { queue.remove(); } // Replace fired TAs with new ones from the replacements file List replacementList = getTAsFromFile(REPLACEMENTS_FILE); for (int i = 0; i < fire; i++) { queue.add(replacementList.get(i)); } // print the remaining TAs in order of experience while (!queue.isEmpty()) { System.out.println(queue.remove()); } } // Given a file name, attempts to read the names and experience levels of // TAs from that file. Each TAs data is inserted into a TA object, and the // lot are returned in a list. private static List getTAsFromFile(String filename) throws IOException { List list = new ArrayList(); Scanner in = new Scanner(new File(filename)); while (in.hasNext()) { String name = in.next(); int exp = in.nextInt(); TA ta = new TA(name, exp); list.add(ta); } return list; } }