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

Develop a C++ application. The application defines and uses two classes: Custome

ID: 671825 • Letter: D

Question


Develop a C++ application. 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.

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

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

fstream custFile;
fstream tempFile;

const int NAME_LEN = 45;
const int ADDR_LEN = 45;
const int CITY_LEN = 20;
const int STATE_LEN = 4;
const int ZIP_LEN = 11;
const int PHONE_LEN = 14;
const int ARRAY_SIZE = 11;

// Structure used to represent customer data
struct Customer
{
char name [NAME_LEN];
char address [ADDR_LEN];
char city [CITY_LEN];
char state [STATE_LEN];
char zip [ZIP_LEN];
char phone [PHONE_LEN];
double balance; //[BALANCE_LEN];
char lastPay[ARRAY_SIZE];
};

//Prototypes
void setInfo(long);
void display(long);
long search();
void deleteRec(long);
void showAll();

bool rnew = 0;
bool mod = 0;

int main()
{
long fpos;
int choice = 6;
char YorN;
do
{
cout << " * * * * M A I N M E N U * * * * * * * ";
cout << "1. Enter a new Customer Account ";
cout << "2. Display a Customer Account ";
cout << "3. Delete a Customer Account ";
cout << "4. Change a Customer Account ";
cout << "5. Exit the program ";

// Determine user's choice
do
{
cout << "Enter your choice (1-6): ";
cin >> choice;
} while (choice < 1 || choice > 6);

//Process user's choice
switch (choice)
{
case 1:
cin.get();
cout << " You selected Enter a new Customer Account. ";
rnew = 1;
setInfo(0);
rnew = 0;
break;
case 2:
cout << " You selected Display a Customer Account. ";
fpos = search();
if (fpos != -1)
{
display(fpos);
}
else
cout << " Record not found.";
break;
case 3:
cout << " You selected Delete a Customer Account. ";
fpos = search();
if (fpos != -1)
{
display(fpos);
cout << " ARE YOU SURE YOU WANT TO DELETE THIS RECORD? (Y/N)";
cin >> YorN;
YorN = toupper(YorN);
if (YorN == 'Y')
{
deleteRec(fpos);
break;
}
else
{
cout << " Record was not deleted. ";
}
}
else
cout << " Record not found. ";
break;
case 4:
cout << " You selected Change a Customer Account. ";
fpos = search();
if (fpos != -1)
{
cout << "'nRECORD CONTENTS: ";
display(fpos);
cout << " ENTER NEW CONTENTS: ";
setInfo(fpos);
mod = 0;
}
else
cout << " Record not found. ";
break;
case 5:
cout << " You selected Change a Customer Account. ";
showAll();
break;
case 6:
exit(0);
break;
default: //Anything not between 1-5
break;
} // End switch
} while (choice != 6);
return 0;
}// End of Main

// Function definitions

// setInfo *
// Get info for customer record and write to file. *

void setInfo(long fp)
{
Customer c;
int valid;

do
{
valid = 1;
cout << " Please enter the following information: ";
cout << " Customer Name: ";
cin.getline(c.name, 45);
cout << " Customer Address: ";
cin.getline(c.address, 45);
cout << " City: ";
cin.getline(c.city, 20);
cout << " State: ";
cin.getline(c.state, 4);
cout << " Zip: ";
cin.getline(c.zip, 11);
cout << " Telephone: ";
cin.getline(c.phone, 14);
cout << " Account Balance: ";
cin >> c.balance;
cin.get();
cout << " Date of last payment: ";
cin.getline(c.lastPay, 11);

if (strlen(c.name) ==0 || strlen(c.address) ==0 || strlen(c.city) ==0 || strlen(c.state) ==0 || strlen(c.zip) ==0 || strlen(c.lastPay) ==0)
{
cout << "You must enter infroamtion for each field. ";
valid = 0;
}
if (c.balance < 0)
{
cout << "Please enter 0 or greater for account balance. ";
valid = 0;
}
} while (!valid);

if (rnew)
{
custFile.open("cust.dat", ios::out | ios::app | ios ::binary );
}
else if (mod)
{
custFile.open("cust.dat", ios::out | ios::app | ios ::binary );
custFile.seekp(fp, ios::beg);
}
if (custFile.fail())
{
cout << " Error opening file. ";
return;
}
custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
if (custFile.fail())
{
cout << " Error writing record to file. ";
custFile.clear();
custFile.close();
return;
}
}


