Question

In: Computer Science

1. Design a class based on a modified Customer Structure. Use private and public definitions. 2....

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.

Solutions

Expert Solution

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


Related Solutions

Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
in the typology of public-private-agreement, _____________ means that the customer is citizen and the public and...
in the typology of public-private-agreement, _____________ means that the customer is citizen and the public and private share the financial risk    A. COntracting B. Outsourcing C. Mixed Delivery D. Partnership
public class Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
Consider the following class definition:                   public class Parent {               private
Consider the following class definition:                   public class Parent {               private int varA;               protected double varB;               public Parent(int a, double b){ varA = a; varB = b;               }               public int sum( ){                    return varA + varB;               } public String toString( ){                    return "" + varA + "   " + varB;               }         } Consider that you want to extend Parent to Child. Child will have a third int instance data varC....
Consider the following class definition:                   public class Parent {               private
Consider the following class definition:                   public class Parent {               private int varA;               protected double varB;               public Parent(int a, double b){ varA = a; varB = b;               }               public int sum( ){                    return varA + varB;               } public String toString( ){                    return "" + varA + "   " + varB;               }         } Consider that you want to extend Parent to Child. Child will have a third int instance data varC....
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public int compareTo(MyString other) { /* code from HW1 here */ } public char charAt(int i) { return data[i]; } public int length() { return data.length; } public int indexOf(char c) { /* code from HW1 here */ } public boolean equals(MyString other) { /* code from HW1 here */ } /* * Change this MyString by removing all occurrences of * the argument character...
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
In C++ Design an Essay class that is derived from the GradedActivity class: class GradedActivity{ private:...
In C++ Design an Essay class that is derived from the GradedActivity class: class GradedActivity{ private: double score; public: GradedActivity() {score = 0.0;} GradedActivity(double s) {score = s;} void setScore(double s) {score = s;} double getScore() const {return score;} char getLetterGrade() const; }; char GradedActivity::getLetterGrade() const{ char letterGrade; if (score > 89) { letterGrade = 'A'; } else if (score > 79) { letterGrade = 'B'; } else if (score > 69) { letterGrade = 'C'; } else if (score...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT