refactoring Student class grade field
@@ -4,19 +4,21 @@ import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
+import java.util.Date;
public class Student {
- private BigDecimal grade; //new representation
+ private StudentGrade grade; //new data representation
+// private int grade; //old data representation
private String name;
private int age;
- public int getGrade() {
- return grade.intValue(); //Backward compatibility
+ public void setGrade(int grade) {
+ this.grade = new StudentGrade(grade);
}
- public BigDecimal getBigDecimalGrade() {
- return grade;
+ public int getGrade() {
+ return this.grade.getGrade().intValue(); //int is returned for backward compatibility
}
public Connection getConnection() throws SQLException {
@@ -43,4 +45,23 @@ public class Student {
return this.name;
}
+ private class StudentGrade {
+ private BigDecimal grade = BigDecimal.ZERO;
+ private Date updatedAt;
+
+ public StudentGrade(int grade) {
+ this.grade = new BigDecimal(grade);
+ this.updatedAt = new Date();
+ }
+
+ public BigDecimal getGrade() {
+ return grade;
+ }
+
+ public Date getDate() {
+ return updatedAt;
+ }
+
+ }
+
}
