Programming assignment #8
Developing a simple contact-information database program
Purpose:
We
would like to define and implement a ContactInfo class and use objects
of this ContactInfo class to store and process the contact information of
people. You will need to use the DateType class and the C++ string class in the implementation of a
ContactInfo class. Well then use this ContactInfo class as a
building block in implementing a simple contact information database program.
What to do:
- Download the
basic code framework: Download this zip
file and unzip it to get a project containing the basic code to work on.
You will also find DateType.h
and DateType.cpp included in it.
You should replace these tow files with your own version of DateType.h and DateType.cpp instead.
- Understand the
design of the ContactInfo class:
Look at the interface (i.e. the member functions and data members) of the ContactInfo
class as defined and commented
in ContactInfo.h to
understand what kind of data are stored in a ContactInfo object and
what kind of services (member functions) a ContactInfo object can
do for you.
- Implement the of the ContactInfo class: ContactInfo.cpp contains
the implementation details of the member functions of the ContactInfo
class. The implementation there is not completed yet. You have to fill in
code to complete the implementation of these member functions according to the specification comments
of these member functions in ContactInfo.h. Regarding the
implementations of the relational operator > and others like < and <=, we define a total order over ContactInfo
objects by defining that C1 >
C2 is true if and only if either
we have C1.CompareFullName (C2) = = GREATER or we have C1.SameFullName (C2) = = true
and C1.CompareBirthday (C2) = =
GREATER.
- Test the implementation of your ContactInfo class: The main
function in ContactTest.cpp given
to you is intended as the test ground for testing whether your
implementation of the ContactInfo class is sound. At the
moment, we have a vector of ContactInfo objects (initialized to
hold 5 ContactInfo objects) to serve as a contact-information
database. The code in the main function then does some basic operations
such as reading the contact information of 5 persons into the database,
display the contact information of these people from the database, search
the database for the contact information of persons of a particular name,
and search the database to find out the contact information of all people
born on a particular date. You should add similar code to extensively test
your implementation in Step 3 above.
- Incorporate and
revise the functions mergeTwoSortedVectors
and mergeSort similar to what you did in Programming #2 or #5 into ContactTest.cpp
to provide the capability of
sorting ContactInfo objects
instead of double values or dateType objects: First, make a copy of these two functions
from programming #2 or #5
into ContactTest.cpp in this project. Modify
the prototype bool mergeTwoSortedVectors(vector<double> & vecA,
vector<double> & vecB, vector<double> & vecC) to bool
mergeTwoSortedVectors(vector< ContactInfo e> & vecA, vector< ContactInfo > & vecB, vector< ContactInfo e > & vecC) and modify the
implementation so that it can merge two sorted vectors of ContactInfo objects. Modify the
prototype of bool mergeSort(vector<double> &
vecToSort) to bool mergeSort(vector< ContactInfo > & vecToSort) and modify the implementation so that it
can sort a vector of ContactInfo
objects. Add code in the main function to test and verify that the
newly modified functions can work
well for merging and sorting ContactInfo objects.
- Implement your own very simple
contact information database program: Make a copy of
ContactTest.cpp in the project
folder and name it ContactDB.cpp.
Under Visual studio, remove ContactTest.cpp from
the project and add ContactDB.cpp into the project. Then totally
rewrite the main function in ContactDB.cpp to provide a menu
serving as a simple database user interface similar to what you did
for Program#5. See the details in the following.
- Extensively test all the services provided by your program.
Detailed
specification of the main function in ContactDB.cpp:
Overview:
Your main function in Contact.cpp should provide services to read
dates from the keyboard or files, save the dates into files, search for dates
within a given range of dates, and sort dates in order. To accomplish these
purposes, in the main function well use a vector<ContactInfo>
object as a container in the main memory to store and manage the date
information by using various operators and member functions of the ContactInfo class. See more details
below.
Key data structures:
·
To dynamically store the information of dates,
you should declare in your main function vector<ContactInfo> contactDB;
to create a local vector for storing ContactInfo
objects. You should also declare in your
main function a local ContactInfo object
like aContact, local string objects like string aFirstName, aLAstName; and DateType objects like DateType
dBegin, dEnd; for storing temporary date information.
·
Add the line #include
<fstream> in the beginning of the .cpp file for file I/O support. In the main function, you
should declare a character array char filename[20] for storing the file name given
by the user. You should declare two file handles ifstream fin; and ofstream fout;
for file input/output purposes respectively. You should also declare in your
main function local integer variable int numContsctInfoRecords;
for temporarily storing the information of the number of date records involved
in the file I/O.
Service menu: The main function should set up a
loop that would repeatedly display a menu to prompt the user to choose one of
the following services:
- K: to read a ContactInfo object manually Keyed in by the user and store it (i.e. push_back) in the end of the vector contactDB,
- R: to clear the
vector contactDB into an empty vector first and then Read ContactInfo objects from a file
specified by the users into the vector contactDB,
- D: to Display the ContactInfo objects currently
stored in the vector contactDB to the screen,
- M: to Modify one of the ContactInfo objects by
reading a new ContactInfo object from the keyboard to overwrite the
contents of an existing ContactInfo object
(specified by the user) in the vector contactDB,
- X: to eXclude one of the ContactInfo objects by
removing it from the vector contactDB,
- W: to Write the ContactInfo objects currently
stored in the vector contactDB into a file specified by the user,
- B to Find and
display ContactInfo objects with Birthdays
falling between two dates specified by the user,
- N to Find and
display ContactInfo objects with the full Name given
by the user,
- S: to Sort the ContactInfo objects currently
stored in the vector contactDB in order,
- Q: to Quit
the program.
Implementation of the services: On each
iteration of that loop, the program should read the users choice and use a switch
statement to do things according to the choice:
- If the choice is K:
the program should ask the local ContactInfo
object aContact to read in a new
record of contact information (i.e.
cin >> aContact; ) and then add the ContactInfo object into the end of the vector contactDB
(i.e. contactDB.push_back(aContact); ).
- If the choice is R:
the program should ask the user to provide the name (less than 20
characters) of an input file (e.g. dates.txt),
and read the file name into a character array char filename[20] (i.e. cin >> filename; ).
The program should then open that file through the input file
object fin (i.e. fin.open(filename ); ) and read the
first integer from the file into numContsctInfoRecords (i.e.
fin >> numContsctInfoRecords; ), and the program should check to make sure this number
is non-negative. The program should clear contactDB (i.e.
contactDB.clear( );
) to an empty vector, and then set up a loop that iterates
for numContsctInfoRecords iterations to repeatedly read in a ContactInfo object from the file
into the local DateType object date (i.e. fin
>> date;
) and then add the date into the end of the vector contactDB (i.e. contactDB.push_back(date); ). After the loop is
finished, the program should close that file (i.e. fin.close( ); ).
- If the choice is D:
the program should set up a loop to
display each element contactDB
[i] in the vector contactDB (i.e. cout << dateDB[i] ;
).
- If the choice is M:
the program should ask the user the index i of the date in contactDB[i] he/she wants to modify, read the index as an integer from
the user, check to make sure i is non-negative and less than contactDB.size( ), and read the new contact
information into contactDB[i]
(i.e. cin >> contactDB[i] ; ).
- If the choice is X:
the program should ask the user the index i of the date in contactDB[i] he/she wants to exclude, read the index as an integer from
the user, check to make sure i is non-negative and less than contactDB.size( ), and then erase the object
from the vector by calling the erase method (i.e. contactDB.erase(contactDB.begin(
)+ i;
).
- If the choice is W:
the program should ask the user to provide the name (less than 20
characters) of an output file (e.g. dates.txt),
and read the file name into a character array char filename[20]. The program should then open that file
through the output file object fout
(i.e. fout.open(filename ); ) and write the
size of contactDB into the file (i.e.
fout << contactDB.size( ); ). The program should then set up a loop that iterates
for contactDB.size( ) iterations to repeatedly write each element contactDB[i] into the file (i.e. fout
<< contactDB[i]);
). After the loop is finished, the program should close that
file (i.e. fout.close( ); ).
- If the choice is N,
the program should ask the user to provide the first name and the last
name (i.e. cin >> dBegin; and cin >>
dEnd; ), and then the
program should set up a loop to
check each element contactDB[i]
in the vector contactDB and display the contents of contactDB[i] if the birthday field of contactDB[i] is within the range (i.e.
if contactDB[i].CompareBirthday( dBegin)!=
LESS && contactDB[i].CompareBirthday(
dEnd)!= GREATER is true).
- If the choice is B,
the program should ask the user to provide the beginning date and the
ending date of the date range (i.e. cin >>
aFirstName; and cin >>
aLastName;
), and then the program should set up a loop to check each element contactDB[i] in the vector contactDB
and display the contents of contactDB[i] if contactDB[i] has the same first name and
the last name (i.e. if contactDB[i].SameFullName(aFirsttName,
aLastName) = = true).
- If the choice is S,
the program should call the mergeSort function sort the dates in order (i.e.
mergeSort(contactDB ); ).
- If the choice is Q,
the program will output a thank-you message, exit the loop, and end the
program.
Reference:
·
Vector class: Member Functions with no
iterators used:
|
back
|
Returns a reference to the last element of the vector.
|
|
capacity
|
Returns the number of elements that the vector could
contain without allocating more storage.
|
|
clear
|
Erases the elements of the vector.
|
|
empty
|
Tests if the vector container is empty.
|
|
front
|
Returns a reference to the first element in a vector.
|
|
max_size
|
Returns the maximum length of the vector.
|
|
pop_back
|
Deletes the element at the end of the vector.
|
|
push_back
|
Add an element to the end of the vector.
|
|
resize
|
Specifies a new size for a vector.
|
|
reserve
|
Reserves a minimum length of storage for a vector object.
|
|
size
|
Returns the number of elements in the vector.
|
|
swap
|
Exchanges the elements of two vectors.
|
|
vector
|
Constructs a vector of a specific size or with elements of
a specific value or with a specific allocator or as a copy of some other
vector.
|
·
Vector class: Member Functions with iterators
used:
|
begin
|
Returns a random-access iterator to the first element in the
container.
|
|
end
|
Returns a random-access iterator that points just beyond
the end of the vector.
|
|
erase
|
Removes an element or a range of elements in a vector from
specified positions.
|
|
insert
|
Inserts an element or a number of elements into the vector
at a specified position.
|
·
See MSDN on all the vector class members here