-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileReader.java
80 lines (68 loc) · 1.99 KB
/
FileReader.java
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
71
72
73
74
75
76
77
78
79
80
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Static class for reading files and creating tasks.
* @author kristebo
*
*/
public class FileReader {
public static int numberofTasks=0;
private static ArrayList<Integer> predecessor;
private static ArrayList<Task> tasks;
/**
* This static method reads a file and creates task objects.
* @param file
* @return a ArraList of Tasks. and sets number of tasks as static int <tt>numberofTasks</tt>
*/
public static ArrayList<Task> readFileAndCreateTasks(File file){
ArrayList<String> lines=new ArrayList<>();
try {
Scanner scn=new Scanner(file);
while(scn.hasNext()){
lines.add(scn.nextLine());
}
numberofTasks=Integer.parseInt(lines.get(0));
lines.remove(0);//numberofTasks
lines.remove(0);//lineFeed
scn.close();
return createTasks(lines);
} catch (FileNotFoundException e) {
System.err.println("File Not Found "+ file.getName());
e.printStackTrace();
}
return null;
}
/**
* Creates Tasks from a string <tt>data</tt>
* @param data a String describing this Task
* @return
*/
private static ArrayList<Task> createTasks(ArrayList<String> data){
//data for this task;
int manpower, time, id;
String name;
tasks=new ArrayList<Task>();
String delimsator = "\\s+";
for(String s: data){
predecessor=new ArrayList<Integer>();
String[] tmp=s.split(delimsator);
id=Integer.parseInt(tmp[0]);
name=tmp[1];
time=Integer.parseInt(tmp[2]);
manpower=Integer.parseInt(tmp[3]);
if (Integer.parseInt(tmp[4])==0){
tasks.add(new Task(id, name, time, manpower, null)); //first task
} else {
predecessor.add(Integer.parseInt(tmp[4]));
for (int i=5; i<tmp.length && Integer.parseInt(tmp[i])!=0; i++){
predecessor.add(Integer.parseInt(tmp[i]));
}
tasks.add(new Task(id, name, time, manpower, predecessor));
}
}
//System.out.println(tasks); //debug
return tasks;
}
}