I have been working on this phonebook application for a few days now. I am suppose to Create classes Name, PhoneNumber, and PhoneBook that are employed by the provided phone book program, PhoneBookProgram.cpp.
Class Name
Represents the name for a listing in a phone book (example: Phil’s Pizza)
Overloads stream insertion (<<) and stream extraction (>>) operators
Stream extraction operator validates text entered by user for a name
skips any initial white space typed by user
throws a runtime exception if input name is invalid
name must be at least 1 character and a maximum of 25 characters
does not modify Name object if input name is invalid
Class PhoneNumber
Represents the phone number for a listing in a phone book (example: 919-555-1234)
Overloads stream insertion (<<) and stream extraction (>>) operators
Stream extraction operator validates text entered by user for a phone number
skips any initial white space typed by user
throws a runtime exception if input phone number is invalid
phone number must be 12 characters in length
phone number must be in the format xxx-xxx-xxxx
phone number must only contain digits (0-9) except for two dashes (-)
does not modify PhoneNumber object if input phone number is invalid
Class PhoneBook
Represents the listings in a phone book
Overloads stream insertion (<<) operator
Stream insertion operator outputs phone book in tabular format:
Phil's Pizza 919-555-1234
Sarah's Shoes 800-111-1111
Blue's Clues PI 363-712-3434
add member function adds a listing to the phone book
throws a runtime exception if name already added to the phonebook
throws a runtime exception if maximum number of listings in the phone book (maximum listings = 1000)
prototype:
void add(const Name&, const PhoneNumber&);
find member function finds a listing to the phone book
returns the phone number for the listing found with name
throws a runtime exception if name not found in phonebook
prototype:
const PhoneNumber& find(const Name&) const;
Here is my code so far:
#ifndef Name_H
#define Name_H
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class Name :
{
friend ostream &operator<<( ostream &, const Name & );
friend istream &operator>>( istream &, Name & );
public:
Name(string name);
void setName(string n);
string getName() const;
string name;
};
#endif
#include <iostream>
#include <iomanip>
#include "Name.h"
using namespace std;
Name::Name(string n)
{
name = n;
}
void Name::setName(string n)
{
name = n;
}
string Name::getName() const
{
return name;
}
ostream &operator<< ( ostream &output, const Name &name)
{
output << name.name;
return output;
}
istream &operator>>(istream &input, Name &name )
{
char ws;
input >> ws;
getline(input,name.name);
}
#ifndef PhoneNumber_H
#define PhoneNumber_H
#include "Name.h"
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class PhoneNumber
{
friend ostream &operator<<( ostream &, const PhoneNumber & );
friend istream &operator>>( istream &, PhoneNumber & );
public:
PhoneNumber(string number);
void setNumber(string n);
string number;
};
#endif
#include <iostream>
#include <iomanip>
#include "PhoneNumber.h"
using namespace std;
PhoneNumber::PhoneNumber(string n)
{
number = n;
}
void PhoneNumber::setNumber(string n)
{
number = n;
}
ostream &operator<< ( ostream &output, const PhoneNumber &number)
{
output << number.number;
return output;
}
istream &operator>>(istream &input, PhoneNumber &number )
{
char ws;
input >> ws;
getline(input,number.number);
}
#ifndef PhoneBook_H
#define PhoneBook_H
#include "PhoneNumber.h"
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class PhoneBook
{
friend ostream &operator<<( ostream &, const PhoneNumber &);
public:
PhoneBook();
void add(const Name&, const PhoneNumber&);
const PhoneNumber& find(const Name&) const;
private:
string aNames[ 1000 ];
string aNumbers[ 1000 ];
};
#endif
#include <iostream>
#include <iomanip>
#include "PhoneBook.h"
#include "Name.h"
#include "PhoneNumber.h"
using namespace std;
ostream &operator<< ( ostream &output, const PhoneNumber &number)
{
ios_base::fmtflags originalFormat = output.flags();
output << left << setw(30) << Name::getName << number << endl;
output.flags(originalFormat);
}
void add(const Name &name, const PhoneNumber &number)
{
}
const PhoneNumber& find(const Name&)
{
}
Here is the driver which is supposed to be modified afterwards to hold the try/catch blocks
#include <iostream>
using namespace std;
#include "PhoneBook.h"
int main()
{
PhoneBook phoneBook;
bool quit = false; // Flag to stop looping
while (! quit)
{
// Output phone book menu
cout << endl
<< "Phone Book Menu\n"
<< "1: add a listing\n"
<< "2: find a listing\n"
<< "3: print phone book\n"
<< "q: quit\n\n"
<< "Choice: ";
// Input text typed by user
string choice;
cin >> ws; // skip initial whitespace
getline(cin, choice); // input user text
Name name;
PhoneNumber number;
if (choice == "1") // Add a listing
{
cout << "Add a Listing\n";
cout << "Enter listing name: ";
cin >> name;
cout << "Enter listing phone number: ";
cin >> number;
phoneBook.add(name, number);
}
else if (choice == "2") // Find a listing
{
cout << "Find a Listing\n";
cout << "Enter name to find: ";
cin >> name;
cout << phoneBook.find(name) << endl;
}
else if (choice == "3") // Print phone book
{
cout << "Print Phone Book\n";
cout << phoneBook;
}
else if (choice == "q") // Quit
{
cout << "Good-bye" << endl;
quit = true;
}
else // Invalid user input
{
cerr << "Invalid choice: " << choice << endl;
}
}
}
I am having a hard time figuring out how I am suppose to implement the find and add functions using the prototypes provided. If anyone could give me some help, I would appreciate it.