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 Jan 30, 2010
* @bugs None
*/
public class ChengJade3 {
/**
* The main entry point of the application.
*
* @param args The command line arguments (not used).
*/
public static void main(String[] args) {
// Define a scanner that reads input from the user.
final Scanner scanner = new Scanner(System.in);
// Define a telephone directory and the user input string variable.
final TelephoneDirectory myDirectory = new ArrayTelephoneDirectory();
String input = null;
// Loop repeatedly until the directory is full.
while (true) {
// Ask the user to type a new item for the telephone directory.
System.out.print("Please enter a entry: ");
// Read the string and check for errors.
try {
input = scanner.nextLine();
} catch (NoSuchElementException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (IllegalStateException e) {
System.err.println(e.getMessage());
System.exit(2);
}
// Add the item to the telephone directory and check for errors.
try {
myDirectory.add(input);
} catch (DirectoryFullException e) {
System.err.println(e.getMessage());
System.exit(3);
}
// Print out the contents of the telephone directory.
System.out.println(myDirectory.toString());
}
}
}
/**
* 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 Jan 30, 2010
* @bugs None
*/
class ArrayTelephoneDirectory implements TelephoneDirectory {
String[] array = new String[5];
int size = 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(String item) throws DirectoryFullException{
assert item != null;
// Check if the directory is full, and if it is throw the
// DirectoryFullException.
if (this.size == 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.size++] = item;
}
/**
* Returns a readable version of the contents of the telephone directory.
*
* @return A readable version of the contents of the telephone directory.
*/
public String toString() {
// If the directory is empty, return a special note.
if (this.size == 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 " + this.size + " items:\n");
// Loop over each item added to the directory, and add it to the string builder.
for (int i = 0; i < this.size; i++)
builder.append((i + 1) + ". " + this.array[i] + "\n");
// Return the list created above.
return builder.toString();
}
}
/**
* The interface to a telephone directory.
*
* @author Cheng Jade
* @assignment TA 3
* @date Jan 30, 2010
* @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;
}
/**
* An exception that indicates the user attempted to add an item to a telephone
* directory when it was full.
*
* @author Cheng Jade
* @assignment TA 3
* @date Jan 30, 2010
* @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.");
}
}