Last active
August 29, 2015 14:10
-
-
Save carolusquintus/0ec18427e5df983afbbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Edificio(elevadores, planta) { | |
this.elevadores = elevadores; | |
this.planta = planta; | |
} | |
function Elevador(planta) { | |
this.planta = planta; | |
this.enServicio = false; | |
this.subir = function() { | |
}; | |
this.bajar = function() { | |
}; | |
} | |
function Planta(nombrePlanta, sube, baja) { | |
this.nombrePlanta = nombrePlanta; | |
this.sube = sube; | |
this.baja = baja; | |
} | |
function generarTiempoRango(min, max, tipo) { | |
var tiempo = 0; | |
switch (tipo) { | |
case "S": | |
tiempo = generarRandomRango(min, max) * 1000; | |
break; | |
case "M": | |
tiempo = generarRandomRango(min, max) * 60000; | |
break; | |
} | |
return tiempo.toFixed(); | |
} | |
/** | |
* Genera un numero aleatorio flotante en un rango determinado. | |
* El rango | |
* | |
* | |
* | |
* | |
* @method generarRandomRango | |
* @param min {Number} Valor minimo del rango | |
* @param max {Number} Valor maximo del rango | |
* @return {Number} Regresa numero generado del rango | |
*/ | |
function generarRandomRango(min, max) { | |
return (Math.random() * (max - min) + min); | |
} | |
/** | |
* Genera un numero aleatorio flotante en un rango determinado. | |
* | |
* @method detenerEjecucion | |
* @param tiempoInicial {Number} Tiempo inicial en milisegundos | |
* @param tiempoEjecucion {Number} Tiempo que durara la ejecucion en milisegundos | |
* @return {Boolean} Regresa true si el | |
*/ | |
function detenerEjecucion(tiempoInicial, tiempoEjecucion) { | |
var tiempoActual = new Date().getTime(); | |
var diferencia = tiempoActual - tiempoInicial; | |
console.log("Tiempo actual: " + new Date(tiempoActual).toISOString()); | |
return diferencia >= tiempoEjecucion; | |
} | |
var s = new Planta("S", true, false); | |
var pb = new Planta("PB", true, true); | |
var p1 = new Planta("1", true, true); | |
var p2 = new Planta("2", true, true); | |
var p3 = new Planta("3", true, true); | |
var p4 = new Planta("4", true, true); | |
var p5 = new Planta("5", true, true); | |
var p6 = new Planta("6", true, true); | |
var t = new Planta("T", false, true); | |
var planta = [s, pb, p1, p2, p3, p4, p5, p6, t]; | |
var elevador1 = new Elevador(planta); | |
var elevador2 = new Elevador(planta); | |
var elevadores = [elevador1, elevador2]; | |
var edificio = new Edificio(elevadores); | |
var tiempoInicial = new Date(); | |
var tiempoEjecucion = generarTiempoRango(3, 7, "M"); | |
console.log("Inicia ejecucion: " + tiempoInicial.toISOString()); | |
console.log("Duracion aproximada de ejecucion: " + tiempoEjecucion / 60000); | |
(function main(){ | |
var segundos = generarTiempoRango(1, 10, "S"); | |
console.log("Llamando elevador en " + (segundos / 1000) + " segundos"); | |
var idTimeout = setTimeout(function() { | |
var index = Math.floor(generarRandomRango(0, planta.length)); | |
console.log("Llama elevador desde " + planta[index].nombrePlanta); | |
if (detenerEjecucion(tiempoInicial.getTime(), tiempoEjecucion)) | |
clearTimeout(idTimeout); | |
else | |
main(); | |
}, segundos); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment