/home/wpollock1/public_html/Java/PartialArray.java

// Demo of using partially filled arrays.  In this demo we will enroll
// and drop students from a course.  The course is represented by an
// array.  Note this is not a relistic example, but provided to show how
// to deal with partialy-filled arrays.

// Written 10/2020 by Wayne Pollock, Tampa Florida USA

public class PartialArray {
    // Test class Course's methods:
    public static void main ( String[] args ) {
        Course course = new Course( "COP-2800" );
        System.out.println( "Initial # Students should be zero: " +
            course.getNumStudentsEnrolled() );
        System.out.println( "Adding one Student:");
        course.enroll( new Student("Piffl, Hymie", 0001) );
        System.out.println( "\nClass Roster:\n" + course.showRoster() );
        System.out.print( "\nFind Student by id (0001): ");
        System.out.println( course.find(0001) );
        System.out.println( "\nDropping one Student:");
        course.drop( 0001 );
        System.out.println( "\nClass Roster:\n" + course.showRoster() );
    }
}

class Course {
    public final String NAME;
    static final int MAX_ENROLLMENT = 30;
    int currentEnrollment = 0;
    final Student[] roster = new Student[MAX_ENROLLMENT];

    public Course ( String name ) {
        NAME = name;
    }

    public void enroll ( Student student ) {
        // should check student not null and not already enrolled;
        // if problem then throw an exception
        if ( currentEnrollment < MAX_ENROLLMENT ) {
            roster[currentEnrollment] = student;
            ++currentEnrollment;
        }
        // else should throw an exception
    }

    public void drop ( int id ) {
        // Check student is enrolled; throw exception if not.
        // Short list so linear seach is fine:
        for ( int i = 0; i < currentEnrollment; ++i ) {
            if (roster[i].getId() == id ) {
                shiftLeftFrom( i );
                --currentEnrollment;
                break;
            }
        }
    }

    // To delete the i-th element from a partial array, you need to
    // shift all elements down one (starting from i+1)
    private void shiftLeftFrom ( int i ) {
        for ( int j = i+1; j < currentEnrollment; ++j ) {
            roster[j-1] = roster[j];
        }
    }

    public Student find ( int id ) {
        for ( int i = 0; i < currentEnrollment; ++i ) {
            if (roster[i].getId() == id ) {
                return roster[i];
            }
        }
        return null;
    }

    public String showRoster () {
        java.util.Arrays.sort( roster, 0, currentEnrollment );
        StringBuilder sb = new StringBuilder();
        for ( int i = 0; i < currentEnrollment;  ++i ) {
            sb.append("\t");
            sb.append( roster[i].toString() );
        }
        if ( sb.length() == 0 ) {
            sb.append( "\t*** No Students enrolled in " + NAME + " ***" );
        }
        sb.append("\n");
        return sb.toString();
    }

    public int getNumStudentsEnrolled () {
        return currentEnrollment;
    }
}

class Student implements Comparable {
    final String name;
    final int id;

    public Student( String name, int id ) {
        this.name = name;
        this.id = id;
    }

    public int getId () {
        return id;
    }

    public String getName () {
        return name;
    }

    public String toString() {
        return String.format("Name: %s; ID: %04d", name, id);
    }

    // Without this, we could not sort an array of Students so easily.
    public int compareTo ( Object obj ) {
        Student other = (Student) obj;
        return this.id - other.id;
    }
}