Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//header files #ifndef Contact_H // header files should always have this to avoi

ID: 3905709 • Letter: #

Question

//header files

#ifndef Contact_H // header files should always have this to avoid
#define Contact_H    // multiple inclusion in other files

#include <string>

// this is the only programming assignment which will use this statement.
// normally "using namespace std" is looked down upon because it
// introduces many common keywords that could be accidentally used, but
// it identifies useful types such as string and would normally be used
// std::string or std::vector.
using namespace std;

class Contact {
public:
   Contact(string _name = "", int _phoneNumber = 0);
   Contact(string _name, int _phoneNumber,
       Contact & _emergencyContact);

   // accessors for Contact
   string getName() const;
   int getPhoneNumber() const;   
   bool verifyPhoneNumber() const;
   Contact* getEmergencyContact() const;
   int number;
   string name;

   // mutators for Contact
   void changeNumber(const int newNumber);
   void changeNumber(const Contact * newContact);
   void changeName(const string newName);
   void changeName(const Contact * newContact);
   void setEmergencyContact(Contact & _emergencyContact);

private:

   //int number;
   int phoneNumber;
   Contact * emergencyContact;
};


#endif

#ifndef PhoneBook_H

#define PhoneBook_H

#include "Contact.h"

class PhoneBook {

public:

   PhoneBook(Contact * contacts = NULL, int numContacts = 0);

   // search accessors

   string getNameByNumber(const int phoneNumber) const;

   int getNumberByName(const string name) const;

   void printAllContacts() const;

   // mutators

   void verifyAllContacts();

private:

   Contact * contactList;

   int numContacts;

   int num_contacts;

   Contact* contactList1;

   int num_contact;

};

#endif

#include "PhoneBook.h"
#include "Contact.h"
#include <iostream>

// constructor -----------
PhoneBook::PhoneBook(Contact * contacts, int _numContacts){
   if (_numContacts){
       numContacts = _numContacts;
       contactList = new Contact [numContacts];
       for (int i = 0; i < numContacts; i++){
           contactList[i] = Contact((contacts+i)->getName(),
                                   (contacts+i)->getPhoneNumber(),
                                   *(contacts+i)->getEmergencyContact());
       }
   }
}

// accessors ---------------------

int PhoneBook::getNumberByName(const string _name) const {
   // steps through the list of contacts and compares each name to the
   // parameter _name and returns the phone number if found.
   // if not found, return -1.
   // NOTE: remember that '==' is not sufficient for string compare.
   //
   // CODE HERE

   return -1; // None found
}

string PhoneBook::getNameByNumber(const int _number) const {
   // steps through the list of contacts and compares each number to the
   // parameter _number and returns the name if found.
   // if not found, return NULL.
   //
   // CODE HERE

   return NULL; // None found
}

// call to print all Contacts from the given PhoneBook
// print format is a different contact on each line, and each
// line contains:
//        name     phoneNumber    emergencyContact
void PhoneBook::printAllContacts() const {
   std::cout << " num contacts: " << numContacts << std::endl;
   for (int i = 0; i < numContacts; i++){
       std::cout << contactList[i].getName() << " " <<
           contactList[i].getPhoneNumber() << " " <<
           contactList[i].getEmergencyContact() << std::endl;
   }
}


void PhoneBook::verifyAllContacts(){
   // From the given PhoneBook object, call the verifyPhoneNumber()
   // from each of the Contacts contained in the private variable
   // contactList, and also on the emergency contacts from each of
   // the contacts.
   // Remove all Contacts that do not have a valid phoneNumber from the
   // array (and just remove invalid emergencyContacts from valid Contacts,
   // do not remove entire Contact from array) and resize the array so
   // that there are no gaps after removing the Contacts.
   // HINT: resizing an array is hard... it is sometimes easier to simply
   // make a new one.
   //
   // CODE GOES HERE

}

Explanation / Answer

//PhoneBook.cpp

#include "PhoneBook.h"

#include "Contact.h"

#include <iostream>

// constructor -----------

PhoneBook::PhoneBook(Contact * contacts, int _numContacts) {

if (_numContacts) {

numContacts = _numContacts;

contactList = new Contact[numContacts];

for (int i = 0; i < numContacts; i++) {

contactList[i] = Contact((contacts + i)->getName(),

(contacts + i)->getPhoneNumber(),

*(contacts + i)->getEmergencyContact());

}

}

}

// accessors ---------------------

int PhoneBook::getNumberByName(const string _name) const {

// steps through the list of contacts and compares each name to the

// parameter _name and returns the phone number if found.

// if not found, return -1.

// NOTE: remember that '==' is not sufficient for string compare.

//

for (int i = 0; i < numContacts; i++) {

if (contactList[i].getName().compare(_name) == 0) {

return contactList[i].getPhoneNumber();

}

}

return -1; // None found

}

string PhoneBook::getNameByNumber(const int _number) const {

// steps through the list of contacts and compares each number to the

// parameter _number and returns the name if found.

// if not found, return NULL.

//

for (int i = 0; i < numContacts; i++) {

if (contactList[i].getPhoneNumber() == _number) {

return contactList[i].getName();

}

}

return NULL; // None found

}

// call to print all Contacts from the given PhoneBook

// print format is a different contact on each line, and each

// line contains:

// name phoneNumber emergencyContact

void PhoneBook::printAllContacts() const {

std::cout << " num contacts: " << numContacts << std::endl;

for (int i = 0; i < numContacts; i++) {

std::cout << contactList[i].getName() << " " <<

contactList[i].getPhoneNumber() << " " <<

contactList[i].getEmergencyContact() << std::endl;

}

}

void PhoneBook::verifyAllContacts() {

// From the given PhoneBook object, call the verifyPhoneNumber()

// from each of the Contacts contained in the private variable

// contactList, and also on the emergency contacts from each of

// the contacts.

// Remove all Contacts that do not have a valid phoneNumber from the

// array (and just remove invalid emergencyContacts from valid Contacts,

// do not remove entire Contact from array) and resize the array so

// that there are no gaps after removing the Contacts.

// HINT: resizing an array is hard... it is sometimes easier to simply

// make a new one.

//

contactList1 = new Contact[numContacts];

num_contact = 0;

// Pushing valid contacts to the contactList1 temporarily

for (int i = 0; i < numContacts; i++) {

if (!contactList[i].verifyPhoneNumber()){

delete &contactList[i];

continue;

}

else if(!contactList[i].getEmergencyContact()->verifyPhoneNumber()){

delete contactList[i].getEmergencyContact();

}

contactList1[num_contact] = contactList[i];

num_contact++;

}

// Now resize the original contactList and push valid contacts inside it

numContacts = num_contact;

contactList = new Contact[numContacts];

for (int i = 0; i < numContacts; i++) {

contactList[i] = contactList1[i];

}

}