-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.cpp
More file actions
75 lines (65 loc) · 2.32 KB
/
User.cpp
File metadata and controls
75 lines (65 loc) · 2.32 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
//
// Created by matin on 12/30/25.
//
#include "User.h"
#include "LocalUser.h"
#include <sstream>
#include <iostream>
User::User(int id, const string& user, const string& name, const string& pass,const Date& birth, const Date& join): userId(id), username(user), fullName(name), password(pass),birthdate(birth), joinDate(join) {}
string User::getBasicInfo() const {
return "ID: " + to_string(userId) + ", Username: " + username + ", Name: " + fullName;
}
void User::validateFullName(const string& name) {
if (name.length() < 3) {
throw SignUpException("Full name must be at least 3 characters long");
}
for (char c : name) {
if (!isalpha(c) && c != ' ') {
throw SignUpException("Full name can only contain letters and spaces");
}
}
}
void User::validateUsername(const string& name) {
if (name.length() < 3) {
throw SignUpException("Username must be at least 3 characters long");
}
for (char c : name) {
if (!isalnum(c) && c != '_') {
throw SignUpException("Username can only contain letters, numbers, and underscores");
}
}
}
void User::validatePassword(const string& pass) {
if (pass.length() < 8) {
throw SignUpException("Password must be at least 8 characters long");
}
}
void User::validateBirthdate(const string& date) {
if (date.length() != 10) {
throw SignUpException("Birthdate must be in format YYYY-MM-DD");
}
if (date[4] != '-' || date[7] != '-') {
throw SignUpException("Birthdate must be in format YYYY-MM-DD");
}
}
string User::serialize() const {
return to_string(userId) + "|" + username + "|" + fullName + "|" +birthdate.toString() + "|" + password + "|" + joinDate.toString();
}
User* User::deserialize(const string& line) {
istringstream iss(line);
string token;
vector<string> tokens;
while (getline(iss, token, '|')) {
tokens.push_back(token);
}
if (tokens.size() < 6) {
throw FileIOException("Invalid user data format in file");
}
int id = stoi(tokens[0]);
string username = tokens[1];
string fullName = tokens[2];
Date birthdate = Date::fromString(tokens[3]);
string password = tokens[4];
Date joinDate = Date::fromString(tokens[5]);
return new LocalUser(id, username, fullName, password, birthdate, joinDate);
}