-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAluno.js
200 lines (151 loc) · 5.53 KB
/
Aluno.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import ModelError from "/ModelError.js";
export default class Aluno {
//
// ATRIBUTOS PRIVADOS
//
#matricula;
#cpf;
#nome;
#email;
#telefone;
//-----------------------------------------------------------------------------------------//
constructor(matr, cpf, nome, email, telefone) {
this.setMatricula(matr);
this.setCpf(cpf);
this.setNome(nome);
this.setEmail(email);
this.setTelefone(telefone);
}
//-----------------------------------------------------------------------------------------//
static assign(obj) {
return new Aluno(obj.matricula, obj.cpf, obj.nome, obj.email, obj.telefone);
}
//-----------------------------------------------------------------------------------------//
getMatricula() {
return this.#matricula;
}
//-----------------------------------------------------------------------------------------//
setMatricula(matr) {
if(!Aluno.validarMatricula(matr))
throw new ModelError("Matrícula Inválida: " + matr);
this.#matricula = matr;
}
//-----------------------------------------------------------------------------------------//
getCpf() {
return this.#cpf;
}
//-----------------------------------------------------------------------------------------//
setCpf(cpf) {
if(!Aluno.validarCpf(cpf))
throw new ModelError("CPF Inválido: " + cpf);
this.#cpf = cpf;
}
//-----------------------------------------------------------------------------------------//
getNome() {
return this.#nome;
}
//-----------------------------------------------------------------------------------------//
setNome(nome) {
if(!Aluno.validarNome(nome))
throw new ModelError("Nome Inválido: " + nome);
this.#nome = nome;
}
//-----------------------------------------------------------------------------------------//
getEmail() {
return this.#email;
}
//-----------------------------------------------------------------------------------------//
setEmail(email) {
if(!Aluno.validarEmail(email))
throw new ModelError("Email inválido: " + email);
this.#email = email;
}
//-----------------------------------------------------------------------------------------//
getTelefone() {
return this.#telefone;
}
//-----------------------------------------------------------------------------------------//
setTelefone(telefone) {
if(!Aluno.validarTelefone(telefone))
throw new ModelError("Telefone inválido: " + telefone);
this.#telefone = telefone;
}
//-----------------------------------------------------------------------------------------//
toJSON() {
return '{' +
'"matricula" : "'+ this.#matricula + '",' +
'"cpf" : "' + this.#cpf + '", ' +
'"nome" : "' + this.#nome + '", ' +
'"email" : "' + this.#email + '", ' +
'"telefone" : "' + this.#telefone + '", ' +
'}';
}
//-----------------------------------------------------------------------------------------//
static validarMatricula(matr) {
if(matr == null || matr == "" || matr == undefined)
return false;
const padraoMatricula = /[0-9]/;
if (!padraoMatricula.test(matr))
return false;
return true;
}
//-----------------------------------------------------------------------------------------//
static validarCpf(strCpf) {
let soma;
let resto;
let i;
soma = 0;
strCpf = strCpf.replace(".", "");
strCpf = strCpf.replace(".", "");
strCpf = strCpf.replace("-", "");
if (strCpf == "00000000000") return false;
for (i = 1; i <= 9; i++)
soma = soma + parseInt(strCpf.substring(i - 1, i)) * (11 - i);
resto = (soma * 10) % 11;
if (resto == 10 || resto == 11) resto = 0;
if (resto != parseInt(strCpf.substring(9, 10))) return false;
soma = 0;
for (i = 1; i <= 10; i++)
soma = soma + parseInt(strCpf.substring(i - 1, i)) * (12 - i);
resto = (soma * 10) % 11;
if (resto == 10 || resto == 11) resto = 0;
if (resto != parseInt(strCpf.substring(10, 11))) return false;
return true;
}
//-----------------------------------------------------------------------------------------//
static validarNome(nome) {
if(nome == null || nome == "" || nome == undefined)
return false;
if (nome.length > 40)
return false;
const padraoNome = /[A-Z][a-z] */;
if (!padraoNome.test(nome))
return false;
return true;
}
//-----------------------------------------------------------------------------------------//
static validarEmail(email) {
if(email == null || email == "" || email == undefined)
return false;
const padraoEmail = /[a-zA-Z0-9._%-]+@[a-zA-Z0-9-]+.[a-zA-Z]{2,4}/;
if (!padraoEmail.test(email))
return false;
return true;
}
//-----------------------------------------------------------------------------------------//
static validarTelefone(telefone) {
if(telefone == null || telefone == "" || telefone == undefined)
return false;
const padraoTelefone = /^(?:(?:\+|00)?(55)\s?)?(?:\(?([1-9][0-9])\)?\s?)?(?:((?:9\d|[2-9])\d{3})\-?(\d{4}))$/;
if (!padraoTelefone.test(telefone))
return false;
return true;
}
//-----------------------------------------------------------------------------------------//
mostrar() {
let texto = "Matrícula: " + this.matricula + "\n";
texto += "Nome: " + this.nome + "\n";
alert(texto);
alert(JSON.stringify(this));
}
}