gpaを計算するプログラムを作っています。私の変数には、グレードA +などが含まれます...
どんな助けでも大歓迎です、ありがとう
独自の列挙型クラスを作成します。
enum Grade {
A(4.0, "A"), B_PLUS(3.5, "B+"), B(3.0, "B"), // etc.
F(0, "F");
private float gradePoints;
private String asString;
private Grade(float gradePoints, String asString) {
this.gradePoints = gradePoints;
this.asString = asString;
}
public float getGradePoints() {
return gradePoints;
}
@Override
public String toString() {
return asString;
}
}
これは、ドメイン駆動設計の値オブジェクトの良い候補かもしれません。
public class GpaGrade {
private Int32 _inPercent;
public GpaGrade(Int32 inPercent) {
// TODO: check that inPercent is between 1 and 100
_inPercent = inPercent;
}
public Int32 AsPercents() {
return _inPercent;
}
public Double In40Scale() {
// TODO: convert to 4.0 scale
}
public String AsLetterCode() {
// TODO: convert to letter code
}
// TODO: override Equals and GetHashCode
}