Files
CS345-Software-Engineering/Homework 6/src/grading/io/CourseReader.java
2025-09-10 14:25:37 -04:00

104 lines
3.2 KiB
Java
Executable File

package grading.io;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import grading.*;
/**
* A utility class for reading in Grade data from a .grd file and constructing Categories and
* Courses.
*/
public class CourseReader
{
private static final String TAB_DELIMITER = "\t";
/**
* Empty constructor so the coverage tests stop yelling at me.
*/
public CourseReader()
{
}
/**
* @param in
* A BufferedReader for reading data from the file
* @param size
* The number of grade entries in a given category to read
* @param key
* The key for the category
* @param filter
* The filter to construct the Category with
* @return A Category containing all the grades read from the file
* @throws IOException
* @throws IllegalArgumentException
*/
public static CompositeGrade readCategory(final BufferedReader in, final int size,
final String key, final Filter filter) throws IOException, IllegalArgumentException
{
final List<Grade> grades = new ArrayList<Grade>();
for (int i = 0; i < size; i++)
{
final String[] gradeData = in.readLine().split(TAB_DELIMITER);
String gradeKey = gradeData[0];
String value = gradeData.length > 1 ? gradeData[1] : null;
grades.add(LeafGrade.parseLeafGrade(gradeKey, value));
}
CompositeGrade compositeGrade = new CompositeGrade(key);
compositeGrade.setFilter(filter);
compositeGrade.setStrategy(new TotalStrategy());
grades.forEach((grade) -> compositeGrade.add(grade));
return compositeGrade;
}
/**
* @param in
* A BufferedReader for reading data from the file
* @param size
* The number of categories in the file to read
* @return A Course consisting of all Categories read from the file
* @throws IOException
*/
public static CompositeGrade readCourse(final BufferedReader in, final int size)
throws IOException
{
final List<CompositeGrade> categories = new ArrayList<CompositeGrade>();
final Map<String, Double> weights = new HashMap<String, Double>();
for (int i = 0; i < size; i++)
{
final String[] categoryFields = in.readLine().split(TAB_DELIMITER);
final String categoryName = categoryFields[0];
final int gradeCount = Integer.parseInt(categoryFields[1]);
weights.put(categoryName, Double.parseDouble(categoryFields[2]));
final boolean shouldDropLowest = Boolean.parseBoolean(categoryFields[3]);
final boolean shouldDropHighest = Boolean.parseBoolean(categoryFields[4]);
final Filter filter = new DropFilter(shouldDropLowest, shouldDropHighest);
final CompositeGrade categoryGrade = CourseReader.readCategory(in, gradeCount, categoryName,
filter);
categories.add(categoryGrade);
}
final GradingStrategy strategy = new WeightedTotalStrategy(weights);
CompositeGrade courseGrade = new CompositeGrade("Course");
courseGrade.setStrategy(strategy);
categories.forEach((grade) -> courseGrade.add(grade));
return courseGrade;
}
}