Advertisement
javipinero

Task Duration Java Tuenti Contest

Jun 21st, 2011
1,498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. /****************************************
  2.  ** Fichero CargarConfiguracion.java
  3.  ***************************************/
  4.  
  5.  
  6. package taskDuration;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.File;
  10. import java.io.FileReader;
  11. import java.util.StringTokenizer;
  12.  
  13. public class CargarConfiguracion {
  14.  
  15.     private int numTareas;
  16.     public Task listaTareas[];
  17.  
  18.     public CargarConfiguracion(String fichero) {
  19.  
  20.         int estado = 0;
  21.  
  22.         try {
  23.             File archivo = new File(fichero);
  24.             FileReader fr = new FileReader(archivo);
  25.             BufferedReader br = new BufferedReader(fr);
  26.             while (br.ready()) {
  27.                 String linea = br.readLine();
  28.                 if (linea.startsWith("#")) {
  29.                     estado++;
  30.                 } else if (!linea.isEmpty()) {
  31.                     if (estado == 1) {
  32.                         numTareas(linea);
  33.                     } else if (estado == 2) {
  34.                         crearTarea(linea);
  35.                     } else if (estado == 3) {
  36.                         rellenarDependencias(linea);
  37.                     }
  38.                 }
  39.  
  40.             }
  41.         } catch (Exception e) {
  42.             e.printStackTrace();
  43.         }
  44.     }
  45.  
  46.     private void crearTarea(String line) {
  47.         StringTokenizer tokenizer = new StringTokenizer(line, ",");
  48.         int tarea = Integer.parseInt(tokenizer.nextToken());
  49.         int tiempo = Integer.parseInt(tokenizer.nextToken());
  50.         listaTareas[tarea] = new Task(tiempo);
  51.     }
  52.  
  53.     private void numTareas(String line) {
  54.         numTareas = Integer.parseInt(line);
  55.         listaTareas = new Task[numTareas];
  56.     }
  57.  
  58.     private void rellenarDependencias(String line) {
  59.         StringTokenizer tokenizer = new StringTokenizer(line, ",");
  60.         int tarea = Integer.parseInt(tokenizer.nextToken());
  61.         Task dependencias[] = new Task[tokenizer.countTokens()];
  62.         int i = 0;
  63.         while (tokenizer.hasMoreTokens()) {
  64.             int dependencia = Integer.parseInt(tokenizer.nextToken());
  65.             dependencias[i] = listaTareas[dependencia];
  66.             i++;
  67.         }
  68.         listaTareas[tarea].setDependencias(dependencias);
  69.     }
  70. }
  71.  
  72. /****************************************
  73.  ** Fichero Task.java
  74.  ***************************************/
  75.  
  76. package taskDuration;
  77.  
  78. public class Task {
  79.  
  80.     private int tiempoEjecucion;
  81.     private Task dependencias[];
  82.     private int tiempoTotal;
  83.  
  84.     public Task(int tiempoEjecucion, Task dependendencias[]) {
  85.  
  86.         this.tiempoEjecucion = tiempoEjecucion;
  87.         this.dependencias = dependendencias;
  88.         this.tiempoTotal = -1;
  89.  
  90.     }
  91.  
  92.     public Task(int tiempoEjecucion) {
  93.         this(tiempoEjecucion, null);
  94.     }
  95.  
  96.     public void setDependencias(Task dependencias[]) {
  97.         this.dependencias = dependencias;
  98.     }
  99.  
  100.     public int calcularTiempo() {
  101.         if (tiempoTotal != -1) {
  102.             return tiempoTotal;
  103.         }
  104.         if (dependencias == null) {
  105.             tiempoTotal = tiempoEjecucion;
  106.             return tiempoTotal;
  107.         }
  108.         int tiempoMaximo = 0;
  109.         int tiempoActual = 0;
  110.         for (int i = 0; i < dependencias.length; i++) {
  111.             Task tarea = dependencias[i];
  112.             tiempoActual = tarea.calcularTiempo();
  113.             if (tiempoActual > tiempoMaximo) {
  114.                 tiempoMaximo = tiempoActual;
  115.             }
  116.         }
  117.         tiempoTotal = tiempoMaximo + tiempoEjecucion;
  118.         return tiempoTotal;
  119.     }
  120.  
  121. }
  122.  
  123.  
  124. /****************************************
  125.  ** Fichero Main.java
  126.  ***************************************/
  127.  
  128. package taskDuration;
  129.  
  130. import java.io.BufferedReader;
  131. import java.io.InputStreamReader;
  132. import java.util.StringTokenizer;
  133.  
  134. public class Main {
  135.  
  136.     /**
  137.      * @param args
  138.      */
  139.     public static void main(String[] args) {
  140.  
  141.         CargarConfiguracion configuration = new CargarConfiguracion("in");
  142.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  143.  
  144.         try {
  145.             while (br.ready()) {
  146.             //while (true) {
  147.                 String entrada = br.readLine();
  148.                 StringTokenizer tokens = new StringTokenizer(entrada, ",");
  149.                 while (tokens.hasMoreTokens()) {
  150.                     int tarea = Integer.parseInt(tokens.nextToken());
  151.                     System.out.println(tarea + " " + configuration.listaTareas[tarea].calcularTiempo());                   
  152.                 }
  153.             }
  154.         } catch (Exception e) {
  155.             e.printStackTrace();
  156.         }
  157.  
  158.     }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement