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

Develop a C++ application on the UNIX operating system. The application defines

ID: 671778 • Letter: D

Question

Develop a C++ application on the UNIX operating system. The application defines and uses two classes: Customer and Contact. The Customer class has a vector of Contact as a one of the data members.

II. Requirements:

1. The class Customer should be implemented based on the class diagram shown below:

Customer

-CompanyName: String

-Address: String

-City: String

-State: String

-Zip: String

-Contacts: vector <Contact>

«constructor»+ Customer()

«constructor»+ Customer(companyname:String, address:String, city:String state:String, zip: String)

+setCompanyName(company_name: String)

+getCompanyName(): String

+setAddress(address: String)

+setCity(city: String)

+setState(state: String)

+setZip(zip: String)

+getAddress(): String

+getCity(): String

+getState(): String

+getZip(): String

+getFullAddress(): String

+addContact(contact: Contact)

+RemoveContact(contact: Contact): bool

+listContacts(): String

2. The class Contact should be implemented based on the class diagram shown below:

Contact

-FirstName: String

-LastName: String

-Phone: String

«constructor»+ Contact()

«constructor»+ Contact(firstname:String, lastname: String, phone: String)

+setFirstName(firstname: String)

+setLastName(lastname: String)

+getFirstName(): String

+getLastName(): String

+setPhone(phone: String)

+getPhone(): String

+getContactInfo(): String

3. Description of the member functions for class Customer

*Customer() This constructor initializes CompanyName, Address, City, Zip to a zero-length string ("").

*Customer(address: String, city: String, state: String, zip: String) This constructor initializes CompanyName, Address, City, State and Zip using the arguments passed in.

*setCompanyName(), setAddres(address:String), setCit(city:String), setZip(zip:String) These setter functions update corresponding data members.

*getCompanyName(), getAddress(), getCity(), getZip() These getter functions returns corresponding data members.

*getFullAddress() returns the combined address with this format: <address>,<City>,<State>,<Zip>

*addContact(contact: Contact) Add a contact to the vector of Contact

*removeContact(contact: Contact) Compare the argument with the contacts in the vector of Contact, if the first name, last name and phone number match, remove the contact from the vector

*listContacts() Return a String that contains information of all contacts. The information of each contact is obtained by calling the getContactInfo of the Contact class.

Note: The word "return" in the description above means the "return" keyword in C++, rather than display on screen.

4. Description of the member functions for class Contact

*Contact() This default constructor initializes FirstName, LastName and Phone to zero-length string ("").

*Contact(firstname:String, lastname: String, phone: String) This constructor initializes FirstName, LastName and Phone using the arguments passed in.

*setFirstName(firstname: String) setLastName(lastname: String) setPhone(phone: String) These setter functions update corresponding data members.

*getFirstName(): String getLastName(): String getPhone(): String These getter functions returns corresponding data members.

*getContactInfo(): String returns a String with this format: <First Name> <Last Name> < Phone>

Note: The word "return" in the description above means the "return" keyword in C++, rather than display on screen.

5. Write a driver program as the client of the Customer class. It should instantiate an object of the Cusotmer class; then it should demonstrate the use of all member functions. However, only one constructor should be used.

Explanation / Answer

1)

2) #include <iostream>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
// contact structure
struct Contact {
string name, street, city, email, phone, birthdate;
};
// global array
const int size = 20;
Contact entry[size];
// contacts database file
ofstream database;
// function prototypes
int addContact(const int);
void writeContactsToFile(void);
int main (int argc, char *argv[]) {
int menuChoice;
char response;
string line;
stringstream ss;
bool done = false;
int numContacts = 0;
while (done == false) {
// call loadContacts();
// display menu
cout << endl << endl
<< "Welcome to your address book! What would you like to do?"
<< endl << endl
<< "1. Print my contacts to the screen." << endl
<< "2. Add a contact." << endl
<< "3. Remove a contact." << endl
<< "4. Quit program." << endl << endl
<< "Choose 1, 2, 3, or 4: ";
getline(cin,line);
ss.clear(); ss.str(line);
if (!(ss >> menuChoice)) menuChoice = 0;
switch (menuChoice) {
case 1:
// call printContacts();
break;
case 2:
numContacts = addContact(numContacts);
cout << "Would you like to save your changes to contacts.txt? (y/n) : ";
getline(cin,line);
if ((ss >> response) && (response == 'y')) {
writeContactsToFile();
}
break;
case 3:
// call removeContact();
cout << "Would you like to save your changes to contacts.txt? (y/n) : ";
getline(cin,line);
if ((ss >> response) && (response == 'y')) {
writeContactsToFile();
}
break;
case 4:
cout << " Have a nice day!";
done = true;
break;
default:
cout << endl << "You did not enter a valid option." << endl;
break;
} // end case of menuChoice
} // end while not done
return 0;
}
// addContact function
int addContact(int n) {
// get information from user
cout << endl << "You wish to add a new contact." << endl;
cout << "Name? (Lastname, Firstname) : ";
getline(cin,entry[n].name);
cout << "Address? (9999 Boardwalk Ave.) : ";
getline(cin,entry[n].street);
cout << "City, state, & zip code? (City, State, Zip) : ";
getline(cin,entry[n].city);
cout << "e-mail address? (name@gmail.com) : ";
getline(cin,entry[n].email);
cout << "Phone number? (xxx-xxx-xxxx) : ";
getline(cin,entry[n].phone);
cout << "Birth date? (MM/DD/YYYY) : ";
getline(cin,entry[n].birthdate);

return n+1;
}
void writeContactsToFile() {
// open contacts.txt database to store user's input
database.open("contacts.txt", ios::out | ios::app);

database.close();
}

5)