import java.util.Scanner; import java.util.Hashtable; import java.util.Enumeration; import java.util.StringTokenizer; import java.util.ArrayList; import java.util.Collections; import java.io.FileInputStream; import java.io.FileNotFoundException; /** Driver.java for User Login lab. * This is the only file you need to modify. * The purpose is to practice with Java's built-in Hashtable class. * We'll read an input file that stores user login information for * a computer system. Each line of the file contains a user's name * and an amount of time logged in. See the file login.txt. Read * the file, and create a Hashtable of users. The keys will be the * user names, and the values will be the minutes of computer time * that the person was logged in. * As you read in the data, you need to note if you have ever seen * this person before. If the person is already in the hash table, * then just update the value, otherwise insert a new name/minutes * pair into the hash table. * When you're done, dump the contents of the hash table into an * ArrayList and sort the users in descending order of login time. * Print the users' info, one person per line. */ public class Driver { public static void main(String [] args) throws FileNotFoundException { // This declaration says that the keys are Strings (i.e. names) // and the values are integers (total minutes of login time). Hashtable table = new Hashtable(); Scanner in = new Scanner(new FileInputStream("login.txt")); while(in.hasNextLine()) { // Part 1: Input // The first 8 characters on the line are the user name. // The amount of login time is given as hh:mm inside parentheses. // We can tokenize on () and look for the 2nd token. // From hh and mm we can compute the number of minutes // for this login session. // Part 2: Insert/update information in Hashtable. // Have we seen this user before? If so, just update time. if (false) { } // Otherwise, we need to put this user name and time into // the Hashtable for the first time. else { } } // Part 3: Now we are done reading the file and initializing the // hash table. Time to sort and output. // A "User" is someone who has a username and number of login minutes. // Create an ArrayList of User objects. // Use the technique we did in the Hash2 example of food to // "enumerate" the keys of the table, in order to dump each (key,value) // pair into the array list. // Use the Java API and UserComparator class to sort the list, // and then print it out. // The names in the output should be left justified, and the numbers // should be right justified. } }