Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************
- ** Fichero CargarConfiguracion.java
- ***************************************/
- package taskDuration;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.util.StringTokenizer;
- public class CargarConfiguracion {
- private int numTareas;
- public Task listaTareas[];
- public CargarConfiguracion(String fichero) {
- int estado = 0;
- try {
- File archivo = new File(fichero);
- FileReader fr = new FileReader(archivo);
- BufferedReader br = new BufferedReader(fr);
- while (br.ready()) {
- String linea = br.readLine();
- if (linea.startsWith("#")) {
- estado++;
- } else if (!linea.isEmpty()) {
- if (estado == 1) {
- numTareas(linea);
- } else if (estado == 2) {
- crearTarea(linea);
- } else if (estado == 3) {
- rellenarDependencias(linea);
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void crearTarea(String line) {
- StringTokenizer tokenizer = new StringTokenizer(line, ",");
- int tarea = Integer.parseInt(tokenizer.nextToken());
- int tiempo = Integer.parseInt(tokenizer.nextToken());
- listaTareas[tarea] = new Task(tiempo);
- }
- private void numTareas(String line) {
- numTareas = Integer.parseInt(line);
- listaTareas = new Task[numTareas];
- }
- private void rellenarDependencias(String line) {
- StringTokenizer tokenizer = new StringTokenizer(line, ",");
- int tarea = Integer.parseInt(tokenizer.nextToken());
- Task dependencias[] = new Task[tokenizer.countTokens()];
- int i = 0;
- while (tokenizer.hasMoreTokens()) {
- int dependencia = Integer.parseInt(tokenizer.nextToken());
- dependencias[i] = listaTareas[dependencia];
- i++;
- }
- listaTareas[tarea].setDependencias(dependencias);
- }
- }
- /****************************************
- ** Fichero Task.java
- ***************************************/
- package taskDuration;
- public class Task {
- private int tiempoEjecucion;
- private Task dependencias[];
- private int tiempoTotal;
- public Task(int tiempoEjecucion, Task dependendencias[]) {
- this.tiempoEjecucion = tiempoEjecucion;
- this.dependencias = dependendencias;
- this.tiempoTotal = -1;
- }
- public Task(int tiempoEjecucion) {
- this(tiempoEjecucion, null);
- }
- public void setDependencias(Task dependencias[]) {
- this.dependencias = dependencias;
- }
- public int calcularTiempo() {
- if (tiempoTotal != -1) {
- return tiempoTotal;
- }
- if (dependencias == null) {
- tiempoTotal = tiempoEjecucion;
- return tiempoTotal;
- }
- int tiempoMaximo = 0;
- int tiempoActual = 0;
- for (int i = 0; i < dependencias.length; i++) {
- Task tarea = dependencias[i];
- tiempoActual = tarea.calcularTiempo();
- if (tiempoActual > tiempoMaximo) {
- tiempoMaximo = tiempoActual;
- }
- }
- tiempoTotal = tiempoMaximo + tiempoEjecucion;
- return tiempoTotal;
- }
- }
- /****************************************
- ** Fichero Main.java
- ***************************************/
- package taskDuration;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.StringTokenizer;
- public class Main {
- /**
- * @param args
- */
- public static void main(String[] args) {
- CargarConfiguracion configuration = new CargarConfiguracion("in");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- try {
- while (br.ready()) {
- //while (true) {
- String entrada = br.readLine();
- StringTokenizer tokens = new StringTokenizer(entrada, ",");
- while (tokens.hasMoreTokens()) {
- int tarea = Integer.parseInt(tokens.nextToken());
- System.out.println(tarea + " " + configuration.listaTareas[tarea].calcularTiempo());
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement