First commit

This commit is contained in:
2025-09-10 14:25:37 -04:00
parent dfbabbd9cd
commit 1fdbcd7fb8
161 changed files with 13820 additions and 2 deletions

18
Homework 6/complete_01.grd Executable file
View File

@@ -0,0 +1,18 @@
4
Programming Assignments 6 0.40 true false
PA1 20
PA2 18
PA3 5
PA4 15
PA5 20
PA6 20
Homework Assignments 5 0.10 false false
HW1 20
HW2 5
HW3 0
HW4 10
HW5 15
Midterm Exam 1 0.20 false false
Midterm 80
Final Exam 1 0.30 false false
Final 75

18
Homework 6/complete_02.grd Executable file
View File

@@ -0,0 +1,18 @@
4
Programming Assignments 6 0.40 true false
PA1 20
PA2 20
PA3 20
PA4 20
PA5 20
PA6 20
Homework Assignments 5 0.10 false false
HW1 20
HW2 20
HW3 20
HW4 20
HW5 20
Midterm Exam 1 0.20 false false
Midterm 100
Final Exam 1 0.30 false false
Final 100

18
Homework 6/complete_03.grd Executable file
View File

@@ -0,0 +1,18 @@
4
Programming Assignments 6 0.40 true false
PA1 10
PA2 10
PA3 5
PA4 15
PA5 5
PA6 20
Homework Assignments 5 0.10 false false
HW1 20
HW2 0
HW3 0
HW4 10
HW5 15
Midterm Exam 1 0.20 false false
Midterm 60
Final Exam 1 0.30 false false
Final 45

BIN
Homework 6/cs111-screenshot.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

18
Homework 6/cs111.grd Executable file
View File

@@ -0,0 +1,18 @@
4
Programming Assignments 6 0.40 true false
PA1
PA2
PA3
PA4
PA5
PA6
Homework Assignments 5 0.10 false false
HW1
HW2
HW3
HW4
HW5
Midterm Exam 1 0.20 false false
Midterm
Final Exam 1 0.30 false false
Final

View File

@@ -0,0 +1,21 @@
5
Homework Assignments 5 0.25 true false
HW01 20
HW02 5
HW03 0
HW04 10
HW05 15
Quizzes 5 0.25 true true
Quiz01 12
Quiz02 8
Quiz03 20
Quiz04 13
Quiz05 20
Misc 2 0.10 false false
Attendance 40
Participation 50
Midterm Exams 2 0.20 false false
Midterm01 45
Midterm02 40
Final Exam 1 0.20 false false
Final 75

18
Homework 6/missing_all.grd Executable file
View File

@@ -0,0 +1,18 @@
4
Programming Assignments 6 0.40 true false
PA1
PA2
PA3
PA4
PA5
PA6
Homework Assignments 5 0.10 false false
HW1
HW2
HW3
HW4
HW5
Midterm Exam 1 0.20 false false
Midterm
Final Exam 1 0.30 false false
Final

View File

@@ -0,0 +1,18 @@
4
Programming Assignments 6 0.40 true false
PA1
PA2 20
PA3 5
PA4 15
PA5
PA6
Homework Assignments 5 0.10 false false
HW1 20
HW2
HW3 0
HW4
HW5 15
Midterm Exam 1 0.20 false false
Midterm
Final Exam 1 0.30 false false
Final

View File

@@ -0,0 +1,18 @@
4
Programming Assignments 6 0.40 true false
PA1 20
PA2 18
PA3 5
PA4 15
PA5
PA6 20
Homework Assignments 5 0.10 false false
HW1 20
HW2 5
HW3 0
HW4
HW5 15
Midterm Exam 1 0.20 false false
Midterm
Final Exam 1 0.30 false false
Final

View File

@@ -0,0 +1,18 @@
4
Programming Assignments 6 0.40 true false
PA1 20
PA2 18
PA3 5
PA4 15
PA5 20
PA6 20
Homework Assignments 5 0.10 false false
HW1 20
HW2 5
HW3
HW4 10
HW5 15
Midterm Exam 1 0.20 false false
Midterm 80
Final Exam 1 0.30 false false
Final 75

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

18
Homework 6/potterhj-cs111.grd Executable file
View File

@@ -0,0 +1,18 @@
4
Programming Assignments 6 0.40 true false
PA1 20
PA2 18
PA3 5
PA4 15
PA5 20
PA6 20
Homework Assignments 5 0.10 false false
HW1 20
HW2 5
HW3 0
HW4 10
HW5 15
Midterm Exam 1 0.20 false false
Midterm 80
Final Exam 1 0.30 false false
Final 75

View File

@@ -0,0 +1,36 @@
6
Programming Assignments 6 0.15 false false
PA01 20
PA02 18
PA03 5
PA04 15
PA05 20
PA06 20
Homework Assignments 5 0.15 false false
HW01 20
HW02 5
HW03 0
HW04 10
HW05 15
Labs 10 0.10 false false
Lab01 10
Lab02 5
Lab03 10
Lab04 10
Lab05 0
Lab06 10
Lab07 10
Lab08 10
Lab09 5
Lab10 10
Quizzes 5 0.10 false false
Quiz01 12
Quiz02 8
Quiz03 20
Quiz04 13
Quiz05 16
Midterm Exams 2 0.20 false false
Midterm01 45
Midterm02 40
Final Exam 1 0.30 false false
Final 75

View File

@@ -0,0 +1,76 @@
package app;
import grading.*;
import grading.io.*;
import gui.*;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.SwingUtilities;
/**
* An application for calculating the numeric grade for
* a course from the grades on individual assignments.
*
* @version 4.0
* @author Sagacious Media
*
*/
public class Gradient implements Runnable
{
private CompositeGrade course;
/**
* The entry point for the application.
*
* @param args args[0] contains the path (relative or absolute) to the .grd file
*/
public static void main(String[] args)
{
// Early exit
if ((args == null) || (args.length < 1))
{
System.err.println("You must enter a file name.");
System.exit(1);
}
try
{
BufferedReader in = new BufferedReader(new FileReader(args[0]));
String line = in.readLine();
int categories = Integer.parseInt(line);
CompositeGrade course = CourseReader.readCourse(in, categories);
in.close();
SwingUtilities.invokeAndWait(new Gradient(course));
}
catch (IOException ioe)
{
System.out.println("Unable to open file.");
System.exit(1);
}
catch (InterruptedException | InvocationTargetException ex)
{
System.out.println("Problem with the Event Dispatch Thread.");
System.exit(1);
}
}
/**
* Explicit Value Constructor.
*
* @param course The CompositeGrade to use
*/
public Gradient(CompositeGrade course)
{
this.course = course;
}
/**
* The code to run in the EventDispatchThread.
*/
@Override
public void run()
{
new GradientWindow(course);
}
}

View File

@@ -0,0 +1,72 @@
package grading;
/**
* Abstract implementation of Grade meant for extension with custom getValue implementations.
*/
public abstract class AbstractGrade implements Grade
{
private String key;
/**
* @param key
* The key for the Grade
* @throws IllegalArgumentException
*/
public AbstractGrade(final String key) throws IllegalArgumentException
{
if (key == null || key.isEmpty())
throw new IllegalArgumentException();
this.key = key;
};
/**
* @param other
* Another Grade object to compare to this grade
* @return The value 0 if other's value is equal to this Grade's value; -1 if this Grade's value
* is less than other's; and 1 if this Grade's value is greater than other's.
*/
public int compareTo(final Grade other)
{
Double value = this.getValue();
Double otherValue = other.getValue();
if (value == null)
{
return (otherValue == null) ? 0 : -1;
}
if (otherValue == null)
{
return 1;
}
return value.compareTo(otherValue);
}
/**
* @return The key of for this Grade
*/
public String getKey()
{
return key;
}
/**
* @return The value for this Grade
*/
public abstract Double getValue();
/**
* @return A string representation of the Grade
*/
public String toString()
{
Double value = this.getValue();
if (value == null)
{
return String.format("%s: NA", key);
}
return String.format("%s: %5.1f", key, value);
}
}

View File

@@ -0,0 +1,111 @@
package grading;
import java.util.ArrayList;
import java.util.List;
/**
* An aggregate implementation of Grade that allows for multiple grades to be treated as one.
*/
public class CompositeGrade extends AbstractGrade
{
private Filter filter;
private GradingStrategy strategy;
private List<Grade> components = new ArrayList<Grade>();
/**
* @param key
* The key for the CompositeGrade
* @throws IllegalArgumentException
*/
public CompositeGrade(final String key) throws IllegalArgumentException
{
super(key);
}
/**
* @param grade
* A sub-grade to add to the CompositeGrade
*/
public void add(final Grade grade)
{
components.add(grade);
}
/**
* @return The sub-grades that make up the CompositeGrade
*/
public List<Grade> getComponents()
{
return components;
}
/**
* @return The Filter that get's applied to the components of the CompositeGrade when calculating
* its value
*/
public Filter getFilter()
{
return filter;
}
/**
* @return The GradingStrategy used on the components of the CompositeGrade to calculate its value
*/
public GradingStrategy getStrategy()
{
return strategy;
}
/**
* @return The calculated value of the CompositeGrade using the Filter and GradingStrategy
* supplied
*/
public Double getValue()
{
if (strategy == null)
return null;
List<Grade> componentsToCalculate = components;
if (filter != null)
{
try
{
componentsToCalculate = filter.apply(components);
}
catch (SizeException e)
{
return null;
}
}
try
{
return strategy.calculate(getKey(), componentsToCalculate).getValue();
}
catch (SizeException e)
{
return null;
}
}
/**
* @param filter
* The filter to be applied to the components of the CompositeGrade when calculating its
* value
*/
public void setFilter(final Filter filter)
{
this.filter = filter;
}
/**
* @param strategy
* The GradingStrategy to be used on the components of the CompositeGrade to calculate
* its value
*/
public void setStrategy(final GradingStrategy strategy)
{
this.strategy = strategy;
}
}

View File

@@ -0,0 +1,79 @@
package grading;
import java.util.*;
/**
* A filter to apply to a list of grades to optionally drop the lowest or highest grades.
*/
public class DropFilter implements Filter
{
private boolean shouldDropLowest;
private boolean shouldDropHighest;
/**
* Default constructor that will drop the lowest and highest grades.
*/
public DropFilter()
{
this.shouldDropLowest = true;
this.shouldDropHighest = true;
}
/**
* @param shouldDropLowest
* Whether or not to drop the lowest grade
* @param shouldDropHighest
* Whether or not to drop the highest grade
*/
public DropFilter(final boolean shouldDropLowest, final boolean shouldDropHighest)
{
this.shouldDropLowest = shouldDropLowest;
this.shouldDropHighest = shouldDropHighest;
}
/**
* @param grades
* The list of grades to apply the DropFilter to.
* @return A new list containing the original grades with filter applied
*/
public List<Grade> apply(final List<Grade> grades) throws SizeException
{
int minSize = (shouldDropLowest ? 1 : 0) + (shouldDropHighest ? 1 : 0);
if (grades == null || grades.size() <= minSize)
{
throw new SizeException();
}
List<Grade> droppedGrades = new ArrayList<Grade>();
droppedGrades.addAll(grades);
Grade lowest = null;
Grade highest = null;
for (Grade grade : grades)
{
if (lowest == null || grade.compareTo(lowest) < 0)
{
lowest = grade;
}
if (highest == null || grade.compareTo(highest) > 0)
{
highest = grade;
}
}
if (shouldDropLowest)
{
droppedGrades.remove(lowest);
}
if (shouldDropHighest)
{
droppedGrades.remove(highest);
}
return droppedGrades;
}
}

View File

@@ -0,0 +1,17 @@
package grading;
import java.util.*;
/**
* An interface for filters that can be applied to a list of grades.
*/
public interface Filter
{
/**
* @param grades
* The list of grades to provide the filter to
* @return A new list contain the original grades with the filter applied
* @throws SizeException
*/
public List<Grade> apply(List<Grade> grades) throws SizeException;
}

View File

@@ -0,0 +1,17 @@
package grading;
/**
* An interface for implementing a grade with a key and a value to store the score.
*/
public interface Grade extends Comparable<Grade>
{
/**
* @return The key of for this Grade
*/
public String getKey();
/**
* @return The value for this Grade
*/
public Double getValue();
}

View File

@@ -0,0 +1,19 @@
package grading;
import java.util.*;
/**
* An interface for strategies of calculating a final grade from a list of grades.
*/
public interface GradingStrategy
{
/**
* @param key
* The key for the final calculated grade
* @param grades
* The list of grades to from which to calculate the final grade
* @return A new grade representing the final grade calculated from grades
* @throws SizeException
*/
public Grade calculate(String key, List<Grade> grades) throws SizeException;
}

View File

@@ -0,0 +1,74 @@
package grading;
/**
* A data class for storing a grade with a key and a value to store the score.
*/
public class LeafGrade extends AbstractGrade
{
private Double value;
/**
* @param key
* The type of grade
*/
public LeafGrade(final String key) throws IllegalArgumentException
{
this(key, 0.0);
}
/**
* @param key
* The type of grade
* @param value
* The score for the grade
*/
public LeafGrade(final String key, final double value) throws IllegalArgumentException
{
this(key, Double.valueOf(value));
}
/**
* @param key
* The type of grade
* @param value
* The score for the grade
*/
public LeafGrade(final String key, final Double value) throws IllegalArgumentException
{
super(key);
this.value = value;
}
/**
* @return The value of the grade
*/
public Double getValue()
{
return value;
}
/**
* @param key
* The key for the Grade
* @param value
* The string representation of the value for the Grade
* @return A new LeafGrade object with the passed key and value
* @throws IllegalArgumentException
*/
public static LeafGrade parseLeafGrade(final String key, final String value)
throws IllegalArgumentException
{
if (value == null)
return new LeafGrade(key, null);
try
{
Double parsedValue = Double.valueOf(value);
return new LeafGrade(key, parsedValue);
}
catch (NumberFormatException e)
{
return new LeafGrade(key, null);
}
}
}

View File

@@ -0,0 +1,35 @@
package grading;
/**
* A utility class for supplying a default double value in the case that a provided double is null.
*/
public class Missing
{
private static double DEFAULT_MISSING_VALUE = 0.0;
private Missing()
{
}
/**
* @param number
* The supplied double to check for null.
* @return Either number if it was not null, or a default value of 0.0 if it was.
*/
public static double doubleValue(final Double number)
{
return number != null ? number : DEFAULT_MISSING_VALUE;
}
/**
* @param number
* The supplied double to check for null.
* @param missingValue
* The default value to return if number was null
* @return Either number if it was not null, or missingValue if it was
*/
public static double doubleValue(final Double number, final double missingValue)
{
return number != null ? number : missingValue;
}
}

View File

@@ -0,0 +1,19 @@
package grading;
/**
* A custom exception for cases where a provided list does not meet a size requirement.
*/
public class SizeException extends RuntimeException
{
/**
* Exception identifier.
*/
static final long serialVersionUID = 1L;
/**
* Creates a new SizeException instance.
*/
public SizeException()
{
}
}

View File

@@ -0,0 +1,40 @@
package grading;
import java.util.List;
/**
* An implementation GradingStrategy that simply totals the scores for all grades.
*/
public class TotalStrategy implements GradingStrategy
{
/**
* Creates a new instance of TotalStrategy.
*/
public TotalStrategy()
{
}
/**
* @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 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)
{
total += Missing.doubleValue(grade.getValue());
}
return new LeafGrade(key, total);
}
}

View 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);
}
}

View File

@@ -0,0 +1,70 @@
package grading.io;
import grading.*;
import java.util.*;
/**
* An aggregate for Grade representing a specific type of assignment with a specific Filter and
* GradingStrategy.
*/
public class Category
{
private final String key;
private final List<Grade> grades;
private final Filter filter;
private final GradingStrategy strategy;
/**
* @param key
* The key for the Category
* @param grades
* The Grades in the category
* @param filter
* The filter to apply to the collection of grades
* @param strategy
* The strategy used to calculate the final Category Grade
*/
public Category(final String key, final List<Grade> grades, final Filter filter,
final GradingStrategy strategy)
{
if (key == null || key.isEmpty())
throw new IllegalArgumentException();
this.key = key;
this.grades = grades;
this.filter = filter;
this.strategy = strategy;
}
/**
* @return The filter for the Category
*/
public Filter getFilter()
{
return filter;
}
/**
* @return The GradingStrategy for the Category
*/
public GradingStrategy getStrategy()
{
return strategy;
}
/**
* @return The Grades making up the Category
*/
public List<Grade> getGrades()
{
return grades;
}
/**
* @return The key for the Category
*/
public String getKey()
{
return key;
}
}

View File

@@ -0,0 +1,41 @@
package grading.io;
import grading.GradingStrategy;
import java.util.List;
/**
* An aggregate for Categories with a specific GradingStrategy for calculating the final Course
* Grade.
*/
public class Course
{
private final GradingStrategy strategy;
private final List<Category> categories;
/**
* @param categories The Categories making up the Course
* @param strategy The strategy by which to calculate the final grade
*/
public Course(final List<Category> categories, final GradingStrategy strategy)
{
this.categories = categories;
this.strategy = strategy;
}
/**
* @return The Categories making up the Course
*/
public List<Category> getCategories()
{
return categories;
}
/**
* @return The strategy used for calculating the final Grade for the Course
*/
public GradingStrategy getStrategy()
{
return strategy;
}
}

View File

@@ -0,0 +1,103 @@
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;
}
}

View File

@@ -0,0 +1,72 @@
package gui;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import grading.CompositeGrade;
import grading.Grade;
import grading.LeafGrade;
/**
* A grouping of LeafGradeFields for a specific category of grades.
*/
public class CategoryField extends JPanel
{
private static final long serialVersionUID = 1L;
private List<LeafGradeField> fields;
private CompositeGrade template;
/**
* @param template The CompositeGrade to use as a default key and value.
*/
public CategoryField(final CompositeGrade template)
{
this.template = template;
this.fields = new ArrayList<LeafGradeField>();
setLayout(new FlowLayout(FlowLayout.LEFT));
setBorder(BorderFactory.createTitledBorder(template.getKey()));
for (Grade grade : template.getComponents())
{
if (grade instanceof LeafGrade)
{
LeafGradeField field = new LeafGradeField((LeafGrade) grade);
fields.add(field);
add(field);
}
}
}
/**
* @return A new Grade object representing the composite grade of all fields in the category.
*/
public Grade getGrade()
{
CompositeGrade grade = new CompositeGrade(getKey());
grade.setFilter(template.getFilter());
grade.setStrategy(template.getStrategy());
fields.forEach((field) -> grade.add(field.getGrade()));
return grade;
}
/**
* @return The key for this category
*/
public String getKey()
{
return template.getKey();
}
/**
* Clears any text from all grades in this category.
*/
public void reset()
{
fields.forEach((field) -> field.reset());
}
}

View File

@@ -0,0 +1,105 @@
package gui;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import grading.CompositeGrade;
import grading.Grade;
/**
* The main window for the Gradient Application.
*/
public class GradientWindow extends JFrame
{
private static final long serialVersionUID = 1L;
private List<CategoryField> categoryFields;
private CompositeGrade template;
/**
* @param template
* The CompositeGrade to use as a default key and value for the total calculated grade.
*/
public GradientWindow(final CompositeGrade template)
{
this.template = template;
this.categoryFields = new ArrayList<CategoryField>();
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Gradient by tamassno");
Container contentPane = getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
for (Grade grade : template.getComponents())
{
if (grade instanceof CompositeGrade)
{
CategoryField category = new CategoryField((CompositeGrade) grade);
contentPane.add(category);
categoryFields.add(category);
}
}
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
dispose();
}
});
fileMenu.add(exitItem);
JMenu editMenu = new JMenu("Edit");
JMenuItem calculateItem = new JMenuItem("Calculate");
calculateItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
JOptionPane.showMessageDialog(null, String.valueOf(calculateGrade().getValue()),
"Course Grade", JOptionPane.INFORMATION_MESSAGE);
}
});
editMenu.add(calculateItem);
JMenuItem resetItem = new JMenuItem("Reset");
resetItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent e)
{
categoryFields.forEach((field) -> field.reset());
}
});
editMenu.add(resetItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);
}
private Grade calculateGrade()
{
CompositeGrade grade = new CompositeGrade(template.getKey());
grade.setFilter(template.getFilter());
grade.setStrategy(template.getStrategy());
categoryFields.forEach((field) -> grade.add(field.getGrade()));
return grade;
}
}

View File

@@ -0,0 +1,92 @@
package gui;
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.BorderFactory;
import javax.swing.JTextField;
import grading.Grade;
import grading.LeafGrade;
/**
* A text field for entering grades.
*/
public class LeafGradeField extends JTextField implements FocusListener
{
private static final long serialVersionUID = 1L;
private LeafGrade template;
/**
* @param template
* The LeafGrade to use as a default key and value.
*/
public LeafGradeField(final LeafGrade template)
{
this.template = template;
addFocusListener(this);
setBorder(BorderFactory.createTitledBorder(template.getKey()));
setColumns(template.getKey().length() + 1);
Double gradeValue = template.getValue();
if (gradeValue != null)
{
setText(String.valueOf(gradeValue));
}
}
/**
* @return A new Grade object with the value entered in the text field.
*/
public Grade getGrade()
{
Double value;
try
{
value = Double.valueOf(getText());
}
catch (NumberFormatException e)
{
value = null;
}
return new LeafGrade(getKey(), value);
}
/**
* @return The key for this text field
*/
public String getKey()
{
return template.getKey();
}
/**
* Clears any text from the text field.
*/
public void reset()
{
setText("");
}
@Override
public Dimension getMaximumSize()
{
return getMinimumSize();
}
@Override
public void focusGained(final FocusEvent e)
{
selectAll();
}
@Override
public void focusLost(final FocusEvent e)
{
setCaretPosition(getDocument().getLength());
setSelectionStart(getSelectionEnd());
}
}