I NEED A C++ CODE IN A VECTOR.... A) Using classes,design an online address book
ID: 3673145 • Letter: I
Question
I NEED A C++ CODE IN A VECTOR....
A) Using classes,design an online address book to keep track of the names,addresses,phone number and birthdays of family memebers,close friends and certain bussiness associates.your program should be able to handle a maximum of 500 entries. by first
a. Define the class Address that can store a street address, city, state and zip code. Use the appropriate methods to print and store the address. Also, use constructors to automatically initialize the data members.
b. Define the class ExtPerson using the class Person, the class Date, and the class Address. Add a data member to this class to classify the person as a family member, friend, or business associate. Also, add a data member to store the phone number. Add (or override) methods to print and store the appropriate information. Use constructors to automatically initialize the data members.
c. Define the class AddressBook using previously defined classes. An object of type AddressBook should be able to process a maximum of 500 entries. The program should perform:
________________________________________________________________________
i. Load the data into the address book from a disk.
ii. Sort the address book by last name
iii. Search for a person by last name
iv. Print the address, phone number, and date of birth (if available) of a given person
v. Print the names of people whose birthdays are in a given month or between two given dates
vi. Print the names of all the people between two last names.
Explanation / Answer
main.cpp
#include <iostream> //for cin, cout, etc
#include <fstream> // for files
#include <string> // for string type
#include "addressBookType.h" // headers, declarations for addressBookType class
using namespace std;
void loadAddressBook(addressBookType& addressBook); // loads the address book
void saveData(addressBookType& addressBook); // saves the data into a file specified
void showMenu(); // displays the menu in the console.
int main()
{
addressBookType addressBook;
string str; // for prompt that takes 1 input
string str1; // for prompts that takes 2 inputs
string str2; // for prompt that takes 2 inputs
int choice; // for menu selection shown using showMenu
int locate; // index location found via addressBook.search(str)
int month; // prompt for month
loadAddressBook(addressBook);
showMenu();
cin >> choice;
while (choice != 8)//if 8 then exit
{
switch (choice)
{
case 1:
cout << "Enter the last name of the person: ";
cin >> str;
cout << endl;
locate = addressBook.search(str);
if (locate != -1)
cout << str << " is in the address book" << endl;
else
cout << str << " is not in the address book" << endl;
break;
case 2:
cout << "Enter the last name of the person: ";
cin >> str;
cout << endl;
locate = addressBook.search(str);
if (locate != -1)
addressBook.printAt(locate);
else
cout << str << " is not in the address book" << endl;
break;
case 3:
cout << "Enter the month number: ";
cin >> month;
cout << endl;
addressBook.printNameByMonth(month);
break;
case 4:
cout << "Enter person type Family, Friend, Business: ";
cin >> str;
cout << endl;
addressBook.printNamesWithStatus(str);
break;
case 5:
cout << "Enter starting last name: ";
cin >> str1;
cout << endl;
cout << "Enter ending last name: ";
cin >> str2;
cout << endl;
addressBook.printNamesBetweenLastNames(str1, str2);
break;
case 6:
addressBook.print();
break;
case 7:
saveData(addressBook);
break;
default:
cout << "Invalid choice" << endl;
}
showMenu();
cin >> choice;
}
char response;
cout << "Save data Yes (Y/y) No(N/n)?: ";
cin >> response;
cout << endl;
if (response == 'y' || response == 'Y')
saveData(addressBook);
return 0;
}
void loadAddressBook(addressBookType& addressBook)
{
ifstream infile;
char filename[50];
string first;
string last;
int month;
int day;
int year;
string street;
string city;
string state;
string zip;
string phone;
string pStatus;
extPersonType temp;
cout << "Enter file name: ";
cin >> filename;
cout << endl;
infile.open(filename);
if (!infile)
{
cout << "Input file does not exist. "
<< "Program terminated!" << endl;
return;
}
int i = 0;
infile >> first >> last >> month >> day >> year;
infile.ignore(100, ' ');
getline(infile, street);
getline(infile, city);
getline(infile, state);
infile >> zip >> phone >> pStatus;
while (infile)
{
temp.setInfo(first, last, month, day, year, street, city, state,
zip, phone, pStatus);
addressBook.insertAt(i, temp);
i++;
infile >> first >> last >> month >> day >> year;
infile.ignore(100, ' ');
getline(infile, street);
getline(infile, city);
getline(infile, state);
infile >> zip >> phone >> pStatus;
}
}
void saveData(addressBookType& addressBook)
{
ofstream outfile;
string filename;
filename = "HW3Data.txt";
outfile.open(filename);
if (!outfile)
{
cout << "Input file does not exists. "
<< "Program terminates!!!" << endl;
return;
}
addressBook.saveData(outfile);
}
void showMenu()
{
cout << "Choose from the following option:" << endl;
cout << "1: Check if a person is in the address book" << endl;
cout << "2: Search for a person by last name" << endl;
cout << "3: Print the names of all the people whose birthdays are in a given month" << endl;
cout << "4: Print the names of all the people having the same status" << endl;
cout << "5: Print the names of all the people between two last names" << endl;
cout << "6: Print the address book" << endl;
cout << "7: Save data" << endl;
cout << "8: Terminate the program" << endl;
}
addressBookType.h
#ifndef H_addressBookType
#define H_addressBookType
#include <string>
#include <fstream>
#include "extPersonType.h"
#include "arrayListType.cpp"
using namespace std;
class addressBookType : public arrayListType<extPersonType>
{
public:
void print() const;
void printNameByMonth(int month); //Prints info of objects that have month input
void printInfoOf(string lName); //prints info based on last name
void printNamesWithStatus(string status); //prints info based on status
void printAt(int i); //prints info based on position in addressbook
void printNamesBetweenLastNames(string last1, string last2); // prints all objects between names
int search(string lName); //search by lastname
void saveData(ofstream&); //output
addressBookType();
};
#endif
addressBookType.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "addressBookType.h"
using namespace std;
void addressBookType::print() const
{
for (int i = 0; i < length; i++)
list[i].printInfo();
}
void addressBookType::printNameByMonth(int month)
{
for (int i = 0; i < length; i++)
{
if (list[i].isMonth(month)) //if object in list has birthday of that month then info is printed
{
list[i].print();
cout << endl;
}
}
}
void addressBookType::printInfoOf(string lName)
{
int i = search(lName);
if (i != -1)
list[i].printInfo();
else
cout << lName << " is not in address book." << endl;
}
void addressBookType::printNamesWithStatus(string status)
{
for (int i = 0; i < length; i++)
if (list[i].isStatus(status))
{
list[i].print();
cout << endl;
}
}
void addressBookType::printAt(int i)
{
if (i < length)
list[i].printInfo();
else
cout << "No such person" << endl;
}
void addressBookType::printNamesBetweenLastNames(string last1, string last2)
{
string lName;
for (int i = 0; i < length; i++)
{
list[i].getLastName(lName);
if (last1 <= lName && lName <= last2)
{
list[i].print();
cout << endl;
}
}
}
int addressBookType::search(string lName)
{
bool found = false;
int i = 0;
for (i = 0; i < length; i++)
if (list[i].isLastName(lName))
{
found = true;
break;
}
if (found)
return i;
else
return -1;
}
addressBookType::addressBookType()
{
length = 0;
}
void addressBookType::saveData(ofstream& outFile)
{
string first;
string last;
int month;
int day;
int year;
string street;
string city;
string state;
string zip;
string phone;
string pStatus;
for (int i = 0; i < length; i++)
{
list[i].getName(first, last);
list[i].getDOB(month, day, year);
list[i].getAddress(street, city, state, zip);
list[i].getPhoneNumber(phone);
list[i].getStatus(pStatus);
outFile << first << " " << last << endl;
outFile << month << " " << day << " " << year << endl;
outFile << street << endl << city << endl << state << endl << zip << endl;
outFile << phone << endl << pStatus << endl;
}
}
addressType.cpp
#include <iostream> // cin, cout, etc...
#include <string> // to use string class type
#include "addressType.h" //
using namespace std;
void addressType::print() const // const because the member var don't change
{
cout << streetAddress << endl;
cout << city << ", " << state << " - " << zip << endl;
}
void addressType::setAddress(string stAddress, string c, string s, string z)
{
streetAddress = stAddress;
city = c;
state = s;
zip = z;
}
void addressType::getAddress(string& stAddress, string& c,
string& s, string& z)
{
stAddress = streetAddress;
c = city;
s = state;
z = zip;
}
addressType::addressType(string stAddress, string c, string s, string z)
{
streetAddress = stAddress;
city = c;
state = s;
zip = z;
}
addressType.h
#ifndef H_addressType
#define H_addressType
#include <string> // to use string class
using namespace std;
class addressType
{
public:
void print() const;
void setAddress(string stAddress, string c, string s, string z); //set function
void getAddress(string& stAddress, string& c,
string& s, string& z); //get function
addressType(string stAddress = "", string c = "",
string s = "", string z = ""); // default constructor sets all fields to blank
private:
string streetAddress;
string city;
string state;
string zip;
};
#endif
arrayListType.cpp
#ifndef H_arrayListType
#define H_arrayListType
#include <iostream>
#include <cassert>
using namespace std;
template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>& operator=
(const arrayListType<elemType>&);
//Overloads the assignment operator
bool isEmpty() const;
//Function to determine whether the list is empty
//Postcondition: Returns true if the list is empty;
// otherwise, returns false.
bool isFull() const;
//Function to determine whether the list is full.
//Postcondition: Returns true if the list is full;
// otherwise, returns false.
int listSize() const;
//Function to determine the number of elements in the list
//Postcondition: Returns the value of length.
int maxListSize() const;
//Function to determine the size of the list.
//Postcondition: Returns the value of maxSize.
void print() const;
//Function to output the elements of the list
//Postcondition: Elements of the list are output on the
// standard output device.
bool isItemAtEqual(int location, const elemType& item) const;
//Function to determine whether the item is the same
//as the item in the list at the position specified by
//Postcondition: Returns true if the list[location]
// is the same as the item; otherwise,
// returns false.
void insertAt(int location, const elemType& insertItem);
//Function to insert an item in the list at the
//position specified by location. The item to be inserted
//is passed as a parameter to the function.
//Postcondition: Starting at location, the elements of the
// list are shifted down, list[location] = insertItem;,
// and length++;. If the list is full or location is
// out of range, an appropriate message is displayed.
void insertEnd(const elemType& insertItem);
//Function to insert an item at the end of the list.
//The parameter insertItem specifies the item to be inserted.
//Postcondition: list[length] = insertItem; and length++;
// If the list is full, an appropriate message is
// displayed.
void removeAt(int location);
//Function to remove the item from the list at the
//position specified by location
//Postcondition: The list element at list[location] is removed
// and length is decremented by 1. If location is out of
// range,an appropriate message is displayed.
void retrieveAt(int location, elemType& retItem) const;
//Function to retrieve the element from the list at the
//position specified by location.
//Postcondition: retItem = list[location]
// If location is out of range, an appropriate message is
// displayed.
void replaceAt(int location, const elemType& repItem);
//Function to replace the elements in the list at the
//position specified by location. The item to be replaced
//is specified by the parameter repItem.
//Postcondition: list[location] = repItem
// If location is out of range, an appropriate message is
// displayed.
void clearList();
//Function to remove all the elements from the list.
//After this operation, the size of the list is zero.
//Postcondition: length = 0;
int seqSearch(const elemType& item) const;
//Function to search the list for a given item.
//Postcondition: If the item is found, returns the location
// in the array where the item is found; otherwise,
// returns -1.
void insert(const elemType& insertItem);
//Function to insert the item specified by the parameter
//insertItem at the end of the list. However, first the
//list is searched to see whether the item to be inserted
//is already in the list.
//Postcondition: list[length] = insertItem and length++
// If the item is already in the list or the list
// is full, an appropriate message is displayed.
void remove(const elemType& removeItem);
//Function to remove an item from the list. The parameter
//removeItem specifies the item to be removed.
//Postcondition: If removeItem is found in the list,
// it is removed from the list and length is
// decremented by one.
arrayListType(int size = 100);
//constructor
//Creates an array of the size specified by the
//parameter size. The default array size is 100.
//Postcondition: The list points to the array, length = 0,
// and maxSize = size
arrayListType(const arrayListType<elemType>& otherList);
//copy constructor
~arrayListType();
//destructor
//Deallocates the memory occupied by the array.
protected:
elemType *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
};
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
return (length == 0);
}
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
return (length == maxSize);
}
template <class elemType>
int arrayListType<elemType>::listSize() const
{
return length;
}
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
return maxSize;
}
template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
template <class elemType>
bool arrayListType<elemType>::isItemAtEqual
(int location, const elemType& item) const
{
return (list[location] == item);
}
template <class elemType>
void arrayListType<elemType>::insertAt
(int location, const elemType& insertItem)
{
if (location < 0 || location >= maxSize)
cerr << "The position of the item to be inserted "
<< "is out of range" << endl;
else
if (length >= maxSize) //list is full
cerr << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at the
//specified position
length++; //increment the length
}
} //end insertAt
template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{
if (length >= maxSize) //the list is full
cerr << "Cannot insert in a full list" << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
}
} //end insertEnd
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if (location < 0 || location >= length)
cerr << "The location of the item to be removed "
<< "is out of range" << endl;
else
{
for (int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
} //end removeAt
template <class elemType>
void arrayListType<elemType>::retrieveAt
(int location, elemType& retItem) const
{
if (location < 0 || location >= length)
cerr << "The location of the item to be retrieved is "
<< "out of range." << endl;
else
retItem = list[location];
} //end retrieveAt
template <class elemType>
void arrayListType<elemType>::replaceAt
(int location, const elemType& repItem)
{
if (location < 0 || location >= length)
cerr << "The location of the item to be replaced is "
<< "out of range." << endl;
else
list[location] = repItem;
} //end replaceAt
template <class elemType>
void arrayListType<elemType>::clearList()
{
length = 0;
} //end clearList
template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const
{
int loc;
bool found = false;
for (loc = 0; loc < length; loc++)
if (list[loc] == item)
{
found = true;
break;
}
if (found)
return loc;
else
return -1;
} //end seqSearch
template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem)
{
int loc;
if (length == 0) //list is empty
list[length++] = insertItem; //insert the item and
//increment the length
else if (length == maxSize)
cerr << "Cannot insert in a full list." << endl;
else
{
loc = seqSearch(insertItem);
if (loc == -1) //the item to be inserted
//does not exist in the list
list[length++] = insertItem;
else
cerr << "the item to be inserted is already in "
<< "the list. No duplicates are allowed." << endl;
}
} //end insert
template<class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
int loc;
if (length == 0)
cerr << "Cannot delete from an empty list." << endl;
else
{
loc = seqSearch(removeItem);
if (loc != -1)
removeAt(loc);
else
cout << "The item to be deleted is not in the list."
<< endl;
}
} //end remove
template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if (size < 0)
{
cerr << "The array size must be positive. Creating "
<< "an array of size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
assert(list != NULL);
}
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete[] list;
}
template <class elemType>
arrayListType<elemType>::arrayListType
(const arrayListType<elemType>& otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize]; //create the array
assert(list != NULL); //terminate if unable to allocate
//memory space
for (int j = 0; j < length; j++) //copy otherList
list[j] = otherList.list[j];
} //end copy constructor
template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=
(const arrayListType<elemType>& otherList)
{
if (this != &otherList) //avoid self-assignment
{
delete[] list;
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize]; //create the array
assert(list != NULL); //if unable to allocate memory
//space, terminate the program
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
}
#endif
dateType.cpp
#include <iostream>
#include "dateType.h"
using namespace std;
void dateType::setDate(int month, int day, int year)
{
if (year >= 1)
dYear = year;
else
dYear = 1900;
if (1 <= month && month <= 12)
dMonth = month;
else
dMonth = 1;
switch (dMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (1 <= day && day <= 31)
dDay = day;
else
dDay = 1;
break;
case 4:
case 6:
case 9:
case 11:
if (1 <= day && day <= 30)
dDay = day;
else
dDay = 1;
break;
case 2:
if (isLeapYear(year))
{
if (1 <= day && day <= 29)
dDay = day;
else
dDay = 1;
}
else
{
if (1 <= day && day <= 28)
dDay = day;
else
dDay = 1;
}
}
}
void dateType::getDate(int& month, int& day, int& year)
{
month = dMonth;
day = dDay;
year = dYear;
}
void dateType::printDate() const
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
dateType::dateType(int month, int day, int year) //constructor with parameter
{
setDate(month, day, year);
}
bool dateType::isLeapYear(int y)
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
return true;
else
return false;
}
dateType.h
#ifndef date_H
#define date_H
class dateType
{
public:
void setDate(int month, int day, int year);
void getDate(int& month, int& day, int& year);
void printDate() const;
dateType(int month = 1, int day = 1, int year = 1900);
private:
bool isLeapYear(int y);
int dMonth;
int dDay;
int dYear;
};
#endif
extPersonType.cpp
#include <iostream>
#include <string>
#include "extPersonType.h"
void extPersonType::printAddress()
{
personType::print();
cout << endl;
address.print();
}
void extPersonType::printInfo()
{
personType::print();
cout << endl;
cout << "Date of Birth: ";
dob.printDate();
cout << endl;
cout << "Phone Number: " << phoneNumber << endl;
cout << "Person Type: " << personStatus << endl;
address.print();
cout << endl;
}
void extPersonType::setInfo(string fName, string lName,
int month, int day, int year,
string street, string c, string s,
string z, string phone, string pStatus)
{
personType::setName(fName, lName);
dob.setDate(month, day, year);
address.setAddress(street, c, s, z);
phoneNumber = phone;
personStatus = pStatus;
}
void extPersonType::setInfo(string fName, string lName,
dateType d, addressType ad,
string phone, string pStatus)
{
personType::setName(fName, lName);
dob = d;
address = ad;
phoneNumber = phone;
personStatus = pStatus;
}
bool extPersonType::isLastName(string lName)
{
string first;
string last;
personType::getName(first, last);
return (last == lName);
}
void extPersonType::getLastName(string& lName)
{
string first;
string last;
personType::getName(first, last);
lName = last;
}
void extPersonType::getAddress(string& sAddress, string& c,
string& s, string& z)
{
address.getAddress(sAddress, c, s, z);
}
void extPersonType::getPhoneNumber(string& phone)
{
phone = phoneNumber;
}
void extPersonType::getStatus(string& status)
{
status = personStatus;
}
bool extPersonType::isStatus(string status)
{
return (status == personStatus);
}
bool extPersonType::isDOB(int month, int day, int year)
{
int m;
int d;
int y;
dob.getDate(m, d, y);
return (m == month && d == day && y == year);
}
void extPersonType::getDOB(int& month, int& day, int& year)
{
dob.getDate(month, day, year);
}
bool extPersonType::isMonth(int month)
{
int m;
int d;
int y;
dob.getDate(m, d, y);
return (m == month);
}
extPersonType::extPersonType(string fName, string lName,
int month, int day, int year,
string street, string c, string s,
string z, string phone, string pStatus)
{
personType::setName(fName, lName);
dob.setDate(month, day, year);
address.setAddress(street, c, s, z);
phoneNumber = phone;
personStatus = pStatus;
}
extPersonType.h
#ifndef H_extPersonType
#define H_extPersonType
#include <string>
#include "addressType.h"
#include "dateType.h"
#include "personType.h"
class extPersonType : public personType //inherits functions of personType
{
public:
void printAddress() ; //prints address
void printInfo(); // prints person info
void setInfo(string fName, string lName, int month, int day, int year, string street, string c, string s, string z, string phone, string pStatus); //set function
void setInfo(string fName, string lName, dateType d, addressType ad, string phone, string pStatus);
bool isLastName(string lName); //checks to see if name matches
void getLastName(string& lName); //get function for last name
void getAddress(string& sAddress, string& c,
string& s, string& z); //gets address
void getStatus(string& status); //gets relationship status
void getPhoneNumber(string& phone); // gets phone number
bool isStatus(string status);
bool isDOB(int month, int day, int year);
void getDOB(int& month, int& day, int& year);
bool isMonth(int month);
extPersonType(string fName = "", string lName = "",
int month = 1, int day = 1, int year = 1900,
string street = "", string c = "", string s = "",
string z = "", string phone = "", string pStatus = ""); //default constructor
addressType address;
dateType dob;
string phoneNumber;
string personStatus;
};
#endif
personType.cpp
#include <iostream>
#include <string>
#include "personType.h"
using namespace std;
void personType::print() const
{
cout << firstName << " " << lastName;
}
void personType::setName(string first, string last)
{
firstName = first;
lastName = last;
}
void personType::getName(string& first, string& last)
{
first = firstName;
last = lastName;
}
//constructor with parameters
personType::personType(string first, string last)
{
firstName = first;
lastName = last;
}
personType::personType() //default constructor
{
firstName = "";
lastName = "";
}
personType.h
#include <iostream>
#include <string>
#include "personType.h"
using namespace std;
void personType::print() const
{
cout << firstName << " " << lastName;
}
void personType::setName(string first, string last)
{
firstName = first;
lastName = last;
}
void personType::getName(string& first, string& last)
{
first = firstName;
last = lastName;
}
//constructor with parameters
personType::personType(string first, string last)
{
firstName = first;
lastName = last;
}
personType::personType() //default constructor
{
firstName = "";
lastName = "";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.