-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokimonProcessor.java
More file actions
144 lines (126 loc) · 5.28 KB
/
TokimonProcessor.java
File metadata and controls
144 lines (126 loc) · 5.28 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package ca.cmpt213.as2;
import java.sql.SQLOutput;
import java.util.*;
import java.io.*;
/**
* The tokimon processor class checks the inputs it is
* given and makes sure the correct arguments were used.
* It sends the given files to the JsonReader class and
* gets the information needed back.
* It also catches many errors that stop the program
* from working properly
*/
public class TokimonProcessor {
public static void main(String[] args) throws FileNotFoundException, IOException {
if(args.length != 2){ //makes sure they put 2 arguments
System.out.println("You need 2 arguments, the .json file directory and the path you want your .csv file to go");
System.exit(-1);
}
File input = new File(args[0]);
File test_output = new File(args[1]);
ArrayList<ArrayList<String>> teams = new ArrayList<ArrayList<String>>(); //teams and their members
if(!input.isDirectory()){
System.out.println("this input directory does not exist"); //exits if given nonexistant input
System.exit(-1);
}
if(!test_output.isDirectory()){
System.out.println("this output directory does not exist"); //exits if given nonexistant output
System.exit(-1);
}
File output = new File(test_output + "/team_info.csv");
JsonReader test = new JsonReader();
ArrayList<File> allFiles = new ArrayList<File>();
test.findJsons(input, allFiles);
if(allFiles.size() == 0){
System.out.println("there are no json files");
System.exit(-1);
}
ArrayList<String[]> information = new ArrayList<String[]>();
ArrayList<String> names = new ArrayList<String>();
for(File x : allFiles){
test.JasonReaderFile(x,information, teams, names);
}
//first error checker, makes sure that there are no tokis in more than one team
for(int i = 0; i < teams.size(); i++){
for(int j = 1; j < teams.get(i).size(); j++){
for(int k = 0; k < teams.size(); k++){
if(k != i){
if(teams.get(k).contains(teams.get(i).get(j))){
System.out.println("Error: a tokimon is mentioned in multiple teams");
System.exit(-1);
}
}
}
}
}
//second error checker. makes sure that there are no tokimon's without their own json file
boolean hasJson = false;
for(int i = 0; i < information.size(); i++){
hasJson = false;
if(!(information.get(i)[2].equals("-"))){
for(int j = 0; j < teams.size(); j++){
if(teams.get(j).contains(information.get(i)[2])){
hasJson = true;
break;
}
}
}
else{
hasJson = true;
}
if(!hasJson){
System.out.println("Error: there is a tokimon that does not have its own json file");
System.exit(-1);
}
}
//third error checker, makes sure that ther are no tokimon that do not include one or more of their team members
ArrayList<String> mentions = new ArrayList<String>();
for(int i = 0; i < teams.size(); i ++) {
for(int j = 1; j < teams.get(i).size(); j++){
for(int k = 0; k < information.size(); k++){
if(information.get(k)[1].trim().equals(teams.get(i).get(j))){
mentions.add(information.get(k)[2]);
}
}
for(int k = 1; k < teams.get(i).size(); k++){
if(!(teams.get(i).get(k).equals(teams.get(i).get(j)))){
if(!mentions.contains(teams.get(i).get(k))){
System.out.println("Error: there is a tokimon that does not mention its team member");
System.exit(-1);
}
}
}
}
}
//fourth error checker, checks to see that all of the tokimon have the same properties as initially given
int counter = 0;
int teamNumber = 0;
int innerTeam = 0;
Collections.sort(names);
String currentToki = names.get(0);
for(int i = 0; i < names.size(); i++){
if(names.get(i).equals(currentToki)){
counter++;
}
else{
if(counter != ((teams.get(teamNumber).size()-1)*2)-1){
System.out.println("Error: a tokimon's property does not match its existing recorded properties");
System.exit(-1);
}
counter = 1;
innerTeam++;
if(innerTeam == teams.get(teamNumber).size()-1){
innerTeam = 0;
teamNumber++;
}
currentToki = names.get(i);
}
}
CsvWriter result = new CsvWriter();
result.sort(information);
for( int i = 0; i < teams.size(); i++) {
Collections.sort(teams.get(i));
}
result.csvOutput(output,information,teams);
}
}