Advertisement
dawciobiel

Student

Apr 24th, 2020
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Calendar;
  3.  
  4. public class Student {
  5.  
  6.     private static final int DEFAULT_VALUE_YOB = 2000;
  7.     private static final double DEFAULT_VALUE_GRADE = 60;
  8.     private static final int CURRENT_YEAR;
  9.  
  10.     static {
  11.         // Set the current year value or just set is as final value 2020
  12.         CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);
  13. //      CURRENT_YEAR = 2020;
  14.     }
  15.  
  16.     private final int id;
  17.     private final String firstName;
  18.     private final String lastName;
  19.  
  20.     private int YOB;
  21.     private double grade;
  22.  
  23.     public Student(int id, String firstName, String lastName) {
  24.         this.id = id;
  25.         this.firstName = firstName;
  26.         this.lastName = lastName;
  27.         this.YOB = DEFAULT_VALUE_YOB;
  28.         this.grade = DEFAULT_VALUE_GRADE;
  29.     }
  30.  
  31.     // Method for manual testing purpose only. Should be replaced by test class StudentTest
  32.     public static void main(String[] args) {
  33.         Student student = new Student(1001, "SAMIR", "SALEM");
  34.         student.setYOB(2002);
  35.         student.setGrade(80);
  36.         System.out.println(student); // Instead of using System.out.print() should use Logger class
  37.        
  38.         //todo Add other cases to test default value for YOB and grade  
  39.     }
  40.  
  41.     public int getId() {
  42.         return id;
  43.     }
  44.  
  45.     public String getFirstName() {
  46.         return firstName;
  47.     }
  48.  
  49.     public String getLastName() {
  50.         return lastName;
  51.     }
  52.  
  53.     public int getYOB() {
  54.         return YOB;
  55.     }
  56.  
  57.     public void setYOB(int YOB) {
  58.         boolean isYOBvalid = changeYOB(YOB);
  59.         this.YOB = isYOBvalid ? YOB : DEFAULT_VALUE_YOB;
  60.     }
  61.  
  62.     // In my opinion this method should be named: verifyYOB()
  63.     private boolean changeYOB(int yob) {
  64.         return (1940 <= YOB) && (YOB <= 2017);
  65.     }
  66.  
  67.     public double getGrade() {
  68.         return grade;
  69.     }
  70.  
  71.     public void setGrade(double grade) {
  72.         boolean isGradeValid = changeGrade(grade);
  73.         this.grade = isGradeValid ? grade : DEFAULT_VALUE_GRADE;
  74.     }
  75.  
  76.     // In my opinion this method should be named: verifyGrade()
  77.     public boolean changeGrade(double g) {
  78.         return (0 <= grade) && (grade <= 100);
  79.     }
  80.  
  81.     @Override
  82.     public String toString() {
  83.         StringBuilder sb = new StringBuilder();
  84.         sb.append("NAME=").append(this.firstName.toUpperCase());
  85.         sb.append(" ").append(this.lastName.toUpperCase());
  86.         sb.append(", ID=").append(this.id);
  87.         sb.append(", age=").append(CURRENT_YEAR - this.YOB);
  88.         sb.append(", grade=").append(removeTrailingZero(this.grade));
  89.         return sb.toString();
  90.     }
  91.  
  92.     private String removeTrailingZero(double grade) {
  93.         DecimalFormat df = new DecimalFormat("###.#");
  94.         return df.format(this.grade);
  95.     }
  96.  
  97.     @Override
  98.     public boolean equals(Object o) {
  99.         if (this == o) return true;
  100.         if (o == null || getClass() != o.getClass()) return false;
  101.  
  102.         Student student = (Student) o;
  103.  
  104.         if (id != student.id) return false;
  105.         if (YOB != student.YOB) return false;
  106.         if (Double.compare(student.grade, grade) != 0) return false;
  107.         if (!firstName.equals(student.firstName)) return false;
  108.         return lastName.equals(student.lastName);
  109.     }
  110.  
  111.     @Override
  112.     public int hashCode() {
  113.         int result;
  114.         long temp;
  115.         result = id;
  116.         result = 31 * result + firstName.hashCode();
  117.         result = 31 * result + lastName.hashCode();
  118.         result = 31 * result + YOB;
  119.         temp = Double.doubleToLongBits(grade);
  120.         result = 31 * result + (int) (temp ^ (temp >>> 32));
  121.         return result;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement