Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1 - lab (50%), The Contact class Your task for this part of your workshop is to complete the implementation of the Contact class.

Part 1 - lab (50%), The Contact class

Your task for this part of your workshop is to complete the implementation of the Contact class. This class encapsulates the phone contact information of a person using the following attributes:

class Contact { char* m_name{}; // sets m_name to nullptr before any constructor invocation int m_area; int m_exchangeCode; int m_number; public: ... };

  • Full name (Dynamically allocated)
  • A Phone number is kept in three separate parts; Area code, Exchange Code and Number. For example in the phone number (416) 491-5050 416 is the area code, 491 is the exchange code and 5050 is the number.

Validation

These values are validated (considered valid) as follows:

  • A Valid full name is a Cstring that is not null and not empty.
  • area code having exactly 3 digits (100 to 999, inclusive)
  • Exchange code having exactly 3 digits (100 to 999, inclusive)
  • Number being an integer between 0 and 9999 inclusive.

The Contact class is to validate and store the above information via initialization and data entry from istream.

Also, the Contact class must comply with the rule of three not to cause crash or memory leak (i.e. the implementation of copy constructor, copy assignment and destructor)

Although the name of the Contact is dynamically held, but thorough a constant value the maximum possible size for a name is set to be 55 characters:

 const int MaxNameLength = 55; 

Finally, A Contact object should reveal its status (of being valid or invalid) via type conversion overload of the Boolean type (a true outcome means the object is valid and a false outcome means it is invalid).

To accomplish the above and have an organized and modular code, implement these private methods to help you with the implementation of the whole logic: (you can add more if you like to)

Private Methods

bool validPhone(int areaCode, int exchangeCode, int number)const;

Returns true is the three parts of a phone number are valid. (see Validation)

void setEmpty();

Sets the Contact object to a recognizable empty (invalid) state by setting m_name to nullptr;

void allocateAndCopy(const char* name);

  • First is will free the memory pointed by m_name
  • allocates memory enough to store name Cstring
  • copies the Cstring pointed by name into the newly allocated memory pointed by m_name

void extractChar(std::istream& istr, char ch)const;

  • First, it will peek and see if the next character in the keyboard buffer is the same as the ch argument
  • If yes, it will read and remove it from the keyboard. (it throws it away!)
  • If not, it will set the istream into a fail state.

ostream& printPhoneNumber(ostream& istr)const;

Inserts the three parts of the phone number into the istr object in following format: (999) 999-0009 and then returns the istr object reference

Note: the last part (number) is right justified and padded with zeros in width of 4.

void set(const char* name, int areaCode, int exchangeCode, int number);

sets the object attributes to their values after validating the arguments and then safely allocating memory for the name.

  • If the name and the three parts are valid (see Validation) Use private functions
    • Calls the allocateAndCopy function to set the name
    • Sets the three parts to their values
  • If not, it deletes the memory pointed by m_name and sets the object to a safe empty state (setEmpty())

Constructors.

The Contact can either get created with no values (default constructor) into a safe empty state or using all four values.

Use the set function for the latter case.

Note that since the m_name attribute is initialized in the class definition to be nullptr, there is no need to worry about setting it to nullptr before calling the set function.

Rule of three

Copy Constructor

Contact(const Contact& cnt);

  • if the cnt object is valid it will set the values of the current object to the those of the incoming argument (cnt).

Copy Assignment operator overload

Contact& operator=(const Contact& cnt);

  • First, it will make sure that this is not a "self copy" by comparing the address of the current object and the address of the incoming argument.
  • If it is not a self copy this function works exactly like the copy constructor
  • If it is a self copy don't perform any action At the end return the reference of the current object.

Destructor

deletes the memory pointed by m_name.

Boolean type conversion operator

Returns true if m_name is not null, else it will return false;=

ostream& print(ostream& ostr, bool toFile = true) const;

If the current object is in a valid state it inserts the values of the Contact information in two different formats based on the value of the toFile argument:

  • if toFile is true, prints the data in comma separated format:
    • prints the name
    • print comma
    • print the phone number using the private function
  • if toFile is false prints the data in the following format:
    • In 50 characters, left justified and padded with dots ('.')
    • prints the name
    • print the phone number using the private function
  • At the end it returns the ostr reference

istream& read(istream& istr);

Reads the Contact information in followng format:

  • name
  • comma
  • (
  • area code
  • )
  • space
  • exchange code
  • dash ('-')
  • number

Example: Luke Skywalker,(647) 555-9475

read implementation

In local variables read each part and if data is invalid set the istr to failure:

  • using istream::get() read the name up to MaxNameLength or a comma (do not extract comma)
  • extract a comma. (using extractChar private function)
  • extract an open parentheses '(' character (using extractChar private function)
  • extract the area code into a local variable (istr >> value;)
  • extract a close parentheses ')' (using extractChar private function)
  • extract a space character (using extractChar private function)
  • extract the exchange code into a local variable
  • extract a dash character '-' (using extractChar private function)
  • extract the number into a local variable
  • extract a new line character ' ' (using extractChar private function)
  • if istr is not in a failure state
    • all data were read successfully, use the set private function to set values of the object to read value
  • if istr is in a failure state, don't do anything a leave the Contact to its original values.
  • at the end return the istr reference

insertion operator overload

ostream& operator<<(ostream& ostr, const Contact& cnt);

if cnt is valid it will print it using the print function on the screen and not on File, (i.e. onFile is false). Otherwise it will print "Invalid Phone Record".

In the end, it will return the ostr reference.

extraction operator overload

istream& operator>>(istream& istr, Contact& cnt)

returns the read method of the cnt argument.

the tester program

// Workshop #6: // Version: 0.9 // Date: 2021/10/17 // Author: Fardad Soleimanloo // Description: // This file tests the lab section of your workshop /////////////////////////////////////////////////// #include #include #include "Contact.h" using namespace std; using namespace sdds;

int noOfRecs(const char* filename); void showFile(const char* filename); Contact ReadPhoneFromFile(istream& istr); void dataEntryTest(); void validationTest();

int main() { int i; int recs = noOfRecs("phoneNumbers.csv"); Contact C{ "Gandalf The Grey",111,222,3 }; ifstream phoneFile("phoneNumbers.csv"); ofstream goodPhoneFile("goodNumbers.txt"); validationTest(); dataEntryTest(); cout << endl << "Rule of three test ---------------------------------------------" << endl; for (i = 0; phoneFile && i < recs; i++) { C = ReadPhoneFromFile(phoneFile); cout << C << endl; if(phoneFile) goodPhoneFile << C << endl; } if (i == recs) cout << "All records were read successfuly!" << endl; else { cout << "Read " << i - 1 << " out of " << recs << " Records successfuly" << endl; cout << "Record number " << i << " is invalid!" << endl; } showFile("goodNumbers.txt"); return 0; }

void validationTest() { int i; Contact C[]{ {"Fred Soley", 416,491,50}, {nullptr, 416,491,5050 }, {"Fred Soley", 41,491,5050 }, {"Fred Soley", 416,49,5050 }, {"Fred Soley", 416,491,50500 }, {"Fred Soley", 1000,491,5050 }, {"Fred Soley", 416,1000,5050 }, {"Fred Soley", 416,491,-1 }, }; cout << "Validation Test" << endl; for (i = 0; i < 8; cout << C[i++] << endl); }

int noOfRecs(const char* filename) { int num = 0; ifstream file(filename); while (file) num += (file.get() == ' '); return num; }

void showFile(const char* filename) { ifstream file(filename); char ch; cout << "Contents of " << filename << endl << "----------------------------------------------------------------" << endl; while (file.get(ch)) { cout << ch; } }

void dataEntryTest() { Contact C{ "Gandalf The Grey",111,222,3 }; cout << endl << "Data entry test." << endl; cout << "Enter the test data using copy and paste to save time:" << endl << endl; cout << "Enter the following:" << endl << ">Gandalf-(111) 222,3333" << endl << ">"; cin >> C; if (!cin) { cout << "Invalid Contact information" << endl; cin.clear(); cin.ignore(1000, ' '); } cout << "Contact Conent:" << endl << C << endl;

cout << "Enter the following:" << endl << ">Gandalf,111222-3333" << endl << ">"; cin >> C; if (!cin) { cout << "Invalid Contact information" << endl; cin.clear(); cin.ignore(1000, ' '); } cout << "Contact Conent:" << endl << C << endl; cout << "Enter the following:" << endl << ">Gandalf,(111)222-3333" << endl << ">"; cin >> C; if (!cin) { cout << "Invalid Contact information" << endl; cin.clear(); cin.ignore(1000, ' '); } cout << "Contact Conent:" << endl << C << endl; cout << "Enter the following:" << endl << ">Gandalf,(111) 2223333" << endl << ">"; cin >> C; if (!cin) { cout << "Invalid Contact information" << endl; cin.clear(); cin.ignore(1000, ' '); } cout << "Contact Conent:" << endl << C << endl; cout << "Enter the following:" << endl << ">,(111) 222-3333" << endl << ">"; cin >> C; if (!cin) { cout << "Invalid Contact information" << endl; cin.clear(); cin.ignore(1000, ' '); } cout << "Contact Conent:" << endl << C << endl;

cout << "Enter the following:" << endl << ">Gandalf,(111) 222-3333" << endl << ">"; cin >> C; if (!cin) { cout << "Invalid Contact information" << endl; cin.clear(); cin.ignore(1000, ' '); } cout << "Contact Conent:" << endl << C << endl; }

Contact ReadPhoneFromFile(istream& istr) { Contact C; istr >> C; return C; }

Tester output

Validation Test Fred Soley........................................(416) 491-0050 Invalid Phone Record Invalid Phone Record Invalid Phone Record Invalid Phone Record Invalid Phone Record Invalid Phone Record Invalid Phone Record Data entry test. Enter the test data using copy and paste to save time: Enter the following: >Gandalf-(111) 222,3333 >Gandalf-(111) 222,3333 Invalid Contact information Contact Content: Gandalf The Grey..................................(111) 222-0003 Enter the following: >Gandalf,111222-3333 >Gandalf,111222-3333 Invalid Contact information Contact Content: Gandalf The Grey..................................(111) 222-0003 Enter the following: >Gandalf,(111)222-3333 >Gandalf,(111)222-3333 Invalid Contact information Contact Content: Gandalf The Grey..................................(111) 222-0003 Enter the following: >Gandalf,(111) 2223333 >Gandalf,(111) 2223333 Invalid Contact information Contact Content: Gandalf The Grey..................................(111) 222-0003 Enter the following: >,(111) 222-3333 >,(111) 222-3333 Invalid Contact information Contact Content: Gandalf The Grey..................................(111) 222-0003 Enter the following: >Gandalf,(111) 222-3333 >Gandalf,(111) 222-3333 Contact Content: Gandalf...........................................(111) 222-3333 Rule of three test --------------------------------------------- Jango Fett........................................(416) 555-8015 Padme Amidala.....................................(905) 555-9325 Q'ira.............................................(301) 555-3317 Firmus Piett......................................(705) 555-2063 Wedge Antilles....................................(647) 555-0495 Mon Mothma........................................(289) 555-6015 Count Dooku.......................................(416) 555-9903 Nien Nunb.........................................(905) 555-9141 Ponda Baba........................................(301) 555-4136 Max Rebo..........................................(705) 555-5202 Salacious B. Crumb................................(647) 555-6071 Enfys Nest........................................(289) 555-4382 Figrin D'an and the Modal Nodes...................(416) 555-5310 Wicket W. Warrick.................................(905) 555-7088 Poe Dameron.......................................(301) 555-7736 Qui-Gon Jinn......................................(705) 555-6073 Bib Fortuna.......................................(647) 555-4729 Finn..............................................(289) 555-9090 Kylo Ren..........................................(416) 555-3082 Chirrut Imwe......................................(905) 555-3874 Mace Windu........................................(301) 555-1845 Chewbacca.........................................(705) 555-3166 Jabba the Hut.....................................(647) 555-8392 Greedo............................................(289) 555-5763 Lando Calrissian..................................(416) 555-6912 Darth Maul........................................(905) 555-8201 Obi-Wan Kenobi....................................(301) 555-3858 Rey...............................................(705) 555-7583 Luke Skywalker....................................(647) 555-9475 Princess Leia.....................................(289) 555-7976 Yoda..............................................(416) 555-5461 Boba Fett.........................................(905) 555-9868 Darth Vader.......................................(301) 555-0086 Han Solo..........................................(705) 555-8107 Invalid Phone Record Read 34 out of 36 Records successfully Record number 35 is invalid! Contents of goodNumbers.txt ---------------------------------------------------------------- Jango Fett........................................(416) 555-8015 Padme Amidala.....................................(905) 555-9325 Q'ira.............................................(301) 555-3317 Firmus Piett......................................(705) 555-2063 Wedge Antilles....................................(647) 555-0495 Mon Mothma........................................(289) 555-6015 Count Dooku.......................................(416) 555-9903 Nien Nunb.........................................(905) 555-9141 Ponda Baba........................................(301) 555-4136 Max Rebo..........................................(705) 555-5202 Salacious B. Crumb................................(647) 555-6071 Enfys Nest........................................(289) 555-4382 Figrin D'an and the Modal Nodes...................(416) 555-5310 Wicket W. Warrick.................................(905) 555-7088 Poe Dameron.......................................(301) 555-7736 Qui-Gon Jinn......................................(705) 555-6073 Bib Fortuna.......................................(647) 555-4729 Finn..............................................(289) 555-9090 Kylo Ren..........................................(416) 555-3082 Chirrut Imwe......................................(905) 555-3874 Mace Windu........................................(301) 555-1845 Chewbacca.........................................(705) 555-3166 Jabba the Hut.....................................(647) 555-8392 Greedo............................................(289) 555-5763 Lando Calrissian..................................(416) 555-6912 Darth Maul........................................(905) 555-8201 Obi-Wan Kenobi....................................(301) 555-3858 Rey...............................................(705) 555-7583 Luke Skywalker....................................(647) 555-9475 Princess Leia.....................................(289) 555-7976 Yoda..............................................(416) 555-5461 Boba Fett.........................................(905) 555-9868 Darth Vader.......................................(301) 555-0086 Han Solo..........................................(705) 555-8107 

Files to be made

Contact.cpp Contact.h phoneNumbers.csv w6p1_tester.cpp

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions