Description
The purpose of this challenge is to define a basic class with private and public data members and functions. This challenge simulates a Credit Card account.
Requirements
- Define a class called CreditCard as below
class CreditCard { private: string name; string card_no; int expire_month; int expire_year; double limit; double balance; public: }; - Create a default constructor for this class.
- Create an overloaded constructor to pass in values for name, card_no, expire_month and expire_year. Use the values passed in the parameters to initialize the appropriate private data members.
- In the overloaded constructor, set limit to 2000 and balance to 0 (zero).
- Create a private getter function double available_credit(). This simply returns the difference between the limit and the balance.
- Create a public getter function string get_card_type(). This returns VISA if the first digit of the card number is a ‘4’, MASTERCARD if ‘5’, AMEX if ‘3’ and OTHER for any other digit.
- Create a public getter function string get_info(). This function returns a string that looks as below (using the appropriate information, of course). You can use the to_string() function to convert int or double to a string represenation of a numeric value. For example, to_string(100) returns “100”, to_string(1.99) returns “1.99”. You will have to concatenate various strings to create a string in the format below. You may end up with lots of extra decimals using to_string() on a double — don’t worry about it, you may leave it as is.
VISA card expires on 12/2025, available credit $1000.0000
- Create a function bool pre_authorize(int exp_month, int exp_year, double charge_amt). This function will return true if exp_month and exp_year matches the stored private data, and that charge_amt is less than the available credit. This function will return false otherwise.
- Create a function bool charge(int exp_month, int exp_year, double charge_amt). This function will call pre_authorize() passing in all the appropriate parameters. If the call to pre_authorize() returns true then add the charge_amt to balance, and return true. Otherwise, return false.
- Make sure that your class does not contain any cout or cin code.
- Use the following main() function to test your class.
Sample main()
int main()
{
CreditCard card("MICKEY MOUSE", "4128002072554673", 12, 2025);
char input;
double amount;
int expmonth, expyear;
do
{
cout << "c - Check if a charge will go through" << endl;
cout << "m - Make a purchase" << endl;
cout << "s - Show card info" << endl;
cout << "q - Quit" << endl;
cout << "Command: " << endl;
cin >> input;
switch (input)
{
case 'c': cout << "Pre-authorize how much? ";
cin >> amount;
if (card.pre_authorize(12, 2025, amount))
cout << "Yes, that will go through" << endl;
else
cout << "Pre-auth failed" << endl;
break;
case 'm': cout << "Enter expire month: ";
cin >> expmonth;
cout << "Enter expire year: ";
cin >> expyear;
cout << "Enter charge amount: ";
cin >> amount;
if (card.charge(expmonth, expyear, amount))
cout << "Approved" << endl;
else
cout << "Declined" << endl;
break;
case 's': cout << card.get_info() << endl;
break;
}
} while (input != 'q');
return 0;
}
Sample Interaction
c - Check if a charge will go through m - Make a purchase s - Show card info q - Quit Command: c Pre-authorize how much? 3000 Pre-auth failed c - Check if a charge will go through m - Make a purchase s - Show card info q - Quit Command: c Pre-authorize how much? 100 Yes, that will go through c - Check if a charge will go through m - Make a purchase s - Show card info q - Quit Command: m Enter expire month: 1 Enter expire year: 2011 Enter charge amount: 200 Denied c - Check if a charge will go through m - Make a purchase s - Show card info q - Quit Command: m Enter expire month: 12 Enter expire year: 2025 Enter charge amount: 1200 Approved c - Check if a charge will go through m - Make a purchase s - Show card info q - Quit Command: s VISA Card expires on 12/2025, available balance $800.00 c - Check if a charge will go through m - Make a purchase s - Show card info q - Quit Command: m Enter expire month: 12 Enter expire year: 2025 Enter charge amount: 200 Approved c - Check if a charge will go through m - Make a purchase s - Show card info q - Quit Command: s VISA Card expires on 12/2025, available balance $600.00 c - Check if a charge will go through m - Make a purchase s - Show card info q - Quit Command: q
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL0061
Print Requirements
