You are on page 1of 6

THE UNIVERSITY OF HONG KONG

COMP1117B Computer Programming I


Assignment 5 (Updated)
Deadline: 17:00pm 03/12/2015
1. [100%] Write a program to manage an address book. The file that contains the main function
should be named main.cpp. The header file of the Person class should be named Person.h. The
header file of the ContactBook class should be named ContactBook.h. The implementation of these
two classes should be named Person.cpp and ContactBook.cpp respectively. You must strictly
follow the name of files above.
This assignment gives you a chance to implement an object-oriented program. You are asked to manage
a simplified address book. You need to implement two classes ContactBook and Person, whose
definitions are given as follows:
class ContactBook {
public:
ContactBook();
void add(string name, string addr, string phone, string relation);
void delete_contact(string name);
void edit_relation_based_on_addr(string addr, string rel);
void edit_contact_phone(string name, string phone);
//This is an auxiliary function. Find a contact based on the name passed in, if found,
return the index of this contact in plist; if not found, return -1.
int find_contact_name(string name);
// write the contacts in the plist to a file with filename specified in the parameter.
void write_to_file(const char* filename);
private:
Person plist[50];
int size;
int max_size;
};
class Person {
public:
//Default constructor function, you do nothing in this function.
Person(){};
//Another constructor function to construct a person.
Person(string name, string addr, string phone, string relation);
// The following set of functions are auxiliary functions that help access the private
member variables name, address, phone and relation. Maybe not all of them will be used in this
assignment, for the completeness, please implement them all. get functions are used to return
corresponding variables, and set functions are used to set corresponding variables to the value of
the parameter.
string get_name();

string get_addr();
string get_phone();
string get_relation();
void set_name(string a_name);
void set_addr(string addr);
void set_phone(string a_phone);
void set_relation(string a_relation);
private:
string name;
string address;
string phone;
string relation;
};

All member functions explain themselves well by the name of the function. The definition of the class
ContactBook should be put in the file ContactBook.h, and that of Person should be put in the file
Person.h. Your first task is to implement all the member functions of ContactBook and Person. Your
implementation of ContactBook's member function should be put in the file ContactBook.cpp, and those
of Person should be put in the file Person.cpp.
Your second task is the implement the main function. It should first read data from a text file called
contact.txt (which is in the same directory as your program).
Following shows an example of the content of the file.
Mell 51232345 Central friend
Jerry 56789876 Kennedy_Town friend
Bill 23456789 North_Point family
Eric 78949876 Jordan partner

Format: Each line of the file contains 4 fields separated by a single white space. The first field is the
name of the contact. The second field is the phone number. The third field is the address of the contact.
Note that the words in an address are joined by the character `_'. The last field is the relationship with
the contact. There is no length restriction on the length of each field. Normally, in our test cases, the
length wont exceed 30.
The number of contacts in contact.txt is no more than 50. All names are unique in the contact book.
In the main function, your program should declare an object variable of class ContactBook, and add
each contact read from contact.txt into this variable. Then, it should print a menu shown as below and
repeatedly ask the user to choose an option until the user selects ``Exits.

For each option, you need to perform the corresponding task as described below. All task specifications
are given based upon the content of contact.txt below.

Tom 56732345 Central_Luk_Building partner


Jeremy 1231231 Central_ifc partner
Morry 2312341 Central_Lan_Fong_House partner
Lucy 1231232 Kennedy_Town friend
Bill 2345123 Jordan family

1. Add a contact: void add(string name, string addr, string phone, string relation)
First, your program should let the user input the name, phone number, address and the
relationship of a new contact into your contact. (1) When adding a contact, you should check
whether the contact is already in the address book, and you should not add it again if it is
already in the book. For example, if the user wants to add Bill into the contact, you should tell
the user that Bill has already in the contact book. (2) Also, you should be aware of the number
of contacts should be controlled within 0-50. If a contact to be added is the 51th one, you
should not add it; instead, you should print the message "Too many contacts!" (3) The new
contact should be appended to the end of the existing contact list, which means that if Eric is
going to be added to the contact list shown above, it should be stored after Bill. An example is
shown here.

2. Delete a contact: void delete_contact(string name)


First you should ask the user to input the name of the contact and then delete it from the
contact list. When the user inputs a name that doesnt exist in the contact book, you should
output the message "The contact doesn't exist!". Here is an example:

3. Edit relationship based on the address: void edit_relation_based_on_addr(string addr,


string rel)
For this task, you are asked to find all contacts with the address that contains the substring the
user provides and change their relationships to the one the user specifies. If such contacts
doesnt exist, you do nothing. You should first ask the user to input a substring of an address
and the relationship he wants to change to. Then, you change the relationship of all contacts
that have an address containing the inputted substring to the one the user specifies. For
example,

In this example, the user inputs Central and leader, then the relationship of Tom, Jeremy and
Morry will all be changed to leader, since their address contains the substring Central. Note
that there is no break line after wan; it breaks a line just due to the limited size of the
command window. Also, you may assume that there is no white space in the substring.

4. Edit the phone number of a contact: void edit_contact_phone(string name, string


phone)
The last task asks you to edit the phone number of a given contact. Its similar to task 3. You first
let the user input the name of the contact and the phone number he wants to change to. Then
you edit the phone number of the given contact to the one the user specifies. Note that the
contact might not exist, in such case, you should tell the user that the user doesnt exist. For
example,

After the user selects Exit, the contact list should be well managed and updated after a sequence of
options. Then you are asked to write the new contact book into the file named new_contact.txt with
the same format as the input file (Each contact one line, with a single white space as the separator
between the fields). --- void write_to_file(const char* filename);

This last step is very important because your program will be judged based on the output file.

Sample run:
In the sample run, the input file is the same as the one we use to describe tasks shown as the second
text block at the first page.
The following is a screen shot of a sequence of options:

Now, the ``new_contact.txt has the content shown as below:


Jeremy 424252 Central_ifc leader
Morry 2312341 Central_Lan_Fong_House leader
Lucy 1231232 Kennedy_Town friend
Bill 2345123 Jordan family
Violet 1234525 Milky_Way friend
Eric 7898798 Canada friend
Hint: You may add a print function (to print the current contact list) to the ContactBook for debugging.

Important Notes:
I.
Your program must follow the formats of the sample input / output strictly.

II.
III.

Your program should be properly indented and documented.


You should only submit source codes (*.cpp) and header codes (*.h) but not executables.
You should submit 5 files in total.

IV.

The .cpp file that contains the main function must be named main.cpp. The header file of
Person class should be named Person.h. The header file of ContactBook class should be
named ContactBook.h. The implementation of these two files should be named Person.cpp
and ContactBook.cpp respectively. You must strictly follow the name of files above.

V.

Please make sure that your source code could be compiled in Code::Blocks environment
before submission.

Handin:
Submit your program electronically using the Moodle system to Assignment 5 under
Assignments section. Late submission will NOT be accepted.

You might also like