-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackendProcessing.java
More file actions
148 lines (123 loc) · 5.07 KB
/
Copy pathbackendProcessing.java
File metadata and controls
148 lines (123 loc) · 5.07 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
145
146
147
148
import java.util.*;
import java.io.*;
import dataObjs.ecoPriorityObj;
import dataObjs.foodObj;
import dataObjs.macroPriorityObj;
public class backendProcessing {
private List<foodObj> totalFoodList = new ArrayList<foodObj>();
private float avgEmission=0;
private float avgLand=0;
private float avgWater=0;
private int rows=99;
public backendProcessing(String csvFilename) {
try {
ReadFile(csvFilename);
averages();
} catch (Exception e)
{
System.out.println("Error reading file.");
}
}
private void ReadFile(String csvFilename) throws IOException {
File file = new File(csvFilename);
Scanner scn = new Scanner(file);
scn.useDelimiter(",|\\n");
//Skip the CSV Headers
for (int i = 0; i < 17; i++) {
scn.next();
}
//Iterate over CSV rows and store food objects
while (totalFoodList.size() < 99 && scn.hasNextLine()) {
String name = scn.next();
double normEmission = Double.parseDouble(scn.next());
double normLand = Double.parseDouble(scn.next());
double normwaterWithdrawal = Double.parseDouble(scn.next());
double normFat = Double.parseDouble(scn.next());
double normProtein = Double.parseDouble(scn.next());
double normFiber = Double.parseDouble(scn.next());
double normCarbs = Double.parseDouble(scn.next());
double emission = Double.parseDouble(scn.next());
double land = Double.parseDouble(scn.next());
double waterWithdrawal = Double.parseDouble(scn.next());
double price = Double.parseDouble(scn.next());
int fat = Integer.parseInt(scn.next());
int protein = Integer.parseInt(scn.next());
int fiber = Integer.parseInt(scn.next());
int carb = Integer.parseInt(scn.next());
double priceNormalized = Double.parseDouble(scn.next());
totalFoodList.add(new foodObj(name, normEmission, normLand, normwaterWithdrawal, normFat, normProtein, normFiber, normCarbs, emission, land, waterWithdrawal, price, fat, protein, fiber, carb, priceNormalized));
}
//Sort according to most sustainable
Collections.sort(totalFoodList, new FoodComparator());
//Verify stored foods
for (foodObj f : totalFoodList) {
System.out.println(f.tostring());
}
scn.close();
}
private void averages()
{
float sumEmission = 0;
float sumLand = 0;
float sumWater = 0;
for (foodObj f : totalFoodList)
{
sumEmission += f.normEmission;
sumLand += f.normLand;
sumWater += f.normwaterWithdrawal;
}
avgEmission = sumEmission / rows;
avgLand = sumLand / rows;
avgWater = sumWater / rows;
}
public void getIdealFoods(int moneyPriority, ecoPriorityObj ecoPs, macroPriorityObj macPs) {
// here is where we do all ranking and return the ideal list of foods
//WILL NOT BE VOID -- CHANGE RETURN TYPE!!!
}
public double[] gradeWithSuggestions(List<String> inputFoods, double[] ranks) {
// parameters: List<String> is the list of the names of foods that the user selects
// double[] ranks is the values based on slider in the app.
double totalrank = ranks[0] + ranks[1] + ranks[2];
ranks[0] = ranks[0]/totalrank;
ranks[1] = ranks[1]/totalrank;
ranks[2] = ranks[2]/totalrank;
//System.out.println(totalFoodList.get(0).getName());
double[] gradeWithSuggestion = new double[inputFoods.size()];
int doublecount = 0;
int inputfoodscount = 0;
for (int i = 0; i < totalFoodList.size(); i++) {
//System.out.println(i);
if((totalFoodList.get(i).getName().equals(inputFoods.get(inputfoodscount)))){
double temp1 = totalFoodList.get(i).normEmission - avgEmission;
double temp2 = totalFoodList.get(i).normLand - avgLand;
double temp3 = totalFoodList.get(i).normwaterWithdrawal - avgWater;
gradeWithSuggestion[doublecount] = temp1 * Math.pow((1.0 - ranks[0]), -1) + temp2 * Math.pow((1.0 - ranks[1]), -1) + temp3 * Math.pow((1.0 - ranks[2]), -1);
doublecount++;
inputfoodscount++;
i = 0;
if (doublecount == inputFoods.size()) {
break;
}
}
}
return gradeWithSuggestion;
}
public ArrayList<String> getNames() {
ArrayList<String> names = new ArrayList<String>();
for (foodObj f: totalFoodList)
{
names.add(f.getName());
}
return names;
}
}
/*
* Class used for organizing foods in foodlist based on their ecoScore
*/
class FoodComparator implements Comparator<foodObj> {
@Override
public int compare(foodObj a, foodObj b)
{
return (a.getEcoScore() > b.getEcoScore()) ? -1 : (a.getEcoScore() == b.getEcoScore()) ? 0 : 1;
}
}