1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2025-01-31 23:42:45 +01:00

Fix grade regex (#91)

This commit is contained in:
Mikołaj Pich 2018-04-27 01:36:40 +02:00 committed by Rafał Borcz
parent 7dde13585c
commit 3443b01b9a
7 changed files with 64 additions and 52 deletions

View File

@ -10,8 +10,6 @@ import org.greenrobot.greendao.annotation.Property;
import java.io.Serializable; import java.io.Serializable;
import io.github.wulkanowy.R;
@Entity( @Entity(
nameInDb = "Grades", nameInDb = "Grades",
active = true active = true
@ -94,31 +92,6 @@ public class Grade implements Serializable {
this.read = read; this.read = read;
} }
public int getValueColor() {
String replacedString = value.replaceAll("[^0-9]", "");
if (!"".equals(replacedString)) {
switch (Integer.parseInt(replacedString)) {
case 6:
return R.color.six_grade;
case 5:
return R.color.five_grade;
case 4:
return R.color.four_grade;
case 3:
return R.color.three_grade;
case 2:
return R.color.two_grade;
case 1:
return R.color.one_grade;
default:
return R.color.default_grade;
}
}
return R.color.default_grade;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -18,7 +18,7 @@ import eu.davidea.viewholders.ExpandableViewHolder;
import io.github.wulkanowy.R; import io.github.wulkanowy.R;
import io.github.wulkanowy.data.db.dao.entities.Subject; import io.github.wulkanowy.data.db.dao.entities.Subject;
import io.github.wulkanowy.utils.AnimationUtils; import io.github.wulkanowy.utils.AnimationUtils;
import io.github.wulkanowy.utils.AverageCalculator; import io.github.wulkanowy.utils.GradeUtils;
public class GradeHeaderItem public class GradeHeaderItem
extends AbstractExpandableHeaderItem<GradeHeaderItem.HeaderViewHolder, GradesSubItem> { extends AbstractExpandableHeaderItem<GradeHeaderItem.HeaderViewHolder, GradesSubItem> {
@ -168,7 +168,7 @@ public class GradeHeaderItem
} }
private String getGradesAverageString(Subject item) { private String getGradesAverageString(Subject item) {
float average = AverageCalculator.calculate(item.getGradeList()); float average = GradeUtils.calculate(item.getGradeList());
if (average < 0) { if (average < 0) {
return resources.getString(R.string.info_no_average); return resources.getString(R.string.info_no_average);

View File

@ -16,6 +16,7 @@ import butterknife.OnClick;
import io.github.wulkanowy.R; import io.github.wulkanowy.R;
import io.github.wulkanowy.data.db.dao.entities.Grade; import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.utils.CommonUtils; import io.github.wulkanowy.utils.CommonUtils;
import io.github.wulkanowy.utils.GradeUtils;
public class GradesDialogFragment extends DialogFragment { public class GradesDialogFragment extends DialogFragment {
@ -76,7 +77,7 @@ public class GradesDialogFragment extends DialogFragment {
subject.setText(grade.getSubject()); subject.setText(grade.getSubject());
value.setText(grade.getValue()); value.setText(grade.getValue());
value.setBackgroundResource(grade.getValueColor()); value.setBackgroundResource(GradeUtils.getValueColor(grade.getValue()));
weight.setText(grade.getWeight()); weight.setText(grade.getWeight());
date.setText(grade.getDate()); date.setText(grade.getDate());
color.setText(CommonUtils.colorHexToColorName(grade.getColor())); color.setText(CommonUtils.colorHexToColorName(grade.getColor()));

View File

@ -18,6 +18,7 @@ import eu.davidea.flexibleadapter.items.AbstractSectionableItem;
import eu.davidea.viewholders.FlexibleViewHolder; import eu.davidea.viewholders.FlexibleViewHolder;
import io.github.wulkanowy.R; import io.github.wulkanowy.R;
import io.github.wulkanowy.data.db.dao.entities.Grade; import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.utils.GradeUtils;
public class GradesSubItem public class GradesSubItem
extends AbstractSectionableItem<GradesSubItem.SubItemViewHolder, GradeHeaderItem> { extends AbstractSectionableItem<GradesSubItem.SubItemViewHolder, GradeHeaderItem> {
@ -108,7 +109,7 @@ public class GradesSubItem
this.subjectAlertImage = subjectAlertImage; this.subjectAlertImage = subjectAlertImage;
value.setText(item.getValue()); value.setText(item.getValue());
value.setBackgroundResource(item.getValueColor()); value.setBackgroundResource(GradeUtils.getValueColor(item.getValue()));
date.setText(item.getDate()); date.setText(item.getDate());
description.setText(getDescriptionString()); description.setText(getDescriptionString());
alert.setVisibility(item.getRead() ? View.INVISIBLE : View.VISIBLE); alert.setVisibility(item.getRead() ? View.INVISIBLE : View.VISIBLE);

View File

@ -1,12 +1,18 @@
package io.github.wulkanowy.utils; package io.github.wulkanowy.utils;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.github.wulkanowy.R;
import io.github.wulkanowy.data.db.dao.entities.Grade; import io.github.wulkanowy.data.db.dao.entities.Grade;
public final class AverageCalculator { public final class GradeUtils {
private AverageCalculator() { private final static Pattern validGradePattern = Pattern.compile("^(\\++|-|--|=)?[0-6](\\++|-|--|=)?$");
private final static Pattern simpleGradeValuePattern = Pattern.compile("([0-6])");
private GradeUtils() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
@ -58,4 +64,33 @@ public final class AverageCalculator {
private static int getIntegerForWeightOfGrade(String weightOfGrade) { private static int getIntegerForWeightOfGrade(String weightOfGrade) {
return Integer.valueOf(weightOfGrade.substring(0, weightOfGrade.length() - 3)); return Integer.valueOf(weightOfGrade.substring(0, weightOfGrade.length() - 3));
} }
public static int getValueColor(String value) {
Matcher m1 = validGradePattern.matcher(value);
if (!m1.find()) {
return R.color.default_grade;
}
Matcher m2 = simpleGradeValuePattern.matcher(m1.group());
if (!m2.find()) {
return R.color.default_grade;
}
switch (Integer.parseInt(m2.group())) {
case 6:
return R.color.six_grade;
case 5:
return R.color.five_grade;
case 4:
return R.color.four_grade;
case 3:
return R.color.three_grade;
case 2:
return R.color.two_grade;
case 1:
return R.color.one_grade;
default:
return R.color.default_grade;
}
}
} }

View File

@ -7,21 +7,6 @@ import io.github.wulkanowy.R;
public class GradeTest { public class GradeTest {
@Test
public void getValueColorTest() {
Assert.assertEquals(R.color.six_grade, new Grade().setValue("-6").getValueColor());
Assert.assertEquals(R.color.five_grade, new Grade().setValue("--5").getValueColor());
Assert.assertEquals(R.color.four_grade, new Grade().setValue("=4").getValueColor());
Assert.assertEquals(R.color.three_grade, new Grade().setValue("3-").getValueColor());
Assert.assertEquals(R.color.two_grade, new Grade().setValue("2--").getValueColor());
Assert.assertEquals(R.color.two_grade, new Grade().setValue("2=").getValueColor());
Assert.assertEquals(R.color.one_grade, new Grade().setValue("1+").getValueColor());
Assert.assertEquals(R.color.one_grade, new Grade().setValue("+1").getValueColor());
Assert.assertEquals(R.color.default_grade, new Grade().setValue("Np").getValueColor());
Assert.assertEquals(R.color.default_grade, new Grade().setValue("7").getValueColor());
Assert.assertEquals(R.color.default_grade, new Grade().setValue("").getValueColor());
}
@Test @Test
public void equalsTest() { public void equalsTest() {
Assert.assertTrue(new Grade().setSubject("Religia").setValue("5") Assert.assertTrue(new Grade().setSubject("Religia").setValue("5")

View File

@ -6,9 +6,10 @@ import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import io.github.wulkanowy.R;
import io.github.wulkanowy.data.db.dao.entities.Grade; import io.github.wulkanowy.data.db.dao.entities.Grade;
public class AverageCalculatorTest { public class GradeUtilsTest {
@Test @Test
public void averageTest() { public void averageTest() {
@ -28,8 +29,8 @@ public class AverageCalculatorTest {
gradeList1.add(new Grade().setValue("5+").setWeight("10,00")); gradeList1.add(new Grade().setValue("5+").setWeight("10,00"));
gradeList1.add(new Grade().setValue("5").setWeight("10,00")); gradeList1.add(new Grade().setValue("5").setWeight("10,00"));
Assert.assertEquals(4.8f, AverageCalculator.calculate(gradeList), 0.0f); Assert.assertEquals(4.8f, GradeUtils.calculate(gradeList), 0.0f);
Assert.assertEquals(4.8f, AverageCalculator.calculate(gradeList1), 0.0f); Assert.assertEquals(4.8f, GradeUtils.calculate(gradeList1), 0.0f);
} }
@Test @Test
@ -37,6 +38,22 @@ public class AverageCalculatorTest {
List<Grade> gradeList = new ArrayList<>(); List<Grade> gradeList = new ArrayList<>();
gradeList.add(new Grade().setValue("np.").setWeight("1,00")); gradeList.add(new Grade().setValue("np.").setWeight("1,00"));
Assert.assertEquals(-1f, AverageCalculator.calculate(gradeList), 0.0f); Assert.assertEquals(-1f, GradeUtils.calculate(gradeList), 0.0f);
}
@Test
public void getValueColor() {
Assert.assertEquals(R.color.six_grade, GradeUtils.getValueColor("-6"));
Assert.assertEquals(R.color.five_grade, GradeUtils.getValueColor("--5"));
Assert.assertEquals(R.color.four_grade, GradeUtils.getValueColor("=4"));
Assert.assertEquals(R.color.three_grade, GradeUtils.getValueColor("3-"));
Assert.assertEquals(R.color.two_grade, GradeUtils.getValueColor("2--"));
Assert.assertEquals(R.color.two_grade, GradeUtils.getValueColor("2="));
Assert.assertEquals(R.color.one_grade, GradeUtils.getValueColor("1+"));
Assert.assertEquals(R.color.one_grade, GradeUtils.getValueColor("+1"));
Assert.assertEquals(R.color.default_grade, GradeUtils.getValueColor("6 (.XI)"));
Assert.assertEquals(R.color.default_grade, GradeUtils.getValueColor("Np"));
Assert.assertEquals(R.color.default_grade, GradeUtils.getValueColor("7"));
Assert.assertEquals(R.color.default_grade, GradeUtils.getValueColor(""));
} }
} }