Guided Task: Create ActivityRecordIO
CourseRecordIO handles reading course records from a file to create a course catalog and writing Courses to a file. Now that you have Events that should also be written to an output file (as per [UC10], you need to create a new IO class to handle writing Activity objects.
Learning Outcomes
- Refactor file I/O functionality
Create ActivityRecordIO Class
Create a new Java class called ActivityRecordIO in the edu.ncsu.csc216.wolf_scheduler.io package of the WolfScheduler project. Do not create the constructor or any methods in ActivityRecordIO.
Move writeCourseRecords() to ActivityRecordIO
Complete the following steps to move writeCourseRecords() from CourseRecordIO to ActivityRecordIO.
- Open
CourseRecordIOin the editor. - Select the
writeCourseRecordsmethod name in the editor. You only want to select the method name.
- Right click on the
writeCourseRecordsmethod name and select Refactor > Move. - Browse for
ActivityRecordIO. Uncheck the options to keep original member as delegate to moved member.
- The method is now in
ActivityRecordIO.WolfSchedulerandCourseRecordIOTesthave been udpated with the change. - Refactor the method name in
ActivityRecordIO. Select the method name, right click on the method and select Refactor > Rename. EnterwriteActivityRecordsand click Enter. - Update the generic parameter of the
ArrayListto beActivityinstead ofCourse. - Update the local variables in the for loop. You can use the keyboard short cut of Alt + Shift + R to do the rename refactoring.
- Change
ctoa - Change
coursestoactivities
- Change
- Delete the import for
Coursesince it is no longer needed.
At this point, there will be a compilation error in WolfScheduler.exportSchedule(). That’s ok. We’ll fix it later.
The complication error in CourseRecordIOTest we’ll fix after you check that ActivityRecordIO is complete.
After you are done, ActivityRecordIO should look like the following:
package edu.ncsu.csc216.wolf_scheduler.io;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import edu.ncsu.csc216.wolf_scheduler.course.Activity;
/**
* Writes activities to file.
* @author Sarah Heckman
*/
public class ActivityRecordIO {
/**
* Writes the given list of Courses to
* @param fileName file to save to
* @param activities list of course to save
* @throws IOException if the file cannot be written
*/
public static void writeActivityRecords(String fileName, ArrayList<Activity> activities) throws IOException {
PrintStream fileWriter = new PrintStream(new File(fileName));
for (Activity a : activities) {
fileWriter.println(a.toString());
}
fileWriter.close();
}
}
Update CourseRecordIOTest
There is a call to ActivityRecordIO in CourseRecordIOTest that is not compiling. This is because we changed the parameter type from an ArrayList of Courses to an ArrayList of Activitys. We’ll need to update the ArrayList generic type in our test.
Update the first line of the test method to ArrayList<Activity> courses = new ArrayList<Activity>();. You’ll need to import Activity for the test to compile.
Run Tests
Run your tests! You’ll get a warning because WolfScheduler is not compiling, but you can run anyway to make sure that CourseRecordIOTest passes. If any are still failing in the course or io packages, use the debugger to help you find the problem. For now, the WolfScheduler tests may fail due to compilation errors.
However, your suite of tests is not sufficient to evaluate if events are correctly written to file. Create a class ActivityRecordIOTest the edu.ncsu.csc216.wolf_scheduler.io package of the test/ folder in the WolfScheduler project. Copy in the provided ActivityRecordIOTest code into ActivityRecordIOTest. Create a new text file in the test-file/ directory called expected_activity_records.txt and copy the provided expected_activity_records.txt. Run the tests. Ensure they all pass!
Comment ActivityRecordIO and Fix Static Analysis Notifications
Complete the following tasks:
- Update all your Javadoc for
ActivityRecordIO. - Resolve all static analysis notifications.
Reference: Staging and Pushing to GitHub
GitHub Resources:
Check Your Progress
Complete the following tasks before pushing your work to GitHub.
- Make sure that all fields, methods, and constructors are commented.
- Resolve all static analysis notifications.
- Fix test failures.
- Commit and push your code changes with a meaningful commit message. Label your commit with “[Implementation]” for future you!