To rewrite the CMS (Contact Management System) as a class. This Lab is the fourt
ID: 3556781 • Letter: T
Question
To rewrite the CMS (Contact Management System) as a class.
This Lab is the fourth installment of the CMS program exercise, continued on from the part-2, Lab 7. You are to rewrite the program using the Object Oriented design to create a Contact class which contains an array of contact records and its associated member functions, those user accessible and internal helper functions.
Implementation
The constructor shall allocate the initial contact record, and the destructor shall release the allocated memory. The same user menu shuld let the user to create a new record in the file, change the contents of any fields in the record, and display any record that matches the search.
Starter Kit
Use the working Lab7 as the starting file.
#include
#include
#include
#include
#include
using namespace std;
// Constants for array size
const int NUM_CONTACTS = 100;
// Declaration of the Contact structure
struct contactInfo {
string name;
string phone;
string address;
string lastContact;
};
//Function prototypes
string toLower(string);
int str2Int(string);
void findContact(contactInfo *, int, string );
void displayContact(contactInfo *, int);
void editContact(contactInfo *, int);
void deleteContact(contactInfo *, int, int);
void menu(int size)
{
cout << " === Lab7 Test Menu === "
<< " Contact list status ( " << size << "/" << NUM_CONTACTS << " ) "
<< " search "
<< " add "
<< " delete "
<< " modify "
<< " show "
<< " quit - this program "
<< " -- Enter your command: ";
}
int main()
{
int size = 10; // 1 based, as the starting number of contacts
contactInfo infos[NUM_CONTACTS] = { //Array to hold the stats for each contact...
{"Becky Warren", "5551223", "", "010114"},
{"Jeo Looney", "2340097", "", "010114"},
{"Geri Palmer", "5668788", "", "010114"},
{"Lynn Presnell", "7671212", "", "010114"},
{"Holly Gaddis", "2383881", "", "010114"},
{"Sam Wiggins", "5550998", "", "010114"},
{"Bob Kain", "5668712", "", "010114"},
{"Tim Haynes", "5897676", "", "010114"},
{"Warren Gaddis", "5669037", "", "010114"},
{"Ron Palmer", "2344939", "", "010114"}};
while(true){
string option; // user command
menu(size);
getline(cin, option);
if(option == "search") {
// find record
string pattern;
cout<
getline(cin,pattern);
pattern=toLower(pattern);
findContact(infos, size, pattern);
} // end of search
else if( option == "add" ){ // insert entry
// SEGMENT 1
// to be implemented for Lab7
// to add a new record to the first available field
// i.e. at location [size] (0 based addressing)
cout << "COMMING TO YOUR SCREEN SOON! ";
// END OF SEGMENT 1
}
else if( option == "delete" ){ // delete entry
// SEGMENT 2
// to be implemented for the Lab7
cout << "COMMING TO YOUR SCREEN SOON! ";
// END OF SEGMENT 2
}
else if( option == "modify" ){ // modify entry
// SEGMENT 3
// to be implemented for the Lab7
cout << "COMMING TO YOUR SCREEN SOON! ";
// END OF SEGMENT 3
}
else if( option == "show" ){ // show detail
string input;
cout << " Record #: ";
getline(cin, input);
int entry = str2Int(input);
if((entry > 0) && (entry <= size))
displayContact(infos, entry-1);
else
cout << "There is no such record! ";
}
else if(option == "dump"){
// this helper function is only for developer
// not visible on menu
//
for(int i=0; i < size; i++){
cout << "record #" << i+1 << ": "
<< (infos+i)->name << " | "
<< (infos+i)->phone << " | "
<< (infos+i)->address << " | "
<< (infos+i)->lastContact << " ";
}
cout << endl;
}
else if(option == "quit"){
cout << endl << "GOOD BYE :) "<< endl;
break;
}
else
cout << endl<< "INVALID INPUT " ;
} // end of menu loop
return 0;
}
void findContact(contactInfo *p, int size, string pattern) {
// SEGMENT 4
// to be modified for Lab7
//
// This version of findContact is a fix complete pattern search to help you get started.
// The submitted version of your Lab7 shall contain the algorithm developed in Lab5,
// the one-character typo partial pattern search.
//
int matched = 0;
int number = str2Int(pattern);
for(int index=0; index < size-1; index++) {
if(number==0) { // name search
string record = toLower((p+index)->name);
if(record == pattern) {// exact matched name
displayContact(p, index);
matched++;
}
}
else { // number search
if(pattern.compare((p+index)->phone) == 0) {// exact matched number
displayContact(p, index);
matched++;
}
else {}
}
}
if(matched != 0) cout <<"Found " << matched << " matched contacts. " ;
else cout <<"No matching contact. " ;
// END OF SEGMENT 4
}
void displayContact(contactInfo *p, int entry){
cout << "Record #"< << " Name: "<<(p+entry)->name
<< " Phone: "<<(p+entry)->phone
<< " Address: "<<(p+entry)->address
<< " Last Contact: "<<(p+entry)->lastContact< }
void editContact(contactInfo *p, int entry){
// SEGMENT 5
// to be implement for Lab7
// This function should be a simple record based utility.
// No need to implement record level selection.
// You may implement a sequencial update all fields:
// name -> phone -> address -> last_contact -> done.
// END OF SEGMENT 5
}
void deleteContact(contactInfo *p, int entry, int size){
// END OF SEGMENT 6
// To be implemented for Lab7
// On the completion of a delete operaton the array shall be compacted, i.e. no void data record in the array.
// The data records in the array occupy array cells from location 0 to location size-1 (0 based) contiguously.
// The array is not sorted.
// You may use any algorithm to compact the array.
// For example, you may move the last one to the removed entry, or
// you may shift one by one from the right to fill the blank.
// END OF SEGMENT 6
}
string toLower(string s)
{
int i;
for(i=0;i s[i] = tolower(s[i]);
return s;
}
int str2Int(string s) {
int i;
stringstream ss(s); //turn the string into a stream
ss >> i; //convert
return i;
}
Explanation / Answer
#include <sstream>
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
// Constants for array size
const int NUM_CONTACTS = 100;
// Declaration of the Contact structure
struct contactInfo {
string name;
string phone;
string address;
string lastContact;
};
//Function prototypes
string toLower(string);
int str2Int(string);
void findContact(contactInfo *, int, string );
void displayContact(contactInfo *, int);
void editContact(contactInfo *, int);
void deleteContact(contactInfo *, int, int);
void menu(int size)
{
cout << " === Lab7 Test Menu === "
<< " Contact list status ( " << size << "/" << NUM_CONTACTS << " ) "
<< " search "
<< " add "
<< " delete "
<< " modify "
<< " show "
<< " quit - this program "
<< " -- Enter your command: ";
}
int main()
{
int size = 10; // 1 based, as the starting number of contacts
contactInfo infos[NUM_CONTACTS] = { //Array to hold the stats for each contact...
{"Becky Warren", "5551223", "", "010114"},
{"Jeo Looney", "2340097", "", "010114"},
{"Geri Palmer", "5668788", "", "010114"},
{"Lynn Presnell", "7671212", "", "010114"},
{"Holly Gaddis", "2383881", "", "010114"},
{"Sam Wiggins", "5550998", "", "010114"},
{"Bob Kain", "5668712", "", "010114"},
{"Tim Haynes", "5897676", "", "010114"},
{"Warren Gaddis", "5669037", "", "010114"},
{"Ron Palmer", "2344939", "", "010114"}};
while(true){
string option; // user command
menu(size);
getline(cin, option);
if(option == "search") {
// find record
string pattern;
cout<<endl<<"Enter partial name or number to search: ";
getline(cin,pattern);
pattern=toLower(pattern);
findContact(infos, size, pattern);
} // end of search
else if( option == "show" ){ // show detail
string input;
cout << " Record #: ";
getline(cin, input);
int entry = str2Int(input);
if((entry > 0) && (entry <= size))
displayContact(infos, entry-1);
else
cout << "There is no such record! ";
}
else if(option == "dump"){
// this helper function is only for developer
// not visible on menu
//
for(int i=0; i < size; i++){
cout << "record #" << i+1 << ": "
<< (infos+i)->name << " | "
<< (infos+i)->phone << " | "
<< (infos+i)->address << " | "
<< (infos+i)->lastContact << " ";
}
cout << endl;
}
else if(option == "quit"){
cout << endl << "GOOD BYE :) "<< endl;
break;
}
else
cout << endl<< "INVALID INPUT " ;
} // end of menu loop
return 0;
}
void findContact(contactInfo *p, int size, string pattern) {
// SEGMENT 4
// to be modified for Lab7
//
// This version of findContact is a fix complete pattern search to help you get started.
// The submitted version of your Lab7 shall contain the algorithm developed in Lab5,
// the one-character typo partial pattern search.
//
int matched = 0;
int number = str2Int(pattern);
int magicnum = pattern.size() -1;
for(int index=0; index < size-1; index++) {
if(number==0) { // name search
string record = toLower((p+index)->name);
//write the new code here
cout << "Inspecting record " << record << endl;
int position=0,match=0,error=0;
for (int x=0; x < pattern.size(); x++)//go through search one character at a time
{
cout << "currently on letter " << pattern[x] << endl;
for (int i=0; i < record.size()-1; i++) //go through record
{
if(pattern[x] == record[i+position])//if there is a character match
{
cout << "We have a match at position " << i+position << endl;
position = i + position +1;
match++;
cout << "We now have a total of " << match << " matches " << endl;
//switch letters
i = record.size();
}
else//use redemption mechanic
{
error++;
/*if (error >= magicnum)
{
x=0;
i=0;
redemption++;
error=0;
}*/
}
}
}
if(match >= magicnum)
{
cout << record << " is a match!" << endl;
//redemption=0;
}
}
else { // number search
if(pattern.compare((p+index)->phone) == 0) {// exact matched number
displayContact(p, index);
matched++;
}
else {}
}
}
//if(match == magicnum) cout <<"Found " << match << " matched contacts. " ;
//else cout <<"No matching contact. " ;
// END OF SEGMENT 4
}
void displayContact(contactInfo *p, int entry){
cout << "Record #"<<entry + 1
<< " Name: "<<(p+entry)->name
<< " Phone: "<<(p+entry)->phone
<< " Address: "<<(p+entry)->address
<< " Last Contact: "<<(p+entry)->lastContact<<endl;
}
string toLower(string s)
{
int i;
for(i=0;i<s.length();i++)
s[i] = tolower(s[i]);
return s;
}
int str2Int(string s) {
int i;
stringstream ss(s); //turn the string into a stream
ss >> i; //convert
return i;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.