1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <iostream>
#include <string>
class CreditCard {
std::string name;
std::string card_no;
int expire_month {};
int expire_year {};
double charge_amt {};
double limit {2000};
double balance {};
public:
CreditCard() {}
CreditCard(const std::string& n, const std::string& cn, int em, int ey) : name(n), card_no(cn), expire_month(em), expire_year(ey) {}
std::string get_card_type() const {
if (card_no[0] == '4')
return "VISA";
if (card_no[0] == '5')
return "Mastercard";
return "Unknown";
}
void get_info(std::string& em, std::string& ey, std::string& bal) const {
em = std::to_string(expire_month);
ey = std::to_string(expire_year);
bal = std::to_string(balance);
}
bool pre_authorize(int exp_month, int exp_year, double charge_amt) {
if (exp_month)
expire_month = 12;
if (exp_year)
expire_year = 2025;
return true;
}
bool charge(int exp_month, int exp_year, double charge_amt) {
if (!pre_authorize(exp_month, exp_year, charge_amt))
return false;
balance += charge_amt;
return true;
}
};
int main()
{
CreditCard card("MICKEY MOUSE", "4128002072554673", 12, 2025);
char input {};
do {
std::cout << "c - Check if a charge will go through\n";
std::cout << "m - Make a purchase\n";
std::cout << "s - Show card info\n";
std::cout << "q - Quit\n";
std::cout << "\nCommand: ";
std::cin >> input;
switch (input) {
case 'c':
{
double amount {};
std::cout << "Pre-authorize how much? ";
std::cin >> amount;
if (card.pre_authorize(12, 2025, amount))
std::cout << "Yes, that will go through\n";
else
std::cout << "Pre-auth failed\n";
}
break;
case 'm':
{
int expmonth {}, expyear {};
double amount {};
std::cout << "Enter expire month: ";
std::cin >> expmonth;
std::cout << "Enter expire year: ";
std::cin >> expyear;
std::cout << "Enter charge amount: ";
std::cin >> amount;
if (card.charge(expmonth, expyear, amount))
std::cout << "Approved\n";
else
std::cout << "Declined";
}
break;
case 's':
{
std::string em, ey, bal;
card.get_info(em, ey, bal);
std::cout << card.get_card_type() << " " << em << " " << ey << " " << bal << '\n';
}
break;
}
} while (input != 'q');
}
|