forked from netology-code/js-homeworks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
249 lines (211 loc) · 6.78 KB
/
main.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
'use strict'
class Calendar {
constructor(now = new Date()) {
this.now = now;
}
setDate(now) {
this.now = now;
}
get today() {
return this.now.toLocaleString('ru-Ru');
}
}
class PaymentTerminal {
constructor(title, calendar) {
this.title = title;
this.calendar = calendar;
}
get status() {
return this.isActive ? 'работает' : 'недоступен';
}
get isActive() {
return this.checkActive();
}
checkActive() {
return false;
}
}
class RegistrationError extends Error {
constructor(field = null) {
super(`Ошибка в поле ${field}`);
this.field = field;
}
}
class NotValidEmailRegistrationError extends RegistrationError {
constructor(field, email) {
super(field);
this.email = email;
}
}
class NotUniqueRegistrationError extends RegistrationError {
constructor(field, value) {
super(field);
this.value = value;
}
}
class NotSameRegistrationError extends RegistrationError {}
function isValidEmail(email) {
return /^\w+(\.\w+)*@\w+(\.\w+)+$/i.test(email);
}
function isUniqueLogin(login) {
return !['admin', 'boss'].includes(login);
}
function checkPassword(original, copy) {
return original === copy;
}
function registerNewUser(data) {
if (!isValidEmail(data.email)) {
throw new NotValidEmailRegistrationError('Адрес электронной почты', data.email);
}
if (!isUniqueLogin(data.login)) {
throw new NotUniqueRegistrationError('Логин', data.login);
}
if (!checkPassword(data.password, data.passwordCopy)) {
throw new NotSameRegistrationError('Пароль');
}
}
console.log('Задание 1')
class SpaceDate extends Date {
copy() {
return new SpaceDate(this);
}
getNextDate() {
let nextDate = new SpaceDate(this.copy());
nextDate.setDate(this.copy().getDate() + 1);
return nextDate;
}
getPrevDate() {
let prevDate = new SpaceDate(this.copy());
prevDate.setDate(this.copy().getDate() - 1);
return prevDate;
}
getDayBeginning() {
let dayBeginning = new SpaceDate(this.copy());
dayBeginning.setHours(0, 0, 0, 0);
return dayBeginning;
}
getDayEnd() {
let dayEnd = new SpaceDate(this.copy());
dayEnd.setHours(23, 59, 59, 999);
return dayEnd;
}
}
let dateOriginal = new SpaceDate(2017, 1, 22);
let dateCopy = dateOriginal.copy();
dateCopy.setYear(2022);
console.log(`Оригинальная дата: ${dateOriginal.toLocaleDateString('ru-Ru')}`);
console.log(`Измененная копия: ${dateCopy.toLocaleDateString('ru-Ru')}`);
let orderDate = new SpaceDate(2017, 2, 10);
let deliveryDate = orderDate.getNextDate();
console.log(`Дата заказа: ${orderDate.toLocaleDateString('ru-Ru')}`);
console.log(`Дата доставки: ${deliveryDate.toLocaleDateString('ru-Ru')}`);
let supplyDate = new SpaceDate(2017, 3, 3);
let requestDate = supplyDate.getPrevDate();
console.log(`Дата поставки: ${supplyDate.toLocaleDateString('ru-Ru')}`);
console.log(`Дата заявки поставщику: ${requestDate.toLocaleDateString('ru-Ru')}`);
let someDate = new SpaceDate(2017, 2, 10, 12, 44);
let from = someDate.getDayBeginning();
let to = someDate.getDayEnd();
console.log(`В любое время с ${from.toLocaleString('ru-Ru')} по ${to.toLocaleString('ru-Ru')}`);
console.log('Задание 2')
class AllDayPaymentTerminal extends PaymentTerminal {
checkActive() {
return true;
}
}
class WorkspacePaymentTerminal extends PaymentTerminal {
checkActive() {
let day = this.calendar.now;
if (day.getDay() > 0 && day.getDay() < 6 && day.getHours() >= 8 && day.getHours() < 18) {
return true
} else {
return false
}
}
}
class AllDayExceptHolidaysPaymentTerminal extends PaymentTerminal {
constructor(title, calendar, holidays) {
super(title, calendar);
this.holidays = holidays;
}
checkActive() {
let day = this.calendar.now;
for (let holiday of holidays) {
if (holiday.date === day.getDate() && holiday.month === day.getMonth()) {
return false
} else {
return true
}
}
}
}
const holidays = [{
date: 11,
month: 3 - 1
},
{
date: 23,
month: 2 - 1
}
];
const calendar = new Calendar();
const terminals = [
new WorkspacePaymentTerminal('Терминал в офисе Убербанка', calendar),
new AllDayPaymentTerminal('Терминал в аэропорту', calendar),
new AllDayExceptHolidaysPaymentTerminal('Терминал в торговом центре',
calendar, holidays)
];
function showTerminals(date) {
if (date !== undefined) {
calendar.setDate(date);
}
console.log(calendar.today);
terminals
.filter(terminal => terminal instanceof PaymentTerminal)
.forEach(terminal => console.log(`${terminal.title} ${terminal.status}`));
}
showTerminals(new Date(2017, 2 - 1, 23));
showTerminals(new Date(2017, 3 - 1, 11));
showTerminals(new Date(2017, 3 - 1, 14, 18, 1));
showTerminals(new Date(2017, 3 - 1, 14, 8, 3));
console.log('Задание 3')
function handleRegistration(data) {
try {
if (!(registerNewUser(data))) {
console.log("Пользователь успешно зарегистрирован")
}
} catch (err) {
if (NotValidEmailRegistrationError.prototype.isPrototypeOf(err)) {
console.log(`«${err.email}» не является адресом электронной почты`)
}
if (NotUniqueRegistrationError.prototype.isPrototypeOf(err)) {
console.log(`Пользователь с логином «${err.value}» уже зарегистрирован`)
}
if (NotSameRegistrationError.prototype.isPrototypeOf(err)) {
console.log(`Введенные пароли не совпадают`)
}
}
}
const notValidEmailUser = {
email: 'test'
};
handleRegistration(notValidEmailUser);
const notUniqueLoginUser = {
email: '[email protected]',
login: 'boss'
};
handleRegistration(notUniqueLoginUser);
const differentPwUser = {
email: '[email protected]',
login: 'ivan',
password: '123',
passwordCopy: '456'
};
handleRegistration(differentPwUser);
const normalUser = {
email: '[email protected]',
login: 'ivan',
password: '123',
passwordCopy: '123'
};
handleRegistration(normalUser);