Sample Student class with some production quality features

Download StudentPQ.java

// StudentPQ.java - A demo of using optional constructor arguments by
// using overloaded constructors.  Student objects represent students
// enrolled in a college course and have many properties, only a few of which
// are shown here.  The "PQ" in the name stands for "Production Quality".
// In this class I have added many of the features that a real-world Java
// class should have: standard methods defined for equals, hashCode, and
// toString; get and set methods for properties; a full set of constructors;
// Comparable interface defined; returning copies of mutable properties, etc.
// However, in a real PQ Student class we would automatically assign studentID
// numbers, not pass them to constructors.  We would also use enums and other
// types, instead of just strings and ints.
//
// Written 3/2005 by Wayne Pollock, Tampa Florida USA.
// Updated 3/2021: Replaced mutable Date with immutable LocalDate.

import java.time.*;

public class StudentPQ implements Comparable<StudentPQ> {
   private final String studentID;
   private String lastName;
   private String firstName;
   private String address;
   private String homePhone;
   private int level;  // 1 = freshman, ...
   private Date enrolledDate;
   private int GPA;    // This is GPA times 100, e.g., 400 == 4.0
   // ...              // Other properties go here
   public StudentPQ ( String studentID )
   {  this( studentID, "", "" );  }

   public StudentPQ ( String studentID, String lastName )
   {  this( studentID, lastName, "" );  }

   public StudentPQ (String studentID, String lastName, String firstName ) {
      this.studentID = studentID;
      this.lastName = lastName;
      this.firstName = firstName;

      this.address = "unknown";
      this.homePhone = "unknown";
      this.enrolled = LocalDate.now();
      // ...
   }
   public String getName ()   { return firstName + " " + lastName; }

   public int getGPA ()   { return GPA; }

   public void setGPA ( int GPA ) {
      if ( GPA < 0 || GPA > 400 )
         throw new IllegalArgumentException( "GPA of " + GPA
           + " must be between 0 and 400 (0.0 and 4.0)" );
      this.GPA = GPA;
   }
   
   public LocalDate getenrolled () { return enrolled; }

   // Define other get and set methods here...
   public String toString () {
      return "Name: " + firstName + " " + lastName
         + "\tGPA: " + Float.toString( GPA );
   }

   public boolean equals ( Object obj ) {  // Case insensitive
      if ( obj == this )   return true;
      if ( ! ( obj instanceof StudentPQ ) )  return false;

      StudentPQ s = (StudentPQ) obj;
      return studentID.equalsIgnoreCase( s.studentID );
   }

   public int hashCode ()  // Case insensitive
   {  return studentID.toUpperCase().hashCode();  }

   public int compareTo ( StudentPQ s ) {  // Case insensitive
      return String.CASE_INSENSITIVE_ORDER.compare( studentID, s.studentID );
   }
}