Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.Calendar;
- public class Student {
- private static final int DEFAULT_VALUE_YOB = 2000;
- private static final double DEFAULT_VALUE_GRADE = 60;
- private static final int CURRENT_YEAR;
- static {
- // Set the current year value or just set is as final value 2020
- CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);
- // CURRENT_YEAR = 2020;
- }
- private final int id;
- private final String firstName;
- private final String lastName;
- private int YOB;
- private double grade;
- public Student(int id, String firstName, String lastName) {
- this.id = id;
- this.firstName = firstName;
- this.lastName = lastName;
- this.YOB = DEFAULT_VALUE_YOB;
- this.grade = DEFAULT_VALUE_GRADE;
- }
- // Method for manual testing purpose only. Should be replaced by test class StudentTest
- public static void main(String[] args) {
- Student student = new Student(1001, "SAMIR", "SALEM");
- student.setYOB(2002);
- student.setGrade(80);
- System.out.println(student); // Instead of using System.out.print() should use Logger class
- //todo Add other cases to test default value for YOB and grade
- }
- public int getId() {
- return id;
- }
- public String getFirstName() {
- return firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public int getYOB() {
- return YOB;
- }
- public void setYOB(int YOB) {
- boolean isYOBvalid = changeYOB(YOB);
- this.YOB = isYOBvalid ? YOB : DEFAULT_VALUE_YOB;
- }
- // In my opinion this method should be named: verifyYOB()
- private boolean changeYOB(int yob) {
- return (1940 <= YOB) && (YOB <= 2017);
- }
- public double getGrade() {
- return grade;
- }
- public void setGrade(double grade) {
- boolean isGradeValid = changeGrade(grade);
- this.grade = isGradeValid ? grade : DEFAULT_VALUE_GRADE;
- }
- // In my opinion this method should be named: verifyGrade()
- public boolean changeGrade(double g) {
- return (0 <= grade) && (grade <= 100);
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("NAME=").append(this.firstName.toUpperCase());
- sb.append(" ").append(this.lastName.toUpperCase());
- sb.append(", ID=").append(this.id);
- sb.append(", age=").append(CURRENT_YEAR - this.YOB);
- sb.append(", grade=").append(removeTrailingZero(this.grade));
- return sb.toString();
- }
- private String removeTrailingZero(double grade) {
- DecimalFormat df = new DecimalFormat("###.#");
- return df.format(this.grade);
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Student student = (Student) o;
- if (id != student.id) return false;
- if (YOB != student.YOB) return false;
- if (Double.compare(student.grade, grade) != 0) return false;
- if (!firstName.equals(student.firstName)) return false;
- return lastName.equals(student.lastName);
- }
- @Override
- public int hashCode() {
- int result;
- long temp;
- result = id;
- result = 31 * result + firstName.hashCode();
- result = 31 * result + lastName.hashCode();
- result = 31 * result + YOB;
- temp = Double.doubleToLongBits(grade);
- result = 31 * result + (int) (temp ^ (temp >>> 32));
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement