-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibraryUser.cpp
125 lines (97 loc) · 2.81 KB
/
LibraryUser.cpp
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
#include "LibraryUser.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using std::setw;
LibraryUser::LibraryUser() {
}
const string &LibraryUser::getFirstName() const {
return firstName;
}
const string &LibraryUser::getLastName() const {
return lastName;
}
int LibraryUser::getBDay() const {
return day;
}
int LibraryUser::getBMonth() const {
return month;
}
int LibraryUser::getBYear() const {
return year;
}
void LibraryUser::registerUser() {
cout << "Enter first name (only latin letters)" << endl;
cin >> firstName;
checkUserName(firstName);
cout << "Enter last name (only latin letters)" << endl;
cin >> lastName;
checkUserName(lastName);
cout << "Please enter birth date (format: dd-mm-yyyy): ";
char separator;
cin >> day >> separator >> month >> separator >> year;
checkBirthDate();
}
void LibraryUser::checkBirthDate() {
char separator;
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
time_t tt = std::chrono::system_clock::to_time_t(now);
tm local_tm = *localtime(&tt);
int currentYear = local_tm.tm_year + 1900;
while (month < 0 || month > 12 || day < 1 || day > 31 || year < 1920 || year > currentYear) {
cout << "Invalid birth date. Enter the date again" << endl;
cout << "Format: dd-mm-yyyy." << endl;
cin >> day >> separator >> month >> separator >> year;
}
}
void LibraryUser::checkUserName(string name) {
auto is_invalid = [](unsigned char ch) {
return !(isspace(ch) || isalpha(ch));
};
if (is_invalid) {
while (any_of(name.begin(), name.end(), is_invalid)) {
cout << "Invalid character in string." << endl;
cout << "Please input only alphabets or space character." << endl;
cin >> name;
}
}
}
int LibraryUser::getId() const {
return id;
}
void LibraryUser::setId(int idUser) {
id = idUser;
}
void LibraryUser::setFirstName(const string &firstName) {
LibraryUser::firstName = firstName;
}
void LibraryUser::setLastName(const string &lastName) {
LibraryUser::lastName = lastName;
}
void LibraryUser::setDay(int day) {
LibraryUser::day = day;
}
void LibraryUser::setMonth(int month) {
LibraryUser::month = month;
}
void LibraryUser::setYear(int year) {
LibraryUser::year = year;
}
bool LibraryUser::readTxt(ifstream & is) {
char separator;
is >> id;
is >> firstName;
is >> lastName;
is >> day >> separator >> month >> separator >> year;
if (is.fail()) {
return false;
}
return true;
}
void LibraryUser::writeTxt(ofstream & os) const {
os << setw(4) << getId();
os << setw(20) << getFirstName();
os << setw(20) << getLastName();
os << setw(4) << getBDay() << "-" << getBMonth() << "-" << getBYear();
os << endl;
}