// Display *
// display record at a given file position. *

void display(long fp)
{
Customer c;
custFile.open("cust.dat", ios::in | ios::binary);
if (custFile.fail())
{
cout << " Error opening file. ";
return;
}
if (custFile.peek() == EOF)
{
cout << " File is empty. ";
custFile.clear();
custFile.close();
return;
}
custFile.seekg(fp, ios::beg);
custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer));

cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
cout << "Customer Name: " << c.name << endl;
cout << "Customer Address: " << c.address << endl;
cout << "City: " << c.city << endl;
cout << "State: " << c.state << endl;
cout << "Zip: " << c.zip << endl;
cout << "Telephone: " << c.phone << endl;
cout << "Account Balance: " << c.balance << endl;
cout << "Date of last payment: " << c.lastPay << endl;
custFile.clear();
custFile.close();
cout << " Press Enter to continue. ";
cin.get();
}


// search
// Returns file pointer position
// of a customer record

long search()
{
char name[45];
Customer c;
long fp;
cout << " Enter all or part of the Customer's Name: ";
cin.ignore();
cin.getline(name, 45);
if (name[0] == '') //if nothing is entered do nothing
{
return -1;
}

while (true)
{
custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer)); //Read one record
if (custFile.fail())
break;
if (strstr(c.name,name) != NULL) // If search value matches customers name..
{
fp = custFile.tellg();
custFile.clear();
custFile.close();
return (fp - sizeof(c));
}
}
cout << "Record not Found ";
custFile.clear();
custFile.close();
return -1;
}

// ShowAll *
// Show records in file *

void showAll()
{
cin.ignore();
Customer c;
int count = 0;
custFile.open("cust.dat", ios::in | ios::binary);
if (custFile.fail())
{
cout << " Error opening file. ";
return;
}
cout << " ***Begin Customer Record*** ";

while (custFile.peek()!=EOF)
{
custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer));
cout << setprecision(2);
cout << fixed << showpoint;
cout << " RECORD NUMBER " << ++count << ":" << endl;
cout << " Customer name: " << c.name << endl;
cout << "Customer Address: " << c.address << endl;
cout << "City: " << c.city << endl;
cout << "State: " << c.state << endl;
cout << "Zip: " << c.zip << endl;
cout << "Telephone: " << c.phone << endl;
cout << "Account Balance: " << c.balance << endl;
cout << "Date of Last Payment: " << c.lastPay << endl;
cout << endl;
cout << " Press Enter to Continue...";
cin.get();
}
if (count == 0)
{
cout << " File is empty." << endl;
}
cout << " ***End of Customer Record Listing*** ";
custFile.clear();
custFile.close();
}


// deleteREc *
// This function marks a record for deletion by placing *

void deleteRec(long fp)
{
Customer c;
int Rec = 0;
custFile.open("cust.dat", ios::in | ios::binary);
if (custFile.fail())
{
cout << " Error opening file. ";
return;
}

//Mark the file at offset fp for deletion
strcpy_s(c.name, ""); //Indicates deletion of record
custFile.seekp(fp, ios::beg);
custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
custFile.clear();
custFile.close();

//Copy customer file to temporary file
custFile.open("cust.dat", ios::in | ios::binary);
tempFile.open("temp.dat", ios::out | ios::binary);
while (custFile.peek() != EOF)
{
custFile.read(reinterpret_cast<char*>(&c), sizeof(c));
tempFile.write(reinterpret_cast<char*>(&c), sizeof(c));
}
custFile.clear();
tempFile.clear();
custFile.close();
tempFile.close();

// Copy temporary file to customer file, skipping
// the records that are marked for deletion
tempFile.open("temp.dat", ios::out | ios::binary);
custFile.open("cust.dat", ios::out | ios::binary);
while (true)
{
tempFile.read(reinterpret_cast<char*>(&c), sizeof(c));
if (tempFile.fail())
break;
if (c.name[0] != '')
{
custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
}
}
tempFile.clear();
tempFile.close();
custFile.clear();
custFile.close();
cout << " Deletion Successful. ";
}