-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationData.cpp
More file actions
150 lines (138 loc) · 5.26 KB
/
ValidationData.cpp
File metadata and controls
150 lines (138 loc) · 5.26 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "ValidationData.hpp"
#include <limits> //for numeric_limits
#include <ios> // for std::streamsize
#include <cctype> // tolover
#include <iostream>
bool ValidationData::isCorrectMenuChoice(const size_t choosenValueToValid, const size_t sizeMenu) {
if (std::cin.fail() || choosenValueToValid < 1 || choosenValueToValid > sizeMenu) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Incorrect value, please put number betwen 1 and " << sizeMenu << '\n';
return false;
}
return true;
}
bool ValidationData::validationIsAlpabet(std::string stringToCheck) {
auto result = std::all_of(stringToCheck.begin(), stringToCheck.end(), [](auto character) {
return std::isalpha(character);
});
if (result) {
return true;
}
std::cout << "Insert data can be only alphabet\n";
return false;
}
bool ValidationData::validationIsDigit(std::string stringToCheck) {
auto result = std::all_of(stringToCheck.begin(), stringToCheck.end(), [](auto character) {
return std::isdigit(character);
});
if (result) {
return true;
}
std::cout << "Insert data can be only digit\n";
return false;
}
bool ValidationData::validationStringSize(std::string userStringData, uint8_t maxSize, bool isEqual) {
if (isEqual == false && userStringData.size() <= maxSize) {
return true;
}
if (isEqual == true && userStringData.size() == maxSize) {
return true;
}
if (isEqual == true) {
std::cout << "Wrong length, value must have " << +maxSize << " characters\n";
}
std::cout << "Length is to long, maximum length is " << +maxSize << '\n';
return false;
}
bool ValidationData::peselValidDOBCheck(std::string dayOfBirth) {
if ((dayOfBirth[2] == '1' || dayOfBirth[2] == '3' || dayOfBirth[2] == '5' || dayOfBirth[2] == '7' || dayOfBirth[2] == '9') && dayOfBirth[2] != '0' && dayOfBirth[2] != '1' && dayOfBirth[2] != '2') {
return false;
}
if (dayOfBirth[4] != '0' && dayOfBirth[4] != '1' && dayOfBirth[4] != '2' && dayOfBirth[4] != '3') {
return false;
}
if (dayOfBirth[4] == '3' && dayOfBirth[5] != '0' && dayOfBirth[5] != '1' && dayOfBirth[5] != '2') {
return false;
}
return true;
}
bool ValidationData::peselValidGenderCheck(char charGender, std::string peselNumber) {
int genderNumber = (peselNumber[9] - '0') % 2;
if (std::tolower(charGender) == 'f' && genderNumber == 0) {
return true;
}
if (std::tolower(charGender) == 'm' && genderNumber == 1) {
return true;
}
return false;
}
bool ValidationData::peselValidWithCurrentlyDate(std::string peselNumber) {
if (CURRENTLY_DATE[5] == '0' && (peselNumber[2] == '4' || peselNumber[2] == '5' || peselNumber[2] == '6' || peselNumber[2] == '7')) {
return false;
}
if (CURRENTLY_DATE[5] == '1' && (peselNumber[2] == '6' || peselNumber[2] == '7')) {
return false;
}
return true;
}
bool ValidationData::peselValidWithWeight(std::string peselNumber) {
std::string weightCheck = "13791379131";
size_t weightResult = 0;
for (int i = 0; i < 10; i++) {
weightResult = weightResult + (static_cast<size_t>(peselNumber[i] - '0') * (static_cast<size_t>(weightCheck[i] - '0')));
}
weightResult = weightResult % 10;
if (weightResult == 0) {
weightResult = 0;
} else {
weightResult = 10 - weightResult;
}
if (static_cast<size_t>(peselNumber[10] - '0') != weightResult) {
return false;
}
return true;
}
bool ValidationData::validatingPeselNumber(std::string peselNumber) {
bool corectPeselNumber = true;
corectPeselNumber = peselValidDOBCheck(peselNumber);
corectPeselNumber = peselValidWithCurrentlyDate(peselNumber);
corectPeselNumber = peselValidWithWeight(peselNumber);
if (!corectPeselNumber) {
std::cout << "Pesel number is incorrect\n";
}
return corectPeselNumber;
}
bool ValidationData::validatingGender(char userChoice) {
if (std::tolower(userChoice) != 'f' && std::tolower(userChoice) != 'm') {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Wrong value, please choose f or m\n";
return false;
}
return true;
}
ValidationData::YesNoConfirm ValidationData::confirmDataYesNo(std::string question) {
std::string answer { };
YesNoConfirm confirmation { YesNoConfirm::NoConfirm };
std::cout << question << '\n';
std::cout << "Yes(y)\n";
std::cout << "No(n)\n";
std::cout << "Back(b)\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while (confirmation == YesNoConfirm::NoConfirm || confirmation == YesNoConfirm::Error) {
std::cin.clear();
std::getline(std::cin, answer);
if (answer == "Y" || answer == "y") {
confirmation = YesNoConfirm::Yes;
} else if (answer == "N" || answer == "n") {
confirmation = YesNoConfirm::No;
} else if (answer == "B" || answer == "b") {
confirmation = YesNoConfirm::Back;
} else {
std::cout << "Wrong answer, you must choose Y, N or B\n";
confirmation = YesNoConfirm::Error;
}
}
return confirmation;
}