-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
79 lines (69 loc) · 1.81 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
const BallClock = require('./lib/Clock');
const readline = require('readline');
function isValidBallsNumber(num) {
num = parseInt(num);
if (!Number.isInteger(num)) {
return false;
}
if (num < 27 || num > 127) {
return false;
}
return true;
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "waiting for input>"
});
const ballNumbers = [];
console.log("Welcome to Ball Clock app!");
console.log("In order to proceed, you must enter number of balls to setup clock");
console.log("Number must be between 27 and 127 inclusive");
console.log("0 - run clock app");
console.log("q - quit clock app");
rl.prompt();
rl.on('line', (line) => {
line = line.trim();
switch (line) {
case 'q':
exit();
break;
case '0':
if (!!ballNumbers.length) {
rl.close();
} else {
console.log("you must setup at least one clock in order to run app");
rl.prompt();
}
break;
default:
if (isValidBallsNumber(line)) {
ballNumbers.push(parseInt(line));
rl.prompt();
} else {
console.log("Wrong number");
rl.prompt();
}
break;
}
});
rl.on('close', () => {
run(ballNumbers);
exit();
});
rl.on('SIGINT', () => {
exit();
});
function run(ballNumbers) {
ballNumbers.forEach((number, idx) => {
const clock = new BallClock(number);
clock.start();
const days = clock.minutes / (24 * 60);
console.log(`=== clock ${idx +1} ===`);
console.log(`${number} balls cycle after ${days} days`);
})
}
function exit() {
console.log("Bye-Bye!");
process.exit(0);
}