PersonalInformation.cpp
PersonalInformationDemo.cpp
PersonalInformation.h
/* PersonalInformation.h - Specification file for the Personal Information class. */
#ifndef PERSONAL_INFORMATION_H
#define PERSONAL_INFORMATION_H
#include <array>
#include <string>
using std::string;
static constexpr int NUM_ITEMS = 2;
class Personal
{
private:
string name; // Holds the name
string address; // Holding the address
short int age; // Holding the age
string phoneNumber; // Holding the telephone number
static const std::array<Personal, NUM_ITEMS> friends; // Array of class objects
public:
static constexpr int MAX_LEN = 25; // Maximmum name and address length
static constexpr int MAX_AGE = 85; // Maximum age
static constexpr int PNUM_LEN = 10; // Maximum phone number length
// Default Constructor
Personal()
{
name = "";
address = "";
age = 0;
phoneNumber = "";
}
// Constructor accepting arguments for name, address, age and phone number
Personal(string n, string addr, int a, string pNum)
{
name = n;
address = addr;
age = a;
phoneNumber = pNum;
}
// Mutators
bool setName(string);
bool setAddress(string);
bool setAge(short int);
bool setPhoneNumber(string);
// Accessors
string getName() const
{ return name; }
string getAddress() const
{ return address; }
short int getAge() const
{ return age; }
string getPhoneNumber() const
{ return phoneNumber; }
void displayPersonalInfo() const;
void displayFriendsInfo(std::array<Personal, NUM_ITEMS>) const;
};
#endif
PersonalInformation.cpp
/* PersonalInformation.cpp - Implementation file for the Personal
Information class */
#include <array>
#include <iostream>
#include <iomanip>
#include <string>
#include "PersonalInformation.h"
/* **********************************************************
Personal::friends
This function initializes all array elements via default
constructor.
********************************************************** */
const std::array<Personal, NUM_ITEMS> Personal::friends{ Personal() };
/* **********************************************************
Personal::setName
Assigns a value to the name member variable.
********************************************************** */
bool Personal::setName(string n)
{
if (n.length() > 1 && n.length() < MAX_LEN)
{
name = n;
return true;
}
else
{
std::cout << "\nName too short.\n";
}
return false;
}
/* **********************************************************
Personal::setAddress
Assigns a value to the address member variable.
********************************************************** */
bool Personal::setAddress(string addr)
{
if (addr.length() > 1 && addr.length() < MAX_LEN)
{
address = addr;
return true;
}
else
{
std::cout << "\nInvalid address format.\n";
}
return false;
}
/* **********************************************************
Personal::setAge
Assigns a value to the age member variable.
********************************************************** */
bool Personal::setAge(short int a)
{
if (a > 0 && a <= MAX_AGE)
{
age = a;
return true;
}
else
{
std::cout << "\nInvalid age.\n";
return false;
}
return false;
}
/* **********************************************************
Personal::setPhoneNumber
Assigns an address to the phoneNumber member variable.
********************************************************** */
bool Personal::setPhoneNumber(string phone)
{
if (phone[0] != '\0' && phone.size() <= PNUM_LEN)
{
phoneNumber = phone;
return true;
}
else
{
std::cout << "\nPhone number invalid.\n";
}
return false;
}
/* **********************************************************
Personal::displayPersonalInfo
Displays the personal information.
********************************************************** */
void Personal::displayPersonalInfo() const
{
std::cout << "\n\nADDRESS BOOK - PERSONAL INFORMATION\n\n";
std::cout << std::setw(14) << "Name: " << std::setw(2) << getName() << "\n"
<< std::setw(14) << "Address: " << std::setw(2) << getAddress() << "\n"
<< std::setw(14) << "Age: " << std::setw(2) << getAge() << "\n"
<< std::setw(14) << "Phone Number: " << std::setw(2) << getPhoneNumber() << "\n";
}
/* **********************************************************
Personal::displayFriendsInfo
This function accepts an array of class objects, holding
personal information about two friends. This information
is displayed.
********************************************************** */
void Personal::displayFriendsInfo(std::array<Personal, NUM_ITEMS> friends) const
{
std::cout << "\n\nADDRESS BOOK - FRIENDS LIST\n\n";
for (int i = 0; i < NUM_ITEMS; i++)
{
std::cout << std::setw(14) << "Name: " << std::setw(2) << friends[i].getName() << "\n"
<< std::setw(14) << "Address: " << std::setw(2) << friends[i].getAddress() << "\n"
<< std::setw(14) << "Age: " << std::setw(2) << friends[i].getAge() << "\n"
<< std::setw(14) << "Phone Number: " << std::setw(2) << friends[i].getPhoneNumber() << "\n\n";
}
}
PersonalInformationDemo.cpp
/* PersonalInformationDemo - This function demonstrates the Personal Information class. */
#include <array>
#include <string>
#include <iostream>
#include <iomanip>
#include "PersonalInformation.h"
using std::array;
using std::cin;
using std::cout;
using std::getline;
void getInfo(array<Personal, NUM_ITEMS> &);
int main()
{
Personal pInfo("Yuki, Mori", "Hachinohe-shi, 4-choume, 3-4, Mansion 5F", 39, "0178-*****");
std::array<Personal, NUM_ITEMS> friends; // Declare an array of class objects
cout << "WELCOME TO YOUR PERSONAL INFORMATION BUTLER\n\n"
<< "Please enter the following information about your " << NUM_ITEMS << " friends:\n";
getInfo(friends);
pInfo.displayPersonalInfo();
pInfo.displayFriendsInfo(friends);
cin.ignore();
cin.get();
return 0;
}
/* **********************************************************
Function getInfo (accepts an array of class objects)
The user to enter information about his or her friends or
family members. This information is validated. If valid,
the information is stored in the member variables, else
the user is asked to repeat the input.
********************************************************** */
void getInfo(array<Personal, NUM_ITEMS> &friends)
{
bool validInput = false;
string name = "";
string address = "";
short int age = 0;
string phone = "";
for (int i = 0; i < NUM_ITEMS; i++)
{
cout << "\nEnter your friends name: ";
getline(cin, name);
while (!friends[i].setName(name))
{
cout << "Enter your friends name: ";
getline(cin, name);
}
cout << "Enter your friends address: ";
getline(cin, address);
while (!friends[i].setAddress(address))
{
cout << "Enter your friends address: ";
getline(cin, address);
}
cout << "Enter your friends age: ";
cin >> age;
do
{
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "Enter your friends age: ";
cin >> age;
}
else
{
validInput = true;
}
} while (validInput == false);
while (!friends[i].setAge(age))
{
cout << "\nEnter your friends age: ";
cin >> age;
}
cin.ignore();
cout << "Enter your friends phone number: ";
getline(cin, phone);
while (!friends[i].setPhoneNumber(phone))
{
cout << "Enter your friends phone number: ";
cin.clear();
getline(cin, phone);
}
}
}
No comments:
Post a Comment