-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreditCardValidator.cpp
More file actions
56 lines (50 loc) · 1.44 KB
/
creditCardValidator.cpp
File metadata and controls
56 lines (50 loc) · 1.44 KB
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
#include "creditCardValidator.hpp"
bool CreditCard::checkCardNumber() {
long cardNumCopy = intCardNumber_;
int num = 0;
while(cardNumCopy > 0) {
cardNumCopy /= 10;
num++;
}
if(num != 16) {
return false;
}
std::vector<int> vecCardNumber;
vecCardNumber.reserve(16);
while(intCardNumber_ > 0) {
vecCardNumber.push_back(intCardNumber_ % 10);
intCardNumber_ /= 10;
}
long sum = 0;
for(int i = 0; i < 15; ++i) {
if(i % 2 == 0) {
num = vecCardNumber[i];
}
if(i % 2 != 0) {
num = vecCardNumber[i] * 2;
}
if(num > 9) {
num -= 9;
}
sum += num;
}
sum += vecCardNumber[15];
if(sum % 10 != 0) {
return false;
}
return true;
}
bool CreditCard::checkCVV() {
std::regex cvvpattern("^[0-9]{3,4}$");
return (std::regex_match(CVV_, cvvpattern));
}
bool CreditCard::checkExpDate() {
std::regex datepattern("^(0[1-9]|1[0-2])/([0-9][0-9])$");
std::time_t now = std::time(nullptr);
auto tm = *std::localtime(&now);
std::ostringstream oss;
oss << std::put_time(&tm, "%m/%y");
std::string today = oss.str();
int todm = today[0] * 10 + today[1], expm = expDate_[0] * 10 + expDate_[1], tody = today[3] * 10 + today[4], expy = expDate_[3] * 10 + expDate_[4];
return (std::regex_match(expDate_, datepattern) and (todm <= expm and tody <= expy));
}