2 Modify the write function of CreditCard by adding two Bool
2- Modify the write function of CreditCard by adding two Boolean arguments; displayName and displayCardInfo.
Using the two private write functions written in part 1 and default value for arguments re-implement the write function to work as follows:
write() – will provide the same output as before
write(false) – will only output the card information
write(true, false) – will only display the name
write(false, false) – will not output anything
The main program that uses your new implementation contains the following code
main:
#include <iostream>
using namespace std;
#include \"CreditCard.h\"
using namespace sict;
void writeAll(const CreditCard& );
int main() {
CreditCard myCC;
char name[41];
int instCode;
int expiryYear;
int expiryMonth;
unsigned long long cardNumber;
int backNumber;
char slash;
cout << \"Credit Card app\" << endl <<
\"===================\" << endl << endl;
cout << \"Please enter your name: \";
cin >> name;
do {
cout <<
\"Please enter your credit card number, institution code, \" <<
\"expiry date, and security number as follows: \" <<
\"4000123412341234 999 12/1234 999\" << endl << \"> \";
cin >> cardNumber >> instCode >> expiryMonth >> slash >> expiryYear >>
backNumber;
cout << endl;
myCC.name(name);
myCC.initialize(cardNumber, instCode, expiryYear, expiryMonth, backNumber);
} while (!myCC.isValid() && cout << \"Invalid input\" << endl);
cout << endl << \"Thank you!\" << endl;
writeAll(myCC);
return 0;
}
void writeAll(const CreditCard& card)
{
card.write();
cout << endl << \"-------------\" << endl;
card.write(false);
cout << endl << \"-------------\" << endl;
card.write(true, false);
cout << endl << \"-------------\" << endl;
card.write(false, false);
}
a) CreditCard.cpp :
#include \"CreditCard.h\"
#include <cstring>
#include <iostream>
namespace sict {
using namespace std;
CreditCard *cc = new CreditCard;
void CreditCard::initialize(unsigned long long creditCardNumber, int instCode,
int expiryYear, int expiryMonth,
int numberInTheBack) {
m_cardNumber = creditCardNumber;
m_institutionCode = instCode;
m_expiryYear = expiryYear;
m_expiryMonth = expiryMonth;
m_numberInTheBack = numberInTheBack;
m_numberInTheBack = numberInTheBack;
}
void CreditCard::name(const char cardHolderName[]) {
strcpy(m_cardHolderName, cardHolderName);
}
void CreditCard::write() {
cout << \"Cardholder:\" << m_cardHolderName << endl;
cout << \"Card Number:\" << m_cardNumber << endl;
cout << \"Institution:\" << m_institutionCode << endl;
cout << \"Expires:\" << m_expiryMonth << \"/\" << m_expiryYear << endl;
cout << \"Number at the back:\" << m_numberInTheBack << endl;
}
bool CreditCard::isValid() {
bool nameCheck = false, numCheck = false, instCheck = false,
yearCheck = false, monthCheck = false, backNumCheck = false,
finalCheck = false;
if (strlen(m_cardHolderName)) {
nameCheck = true;
}
if (m_cardNumber <= MAX_CARD_NUMBER && m_cardNumber >= MIN_CARD_NUMBER) {
numCheck = true;
}
if (m_institutionCode <= MAX_INST_NUMBER &&
m_institutionCode >= MIN_INST_NUMBER) {
instCheck = true;
}
if (m_expiryYear <= MAX_EXP_YEAR && m_expiryYear >= MIN_EXP_YEAR) {
yearCheck = true;
}
if (m_expiryMonth <= 12 && m_expiryMonth >= 1) {
monthCheck = true;
}
if (m_numberInTheBack % 100 < 100) {
backNumCheck = true;
}
if (nameCheck && numCheck && instCheck && yearCheck && monthCheck &&
backNumCheck) {
finalCheck = true;
} else {
finalCheck = false;
}
return finalCheck;
}
}
b) CreditCard.h :
#include <iostream>
#ifndef CREDITCARD_H
#define CREDITCARD_H
#define MAX_NAME_LENGTH 40
#define MIN_INST_NUMBER 100
#define MAX_INST_NUMBER 999
#define MIN_EXP_YEAR 2017
#define MAX_EXP_YEAR 2037
#define MIN_CARD_NUMBER 4000000000000000
#define MAX_CARD_NUMBER 4000999999999999
namespace sict {
class CreditCard {
public:
void initialize(unsigned long long creditCardNumber, int instCode,
int expiryYear, int expiryMonth, int numberInTheBack);
void name(const char cardHolderName[]);
void write();
bool isValid();
private:
char m_cardHolderName[MAX_NAME_LENGTH];
unsigned long long m_cardNumber;
int m_institutionCode;
int m_expiryYear;
int m_expiryMonth;
int m_numberInTheBack;
};
}
#endif
output:
Credit Card app
===================
Please enter your name: John
Please enter your credit card number, institution code, expiry date, and security number as follows: 4000123412341234 999 12/1234 999
> 1111222233334444 301 12/2020 505
Invalid input
Please enter your credit card number, institution code, expiry date, and security number as follows: 4000123412341234 999 12/1234 999
> 4000111122223333 301 12/2020 505
Thank you!
Cardholder: John
Card Number: 4000111122223333
Institution: 301
Expires: 12/2020
Number at the back: 505
-------------
Card Number: 4000111122223333
Institution: 301
Expires: 12/2020
Number at the back: 505
-------------
Cardholder: John
-------------
Solution
The approach used to solve this problem is method overloading. We will be overloading write() method of Credit Card class.
It will require changes in CreditCard.h for method declaration and CreditCard.cpp for method implementation.
1. CreditCard.h
#include <iostream>
#ifndef CREDITCARD_H
#define CREDITCARD_H
#define MAX_NAME_LENGTH 40
#define MIN_INST_NUMBER 100
#define MAX_INST_NUMBER 999
#define MIN_EXP_YEAR 2017
#define MAX_EXP_YEAR 2037
#define MIN_CARD_NUMBER 4000000000000000
#define MAX_CARD_NUMBER 4000999999999999
namespace sict {
class CreditCard {
public:
void initialize(unsigned long long creditCardNumber, int instCode,
int expiryYear, int expiryMonth, int numberInTheBack);
void name(const char cardHolderName[]);
void write();
void write(bool displayName);
void write(bool displayName,bool displayCardInfo);
bool isValid();
private:
char m_cardHolderName[MAX_NAME_LENGTH];
unsigned long long m_cardNumber;
int m_institutionCode;
int m_expiryYear;
int m_expiryMonth;
int m_numberInTheBack;
};
}
2. CreditCard.cpp
#include \"CreditCard.h\"
#include <cstring>
#include <iostream>
namespace sict {
using namespace std;
CreditCard *cc = new CreditCard;
void CreditCard::initialize(unsigned long long creditCardNumber, int instCode,
int expiryYear, int expiryMonth,
int numberInTheBack) {
m_cardNumber = creditCardNumber;
m_institutionCode = instCode;
m_expiryYear = expiryYear;
m_expiryMonth = expiryMonth;
m_numberInTheBack = numberInTheBack;
m_numberInTheBack = numberInTheBack;
}
void CreditCard::name(const char cardHolderName[]) {
strcpy(m_cardHolderName, cardHolderName);
}
void CreditCard::write() {
cout << \"Cardholder:\" << m_cardHolderName << endl;
cout << \"Card Number:\" << m_cardNumber << endl;
cout << \"Institution:\" << m_institutionCode << endl;
cout << \"Expires:\" << m_expiryMonth << \"/\" << m_expiryYear << endl;
cout << \"Number at the back:\" << m_numberInTheBack << endl;
}
void CreditCard::write(bool displayName) {
if(displayName == false) {
cout << \"Card Number:\" << m_cardNumber << endl;
cout << \"Institution:\" << m_institutionCode << endl;
cout << \"Expires:\" << m_expiryMonth << \"/\" << m_expiryYear << endl;
cout << \"Number at the back:\" << m_numberInTheBack << endl;
}
}
void CreditCard::write(bool displayName,bool displayCardInfo) {
if(displayName == true && displayCardInfo == false) { // displaying only Card Holder Info
cout << \"Card Holder:\" << m_cardHolderName << endl;
}
else if(displayName == true && displayCardInfo == true) {
write(); // will display CardHolder and Card Info
}
else if(displayName == false && displayCardInfo == true) {
write(false); // will display only Card Info
}
else {
// DO NOTHING
}
}
bool CreditCard::isValid() {
bool nameCheck = false, numCheck = false, instCheck = false,
yearCheck = false, monthCheck = false, backNumCheck = false,
finalCheck = false;
if (strlen(m_cardHolderName)) {
nameCheck = true;
}
if (m_cardNumber <= MAX_CARD_NUMBER && m_cardNumber >= MIN_CARD_NUMBER) {
numCheck = true;
}
if (m_institutionCode <= MAX_INST_NUMBER &&
m_institutionCode >= MIN_INST_NUMBER) {
instCheck = true;
}
if (m_expiryYear <= MAX_EXP_YEAR && m_expiryYear >= MIN_EXP_YEAR) {
yearCheck = true;
}
if (m_expiryMonth <= 12 && m_expiryMonth >= 1) {
monthCheck = true;
}
if (m_numberInTheBack % 100 < 100) {
backNumCheck = true;
}
if (nameCheck && numCheck && instCheck && yearCheck && monthCheck &&
backNumCheck) {
finalCheck = true;
} else {
finalCheck = false;
}
return finalCheck;
}
}






