Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Redo Programming so that the address book is stored in a vector object. addressBookType.h #pragma once #include #include arrayListType.h #include extPersonType.h class addressBookType : public

Redo Programming so that the address book is stored in a vector object.

addressBookType.h

#pragma once

#include

#include "arrayListType.h"

#include "extPersonType.h"

class addressBookType :

public arrayListType

{

public:

//constructors

addressBookType();

addressBookType(const string& new_fileName);

int find(const string& lastName) const;

void print(const extPersonType& person) const;

void print(relationShipType relationship) const;

void print(int birthMonth) const;

void print(const dateType& oldDate, const dateType& newDate) const;

void print(const string& highLastName, const string& lowLastName) const;

void print() const;

bool load(ifstream& source);

bool store(ofstream& sink);

private:

int compareDates(const dateType& d1, const dateType& d2) const;

};

addressBookType.cpp

#include

#include "addressBookType.h"

addressBookType::addressBookType()

:arrayListType(500)

{

}

addressBookType::addressBookType(const string& new_fileName)

: arrayListType(500)

{

ifstream source(new_fileName);

assert(source.good());

load(source);

source.close();

}

bool addressBookType::load(ifstream& source)

{

string firstName;

source >> firstName;

while (!source.eof() && !isFull())

{

string lastName;

source >> lastName;

string telephoneNumber;

source >> telephoneNumber;

int month, day, year;

source >> year >> month >> day;

dateType dob(month, day, year);

int temp;

relationShipType relationship;

source >> temp;

relationship = static_cast(temp);

extPersonType extPerson(firstName, lastName, telephoneNumber, dob, relationship);

insertEnd(extPerson);

source >> firstName;

}

return source.eof() || isFull();

}

bool addressBookType::store(ofstream& sink)

{

for (int index = 0; index < listSize(); index++)

{

extPersonType extPerson;

retrieveAt(index, extPerson);

sink << extPerson.getFirstName() << endl;

sink << extPerson.getLastName() << endl;

sink << extPerson.getTelephoneNumber() << endl;

dateType dob = extPerson.getDOB();

sink << dob.getYear() << ' ' << dob.getMonth() << ' ' << dob.getDay() << endl;

sink << extPerson.getRelationShip() << endl;

}

return sink.good();

}

int addressBookType::find(const string& lastName) const

{

for (int loc = 0; loc < listSize(); loc++)

{

extPersonType extPerson;

retrieveAt(loc, extPerson);

if (extPerson.getLastName() == lastName)

return loc;

}

return -1;

}

void addressBookType::print(const extPersonType& person) const

{

person.print();

}

void addressBookType::print(relationShipType relationship) const

{

for (int loc = 0; loc < listSize(); loc++)

{

extPersonType extPerson;

retrieveAt(loc, extPerson);

if (extPerson.getRelationShip() == relationship)

{

extPerson.print();

cout << endl;

}

}

}

void addressBookType::print(int birthMonth) const

{

for (int loc = 0; loc < listSize(); loc++)

{

extPersonType extPerson;

retrieveAt(loc, extPerson);

if (extPerson.getDOB().getMonth() == birthMonth)

{

extPerson.print();

cout << endl;

}

}

}

void addressBookType::print(const dateType& oldDate, const dateType& newDate) const

{

for (int loc = 0; loc < listSize(); loc++)

{

extPersonType extPerson;

retrieveAt(loc, extPerson);

dateType dob = extPerson.getDOB();

if (0 <= compareDates(oldDate, dob) && compareDates(dob, newDate) <= 0)

{

extPerson.print();

cout << endl;

}

}

}

void addressBookType::print() const

{

for (int loc = 0; loc < listSize(); loc++)

{

extPersonType extPerson;

retrieveAt(loc, extPerson);

dateType dob = extPerson.getDOB();

extPerson.print();

cout << endl;

}

}

int addressBookType::compareDates(const dateType& date1, const dateType& date2) const

{

int y1 = date1.getYear();

int m1 = date1.getMonth();

int d1 = date1.getDay();

int y2 = date2.getYear();

int m2 = date2.getMonth();

int d2 = date2.getDay();

if (y1 < y2) return -1;

if (y1 > y2) return 1;

if (m1 < m2) return -1;

if (m1 > m2) return 1;

if (d1 < d2) return -1;

if (d1 > d2) return 1;

return 0;

}

addressType.h

#pragma once

#include

using namespace std;

class addressType

{

public:

//constructors

addressType();

addressType(const string& new_streetAddress, const string& new_city, const string& new_state, const string& new_zipCode);

//accessors

void getAddress(string& new_streetAddress, string& new_city, string& new_state, string& new_zipCode) const;

string getStreetAddress() const;

string getCity() const;

string getState() const;

string getZipCode() const;

//mutators

void setAddress(const string& new_streetAddress, const string& new_city, const string& new_state, const string& new_zipCode);

void setStreetAddress(const string& new_streetAddress);

void setCity(const string& new_city);

void setState(const string& new_state);

void setZipCode(const string& new_zipCode);

private:

string _streetAddress;

string _city;

string _state;

string _zipCode;

};

addressType.cpp

#include "addressType.h"

addressType::addressType()

{

setAddress("855 North Vermont Avenue", "Los Angeles", "California", "90029");

}

addressType::addressType(const string& new_streetAddress, const string& new_city, const string& new_state, const string& new_zipCode)

{

setAddress(new_streetAddress, new_city, new_state, new_zipCode);

}

//accessors

void addressType::getAddress(string& new_streetAddress, string& new_city, string& new_state, string& new_zipCode) const

{

}

string addressType::getStreetAddress() const

{

return _streetAddress;

}

string addressType::getCity() const

{

return _city;

}

string addressType::getState() const

{

return _state;

}

string addressType::getZipCode() const

{

return _zipCode;

}

void addressType::setAddress(const string& new_streetAddress, const string& new_city, const string& new_state, const string& new_zipCode)

{

}

void addressType::setStreetAddress(const string& new_streetAddress)

{

}

void addressType::setCity(const string& new_city)

{

}

void addressType::setState(const string& new_state)

{

}

void addressType::setZipCode(const string& new_zipCode)

{

}

arrayListType.h

#ifndef H_arrayListType

#define H_arrayListType

//***********************************************************

// Author: D.S. Malik

//

// This class specifies the members to implement the basic

// properties of array-based lists.

//***********************************************************

#include

#include

using namespace std;

template

class arrayListType

