Skip to content

Commit 0c04435

Browse files
committed
Added calculation of participants and average for overview if it is not given
1 parent 7e672e4 commit 0c04435

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

app/src/main/java/de/mygrades/database/dao/Overview.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,36 @@ public void setGradeEntryHash(String gradeEntryHash) {
125125

126126
// KEEP METHODS - put your custom methods here
127127

128+
/**
129+
* Gets the amount of an section as int.
130+
* @param section The section to get
131+
* @return The amount of the section
132+
*/
133+
public int getSection(int section) {
134+
switch (section) {
135+
case 1:
136+
return nullToZero(section1);
137+
case 2:
138+
return nullToZero(section2);
139+
case 3:
140+
return nullToZero(section3);
141+
case 4:
142+
return nullToZero(section4);
143+
case 5:
144+
return nullToZero(section5);
145+
}
146+
// if a wrong section is passed return 0
147+
return 0;
148+
}
149+
150+
/**
151+
* Converts null to zero or returns the given value.
152+
* @param n The Integer object
153+
* @return 0 if it is null or the value as int
154+
*/
155+
private int nullToZero(Integer n) {
156+
return n == null ? 0 : n;
157+
}
128158

129159
/**
130160
* Updates Overview with values from other Overview.

app/src/main/java/de/mygrades/main/core/Transformer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ public Overview transformOverview(Double userGrade) throws ParseException {
114114
overview.setAverage(getDoubleProperty(xmlDocument, OVERVIEW_AVERAGE));
115115
overview.setUserSection((int) Math.round(userGrade));
116116

117+
// calculate participants from the sections if it is not given
118+
if (overview.getParticipants() == null) {
119+
int participants = overview.getSection(1) + overview.getSection(2) + overview.getSection(3)
120+
+ overview.getSection(4) + overview.getSection(5);
121+
overview.setParticipants(participants);
122+
}
123+
124+
// calculate average from the sections and the participants if it is not given
125+
if (overview.getAverage() == null) {
126+
if (overview.getParticipants() != null && overview.getParticipants() != 0) {
127+
double average = (overview.getSection(1) + overview.getSection(2) * 2 + overview.getSection(3) * 3
128+
+ overview.getSection(4) * 4 + overview.getSection(5) * 5) / (double)overview.getParticipants();
129+
overview.setAverage(average);
130+
}
131+
132+
}
133+
117134
return overview;
118135
}
119136

0 commit comments

Comments
 (0)