From 97f6ff5ed31900c0bdc72615290cf50b2ee576a0 Mon Sep 17 00:00:00 2001 From: Peter De Jonckheere <peter.de-jonckheere.2014@uni.strath.ac.uk> Date: Thu, 12 Jul 2018 13:26:22 +0100 Subject: [PATCH] Some sorting for places --- .../sedentary/frontEnd/DataAnalysis.java | 56 ++++++++++++++++++- .../sedentary/frontEnd/SaveFile.java | 13 +++-- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java index fe504c3..b854fc1 100644 --- a/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java +++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java @@ -4,11 +4,14 @@ import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.icu.util.RangeValueIterator; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.List; import java.util.Locale; @@ -74,7 +77,58 @@ public class DataAnalysis { private void placeAnalysis(Context context){ List<Place> places = SaveFile.getPlaces(context); + Place[] sortedPlaces = (Place[])(quicksort(places.toArray(), 0, places.size()-1)); + int popularType = getPopularElement(sortedPlaces); - } + } + + private Object[] quicksort(Object[] array, int low, int high) { + if (low < high) { + int p = partition(array, low, high); + quicksort(array, low, p - 1); + quicksort(array, p + 1, high); + } + return array; + } + + private int partition(Object[] array, int low, int high){ + int pivot = ((Place) array[high]).getType(); + int i = low - 1; + for(int j = low; j < high - 1; j++){ + if(((Place)array[j]).getType() < pivot){ + i++; + Place swap = ((Place)array[j]); + array[j] = array[i]; + array[i] = swap; + } + } + Place swap = ((Place) array[i+1]); + array[i+1] = array[high]; + array[high] = swap; + return i+1; + } + private int getPopularElement(Place[] a) + { + int count = 1; + int tempCount; + int popular = a[0].getType(); + int temp = 0; + for (int i = 0; i < (a.length - 1); i++) + { + temp = a[i].getType(); + tempCount = 0; + for (int j = 1; j < a.length; j++) + { + if (temp == a[j].getType()) + tempCount++; + } + if (tempCount > count) + { + popular = temp; + count = tempCount; + } + } + return popular; + } } \ No newline at end of file diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java index cae3153..c57343d 100644 --- a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java +++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java @@ -441,10 +441,15 @@ public class SaveFile { BufferedReader br = new BufferedReader(new FileReader(file)); while ((line = br.readLine()) != null) { items = line.split(" "); - //Need to get other 2 places - com.notificationFramework.sedentary.frontEnd.Place place = new com.notificationFramework.sedentary.frontEnd.Place( - items[2], Float.parseFloat(items[4]), Integer.parseInt(items[3])); - places.add(place); + for(int i = 0; i < 3; i++) { + try { + com.notificationFramework.sedentary.frontEnd.Place place = new com.notificationFramework.sedentary.frontEnd.Place( + items[i + 2], Float.parseFloat(items[i + 4]), Integer.parseInt(items[i + 3])); + places.add(place); + }catch(NumberFormatException e){ + + } + } } } }catch(IOException e){ -- GitLab