【Javascript 项目代码】Home Care -- 类,封装性, 单链表排序 -- 2
思想:
创建三个类,一个Boss类,管理Worker的分配,统计worker和Task的个数。
Worker类,管理这个worker自己的Task。
Task类。
//Boss类 function Boss(){ this.workerList = new Array(); this.totalWorker = 0; this.totalTask = 0; } Boss.prototype = { constructor: Boss, //添加worker,并且使用单链表排序 this.addWorker_sorted : function(head, worker){ var cur = head; var flag = false; this.totalTask++; while(cur.next != null){ if(parseInt(cur.next.workerNo,10) > parseInt(worker.workerNo,10)){ cur.next.total = 1; break; }else if(parseInt(cur.next.workerNo,10) == parseInt(worker.workerNo,10)){ cur.next.taskIndexArr.push(this.totalTask); cur.next.client_id_arr.push(worker.client_id); cur.next.visit_id_arr.push(worker.visit_id); cur.next.constraint_violations_arr.push(worker.constraint_violations); cur.next.lat_arr.push(worker.lat); cur.next.lon_arr.push(worker.lon); cur.next.client_start_time_hours_arr.push(worker.client_start_time_hours); cur.next.client_start_time_minutes_arr.push(worker.client_start_time_minutes); cur.next.client_end_time_hours_arr.push(worker.client_end_time_hours); cur.next.client_end_time_minutes_arr.push(worker.client_end_time_minutes); cur.next.duration_arr.push(worker.duration); cur.next.total++ flag = true; } cur = cur.next; } if(flag == false){ worker.next = cur.next; cur.next = worker; worker.taskIndexArr.push(this.totalTask); worker.visit_id_arr.push(worker.visit_id); worker.client_id_arr.push(worker.client_id); worker.constraint_violations_arr.push(worker.constraint_violations); worker.lat_arr.push(worker.lat); worker.lon_arr.push(worker.lon); worker.client_start_time_hours_arr.push(worker.client_start_time_hours); worker.client_start_time_minutes_arr.push(worker.client_start_time_minutes); worker.client_end_time_hours_arr.push(worker.client_end_time_hours); worker.client_end_time_minutes_arr.push(worker.client_end_time_minutes); worker.duration_arr.push(worker.duration); this.workerList.push(worker); } } } //Worker类 function Worker(workerNo, worker_id, visit_id, client_id, cv, lat, lon, csth, cstm, ceth, cetm, duration){ this.workerNo = workerNo || ''; this.worker_id = worker_id || ''; this.visit_id = visit_id || ''; this.client_id = client_id || ''; this.constraint_violations = cv || ''; this.lat = lat || ''; this.lon = lon || ''; this.client_start_time_hours = csth || ''; this.client_start_time_minutes = cstm || ''; this.client_end_time_hours = ceth || ''; this.client_end_time_minutes = cetm || ''; this.duration = duration || ''; this.avg_lat = -1; this.avg_lon = -1; this.taskList = new Array(); this.total = 0; } Worker.prototype = { constructor: Worker, //添加任务 this.addTask : function(inx, vid, cid, lat, lon, cv, s_h, s_m, e_h, e_m){ var task = new Task(inx, vid, cid, lat, lon, cv, s_h, s_m, e_h, e_m); this.taskList.push(task); this.total++; }, //统计任务个数 this.countTasks : function(){ return this.total; }, //计算lat, lon this.cal_avg_lat_lon : function(){ var sum_lat = 0.0; var sum_lon = 0.0; var avg_lat = 0.0; var avg_lon = 0.0; var num = this.taskList.length; for(var i = 0; i < num; i++){ sum_lat += this.taskList[i].lat; sum_lon += this.taskList[i].lon; } avg_lat = sum_lat / num; avg_lon = sum_lon / num; this.avg_lat = avg_lat; this.avg_lon = avg_lon; }, this.get_avg_lat : function(){ if(this.avg_lat == -1){ this.cal_avg_lat_lon(); }else{ return this.avg_lat; } }, this.get_avg_lon : function(){ if(this.avg_lon == -1){ this.cal_avg_lat_lon(); }else{ return this.avg_lon; } } } //Task类 function Task(inx, vid, cid, lat, lon, cv, s_h, s_m, e_h, e_m){ this.inx = inx; this.vid = vid; this.cid = cid; this.lat = lat; this.lon = lon; this.cv = cv; this.s_h = s_h; this.s_m = s_m; this.e_h = e_h; this.e_m = e_m; }