Statistics.java
001 import java.io.*;
002 import org.w3c.dom.*;
003 import com.javaworld.JavaBeans.XMLBeans.*;
004 
005 public class Statistics
006   implements Serializable {
007 
008   protected int _year;
009   protected int _atbats;
010   protected int _runs;
011   protected int _hits;
012   protected int _homeruns;
013   protected int _runsbattedin;
014   protected int _strikeouts;
015   
016   public Statistics() {
017   }  
018   public int getAtBats() {
019 	return _atbats;
020   }  
021 /**
022  * This method "encrypts" the strikeouts property and returns an
023  * element  that contains the encrypted string.
024  */
025 public DocumentFragment getEncryptedStrikeouts(Document d) {
026 	StringBuffer sbStrikeouts = new StringBuffer(String.valueOf(_strikeouts));
027 
028 	// "Encrypt" the strikeout count by adding 'A' to every character.
029 	for (int i = 0; i < sbStrikeouts.length(); i++) {
030 		sbStrikeouts.setCharAt(i, (char) (sbStrikeouts.charAt(i) + (int) 'A'));
031 	}
032 
033 	// The result is a document fragment
034 	// containing a text node
035 	DocumentFragment dfResult = d.createDocumentFragment();
036 	Text textSo = d.createTextNode(new String(sbStrikeouts));
037 	dfResult.appendChild(textSo);
038 	return dfResult;
039 }
040   public int getHits() {
041 	return _hits;
042   }  
043 // Given an element that contains the number of
044 // hits for a player, return that number. We store
045 // hits in base 16, just to be obtuse.
046 public DocumentFragment getHitsAsDOM(Document d) {
047 	String sValue = Integer.toHexString(_hits);
048 	Text textResult = d.createTextNode("0x" + sValue);
049 	Comment comment =
050 		d.createComment("This node created by Statistics.getHitsAsDOM()");
051 	DocumentFragment df = d.createDocumentFragment();
052 	df.appendChild(comment);
053 	df.appendChild(textResult);
054 	return df;
055 }
056   public int getHomeRuns() {
057 	return _homeruns;
058   }  
059   public int getRuns() {
060 	return _runs;
061   }  
062   public int getRunsBattedIn() {
063 	return _runsbattedin;
064   }  
065   public int getYear() {
066 	return _year;
067   }  
068   public void print() {
069 	System.out.print("Year(" + _year + "), " +
070 					 "AB(" + _atbats +"), " +
071 					 "H(" + _hits +"), " +
072 					 "R(" + _runs +"), " +
073 					 "HR(" + _homeruns +"), " +
074 					 "RBI(" + _runsbattedin + "), " +
075 					 "SO(" + _strikeouts + ")");
076   }      
077   public void setAtBats(int atbats_) {
078 	_atbats = atbats_;
079   }  
080 /**
081  * This method "decrypts" the grade point average from a "Strikeouts" element,
082  * and sets the result in the bean.
083  */
084 public void setEncryptedStrikeouts(Element eStrikeouts) {
085 
086 	// Use the first nonempty text node in the Element's children
087 	Node firstKid = Util.getFirstInterestingChild(eStrikeouts);
088 	String sText = null;
089 	if (firstKid instanceof Text) {
090 		sText = ((Text)firstKid).getData();
091 	}
092 
093 	// If there were no nonempty text nodes, ignore this element
094 	if (sText == null)
095 		return;
096 
097 	StringBuffer sbStrikeouts = new StringBuffer(sText);
098 
099 	// "Decrypt" the Strikeouts by subtracting one from every character.
100 	for (int i = 0; i < sbStrikeouts.length(); i++) {
101 		sbStrikeouts.setCharAt(i, (char)(sbStrikeouts.charAt(i) - (int)'A'));
102 	}
103 
104 	sText = new String(sbStrikeouts);
105 
106 	try {
107 		Integer iStrikeouts = new Integer(sText);
108 		_strikeouts = iStrikeouts.intValue();
109 	} catch (NumberFormatException nfe) {
110 		; // Ignore this exception - this is a demo!
111 	}
112 }
113   public void setHits(int hits_) {
114 	_hits = hits_;
115   }  
116 public void setHitsAsDOM(Element e) throws InvalidPropertyException {
117 	// Find a kid of this element that's a text node.
118 	// It should contain the number of hits in hexadecimal.
119 	// If it fails, throw an exception
120 	Node n = Util.getFirstInterestingChild(e);
121 	boolean set = false;
122 	if (n instanceof Text) {
123 		try {
124 			String sNum = ((Text) n).getData();
125 			_hits = Integer.decode(sNum).intValue();
126 			set = true;
127 		} catch (NumberFormatException nfe) {
128 			throw new InvalidPropertyException("Invalid number format for Statistics.hits");
129 		}
130 	}
131 	if (!set) {
132 		throw new InvalidPropertyException("Invalid DOM structure setting Statistics.hits");
133 	}
134 }
135   public void setHomeRuns(int homeruns_) {
136 	_homeruns = homeruns_;
137   }  
138   public void setRuns(int runs_) {
139 	_runs = runs_;
140   }  
141   public void setRunsBattedIn(int runsbattedin_) {
142 	_runsbattedin = runsbattedin_;
143   }  
144   public void setYear(int year_) {
145 	_year = year_;
146   }  
147 }