Case Study: Designing A Telephone Directory Program Ellen Walker CPSC 201 Data Structures Hiram College Includes figures from Objects, Abstraction & Data Structures in Java v. 5.0, by Koffman & Wolfgang, Wiley © 2005
The Problem Client wants to store a simple telephone directory in her computer Use for storage & retrieval of names & numbers Create from existing data file Insert new names & numbers Change numbers Retrieve numbers Save in data file
Requirements: Inputs & Outputs Inputs –File: one name & number per line –Interactive: name & number when requested Outputs –Name(s) & number(s) - one per line –File: complete directory in sequence (same format as input file)
Analysis Use Case –List user actions and system responses for a particular subproblem (case) as they occur For the Phone Directory 1.Read directory from existing file 2.Insert new entry 3.Edit existing entry 4.Retrieve and display entry
Read Directory From File 1. User issues command to load & run directory 2. System starts program, initializing directory from data file. If the data file does not exist, an initially empty data file is created.
Insert New Entry / Edit Entry 1.User issues insert or edit command 2.System prompts for name 3.User enters name (If canceled, terminate) 4.System prompts for number 5.User enters number (If canceled, terminate) 6.Directory is updated with new name/number (user is informed whether new entry is added or entry was changed; if changed both old and new numbers are shown)
Retrieve Entry
Initial Design (subproblems)
Class Diagram (initial)
Class Diagram (Revised)
loadData() addOrChangeEntry() lookupEntry() … > PhoneDirectoryInterface + loadData() + addOrChangeEntry() +lookupEntry() … ArrayListsBasedPD - String name - String number + getName() + getNumber() … DirectoryEntry processCommands() > PDUserInterface PDGUI PDConsole + main() PDApplication
List of Classes PDUserInterface - interacts with the user PDApplication - contains main, instantiates everything else PhoneDirectoryInterface - the interface for a phone directory ArrayListBasedPD - an implementation of that interface DirectoryEntry - a name/number pair
PDApplication One Method, main –Create a new PhoneDirectory object –Send it a message to read the initial directory data from a file –Create a new PDUserInterface Object –Send it a message to perform all user operations on the PhoneDirectory object This method will set up a loop that accepts and executes commands (using internal methods)
DirectoryEntry Class
PhoneDirectory Interface
ArrayBased PD implements PhoneDirectory
Class structure
Implementing and Testing the Array- Based Phone Directory
Implementing and Testing the Array- Based Phone Directory (continued) Note that some code in this application is controversial –Combination of assignment with the evaluation of a condition –Break statement allows exiting of the while loop without storing an entry
Controversial Code
Implementations of PDUserInterface PDGUI (graphical) PDConsoleUI (text-based)