Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Write code that reads in your text file. Do NOT use Scanner. Then, the program must write the course objects using ObjectOutputStream (writeObject) to

Java

Write code that reads in your text file. Do NOT use Scanner. Then, the program must write the course objects using ObjectOutputStream (writeObject) to a new binary file. Turn in the program that converts the text file to binary with both binary and text files.

 image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed public static void main(String[] args) { CourseList myCourseList = new CourseList(); // Initialize scanner and file. Hard code file name File courseFile = new File("course.txt"); Scanner fileReader = null; // Try statement to catch exceptions / potential bugs. try { fileReader = new Scanner(courseFile); } catch (FileNotFoundException e) { System.out.println("Course(s) not found."); } while (fileReader != null && fileReader.hasNext()) { // Read in a line and parse into various attributes String courseCategory = fileReader.nextLine(); String courseName = fileReader.nextLine(); String crn = fileReader.nextLine(); // Loop to check type if (courseCategory.equals("English")) { String classType = fileReader.nextLine(); String academicClassification = fileReader.nextLine(); // Create ArrayList English tp = new English(courseCategory, courseName, crn, classType, academicClassification); myCourseList.addCourse(tp); } else if (courseCategory.equalsIgnoreCase("Math")) { String temp = fileReader.nextLine(); boolean stem = Boolean.parseBoolean(temp); String classType = fileReader.nextLine(); // Create ArrayList Math tp = new Math(courseCategory, courseName, crn, stem, classType); myCourseList.addCourse(tp); } else if (courseCategory.equalsIgnoreCase("History")) { String temp = fileReader.nextLine(); boolean eligible = Boolean.parseBoolean(temp); String classType = fileReader.nextLine(); // Create ArrayList History tp = new History(courseCategory, courseName, crn, eligible, classType); myCourseList.addCourse(tp); } else { System.out.println("Course unavailable " + courseCategory); } } Scanner sc = new Scanner(System.in); System.out.println("Course Main Menu: "); boolean done = false; while (!done) { System.out.println(""); System.out.println("Please enter a number to continue: "); System.out.println(" 1. View course list"); System.out.println(" 2. Search course by name"); System.out.println(" 3. Search course by CRN"); System.out.println(" 4. Remove course name"); System.out.println(" 5. Remove course CRN"); System.out.println(" 6. Exit"); String tp = sc.nextLine(); int menuInput = Integer.parseInt(tp); // Loop to check type user input. if (menuInput == 1) { myCourseList.printCourseList(); } else if (menuInput == 2) { System.out.println("Enter the name of the course: "); String id = sc.nextLine(); Course item = myCourseList.searchByCourse(id); if (item != null) { System.out.println(item); } else { System.out.println("Course not found."); } } else if (menuInput == 3) { System.out.println("Enter the CRN of the course: "); String id2 = sc.nextLine(); Course item2 = myCourseList.searchByCrn(id2); if (item2 != null) { System.out.println(item2); } else { System.out.println("Course not found."); } } else if (menuInput == 4) { System.out.println("Enter the name of the course to be removed from the list: "); String id3 = sc.nextLine(); myCourseList.removeCourse(id3); System.out.println(id3 + " has been removed from the list."); } else if (menuInput == 5) { System.out.println("Enter the CRN of the course to be removed from the list: "); String id4 = sc.nextLine(); myCourseList.removeCourseCrn(id4); System.out.println(id4 + " has been removed from the list."); } else { done = true; PrintWriter out = null; try { out = new PrintWriter(new File("course.txt")); System.out.println("List updated. Exiting program..."); } catch (FileNotFoundException e) { e.printStackTrace(); } for (Course r : myCourseList.getCourseList()) { out.println(r.getCourseCategory()); out.println(r.getCourseName()); out.println(r.getCrn()); if (r.getCourseCategory().equalsIgnoreCase("English")) { English e = (English) r; out.println(e.getClassType()); out.println(e.getAcademicClassification()); } else if (r.getCourseCategory().equalsIgnoreCase("Math")) { Math m = (Math) r; out.println(m.isStem()); out.println(m.classType()); } else if (r.getCourseCategory().equalsIgnoreCase("History")) { History h = (History) r; out.println(h.isEligible()); out.println(h.classType()); } else { System.out.println("Course category unknown " + r.getCourseCategory()); } } out.close(); } } sc.close(); } }
public class Math extends Course { private boolean isstem; private String classType; //Constructor public Math(String courseCategory, String courseName, String crn, boolean isstem, String classType) { super(courseCategory, courseName, crn); this.isStem = isstem; this.classType = classType; } //Getters / accessors public boolean isStem() { return isstem; } public String classType() { return classType; } //toString method @Override public String toString() { return super.toString() + | STEM Classification: + is Stem + | Class Type: + classType; } public class History extends Course { //Class attributes private boolean isEligible; private String classType; //Constructor public History (String courseCategory, String courseName, String crn, boolean isEligible, String classType) { super (courseCategory, courseName, crn); this.isEligible = isEligible; this.classType = classType; } //Getters / accessors public boolean isEligible() { return isEligible; } public String classType() { return classType; } //toString method @Override public String toString() { return super.toString() + " | Area E Eligible: " + isEligible + " | Class Type: " + classType; public class English extends Course { //Class attributes private String classType; private String academicClassification; //Constructor public English(String courseCategory, String courseName, String crn, String classType, String academicclassification) { super(courseCategory, courseName, crn); this.classType = classType; this.academicClassification = academicClassification; } //Getters accessors public String getClassType() { return classType; } public String getAcademicclassification() { return academicclassification; } //toString method @Override public String toString() { return super.toString() + " | Class Type: } + classType + " | Academic Classification: + academicClassification; public class Course { //Class attributes private String courseCategory; private String courseName; private String crn; //Constructor public Course(String courseCategory, String courseName, String crn) { super(); this.courseCategory = courseCategory; this.courseName = courseName; this.crn = crn; } //Getters / accessors public String getCourseCategory() { return courseCategory; } public String getCourseName() { return courseName; } public String getCrn() { return crn; } //toString method @Override public String toString() { return "Course Category: " + courseCategory + " | Course Name: } courseName + " | Course CRN: " + crn; } public class Math extends Course { private boolean isstem; private String classType; //Constructor public Math(String courseCategory, String courseName, String crn, boolean isstem, String classType) { super(courseCategory, courseName, crn); this.isStem = isstem; this.classType = classType; } //Getters / accessors public boolean isStem() { return isstem; } public String classType() { return classType; } //toString method @Override public String toString() { return super.toString() + | STEM Classification: + is Stem + | Class Type: + classType; } public class History extends Course { //Class attributes private boolean isEligible; private String classType; //Constructor public History (String courseCategory, String courseName, String crn, boolean isEligible, String classType) { super (courseCategory, courseName, crn); this.isEligible = isEligible; this.classType = classType; } //Getters / accessors public boolean isEligible() { return isEligible; } public String classType() { return classType; } //toString method @Override public String toString() { return super.toString() + " | Area E Eligible: " + isEligible + " | Class Type: " + classType; public class English extends Course { //Class attributes private String classType; private String academicClassification; //Constructor public English(String courseCategory, String courseName, String crn, String classType, String academicclassification) { super(courseCategory, courseName, crn); this.classType = classType; this.academicClassification = academicClassification; } //Getters accessors public String getClassType() { return classType; } public String getAcademicclassification() { return academicclassification; } //toString method @Override public String toString() { return super.toString() + " | Class Type: } + classType + " | Academic Classification: + academicClassification; public class Course { //Class attributes private String courseCategory; private String courseName; private String crn; //Constructor public Course(String courseCategory, String courseName, String crn) { super(); this.courseCategory = courseCategory; this.courseName = courseName; this.crn = crn; } //Getters / accessors public String getCourseCategory() { return courseCategory; } public String getCourseName() { return courseName; } public String getCrn() { return crn; } //toString method @Override public String toString() { return "Course Category: " + courseCategory + " | Course Name: } courseName + " | Course CRN: " + crn; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions