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-2014 by Wayne Pollock, Tampa Florida USA.

import java.util.Date;

public class StudentPQ implements Comparable
{
   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.enrolledDate = new Date();
      // ...
   }
   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;
   }
   
   // Always return a copy of mutable objects:
   public Date getEnrolledDate () { return (Date) enrolledDate.clone(); }

   // 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 ( Object obj )  // Case insensitive
   {
      StudentPQ s = (StudentPQ) obj;
      return String.CASE_INSENSITIVE_ORDER.compare( studentID, s.studentID );
   }
}