import org.w3c.dom.*;
import java.util.*;
/**
* This object tracks a student's classes and grades
* received. An instance of this class knows both how
* to create itself from and represent itself as a
* DOM tree.
*/
public class Grades implements java.io.Serializable {
protected Vector _svClassDesc = null;
public Vector _svGrade = null;
/**
* Grades constructor comment.
*/
public Grades() {
super();
}
/**
* Append a grade to the list of class descriptions
* @param sGrade java.lang.String
*/
public void addClassDesc(String sClassDesc) {
_svClassDesc.addElement(sClassDesc);
}
/**
* Append a grade to the list of grades
* @param sGrade java.lang.String
*/
public void addGrade(String sGrade) {
_svGrade.addElement(sGrade);
}
/**
* Return a document fragment representing this object.
* We're simply going to do
* Description of class for all grades.
* @return org.w3c.dom.DocumentFragment
*/
public org.w3c.dom.DocumentFragment getAsDOM(Document doc) {
DocumentFragment dfResult = doc.createDocumentFragment();
dfResult.appendChild(doc.createComment("Begin Grades.getAsDOM()"));
for (int i = 0; i < iGradeCount(); i++) {
Element e = doc.createElement("GRADE");
e.setAttribute("VALUE", getGrade(i));
Text textDesc = doc.createTextNode(getClassDesc(i));
e.appendChild(textDesc);
dfResult.appendChild(e);
}
dfResult.appendChild(doc.createComment("End Grades.getAsDOM()"));
return dfResult;
}
/**
* Return the description of class i.
* @return java.lang.String
* @param i int
*/
public String getClassDesc(int i) {
return (String)(_svClassDesc.elementAt(i));
}
/**
* Return the grade received in the class. Blank if
* class is in progress.
* @param i int
*/
public String getGrade(int i) {
return (String)(_svGrade.elementAt(i));
}
/**
* Return the grade point average for all grades.
* A=4, B=3, C=2, D=1, F=0.
* @return double
*/
public double getGradePointAverage() {
int i = 0;
double total = 0.0;
for (i = 0; i < iGradeCount(); i++) {
String sGrade = getGrade(i);
if (sGrade.length() > 0) {
switch (getGrade(i).charAt(0)) {
case 'A' :
total += 4;
break;
case 'B' :
total += 3;
break;
case 'C' :
total += 2;
break;
case 'D' :
total += 1;
break;
}
}
}
if (i > 0) {
return total / i;
} else {
return -1.0; // Unknown
}
}
/**
* Return the number of classes taken
* @return int
*/
public int iGradeCount() {
return (_svGrade == null) ? 0 : _svGrade.size();
}
/**
* Print this object in human-readable format
*/
public void print() {
System.out.println("Grade\tCourse Description\n-----\t------------------");
for (int i = 0; i < iGradeCount(); i++) {
System.out.println(getGrade(i) + "\t\t" + getClassDesc(i));
}
System.out.println("End of grade report");
}
/**
* Initializes this instance based on contents of DOM document tree.
* @param n org.w3c.dom.Node
*/
public void setAsDOM(Element n) {
Element topNode = n;
// We accept ,
// and also accept . This little piece of
// code is just to figure out if we have to eliminate a "" node.
if (n.getTagName().equals("Property")) {
NodeList nl = n.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
if (nl.item(i) instanceof Element &&
(((Element) (nl.item(i))).getTagName().equals("JavaBean")))
topNode = (Element) (nl.item(i));
}
}
// Clear out any old values, or create for the first time
_svGrade = new Vector();
_svClassDesc = new Vector();
// For each element, get the grade and description and set them
NodeList kids = topNode.getChildNodes();
for (int i = 0; i < kids.getLength(); i++) {
Node nThisNode = kids.item(i);
// Be sure it's a "GRADE" Element
if (!(nThisNode instanceof Element))
continue;
Element eThisElem = (Element) nThisNode;
if (!eThisElem.getTagName().toUpperCase().equals("GRADE"))
continue;
// Get grade and description
String sGrade = eThisElem.getAttribute("VALUE");
// If the first node isn't Text, ignore it.
nThisNode = eThisElem.getFirstChild();
if (!(nThisNode instanceof Text))
continue;
String sClassDesc = ((Text) nThisNode).getData();
addGrade(sGrade);
addClassDesc(sClassDesc);
}
}
/**
* Set description for class number i
* @param i int
*/
public void setClassDesc(int i, String s) {
if (_svClassDesc.size() < i) {
_svClassDesc.setSize(i + 1);
}
_svClassDesc.setElementAt(s, i);
}
/**
* Set grade for class number i
* @param i int
*/
public void setGrade(int i, String s) {
if (_svGrade.size() < i) {
_svGrade.setSize(i + 1);
}
_svGrade.setElementAt(s, i);
}
}