-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkersGame.ts
129 lines (114 loc) · 3.7 KB
/
workersGame.ts
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
type Employee = {
workerId: string;
position: string;
compensation: number;
};
type EmployeeWorkhourRecord = {
workerId: string;
timestamp: number;
type: "in" | "out";
};
class WorkersGame {
workers: Map<string, Employee>;
employeeWorkhourRecords: Map<string, EmployeeWorkhourRecord[]>;
constructor() {
this.workers = new Map<string, Employee>();
this.employeeWorkhourRecords = new Map<string, EmployeeWorkhourRecord[]>();
}
addEmployee(worker: Employee) {
const existingWorker = this.workers.has(worker.workerId);
if (existingWorker) {
return "invalid_request: User already exists";
}
this.workers.set(worker.workerId, worker);
this.employeeWorkhourRecords.set(worker.workerId, []);
return "success";
}
getAllEmployees() {
return Array.from(this.workers.values());
}
registerEmployeeWorkhour(workerId: string, timestamp: number) {
const existingWorker = this.workers.has(workerId);
if (!existingWorker) {
return "invalid_request: User does not exist";
}
const currentEmployeeRecord = this.employeeWorkhourRecords.get(workerId);
if (currentEmployeeRecord == null) {
return "invalid_request: User does not exist";
}
if (currentEmployeeRecord.length % 2 === 0) {
this.employeeWorkhourRecords.set(
workerId,
currentEmployeeRecord.concat({ workerId, timestamp, type: "in" })
);
} else {
this.employeeWorkhourRecords.set(
workerId,
currentEmployeeRecord.concat({ workerId, timestamp, type: "out" })
);
}
return "success";
}
getTotalWorkingHours(workerId: string) {
const currentEmployeeRecord = this.employeeWorkhourRecords.get(workerId);
if (currentEmployeeRecord == null) {
return 0;
}
let len =
currentEmployeeRecord.length % 2 === 0
? currentEmployeeRecord.length
: currentEmployeeRecord.length - 1;
let totalWorkingHours = 0;
for (let i = 0; i < len; i += 2) {
const inRecord = currentEmployeeRecord[i];
const outRecord = currentEmployeeRecord[i + 1];
totalWorkingHours += outRecord.timestamp - inRecord.timestamp;
}
return totalWorkingHours;
}
topNWorkhours(n: number, position: string) {
const workersArray = Array.from(this.workers.values());
const filteredWorkers = workersArray
.filter((worker) => worker.position === position)
.map((worker) => ({
...worker,
timeSpent: this.getTotalWorkingHours(worker.workerId),
}))
.sort((a, b) => b.timeSpent - a.timeSpent)
.slice(0, n)
.map((worker) => `${worker.workerId}(${worker.timeSpent})`);
return filteredWorkers;
}
}
// Test
const wg = new WorkersGame();
wg.addEmployee({
workerId: "Hanif",
position: "Software Engineer",
compensation: 4000,
});
wg.addEmployee({
workerId: "Asad",
position: "Software Engineer",
compensation: 2000,
});
wg.registerEmployeeWorkhour("Hanif", 0);
wg.registerEmployeeWorkhour("Hanif", 15);
wg.registerEmployeeWorkhour("Hanif", 30);
wg.registerEmployeeWorkhour("Hanif", 45);
wg.registerEmployeeWorkhour("Hanif", 60);
wg.registerEmployeeWorkhour("Asad", 50);
wg.registerEmployeeWorkhour("Asad", 70);
wg.registerEmployeeWorkhour("Asad", 75);
wg.addEmployee({ workerId: "Ali", position: "UI/UX", compensation: 1500 });
wg.registerEmployeeWorkhour("Ali", 10);
wg.registerEmployeeWorkhour("Ali", 20);
wg.addEmployee({
workerId: "Nabil",
position: "Software Engineer",
compensation: 1500,
});
console.log("totalWorkingHours Hanif:", wg.getTotalWorkingHours("Hanif"));
console.log("totalWorkingHours Asad:", wg.getTotalWorkingHours("Asad"));
const topPerformers = wg.topNWorkhours(3, "Software Engineer");
console.log("topPerformers:", topPerformers);