In: Computer Science
1. Design a class based on a modified Customer Structure. Use private and public definitions.
2. Create class methods (Setters and Getters) that load the data values. The Setter methods will validate the input data based on the criteria below and load the data into the class if valid.
For character arrays, the data must be within the specified data size. (See #3 below)
For zipCode, the number must be between 0 and 99999.
City and State need to be converted and stored in uppercase.
To get started loading the c-strings.
Getters
The book does not seem to provide a good example of what a (getter) method that returns a char array should look like. Section 9.9 is very close but uses string as examples instead of c-strings.
The best approach is to return a pointer to the c-string- Described as below:
char* getName(); // declaration - also known as prototype
// Customer is my Class name
char* Customer::getName() { // defined member function - Return a pointer to the c-string return name; }
cout << YOUR_INSTANCE_HERE.getName() << endl;
Setters
As for creating a setter method - you can define unsized char arrays in the method header
bool Customer::setName(char inputName[]) {
// code here..
}
3. Write a program that will allow me to enter data for the class and create and call a method that displays the data for the class.
During the input process, pass the input data to the setter method and confirm that the data entered is valid based on the class method return value. If not valid,prompt for me to re-enter the data until it does pass the class method's validation.(A do-while loop works well for this situation) I only need to enter the data for 1 instance of the class. The best way to do this is for the setter methods to return bool instead of void. If the data is valid, return true. Otherwise return false.
There is no need to loop for multiple instances of the object. Do not use cin statements inside the class method definitions.
Below is the original structure to use to create your class definition.
const int NAME_SIZE = 20;
const int STREET_SIZE = 30;
const int CITY_SIZE = 20;
const int STATE_CODE_SIZE = 3;
struct Customer { 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;
};
Hints: The const variables need to be defined outside of the class definition.
All access to the data in the class must use public methods that you define. DO NOT DIRECTLY ACCESS the customer data items.
The class level input methods should return a boolean to confirm the data was loaded and passed any size or data ranges. The customerNumber setter does not need to return a value - It can be a void setter.
Set the customerNumber to 1 via the code - not user input. There is no input loop so only 1 set of customer's data is to be entered.
// C++ program to create Customer class
#include <iostream>
#include <cstring>
#include <cctype>
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 setCustomerNumber(long cn);
bool setName(char n[]);
bool setStreetAddress1(char sa[]);
bool setStreetAddress2(char sa[]);
bool setCity(char c[]);
bool setState(char s[]);
bool setZipCode(int z);
long getCustomerNumber();
char* getName() ;
char* getStreetAddress1();
char* getStreetAddress2();
char* getCity();
char* getState();
int getZipCode();
};
// setters
void Customer::setCustomerNumber(long cn)
{
customerNumber = cn;
}
bool Customer:: setName(char n[])
{
// validate name is not an empty string
if(strlen(n) > 0)
{
strcpy(name, n);
return true;
}
return false;
}
bool Customer:: setStreetAddress1(char sa[])
{
// validate street address1 is not an empty string
if(strlen(sa) > 0)
{
strcpy(streetAddress_1, sa);
return true;
}
return false;
}
bool Customer:: setStreetAddress2(char sa[])
{
// validate street address2 is not an empty string
if(strlen(sa) > 0)
{
strcpy(streetAddress_2, sa);
return true;
}
return false;
}
bool Customer:: setCity(char c[])
{
// validate city is not an empty string
if(strlen(c) > 0)
{
strcpy(city, c);
// validate all characters of city are letters
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:: setState(char s[])
{
// validate state is not an empty string
if(strlen(s) > 0)
{
strcpy(state, s);
// validate all characters of state are letters
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:: setZipCode(int z)
{
// validate zip code is between [0,99999]
if(z >= 0 && z <= 99999)
{
zipCode = z;
return true;
}
return false;
}
// getters
long Customer:: getCustomerNumber()
{
return customerNumber;
}
char* Customer:: getName()
{
return name;
}
char* Customer:: getStreetAddress1()
{
return streetAddress_1;
}
char* Customer:: getStreetAddress2()
{
return streetAddress_2;
}
char* Customer:: getCity()
{
return city;
}
char* Customer:: getState()
{
return state;
}
int Customer:: getZipCode()
{
return zipCode;
}
int main()
{
Customer cust;
cust.setCustomerNumber(1);
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;
// input customer details, re-prompt if invalid until it is
valid
do{
cout<<"Input name: ";
cin.getline(name, NAME_SIZE);
}while(!cust.setName(name));
do{
cout<<"Input street address 1: ";
cin.getline(streetAddress_1, STREET_SIZE);
}while(!cust.setStreetAddress1(streetAddress_1));
do{
cout<<"Input street address 2: ";
cin.getline(streetAddress_2, STREET_SIZE);
}while(!cust.setStreetAddress2(streetAddress_2));
do{
cout<<"Input city: ";
cin.getline(city, CITY_SIZE);
}while(!cust.setCity(city));
do{
cout<<"Input state code: ";
cin.getline(state, STATE_CODE_SIZE);
}while(!cust.setState(state));
do{
cout<<"Input zip: ";
cin>>zipCode;
}while(!cust.setZipCode(zipCode));
// display customer details
cout<<endl<<"Details of Customer: "<<endl;
cout<<"Customer Number:
"<<cust.getCustomerNumber()<<endl;
cout<<"Name: "<<cust.getName()<<endl;
cout<<"Street Address 1:
"<<cust.getStreetAddress1()<<endl;
cout<<"Street Address 2:
"<<cust.getStreetAddress2()<<endl;
cout<<"City: "<<cust.getCity()<<endl;
cout<<"State: "<<cust.getState()<<endl;
cout<<"Zip code: "<<cust.getZipCode()<<endl;
return 0;
}
//end of program
Output: