Grades.java
001 import org.w3c.dom.*;
002 import java.util.*;
003 
004 /**
005  * This object tracks a student's classes and grades
006  * received. An instance of this class knows both how
007  * to create itself from and represent itself as a
008  * DOM tree.
009  */
010 public class Grades implements java.io.Serializable {
011 	protected Vector _svClassDesc = null;
012 	public Vector _svGrade = null;
013 /**
014  * Grades constructor comment.
015  */
016 public Grades() {
017 	super();
018 }
019 /**
020  * Append a grade to the list of class descriptions
021  * @param sGrade java.lang.String
022  */
023 public void addClassDesc(String sClassDesc) {
024 	_svClassDesc.addElement(sClassDesc);
025 }
026 /**
027  * Append a grade to the list of grades
028  * @param sGrade java.lang.String
029  */
030 public void addGrade(String sGrade) {
031 	_svGrade.addElement(sGrade);
032 }
033 /**
034  * Return a document fragment representing this object.
035  * We're simply going to do
036  *   Description of class for all grades.
037  * @return org.w3c.dom.DocumentFragment
038  */
039 public org.w3c.dom.DocumentFragment getAsDOM(Document doc) {
040 	DocumentFragment dfResult = doc.createDocumentFragment();
041 
042 	dfResult.appendChild(doc.createComment("Begin Grades.getAsDOM()"));
043 	
044 	for (int i = 0; i < iGradeCount(); i++) {
045 		
046 		Element e = doc.createElement("GRADE");
047 		e.setAttribute("VALUE", getGrade(i));
048 		
049 		Text textDesc = doc.createTextNode(getClassDesc(i));
050 		
051 		e.appendChild(textDesc);
052 		
053 		dfResult.appendChild(e);
054 	}
055 	dfResult.appendChild(doc.createComment("End Grades.getAsDOM()"));
056 
057 	return dfResult;
058 }
059 /**
060  * Return the description of class i.
061  * @return java.lang.String
062  * @param i int
063  */
064 public String getClassDesc(int i) {
065 	return (String)(_svClassDesc.elementAt(i));
066 }
067 /**
068  * Return the grade received in the class. Blank if
069  * class is in progress.
070  * @param i int
071  */
072 public String getGrade(int i) {
073 	return (String)(_svGrade.elementAt(i));
074 }
075 /**
076  * Return the grade point average for all grades.
077  * A=4, B=3, C=2, D=1, F=0.
078  * @return double
079  */
080 public double getGradePointAverage() {
081 	int i = 0;
082 	double total = 0.0;
083 	
084 	for (i = 0; i < iGradeCount(); i++) {
085 		String sGrade = getGrade(i);
086 		if (sGrade.length() > 0) {
087 			switch (getGrade(i).charAt(0)) {
088 				case 'A' :
089 					total += 4;
090 					break;
091 				case 'B' :
092 					total += 3;
093 					break;
094 				case 'C' :
095 					total += 2;
096 					break;
097 				case 'D' :
098 					total += 1;
099 					break;
100 			}
101 		}
102 	}
103 	
104 	if (i > 0) {
105 		return total / i;
106 	} else {
107 		return -1.0; 	// Unknown
108 	}
109 }
110 /**
111  * Return the number of classes taken
112  * @return int
113  */
114 public int iGradeCount() {
115 	return (_svGrade == null) ? 0 : _svGrade.size();
116 }
117 /**
118  * Print this object in human-readable format
119  */
120 public void print() {
121 	System.out.println("Grade\tCourse Description\n-----\t------------------");
122 	for (int i = 0; i < iGradeCount(); i++) {
123 		System.out.println(getGrade(i) + "\t\t" + getClassDesc(i));
124 	}
125 	System.out.println("End of grade report");
126 }
127 /**
128  * Initializes this instance based on contents of DOM document tree.
129  * @param n org.w3c.dom.Node
130  */
131 public void setAsDOM(Element n) {
132 	Element topNode = n;
133 
134 	// We accept ,
135 	// and also accept . This little piece of
136 	// code is just to figure out if we have to eliminate a "" node.
137 	if (n.getTagName().equals("Property")) {
138 		NodeList nl = n.getChildNodes();
139 		for (int i = 0; i < nl.getLength(); i++) {
140 			if (nl.item(i) instanceof Element &&
141 				(((Element) (nl.item(i))).getTagName().equals("JavaBean")))
142 				topNode = (Element) (nl.item(i));
143 		}
144 	}
145 
146 
147 	// Clear out any old values, or create for the first time
148 	_svGrade = new Vector();
149 	_svClassDesc = new Vector();
150 
151 	// For each element, get the grade and description and set them
152 	NodeList kids = topNode.getChildNodes();
153 	for (int i = 0; i < kids.getLength(); i++) {
154 		Node nThisNode = kids.item(i);
155 
156 		// Be sure it's a "GRADE" Element
157 		if (!(nThisNode instanceof Element))
158 			continue;
159 		Element eThisElem = (Element) nThisNode;
160 		if (!eThisElem.getTagName().toUpperCase().equals("GRADE"))
161 			continue;
162 
163 		// Get grade and description
164 		String sGrade = eThisElem.getAttribute("VALUE");
165 
166 		// If the first node isn't Text, ignore it.
167 		nThisNode = eThisElem.getFirstChild();
168 		if (!(nThisNode instanceof Text))
169 			continue;
170 		String sClassDesc = ((Text) nThisNode).getData();
171 		addGrade(sGrade);
172 		addClassDesc(sClassDesc);
173 	}
174 }
175 /**
176  * Set description for class number i
177  * @param i int
178  */
179 public void setClassDesc(int i, String s) {
180 	if (_svClassDesc.size() < i) {
181 		_svClassDesc.setSize(i + 1);
182 	}
183 	_svClassDesc.setElementAt(s, i);
184 }
185 /**
186  * Set grade for class number i
187  * @param i int
188  */
189 public void setGrade(int i, String s) {
190 	if (_svGrade.size() < i) {
191 		_svGrade.setSize(i + 1);
192 	}
193 	_svGrade.setElementAt(s, i);
194 }
195 }