mostly working, output headers need fixing
This commit is contained in:
		| @@ -2,7 +2,12 @@ package uk.org.mafoo.cpomssummariser; | ||||
|  | ||||
| import java.io.*; | ||||
| import java.util.*; | ||||
| import java.util.Map.Entry; | ||||
|  | ||||
| import org.apache.poi.xssf.usermodel.*; | ||||
|  | ||||
| import uk.org.mafoo.cpomssummariser.DataModel.CategoryGroupData; | ||||
|  | ||||
| import org.apache.poi.ss.usermodel.*; | ||||
|  | ||||
| /** | ||||
| @@ -48,6 +53,7 @@ public class App { | ||||
|           } | ||||
|           System.out.println(""); | ||||
|         } | ||||
|         workbook.close(); | ||||
|         file.close(); | ||||
|     } | ||||
|  | ||||
| @@ -61,6 +67,14 @@ public class App { | ||||
|         List<Record> records = Parser.parse(data); | ||||
|         Summariser summariser = new Summariser(records); | ||||
|  | ||||
|         System.out.println("Data imported..."); | ||||
|  | ||||
|         for (Entry<String, Map<String, CategoryGroupData>> item : summariser.getData().entrySet()) { | ||||
|             System.out.println(item); | ||||
|         } | ||||
|  | ||||
|         Writer.run(summariser, "out.xlsx"); | ||||
|  | ||||
|         System.out.println("Ending..."); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -5,20 +5,26 @@ import java.util.Map; | ||||
|  | ||||
| public class CategoryGroupData { | ||||
|      | ||||
|     String group; | ||||
|     final String parent; | ||||
|     final String name; | ||||
|     final String group; | ||||
|     Map<String, Integer> sex; | ||||
|     Integer SEND; | ||||
|     Integer PP; | ||||
|  | ||||
|     public CategoryGroupData(String group, Map<String, Integer> sex, Integer SEND, Integer PP) { | ||||
|     public CategoryGroupData(String parent, String name, String group, Map<String, Integer> sex, Integer SEND, Integer PP) { | ||||
|         this.parent = parent; | ||||
|         this.name = name; | ||||
|         this.group = group; | ||||
|         this.sex = sex; | ||||
|         this.SEND = SEND; | ||||
|         this.PP = PP; | ||||
|     } | ||||
|  | ||||
|     public CategoryGroupData(String group) { | ||||
|     public CategoryGroupData(String parent, String name, String group) { | ||||
|         this( | ||||
|             parent, | ||||
|             name, | ||||
|             group, | ||||
|             new HashMap<String, Integer>(), | ||||
|             0, | ||||
| @@ -26,7 +32,7 @@ public class CategoryGroupData { | ||||
|         ); | ||||
|         this.sex.put("Male", 0); | ||||
|         this.sex.put("Female", 0); | ||||
|         // System.out.println("[CategoryGroupData] Created new group: " + group); | ||||
|         System.out.println("[CategoryGroupData] Created new group: " + parent + " / " + group); | ||||
|     } | ||||
|  | ||||
|     public void incrementPP() { | ||||
| @@ -42,6 +48,14 @@ public class CategoryGroupData { | ||||
|         this.sex.put(sex, this.sex.get(sex) + 1); | ||||
|     } | ||||
|  | ||||
|     public String getParent() { | ||||
|         return this.parent; | ||||
|     } | ||||
|  | ||||
|     public String getName() { | ||||
|         return this.name; | ||||
|     } | ||||
|  | ||||
|     public int getPP() { | ||||
|         return this.PP.intValue(); | ||||
|     } | ||||
| @@ -57,6 +71,8 @@ public class CategoryGroupData { | ||||
|  | ||||
|     public String toString() { | ||||
|         StringBuilder stringBuilder = new StringBuilder(); | ||||
|         stringBuilder.append("Parent=");  stringBuilder.append(this.parent);                    stringBuilder.append(" "); | ||||
|         stringBuilder.append("Name=");    stringBuilder.append(this.name);                      stringBuilder.append(" "); | ||||
|         stringBuilder.append("Group=");   stringBuilder.append(this.group);                     stringBuilder.append(" "); | ||||
|         stringBuilder.append("Male=");    stringBuilder.append(this.getSexValue("Male"));   stringBuilder.append(" "); | ||||
|         stringBuilder.append("Female=");  stringBuilder.append(this.getSexValue("Female")); stringBuilder.append(" "); | ||||
|   | ||||
| @@ -58,7 +58,9 @@ public class Record { | ||||
|         this.p_send = boolstrToBool(send); | ||||
|         this.p_pp = boolstrToBool(pp); | ||||
|  | ||||
|         if(! p_groups.contains(group)) p_groups.add("Year " + group); | ||||
|         if(! p_groups.contains("Year " + group)) { | ||||
|             p_groups.add("Year " + group); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     protected String getSex() { | ||||
|   | ||||
| @@ -1,5 +1,7 @@ | ||||
| package uk.org.mafoo.cpomssummariser; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.Collections; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| @@ -9,29 +11,20 @@ import uk.org.mafoo.cpomssummariser.DataModel.CategoryGroupData; | ||||
| public class Summariser { | ||||
|  | ||||
|     List<Record> records; | ||||
|     List<String> groups = Record.getGroups(); | ||||
|     Map<String, Map<String, CategoryGroupData>> data = new HashMap<String, Map<String, CategoryGroupData>>(); | ||||
|  | ||||
|     public Summariser(List<Record> records) { | ||||
|         this.records = records; | ||||
|  | ||||
|         // Prepare the top-level data structure | ||||
|         for (String c : Record.getTopLevelCategories()) { | ||||
|             Map<String, CategoryGroupData> cat = new HashMap<String, CategoryGroupData>(); | ||||
|             for (String g : Record.getGroups()) { | ||||
|                 cat.put(g, new CategoryGroupData(g)); | ||||
|             } | ||||
|             data.put(c, cat); | ||||
|             System.out.println("[Summariser] Created: " + c); | ||||
|         } | ||||
|         process(); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     Map<String, CategoryGroupData> data_getOrCreate(String cat) { | ||||
|     Map<String, CategoryGroupData> data_getOrCreate(String parent, String cat) { | ||||
|         if(! data.containsKey(cat)) { | ||||
|             Map<String, CategoryGroupData> tmp = new HashMap<String, CategoryGroupData>(); | ||||
|             for (String g : Record.getGroups()) { | ||||
|                 tmp.put(g, new CategoryGroupData(g)); | ||||
|                 tmp.put(g, new CategoryGroupData(parent, cat, g)); | ||||
|             } | ||||
|             data.put(cat, tmp); | ||||
|         } | ||||
| @@ -44,7 +37,7 @@ public class Summariser { | ||||
|             for (Entry<String, List<String>> category : record.getCategories().entrySet()) { | ||||
|                 for (String subcat : category.getValue()) { | ||||
|                     String group = record.getGroup(); | ||||
|                     Map<String, CategoryGroupData> x = data_getOrCreate(subcat); | ||||
|                     Map<String, CategoryGroupData> x = data_getOrCreate(category.getKey(), subcat); | ||||
|                     CategoryGroupData blob = x.get(group); | ||||
|                     if(record.getSEND()) blob.incrementSEND(); | ||||
|                     if(record.getPP()) blob.incrementPP(); | ||||
| @@ -54,4 +47,15 @@ public class Summariser { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public List<String> getGroups() { | ||||
|         List<String> clone = new ArrayList<String>(); | ||||
|         clone.addAll(groups); | ||||
|         Collections.sort(clone); // FIXME: Sort is wrong! | ||||
|         return clone; | ||||
|     } | ||||
|  | ||||
|     public Map<String, Map<String, CategoryGroupData>> getData() { | ||||
|         return data; | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user