import java.util.NoSuchElementException; import java.util.Scanner; /** * This class repeatedly reads strings from the user and stores the strings into * the telephone directory. After each string is read and inserted, the * telephone directory is printed. * * @author Cheng Jade * @assignment TA 3 * @date Sep 16, 2008 * @bugs None */ public class ChengJade03 { /** * The main entry point of the application. * * @param args The command line arguments (not used). */ public static void main(final String[] args) { // Define a scanner that reads input from the user. final Scanner scanner = new Scanner(System.in); // Define a telephone directory. final TelephoneDirectory directory = new ArrayTelephoneDirectory(); // Loop repeatedly until the directory is full. while (true) { // Print out the contents of the telephone directory. System.out.println(directory); // Ask the user to type a new item for the telephone directory. System.out.print("Type a name to add to the telephone directory: "); String item = null; // Read the string and check for errors. try { item = scanner.nextLine(); } catch (final NoSuchElementException e) { System.err.println("No more tokens are available."); System.exit(1); } catch (final IllegalStateException e) { System.err.println("The scanner has closed."); System.exit(2); } // Add the item to the telephone directory and check for errors. try { directory.add(item); } catch (final DirectoryFullException e) { System.err.println(e.getMessage()); System.exit(3); } } } } /** * An exception that indicates the user attempted to add an item to a telephone * directory when it was full. * * @author Cheng JadeYu * @assignment TA 3 * @date Sep 16, 2008 * @bugs None */ @SuppressWarnings("serial") class DirectoryFullException extends Exception { /** * Initializes a new instance of the class. */ public DirectoryFullException() { // Automatically assign the message text. super("The directory is full."); } } /** * This class satisfies the TelephoneDirectory interface by providing the add * method. It also provides a toString method that returns a readable version of * the contents of the directory. * * @author Cheng Jade * @assignment TA 3 * @date Sep 16, 2008 * @bugs None */ class ArrayTelephoneDirectory implements TelephoneDirectory { private final String[] array = new String[5]; private int count = 0; private int count2 = 0; /** * Adds an item to the telephone directory. * * @param item The item to add to the directory. * @throws DirectoryFullException Thrown if the directory is full. */ public void add(final String item) throws DirectoryFullException { assert item != null; // Check if the directory is full, and if it is throw the // DirectoryFullException. if (this.count == this.array.length) { throw new DirectoryFullException(); } // Add the item to the directory array, and keep track of how many items // have been added. this.array[this.count++] = item; } /** * Returns a readable version of the contents of the telephone directory. * * @return A readable version of the contents of the telephone directory. */ @Override public String toString() { // If the directory is empty, return a special note. if (this.count == 0) { return "The telephone directory is empty.\n"; } // Return the number of items in the directory. StringBuilder builder = new StringBuilder(); builder.append("The telephone directory contains "); builder.append(this.count); builder.append(" items:\n"); // Loop over each item added to the directory, and add it to the string builder. for (int i = 0; i < this.count; i++) { builder.append(" "); builder.append((i + 1)); builder.append(". "); builder.append(this.array[i]); builder.append("\n"); } // Return the list created above. return builder.toString(); } } /** * The interface to a telephone directory. * * @author Cheng Jade * @assignment TA 3 * @date Sep 16, 2008 * @bugs None */ interface TelephoneDirectory { /** * Adds an item to the telephone directory. * * @param item The item to add to the directory. * @throws DirectoryFullException Thrown if the directory is full. */ void add(String item) throws DirectoryFullException; }