Back to project page RaceTimer.
The source code is released under:
GNU General Public License
If you think the Android project RaceTimer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.liamfruzyna.android.racetimer; //from w ww . ja va2 s . c o m import com.liamfruzyna.android.racetimer.objects.Event; import com.liamfruzyna.android.racetimer.objects.Meet; import com.liamfruzyna.android.racetimer.objects.Runner; import com.liamfruzyna.android.racetimer.objects.Team; import com.liamfruzyna.android.racetimer.objects.Time; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by mail929 on 9/13/14. */ public class AppData { public static List<Runner> runners = new ArrayList<Runner>(); public static List<Team> teams = new ArrayList<Team>(); public static List<Meet> meets = new ArrayList<Meet>(); static File fileDir; public static void addRunner(String name) { runners.add(new Runner(runners.size(), name)); } public static void addMeet(String name, String shortName) { meets.add(new Meet(meets.size(), name, shortName)); save(); } public static void addTeam(String name, String shortName, List<Integer> runners) { teams.add(new Team(teams.size(), name, shortName, runners)); save(); } public static void save() { JSONArray jmeets = new JSONArray(); for(int i = 0; i < meets.size(); i++) { JSONObject meet = new JSONObject(); try { meet.put("id", i); meet.put("name", meets.get(i).name); meet.put("shortName", meets.get(i).shortName); } catch(JSONException e) { e.printStackTrace(); } JSONArray events = new JSONArray(); for(int j = 0; j < meets.get(i).events.size(); j++) { JSONObject jevent = new JSONObject(); Event event = meets.get(i).events.get(j); try { jevent.put("level", event.level); jevent.put("distance", event.distance); jevent.put("run", event.run); JSONArray cps = new JSONArray(); for(int l = 0; l < event.checkPoints.size(); l++) { cps.put(event.checkPoints.get(l)); } jevent.put("checkpoints", cps); JSONArray runners = new JSONArray(); for(int l = 0; l < event.runners.size(); l++) { runners.put(event.runners.get(l)); } jevent.put("runners", runners); } catch(JSONException e) { e.printStackTrace(); } events.put(jevent); } try { meet.put("events", events); } catch(JSONException e) { e.printStackTrace(); } jmeets.put(meet); } writeToFile("meets", jmeets.toString()); JSONArray jteams = new JSONArray(); for(int i = 0; i < teams.size(); i++) { JSONObject team = new JSONObject(); try { team.put("id", i); team.put("name", teams.get(i).name); team.put("shortName", teams.get(i).shortName); JSONArray jrunners = new JSONArray(); for(int j = 0; j < teams.get(i).runners.size(); j++) { jrunners.put(teams.get(i).runners.get(j)); } team.put("runners", jrunners); JSONArray tmeets= new JSONArray(); for(int j = 0; j < teams.get(i).meets.size(); j++) { tmeets.put(teams.get(i).meets.get(j)); } team.put("meets", tmeets); } catch(JSONException e) { e.printStackTrace(); } jteams.put(team); } writeToFile("teams", jteams.toString()); JSONArray jrunners = new JSONArray(); for(int i = 0; i < runners.size(); i++) { JSONObject runner = new JSONObject(); try { runner.put("id", i); runner.put("name", runners.get(i).name); JSONArray teamsa = new JSONArray(); for(int j = 0; j < runners.get(i).teams.size(); j++) { teamsa.put(runners.get(i).teams.get(j)); } runner.put("teams", teamsa); JSONArray times = new JSONArray(); for(int j = 0; j < runners.get(i).times.size(); j++) { JSONObject time = new JSONObject(); time.put("meet", runners.get(i).times.get(j).meetId); time.put("event", runners.get(i).times.get(j).eventId); JSONArray cps = new JSONArray(); for(int l = 0; l < runners.get(i).times.get(j).checkPoints.size(); l++) { cps.put(runners.get(i).times.get(j).checkPoints.get(l)); } time.put("checkPoints", cps); times.put(time); } runner.put("times", times); } catch(JSONException e) { e.printStackTrace(); } jrunners.put(runner); } writeToFile("runners", jrunners.toString()); } public static void load(File file) { fileDir = file; try { JSONArray jmeets = new JSONArray(readFromFile("meets")); for(int i = 0; i < jmeets.length(); i++) { JSONObject meet = jmeets.getJSONObject(i); List<Event> events = new ArrayList<Event>(); JSONArray jevents = meet.getJSONArray("events"); for(int j = 0; j < jevents.length(); j++) { JSONObject event = jevents.getJSONObject(j); List<Double> cps = new ArrayList<Double>(); for(int l = 0; l < event.getJSONArray("checkpoints").length(); l++) { cps.add(event.getJSONArray("checkpoints").getDouble(l)); } List<Integer> runners = new ArrayList<Integer>(); for(int l = 0; l < event.getJSONArray("runners").length(); l++) { runners.add(event.getJSONArray("runners").getInt(l)); } events.add(new Event(j, i, event.getDouble("distance"), cps, event.getString("level"), runners, event.getBoolean("run"))); } meets.add(new Meet(meet.getInt("id"), meet.getString("name"), meet.getString("shortName"), events)); } JSONArray jteams = new JSONArray(readFromFile("teams")); for(int i = 0; i < jteams.length(); i++) { JSONObject team = jteams.getJSONObject(i); List<Integer> runnerIds = new ArrayList<Integer>(); for(int j = 0; j < team.getJSONArray("runners").length(); j++) { runnerIds.add(team.getJSONArray("runners").getInt(j)); } List<Integer> meetIds = new ArrayList<Integer>(); for(int j = 0; j < team.getJSONArray("meets").length(); j++) { meetIds.add(team.getJSONArray("meets").getInt(j)); } teams.add(new Team(team.getInt("id"), team.getString("name"), team.getString("shortName"), runnerIds, meetIds)); } JSONArray jrunners = new JSONArray(readFromFile("runners")); for(int i = 0; i < jrunners.length(); i++) { JSONObject runner = jrunners.getJSONObject(i); List<Integer> teamIds = new ArrayList<Integer>(); for(int j = 0; j < runner.getJSONArray("teams").length(); j++) { teamIds.add(runner.getJSONArray("teams").getInt(j)); } List<Time> times = new ArrayList<Time>(); for(int j = 0; j < runner.getJSONArray("times").length(); j++) { List<String> cps = new ArrayList<String>(); for(int l = 0; l < runner.getJSONArray("times").getJSONObject(j).getJSONArray("checkPoints").length(); l++) { cps.add(runner.getJSONArray("times").getJSONObject(j).getJSONArray("checkPoints").getString(l)); } times.add(new Time(runner.getJSONArray("times").getJSONObject(j).getInt("meet"), runner.getJSONArray("times").getJSONObject(j).getInt("event"), cps)); } runners.add(new Runner(runner.getInt("id"), runner.getString("name"), teamIds, times)); } } catch(JSONException e) { e.printStackTrace(); } } private static void writeToFile(String fileName, String data) { File file = new File(fileDir, fileName); try { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(data); bw.close(); } catch(IOException e) { e.printStackTrace(); } } private static String readFromFile(String fileName) { File file = new File(fileDir, fileName); StringBuilder sb = new StringBuilder(); try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; while((line = br.readLine()) != null) { sb.append(line); } return sb.toString(); } catch(IOException e) { e.printStackTrace(); } return ""; } public static List<Double> getTimes(Time time) { List<String> ckps = time.checkPoints; List<Double> times = new ArrayList<Double>(); for(int i = 0; i < ckps.size(); i++) { double min = Integer.parseInt(ckps.get(i).split(":")[0]) + (Double.parseDouble(ckps.get(i).split(":")[1])/60); for(int j = 0; j < times.size(); j++) { min -= times.get(j); } times.add(min); } return times; } public static Double getTime(String time) { double min = Integer.parseInt(time.split(":")[0]) + (Double.parseDouble(time.split(":")[1])/60); return min; } public static double getMin(List<Double> nums) { double smallest = nums.get(0); for(int i = 0; i < nums.size(); i++) { if(nums.get(i) < smallest) { smallest = nums.get(i); } } return smallest; } public static double getMax(List<Double> nums) { double biggest = nums.get(0); for(int i = 0; i < nums.size(); i++) { if(nums.get(i) > biggest) { biggest = nums.get(i); } } return biggest; } public static double getDif(List<Double> nums) { return getMax(nums) - getMin(nums); } }