Skip to content

Commit

Permalink
Added calculation of participants and average for overview if it is n…
Browse files Browse the repository at this point in the history
…ot given
  • Loading branch information
jonastheis committed Aug 23, 2016
1 parent 7e672e4 commit 0c04435
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/src/main/java/de/mygrades/database/dao/Overview.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,36 @@ public void setGradeEntryHash(String gradeEntryHash) {

// KEEP METHODS - put your custom methods here

/**
* Gets the amount of an section as int.
* @param section The section to get
* @return The amount of the section
*/
public int getSection(int section) {
switch (section) {
case 1:
return nullToZero(section1);
case 2:
return nullToZero(section2);
case 3:
return nullToZero(section3);
case 4:
return nullToZero(section4);
case 5:
return nullToZero(section5);
}
// if a wrong section is passed return 0
return 0;
}

/**
* Converts null to zero or returns the given value.
* @param n The Integer object
* @return 0 if it is null or the value as int
*/
private int nullToZero(Integer n) {
return n == null ? 0 : n;
}

/**
* Updates Overview with values from other Overview.
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/de/mygrades/main/core/Transformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ public Overview transformOverview(Double userGrade) throws ParseException {
overview.setAverage(getDoubleProperty(xmlDocument, OVERVIEW_AVERAGE));
overview.setUserSection((int) Math.round(userGrade));

// calculate participants from the sections if it is not given
if (overview.getParticipants() == null) {
int participants = overview.getSection(1) + overview.getSection(2) + overview.getSection(3)
+ overview.getSection(4) + overview.getSection(5);
overview.setParticipants(participants);
}

// calculate average from the sections and the participants if it is not given
if (overview.getAverage() == null) {
if (overview.getParticipants() != null && overview.getParticipants() != 0) {
double average = (overview.getSection(1) + overview.getSection(2) * 2 + overview.getSection(3) * 3
+ overview.getSection(4) * 4 + overview.getSection(5) * 5) / (double)overview.getParticipants();
overview.setAverage(average);
}

}

return overview;
}

Expand Down

0 comments on commit 0c04435

Please sign in to comment.