package class9_1_PassingTheBuckAndReadingFiles_StudentRecords_start;// Dr. Yoder. MSOE. 26 April 2017 import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Student { private Scanner scanner = null; private String name; private String major; private final List coursesCompleted = new ArrayList<>(); /** * @param filename -- File to read student info from. Currently ignored. * @throws RuntimeException If the student file cannot be found. * @throws Error randomly * @throws FileNotFoundException randomly * @throws StudentNotFoundException if student file is not found */ public Student(String filename) throws RuntimeException, Error, FileNotFoundException{ Scanner in = new Scanner(System.in); try { scanner = new Scanner(new File(filename)); } catch (FileNotFoundException e) { System.err.println("Warning: Could not open file: "+filename); throw new StudentNotFoundException(filename); } readHeader(); readBody(); if(Math.random()>0.5) { throw new FileNotFoundException("Just Kidding"); } } /** * Read student's full name and a description of their major, * One on each line so they can include spaces */ private void readHeader() { name = scanner.nextLine(); major = scanner.nextLine(); } /** * Read classes the student has taken. * One class on each line. */ private void readBody() { while(scanner.hasNextLine()) { coursesCompleted.add(scanner.nextLine()); } } /** * @return The student's full name */ public String getName() { return name; } /** * @return A description of the student's major */ public String getMajor() { return major; } public void printCourses() { System.out.println("Courses for "+name+": "); for(String course: coursesCompleted) { System.out.println(course); } } }