/home/wpollock1/public_html/AJava/Student.java
// Student.java - A demo of using optional constructor arguments by
// using overloaded constructors. Student objects represent students
// enrolled in a college course and have many attributes not shown.
// This is sometimes called the "telescoping constructor pattern".
// (Validity checking of arguments and other details omitted for clarity.)
// Note, this same technique can be used for methods with optional
// arguments as well.
//
// Written 2/2013 by Wayne Pollock, Tampa Florida USA.
// Updated 3/2021: Replaced mutable Date with immutable LocalDate.
import java.time.*;
class Student
{
private static int nextStudentID;
static {
// Typically fetched from a DB at application (JVM) startup:
// nextStudentID = ...;
nextStudentID = 1; // For demo purposes
}
// Add a shutdown hook to save the current value of nextStudentID
// to the DB when the application (the JVM) exits:
// ...
// Add a shutdown hook to save the current value of nextStudentID
// to the DB when the application (the JVM) exits:
// ...
private int studentID;
private String lastName;
private String firstName;
private String address;
private String homePhone;
private int level; // 1 = freshman, ... Should be an enum!
private LocalDate enrolled;
// ...
public Student ( String lastName )
{ this( lastName, "" ); }
public Student (String lastName, String firstName )
{ this( lastName, firstName, "Unknown", "", 1, LocalDate.now() ); }
public Student (String lastName, String firstName, String address )
{ this( lastName, firstName, address, "", 1, LocalDate.now() ); }
// This constructor takes an int, but a char "promotes" to an int, so
// the code "new Student ("Last", First", 'M')" calls this constructor,
// rather than cause a compiler error. The correct fix is to use enums
// for the gender field, but as enums haven't been covered yet, I will
// fix the bug by adding another constructor that takes a char:
public Student (String lastName, String firstName, int level )
{ this( lastName, firstName, "Unknown", "", level, LocalDate.now() ); }
//////////////////////////////////////////////////////////////////////
// Repeat for all allowed combinations of optional arguments! Note //
// not all combinations are possible (e.g., last, first, phone). //
// (How many constructors will there be?) //
//////////////////////////////////////////////////////////////////////
// This is the main constructor (which does all the real work),
// and which is ultimately invoked by all the other constructors:
public Student (String lastName, String firstName, String address,
String homePhone, int level, LocalDate enrolled )
{
this.studentID = nextStudentID; ++nextStudentID;
this.lastName = lastName;
this.firstName = firstName;
this.address = address;
this.homePhone = homePhone;
this.level = level;
this.enrolled = enrolled;
}
// public Student methods go here:
// ...
}
// Sample use (good luck figuring out which combinations and the
// order of arguments to use):
class StudentTest
{
public static void main ( String [] args ) {
Student s1 = new Student( "Doe" );
Student s2 = new Student( "Piffl", "Hymie", 1 );
// ...
}
}