In: Computer Science
Refactor Assignment 1 into 3 project related files.
Customer.h - Class Specification
Customer.cpp - Class Implementation (Methods)
TestCustomer.cpp - Your code that performs the logic from Assignment 1.
The 3 files need to be named as listed above and should compile without errors.
I am not understanding how to do this.
Below is the code:
#include
#include
using namespace std;
const int NAME_SIZE = 20;
const int STREET_SIZE = 30;
const int CITY_SIZE = 20;
const int STATE_CODE_SIZE = 3;
class Customer
{
private:
long customerNumber;
char name[NAME_SIZE];
char streetAddress_1[STREET_SIZE];
char streetAddress_2[STREET_SIZE];
char city[CITY_SIZE];
char state[STATE_CODE_SIZE];
int zipCode;
public:
void setNumber(long number);
bool setName(char name[]);
bool setStreetAddress1(char street1[]);
bool setStreetAddress2(char street2[]);
bool setCity(char c[]);
bool setState(char st[]);
bool setZipCode(int zip);
long getNumber();
char* getName();
char* getStreetAddress1();
char* getStreetAddress2();
char* getCity();
char* getState();
int getZipCode();
};
void Customer::setNumber(long number)
{
customerNumber = number;
}
bool Customer::setName(char n[])
{
if (strlen(n) > 0)
{
strcpy(name, n);
return true;
}
return false;
}
bool Customer::setStreetAddress1(char street1[])
{
if (strlen(street1) > 0)
{
strcpy(streetAddress_1,
street1);
return true;
}
return false;
}
bool Customer::setStreetAddress2(char street2[])
{
if (strlen(street2) > 0)
{
strcpy(streetAddress_2,
street2);
return true;
}
return false;
}
bool Customer::setState(char st[])
{
if (strlen(st) > 0)
{
strcpy(state, st);
for (int i = 0; i <
strlen(state); i++)
{
state[i] =
toupper(state[i]);
if (state[i]
< 'A' || state[i] > 'Z')
return false;
}
return true;
}
return false;
}
bool Customer::setCity(char c[])
{
if (strlen(c) > 0)
{
strcpy(city, c);
for (int i = 0; i <
strlen(city); i++)
{
city[i] =
toupper(city[i]);
if (city[i] <
'A' || city[i] > 'Z')
return false;
}
return true;
}
return false;
}
bool Customer::setZipCode(int zip)
{
if (zip >= 0 && zip <= 99999)
{
zipCode = zip;
return true;
}
return false;
}
long Customer::getNumber()
{
return customerNumber;
}
char* Customer::getName()
{
return name;
}
char* Customer::getStreetAddress1()
{
return streetAddress_1;
}
char* Customer::getStreetAddress2()
{
return streetAddress_2;
}
char* Customer::getState()
{
return state;
}
char* Customer::getCity()
{
return city;
}
int Customer::getZipCode()
{
return zipCode;
}
int main()
{
Customer info;
char name[NAME_SIZE];
char streetAddress_1[STREET_SIZE];
char streetAddress_2[STREET_SIZE];
char city[CITY_SIZE];
char state[STATE_CODE_SIZE];
int zipCode;
info.setNumber(1);
do
{
cout << "What is your name:
";
cin.getline(name, NAME_SIZE);
} while (!info.setName(name));
do
{
cout << "Street you live on:
";
cin.getline(streetAddress_1,
STREET_SIZE);
} while
(!info.setStreetAddress1(streetAddress_1));
do
{
cout << "Second street(if
applicable, type N/A if not): ";
cin.getline(streetAddress_2,
STREET_SIZE);
} while
(!info.setStreetAddress2(streetAddress_2));
do
{
cout << "What state do you
live in: ";
cin.getline(state,
STATE_CODE_SIZE);
} while (!info.setState(state));
do
{
cout << "What City do you
live in: ";
cin.getline(city, CITY_SIZE);
} while (!info.setCity(city));
do
{
cout << "what is the zip
code: ";
cin >> zipCode;
} while (!info.setZipCode(zipCode));
cout << "\n" << "Details of Customer: "
<< "\n";
cout << "Customer Number: " <<
info.getNumber() << "\n";
cout << "Name: " << info.getName()
<< "\n";
cout << "Street Address 1: " <<
info.getStreetAddress1() << "\n";
cout << "Street Address 2: " <<
info.getStreetAddress2() << "\n";
cout << "State: " << info.getState()
<< "\n";
cout << "City: " << info.getCity()
<< "\n";
cout << "Zip code: " << info.getZipCode()
<< "\n";
return 0;
}
Program Code [C++]
Customer.h
const int NAME_SIZE = 20;
const int STREET_SIZE = 30;
const int CITY_SIZE = 20;
const int STATE_CODE_SIZE = 3;
#include <cstring>
#include <cctype>
class Customer
{
private:
long customerNumber;
char name[NAME_SIZE];
char streetAddress_1[STREET_SIZE];
char streetAddress_2[STREET_SIZE];
char city[CITY_SIZE];
char state[STATE_CODE_SIZE];
int zipCode;
public:
void setNumber(long number);
bool setName(char name[]);
bool setStreetAddress1(char street1[]);
bool setStreetAddress2(char street2[]);
bool setCity(char c[]);
bool setState(char st[]);
bool setZipCode(int zip);
long getNumber();
char* getName();
char* getStreetAddress1();
char* getStreetAddress2();
char* getCity();
char* getState();
int getZipCode();
};
Customer.cpp
#include "Customer.h"
void Customer::setNumber(long number)
{
customerNumber = number;
}
bool Customer::setName(char n[])
{
if (strlen(n) > 0)
{
strcpy(name, n);
return true;
}
return false;
}
bool Customer::setStreetAddress1(char street1[])
{
if (strlen(street1) > 0)
{
strcpy(streetAddress_1, street1);
return true;
}
return false;
}
bool Customer::setStreetAddress2(char street2[])
{
if (strlen(street2) > 0)
{
strcpy(streetAddress_2, street2);
return true;
}
return false;
}
bool Customer::setState(char st[])
{
if (strlen(st) > 0)
{
strcpy(state, st);
for (int i = 0; i < strlen(state); i++)
{
state[i] = toupper(state[i]);
if (state[i] < 'A' || state[i] > 'Z')
return false;
}
return true;
}
return false;
}
bool Customer::setCity(char c[])
{
if (strlen(c) > 0)
{
strcpy(city, c);
for (int i = 0; i < strlen(city); i++)
{
city[i] = toupper(city[i]);
if (city[i] < 'A' || city[i] > 'Z')
return false;
}
return true;
}
return false;
}
bool Customer::setZipCode(int zip)
{
if (zip >= 0 && zip <= 99999)
{
zipCode = zip;
return true;
}
return false;
}
long Customer::getNumber()
{
return customerNumber;
}
char* Customer::getName()
{
return name;
}
char* Customer::getStreetAddress1()
{
return streetAddress_1;
}
char* Customer::getStreetAddress2()
{
return streetAddress_2;
}
char* Customer::getState()
{
return state;
}
char* Customer::getCity()
{
return city;
}
int Customer::getZipCode()
{
return zipCode;
}
TestCustomer.cpp
#include <iostream>
#include "Customer.h"
using namespace std;
int main() {
Customer info;
char name[NAME_SIZE];
char streetAddress_1[STREET_SIZE];
char streetAddress_2[STREET_SIZE];
char city[CITY_SIZE];
char state[STATE_CODE_SIZE];
int zipCode;
info.setNumber(1);
do
{
cout << "What is your name: ";
cin.getline(name, NAME_SIZE);
} while (!info.setName(name));
do
{
cout << "Street you live on: ";
cin.getline(streetAddress_1, STREET_SIZE);
} while (!info.setStreetAddress1(streetAddress_1));
do
{
cout << "Second street(if applicable, type N/A if not):
";
cin.getline(streetAddress_2, STREET_SIZE);
} while (!info.setStreetAddress2(streetAddress_2));
do
{
cout << "What state do you live in: ";
cin.getline(state, STATE_CODE_SIZE);
} while (!info.setState(state));
do
{
cout << "What City do you live in: ";
cin.getline(city, CITY_SIZE);
} while (!info.setCity(city));
do
{
cout << "what is the zip code: ";
cin >> zipCode;
} while (!info.setZipCode(zipCode));
cout << "\n" << "Details of Customer: " <<
"\n";
cout << "Customer Number: " << info.getNumber()
<< "\n";
cout << "Name: " << info.getName() << "\n";
cout << "Street Address 1: " <<
info.getStreetAddress1() << "\n";
cout << "Street Address 2: " <<
info.getStreetAddress2() << "\n";
cout << "State: " << info.getState() <<
"\n";
cout << "City: " << info.getCity() << "\n";
cout << "Zip code: " << info.getZipCode() <<
"\n";
return 0;
}
Sample Output:-
----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!