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
Dec 30, 2015
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 Directory1. 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 command2. System prompts for name3. User enters name (If canceled, terminate)4. System prompts for number5. 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)
Class Diagram (Revised)
loadData()addOrChangeEntry()lookupEntry()…
<<interface>>PhoneDirectoryInterface
+ loadData()+ addOrChangeEntry()+lookupEntry()…
ArrayListsBasedPD
- String name- String number+ getName()+ getNumber() …
DirectoryEntry
processCommands()
<<interface>>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)
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