forked from devpeerapong/aqoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (77 loc) · 2.21 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
const fs = require("fs");
const Hotel = require("./model/Hotel");
class Command {
constructor(name, params) {
this.name = name;
this.params = params;
}
}
function main() {
const filename = "input.txt";
const commands = getCommandsFromFileName(filename);
let hotel;
commands.forEach((command) => {
switch (command.name) {
case "create_hotel":
const [floors, amountOfRoomPerFloor] = command.params;
hotel = new Hotel({ floors, amountOfRoomPerFloor });
// console.log(
// `Hotel created with ${floor} floor(s), ${roomPerFloor} room(s) per floor.`
// )
return;
case "book":
const [number, name, age] = command.params;
hotel.roomBook(number, name, age);
return;
case "list_available_rooms":
hotel.findAvailableRoom();
return;
case "checkout":
const [cardId, guessName] = command.params;
hotel.roomCheckOut(cardId, guessName);
return;
case "list_guest":
hotel.listGuest();
return;
case "get_guest_in_room":
const [roomNo] = command.params;
hotel.listGuestInRoom(roomNo);
return;
case "list_guest_by_age":
const [operator,ageRange] = command.params;
hotel.listGuestByAge(operator,ageRange);
return;
case "list_guest_by_floor":
const [fl] = command.params;
hotel.listGuestByFloor(fl);
return;
case "checkout_guest_by_floor":
const [checkInfl] = command.params;
hotel.checkoutGuestByFloor(checkInfl);
return;
case "book_by_floor":
const [bookFl,bookerName,bookerAge] = command.params;
hotel.bookByFloor(bookFl,bookerName,bookerAge);
return;
default:
return;
}
});
}
function getCommandsFromFileName(fileName) {
const file = fs.readFileSync(fileName, "utf-8");
return file
.split("\n")
.map((line) => line.split(" "))
.map(
([commandName, ...params]) =>
new Command(
commandName,
params.map((param) => {
const parsedParam = parseInt(param, 10);
return Number.isNaN(parsedParam) ? param : parsedParam;
})
)
);
}
main();