-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerList.js
More file actions
70 lines (64 loc) · 1.66 KB
/
WorkerList.js
File metadata and controls
70 lines (64 loc) · 1.66 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
"use strict";
// worker class use: var w = new Worker(id);
function Worker (id) {
this.type = "classWorker" // will be used for type checking
this.id = id;
this.status = "free"; // free or busy
}
// WorkerList class
function WorkerList () {
this.type = "classWorkerList" // will be used for type checking
this.workers = [];
this.add = function(id) {
var w = new Worker(id);
this.workers.push(w);
};
this.remove = function(id) {
// find worker with worker.id=id and remove it from WorkerList.
// Return index removed. -1 if none found.
var ir=-1; // index to remove
for (var i=0; i<this.workers.length; i++){
if (this.workers[i].id === id){
ir = i;
break;
}
}
if (ir!==-1){
this.workers.splice(ir, 1);
}
return ir
};
this.changeStatus = function(id, status){
// change status of worker in WorkerList with worker.id===id.
// Return the number index of changed worker in WorkerList. -1 if none found.
for (var i=0; i<this.workers.length; i++){
if (this.workers[i].id === id){
this.workers[i].status=status;
return i;
}
}
return -1;
};
this.nextAvailable = function(){
// returns the id of the first free worker from WorkerList
// Return the -1 if no one free
for (var i=0; i<this.workers.length; i++){
if (this.workers[i].status === "free"){
return this.workers[i].id;
}
}
return -1;
};
this.print = function(){
console.log(this.workers);
};
}
module.exports = WorkerList
/*
var wl = new WorkerList();
wl.add("1");
wl.print();
print(wl.nextAvailable());
wl.changeStatus("1", "newStatus");
wl.remove("3");
*/