import java.util.ArrayList; import java.util.Scanner; import java.io.File; import java.io.FileReader; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; import java.net.MalformedURLException; import java.io.InputStream; class Student { private void checkValidity( ) { if( pantherID.length( ) != 7 ) throw new IllegalArgumentException( "PantherID is 7 digits!" ); for( int i = 0; i < pantherID.length( ); i++ ) if( Character.isDigit( pantherID.charAt( i ) ) == false ) throw new IllegalArgumentException( "ID not a number" ); if( usedIDs.contains( pantherID ) ) throw new IllegalArgumentException( "ID already used" ); if( gpa < 0 ) throw new IllegalArgumentException( "GPA out of bounds" ); gpa = Math.min( gpa, 4.0 ); checkMajor( major ); // This Student is successfully constructed usedIDs.add( pantherID ); // add this pantherID to the used list } public static ArrayList getAllIDs( ) { return usedIDs; } public Student( String p, String ln, String fn, String m, double g ) { pantherID = p; lastName = ln; firstName = fn; major = m; gpa = g; checkValidity( ); } /** * * @param studentInfo: in format * ID LN FN MAJOR GPA */ public Student( String studentInfo ) { Scanner sc = new Scanner( studentInfo ); pantherID = sc.next( ); lastName = sc.next( ); firstName = sc.next( ); major = sc.next( ); gpa = sc.nextDouble( ); checkValidity( ); } public String getName( ) { return firstName + " " + lastName; } public String getPantherID( ) { return pantherID; } public double getGPA( ) { return gpa; } public String getMajor( ) { return major; } public void changeMajor( String newMajor ) { checkMajor( newMajor ); major = newMajor; } public boolean equals( Object other ) { if( other instanceof Student == false ) return false; Student rhs = (Student) other; return pantherID.equals( rhs.pantherID ); } public String toString( ) { return getName( ) + " " + "(" + pantherID + ")" + " majoring in " + major + " with " + gpa + " gpa"; } private String pantherID; private String lastName; private String firstName; private String major; private double gpa; private static ArrayList usedIDs = new ArrayList( ); private void checkMajor( String proposedMajor ) { for( String m : validMajors ) if( m.equals( proposedMajor ) ) return; // found a major throw new IllegalArgumentException( "Invalid major " + proposedMajor ); } public static final String [ ] validMajors = { "CS", "IT", "MIS" }; } class Day05 { public static void processData( Scanner sc, String fileName ) { ArrayList arr = new ArrayList( ); System.out.println( "ALL VALID PANTHER IDS: " ); System.out.println( Student.getAllIDs( ) ); System.out.println( "Reading from " + fileName ); while( sc.hasNextLine( ) ) arr.add( new Student( sc.nextLine( ) ) ); System.out.println( "Read " + arr.size( ) + " students" ); for( Student s : arr ) System.out.println( s ); System.out.println( "DEAN'S LIST" ); for( Student s : arr ) if( s.getGPA( ) >= 3.75 ) System.out.println( s ); System.out.println( "ALL VALID PANTHER IDS: " ); System.out.println( Student.getAllIDs( ) ); } public static void main( String [ ] args ) { final String resourceName = // "http://users.cis.fiu.edu/~weiss/cop3804/Day04.txt"; "Day04.txt"; try { //URL url = new URL( resourceName ); //Scanner sc = new Scanner( url.openStream( ) ); Scanner sc = new Scanner( new File( resourceName ) ) ; processData( sc, resourceName ); } catch( IOException e ) { System.out.println( "Error processing " + resourceName ); } } }