-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathmain.js
More file actions
82 lines (72 loc) · 2.06 KB
/
main.js
File metadata and controls
82 lines (72 loc) · 2.06 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
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
class MailSystem {
write(name) {
console.log('--write mail for ' + name + '--');
const context = 'Congrats, ' + name + '!';
return context;
}
send(name, context) {
console.log('--send mail to ' + name + '--');
// Interact with mail system and send mail
// random success or failure
const success = Math.random() > 0.5;
if (success) {
console.log('mail sent');
} else {
console.log('mail failed');
}
return success;
}
}
class Application {
constructor() {
this.people = [];
this.selected = [];
this.mailSystem = new MailSystem();
this.getNames().then(([people, selected]) => {
this.people = people;
this.selected = selected;
});
}
async getNames() {
const data = await readFile('name_list.txt', 'utf8');
const people = data.split('\n');
const selected = [];
return [people, selected];
}
getRandomPerson() {
const i = Math.floor(Math.random() * this.people.length);
return this.people[i];
}
selectNextPerson() {
console.log('--select next person--');
if (this.people.length === this.selected.length) {
console.log('all selected');
return null;
}
let person = this.getRandomPerson();
while (this.selected.includes(person)) {
person = this.getRandomPerson();
}
this.selected.push(person);
return person;
}
notifySelected() {
console.log('--notify selected--');
for (const x of this.selected) {
const context = this.mailSystem.write(x);
this.mailSystem.send(x, context);
}
}
}
// const app = new Application();
// app.selectNextPerson();
// app.selectNextPerson();
// app.selectNextPerson();
// app.notifySelected();
module.exports = {
Application,
MailSystem,
};