home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX08064.CPP
- // member functions of the class PhoneBk
- //--------------------------------------------------------------
-
- // files in this example:
- // %F,15,EX08061.H%EX08061.H definition of class String
- // %F,15,EX08062.H%EX08062.H definition of class PhoneNum
- // %F,15,EX08063.H%EX08063.H definition of List and ListIter
- // %F,15,EX08064.H%EX08064.H definition of class PhoneBk
- // %F,15,EX08061.CPP%EX08061.CPP member functions of the String class
- // %F,15,EX08062.CPP%EX08062.CPP member functions of the class PhoneNum
- // %F,15,EX08063.CPP%EX08063.CPP member functions List and ListIter
- // EX08064.CPP this file
- // %F,15,EX0806.CPP%EX0806.CPP main to exercise PhoneBk class
- //--------------------------------------------------------------
- #include "EX08064.H"
- #include "EX08063.H"
-
- //--------------------------------------------------------------
- // Constructor
- //--------------------------------------------------------------
- PhoneBk::PhoneBk() : numEntry (0), maxsize (100), list (100)
- { };
-
- //--------------------------------------------------------------
- // The function insert() adds or changes an entry in a phonebook
- // PARAMETERS: char* who the name of the new entry- if
- // already in the book, change number
- // long telnum then new telephone number
- // RETURNS: int 1 success
- // int 0 failure, phonebook full
- //--------------------------------------------------------------
- int PhoneBk::insert( char* who, long telnum)
- // look at each name entry in the book to see if its matches who
- { ListIter place;
- for( int i = 0; i < numEntry; i++)
- { PhoneNum* entry = place.next(list);
- if( entry->compare(who ) )
- continue;
- // If name matched, set the associated telephone number = telnum
- entry->change(telnum);
- return ( 1 );
- }
- // If the book is full, new name and number can not be added
- if( numEntry == 100 )
- return ( 0 );
- // Add new entry
- PhoneNum newentry( who, telnum);
- list.add(newentry);
- numEntry++;
- return ( 1 );
- };
-
- //--------------------------------------------------------------
- // The function remove() deletes an entry from a PhoneBk object
- // PARAMETERS: char* who the name of the entry to delete
- // RETURNS: int 1 success
- // int 0 failure, name not in phonebook
- //--------------------------------------------------------------
- int PhoneBk::remove( char* who)
- // look at each name entry in the book to see if its matches who
- { ListIter place;
- for( int i = 0; i < numEntry; i++)
- { PhoneNum* entry = place.next(list);
- if( entry->compare( who ) )
- continue;
- // entry found, delete it - do not leave gaps between entries
- // is this the only entry?
- list.del(entry);
- --numEntry;
- return ( 1 );
- };
- // if name not found in book, return failure code
- return ( 0 );
- };
-
- //--------------------------------------------------------------
- // The function lookup() finds a name in a PhoneBk object
- // and prints associated telephone number
- // PARAMETERS: char* who the name of the entry
- // RETURNS: long !=0 success, the telephone number
- // long 0 failure, name not in phonebook
- //--------------------------------------------------------------
- long PhoneBk::lookup( char* who)
- // look at each name entry in the book to see if its matches who
- { ListIter place;
- for( int i = 0; i < numEntry; i++)
- { PhoneNum* entry = place.next(list);
- if( entry->compare( who ) == 0 )
- // return telephone number associated with matching name
- return ( entry->gettel());
- }
- // if name not found in book, return failure code
- return ( 0 );
- };
-
- //--------------------------------------------------------------
- // The function print() lists every name and telephone number
- // PARAMETERS: ostream& os the output stream
- // RETURNS: ostream& os the output stream
- //--------------------------------------------------------------
- ostream& PhoneBk::print (ostream& os)
- // consider every entry in turn
- { ListIter place;
- for( int i = 0; i < numEntry; i++)
- { PhoneNum* entry = place.next(list);
- // display name, tab for formatting, telephone number, newline
- entry->print(os);
- }
- os << "EOF" << endl;
- return(os);
- };
-
- //--------------------------------------------------------------
-