{

public:

const arrayListType& operator=(const arrayListType&);

//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& 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

bool arrayListType::isEmpty() const

{

return (length == 0);

}

template

bool arrayListType::isFull() const

{

return (length == maxSize);

}

template

int arrayListType::listSize() const

{

return length;

}

template

int arrayListType::maxListSize() const

{

return maxSize;

}

template

void arrayListType::print() const

{

for (int i = 0; i < length; i++)

cout << list[i] << " ";

cout << endl;

}

template

bool arrayListType::isItemAtEqual(int location, const elemType& item) const

{

return (list[location] == item);

}

template

void arrayListType::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

void arrayListType::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

void arrayListType::removeAt(int location)

{

if (location < 0 || location >= length)

cerr << "The location of the item to be removed is out of range" << endl;

else

{

list[location] = list[length - 1];

length--;

}

} //end removeAt

template

void arrayListType::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

void arrayListType::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

void arrayListType::clearList()

{

length = 0;

} //end clearList

template

int arrayListType::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

void arrayListType::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

void arrayListType::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

arrayListType::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

arrayListType::~arrayListType()

{

delete [] list;

}

template

arrayListType::arrayListType(const arrayListType& 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

const arrayListType& arrayListType::operator=(const arrayListType& 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.h

#pragma once

#include

#include

using namespace std;

class dateType

{

friend ostream& operator<<(ostream& outs, const dateType& date);

public:

//constructors

dateType();

dateType(int m, int d, int y);

//accessors

int getMonth() const;

int getDay() const;

int getYear() const;

void getDate(int& m, int& d, int& y);

//mutators

void setMonth(int m);

void setDay(int d);

void setYear(int y);

void setDate(int m, int d, int y);

//predicates

bool isLeapYear() const;

//custom methods

int daysInMonth() const;

int daysInYear() const;

int daysPassedFromDate() const;

int daysRemainingInYear() const;

bool endOfMonth() const;

bool endOfYear() const;

void newDate(int addDays);

private:

int _month, _day, _year;

static int _daysInMonth[];

static int _daysPassedByMonth[];

static int _daysInMonthLeapYear[];

static int _daysPassedByMonthLeapYear[];

};

dateType.cpp

#include "dateType.h"

int dateType::_daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int dateType::_daysPassedByMonth[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };

int dateType::_daysInMonthLeapYear[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int dateType::_daysPassedByMonthLeapYear[] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};

//over loaded I/O operators

ostream& operator<<(ostream& outs, const dateType& date)

{

outs << date._month << '/' << date._day << '/' << date._year;

return outs;

}

//constructors

dateType::dateType()

{

setYear(1900);

setMonth(1);

setDay(1);

}

dateType::dateType(int new_month, int new_day, int new_year)

{

setYear(new_year);

setMonth(new_month);

setDay(new_day);

}

//predicates

bool dateType::isLeapYear() const

{

return _year % 4 == 0 && (_year % 100 != 0 || _year % 400 == 0);

}

//accessors

int dateType::getMonth() const

{

return _month;

}

int dateType::getDay() const

{

return _day;

}

int dateType::getYear() const

{

return _year;

}

void dateType::getDate(int& new_month, int& new_day, int& new_year)

{

new_month = _month;

new_day = _day;

new_year = _year;

}

//mutators

void dateType::setYear(int new_year)

{

_year = new_year < 1900 ? 1900 : new_year;

}

void dateType::setMonth(int new_month)

{

_month = new_month < 1 || 12 < new_month ? 1 : new_month;

}

void dateType::setDay(int new_day)

{

if (new_day < 1 || _month == 2 && isLeapYear() && _daysInMonthLeapYear[_month - 1] < _day) _day = 1;

else if (_daysInMonth[_month - 1] < _day) _day = 1;

else _day = new_day;

}

void dateType::setDate(int new_month, int new_day, int new_year)

{

setYear(new_year);

setMonth(new_month);

setDay(new_day);

}

//custom methods

int dateType::daysInMonth() const

{

return isLeapYear() ? _daysInMonthLeapYear[_month - 1] : _daysInMonth[_month - 1];

}

int dateType::daysInYear() const

{

return isLeapYear() ? 366 : 365;

}

int dateType::daysPassedFromDate() const

{

int daysPassed;

if (isLeapYear()) daysPassed = _daysPassedByMonthLeapYear[_month - 1];

else daysPassed = _daysPassedByMonth[_month - 1];

return daysPassed + _day;

}

int dateType::daysRemainingInYear() const

{

return daysInYear() - daysPassedFromDate();

}

void dateType::newDate(int addDays)

{

if (addDays < 0) addDays = 0;

for (int counter = 1; counter <= addDays; counter++)

{

if (endOfYear())

{

_year++;

_month = 1;

_day = 1;

}

else if (endOfMonth())

{

_month++;

_day = 1;

}

else

_day++;

}

}

bool dateType::endOfMonth() const

{

return isLeapYear() ? _day == _daysInMonthLeapYear[_month - 1] : _day == _daysInMonth[_month - 1];

}

bool dateType::endOfYear() const

{

return _month == 12 && endOfMonth();

}

extPersonType.h

#pragma once

#include "dateType.h"

#include "personType.h"

enum relationShipType {FAMILY, FRIEND, BUSINESS, OTHER};

class extPersonType :

public personType

{

public:

//constructors

extPersonType();

extPersonType(const string& new_firstName, const string& new_lastName, const string& new_telephoneNumber, const dateType& new_dob, relationShipType new_relationship);

//accessors

void getExtPerson(string& new_firstName, string& new_lastName, string& new_telephoneNumber, dateType& new_dob, relationShipType& new_relationship) const;

string getTelephoneNumber() const;

dateType getDOB() const;

relationShipType getRelationShip() const;

//mutators

void setExtPerson(const string& new_firstName, const string& new_lastName, const string& new_telephoneNumber, const dateType& new_dob, relationShipType new_relationship);

void setTelephoneNumber(const string& new_telephoneNumber);

void setDOB(const dateType& new_dob);

void setRelationship(relationShipType new_relationShip);

//custom methods

void print() const;

private:

string _telephoneNumber;

relationShipType _relationship;

dateType _dob;

};

extPersonType.cpp

#include "extPersonType.h"

//constructors

extPersonType::extPersonType()

:personType()

{

_telephoneNumber = "0000000000";

_dob.setDate(01, 01, 1900);

_relationship = OTHER;

}

extPersonType::extPersonType(const string& new_firstName, const string& new_lastName, const string& new_telephoneNumber, const dateType& new_dob, relationShipType new_relationship)

:personType(new_firstName, new_lastName)

{

setTelephoneNumber(new_telephoneNumber);

setDOB(new_dob);

setRelationship(new_relationship);

}

//accessors

void extPersonType::getExtPerson(string& new_firstName, string& new_lastName, string& new_telephoneNumber, dateType& new_dob, relationShipType& new_relationship) const

{

new_firstName = getFirstName();

new_lastName = getLastName();

new_telephoneNumber = getTelephoneNumber();

new_dob = getDOB();

new_relationship = getRelationShip();

}

string extPersonType::getTelephoneNumber() const

{

return _telephoneNumber;

}

dateType extPersonType::getDOB() const

{

return _dob;

}

relationShipType extPersonType::getRelationShip() const

{

return _relationship;

}

//mutators

void extPersonType::setExtPerson(const string& new_firstName, const string& new_lastName, const string& new_telephoneNumber, const dateType& new_dob, relationShipType new_relationship)

{

setName(new_firstName, new_lastName);

setTelephoneNumber(new_telephoneNumber);

setDOB(new_dob);

setRelationship(new_relationship);

}

void extPersonType::setTelephoneNumber(const string& new_telephoneNumber)

{

_telephoneNumber = new_telephoneNumber;

}

void extPersonType::setDOB(const dateType& new_dob)

{

_dob = new_dob;

}

void extPersonType::setRelationship(relationShipType new_relationship)

{

_relationship = new_relationship;

}

//custom methods

void extPersonType::print() const

{

cout << "first name : " << getFirstName() << endl;

cout << "last name : " << getLastName() << endl;

cout << "telephone number: " << getTelephoneNumber() << endl;

cout << "birth date : " << _dob.getYear() << '-' << _dob.getMonth() << '-' << _dob.getDay() << endl;

cout << "relationship : " << getRelationShip() << endl;

}

personType.h

//************************************************************ // Author: D.S. Malik // // class personType // This class specifies the members to implement a name. //************************************************************ #include

using namespace std;

class personType { public: void print() const; //Function to output the first name and last name //in the form firstName lastName. void setName(const string& first, const string& last); //Function to set firstName and lastName according to the //parameters. //Postcondition: firstName = first; lastName = last

string getFirstName() const; //Function to return the first name. //Postcondition: The value of firstName is returned.

string getLastName() const; //Function to return the last name. //Postcondition: The value of lastName is returned.

personType(); //Default constructor //Sets firstName and lastName to null strings. //Postcondition: firstName = ""; lastName = "";

personType(const string& first, const string& last); //Constructor with parameters. //Sets firstName and lastName according to the parameters. //Postcondition: firstName = first; lastName = last;

private: string _first; //variable to store the first name string _last; //variable to store the last name };

personType.cpp

//************************************************************

// Author: D.S. Malik

//

// Implemenation file personType

//************************************************************

#include

#include

#include "personType.h"

using namespace std;

void personType::print() const

{

cout << _first << " " << _last;

}

void personType::setName(const string& new_first, const string& new_last)

{

_first = new_first;

_last = new_last;

}

string personType::getFirstName() const

{

return _first;

}

string personType::getLastName() const

{

return _last;

}

//Default constructor

personType::personType()

{

_first = "";

_last = "";

}

//Constructor with parameters

personType::personType(const string& new_first, const string& new_last)

{

_first = new_first;

_last = new_last;

}

Source.cpp

#include

#include

#include "addressBookType.h"

using namespace std;

int main()

{

addressBookType addressBook("addressBook.txt");

addressBook.print();

ofstream sink("addressBook.txt");

addressBook.store(sink);

sink.close();

system("pause");

return 0;

}

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

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions