First commit
This commit is contained in:
61
Homework 8/src/grading/WeightedTotalStrategy.java
Executable file
61
Homework 8/src/grading/WeightedTotalStrategy.java
Executable file
@@ -0,0 +1,61 @@
|
||||
package grading;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* An implementation GradingStrategy that applies weights to specific grade keys and weighs each
|
||||
* grade before summing a total.
|
||||
*/
|
||||
public class WeightedTotalStrategy implements GradingStrategy
|
||||
{
|
||||
private Map<String, Double> weights;
|
||||
|
||||
/**
|
||||
* Creates a new instance WeightedTotalStrategy.
|
||||
*/
|
||||
public WeightedTotalStrategy()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance WeightedTotalStrategy.
|
||||
*
|
||||
* @param weights
|
||||
* A map of keys and weights to be applied to the grades during calculation.
|
||||
*/
|
||||
public WeightedTotalStrategy(final Map<String, Double> weights)
|
||||
{
|
||||
this.weights = weights;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* The key of the final totaled grade
|
||||
* @param grades
|
||||
* The list of grades to calculate the final total grade from
|
||||
* @return A new grade representing the total of the weighted grades
|
||||
*/
|
||||
public Grade calculate(final String key, final List<Grade> grades) throws SizeException
|
||||
{
|
||||
if (grades == null || grades.isEmpty())
|
||||
{
|
||||
throw new SizeException();
|
||||
}
|
||||
|
||||
Double total = 0.0;
|
||||
|
||||
for (Grade grade : grades)
|
||||
{
|
||||
Double weight = 1.0;
|
||||
if (weights != null && weights.containsKey(grade.getKey()))
|
||||
{
|
||||
Double mapWeight = weights.get(grade.getKey());
|
||||
weight = Math.max(mapWeight, 0.0);
|
||||
}
|
||||
|
||||
total += Missing.doubleValue(grade.getValue()) * weight;
|
||||
}
|
||||
|
||||
return new LeafGrade(key, total);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user