import java.util.ArrayList; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; /** User login accounting program. * Let's read a text file containing a server log. Each line contains * information about 1 login session. On this line of text, the first * word is the username. For now, let's ignore the rest of the line. * We are interested in keeping track of ALL users and finding out the * total number of times EACH person has logged in. * This program features an ArrayList, because we don't know in advance * how many users there have been. */ public class Driver { public static void main(String [] args) throws FileNotFoundException { // To make the program more concise, let's assume the name of the // input file is ultra1.txt. Scanner in = new Scanner(new FileInputStream("ultrax1.txt")); // We need an ArrayList to store users. ArrayList list = new ArrayList(); // For each line in the file, we have a new login to take care of. // We want to stop once we read a blank line or there are no more lines. while (in.hasNextLine()) { String line = in.nextLine(); if (line.equals("")) break; // At this point we know the line is not blank. // Scan this line so we can read the first word. Scanner lineScanner = new Scanner(line); String userName = lineScanner.next(); // Now we have the name of the user associated with this login session. // Question: Have we seen this user listed already? // If so, find the user, and add 1 to that person's login count. // If not, create a new user, add to list, and start login count at 1. // Look thru the list and see if we have this name. boolean found = false; for (int i = 0; i < list.size(); ++i) if (list.get(i).getName().equals(userName)) { // I found the name in the list! // The object list.get(i) is the user object in question, // so all we need to do is add 1 to its number of logins. list.get(i).addLogin(); found = true; break; } // If UNsuccessful in finding the user in the list, create new user, // and append to the list. if (! found) { User newUser = new User(userName, 1); list.add(newUser); // don't forget! } } // We're done reading the file. // Output the list. System.out.printf("There are %d users.\n", list.size()); System.out.printf("Here are the user login totals per user:\n\n"); // loop to sum int sum = 0; for (int i = 0; i < list.size(); ++i) sum += list.get(i).getNumLogins(); System.out.printf("I found a total of %d logins.\n", sum); // Sort the ArrayList by the number of logins. Descending. for (int i = 0; i < list.size(); ++i) for (int j = i+1; j < list.size(); ++j) { if (list.get(i).getNumLogins() < list.get(j).getNumLogins()) { User temp = list.get(i); // WRONG: list.get(i) = list.get(j) list.set(i, list.get(j)); // WRONG: list.get(j) = temp; list.set(j, temp); } } // Print out the list in an attractive format. for (int i = 0; i < list.size(); ++i) System.out.printf("%s\n", list.get(i).toString()); // Query: the user will enter a name, and we'll look that person // up in the list and print out that persons' # of logins Scanner kbd = new Scanner (System.in); System.out.printf("Enter a name: "); String desiredName = kbd.next(); boolean found = false; for (int i = 0; i < list.size(); ++i) { if (desiredName.equals(list.get(i).getName())) { found = true; System.out.printf("%s logged in %d times.\n", desiredName, list.get(i).getNumLogins()); } } if (! found) System.out.printf("That user never logged in.\n"); } }