Commit ac1f02a8 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
GoodPartsExamples_collections/src/org/oreilly/javaGoodParts/examples
package org.oreilly.javaGoodParts.examples.impl;
import org.oreilly.javaGoodParts.examples.statistics.NotEnoughAtBatsException;
import org.oreilly.javaGoodParts.examples.statistics.Batter;
import org.oreilly.javaGoodParts.examples.statistics.Player;
public class BatterImpl implements Batter {
private Player forPlayer;
private int atBats;
private int hits;
private int bases;
private int baseOnBalls;
private int sacrifices;
private String playerName;
public BatterImpl(Player player) {
forPlayer = player;
atBats = hits = bases = baseOnBalls = sacrifices = 0;
}
public Player getPlayer() {
return forPlayer;
}
@Override
public void atBat(AtBatResult what) {
switch (what) {
case strikeOut:
break;
case fieldOut:
break;
case base1:
hits++;
bases++;
break;
case base2:
hits++;
bases += 2;
break;
case base3:
hits++;
bases += 3;
case base4:
hits++;
bases += 4;
case sacrifice:
sacrifices++;
return;
case walk:
baseOnBalls++;
return;
}
atBats++;
}
@Override
public int getAtBats() {
return atBats;
}
@Override
public float getAverage() throws NotEnoughAtBatsException {
if (atBats < 10) {
throw new NotEnoughAtBatsException("Insufficient at bats",
10 - atBats);
}
return (hits / atBats);
}
@Override
public float getOBP() throws NotEnoughAtBatsException {
if ((atBats + sacrifices + baseOnBalls) < 10) {
throw new NotEnoughAtBatsException("Insufficient plate appearances",
10 - atBats);
}
float obp = (hits + baseOnBalls) / (atBats + sacrifices + baseOnBalls);
return obp;
}
@Override
public float getSlugging() throws NotEnoughAtBatsException {
if (atBats < 10) {
throw new NotEnoughAtBatsException("Insufficient plate appearances",
10 - atBats);
}
return (bases / atBats);
}
@Override
public int getTotalBases() throws NotEnoughAtBatsException {
if (atBats < 10) {
throw new NotEnoughAtBatsException("Insufficient at bats",
10 - atBats);
}
return bases;
}
}
/**
*
*/
package org.oreilly.javaGoodParts.examples.impl;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Random;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.oreilly.javaGoodParts.examples.statistics.NotEnoughAtBatsException;
import org.oreilly.javaGoodParts.examples.statistics.Batter.AtBatResult;
/**
* @author waldo
*
*/
public class BatterImplTest {
private BatterImpl[] testBatters;
private Random dataGen = new Random(1);
/**
* Set up the test array testBatters. We will test on 100 {@link BatterImpl}
* objects. These will each be initialized in a separate routine.
*/
@Before
public void setUp() {
testBatters = new BatterImpl[100];
for (int i = 0; i < 100; i++) {
testBatters[i] = new BatterImpl();
initBatter(testBatters[i]);
}
System.out.println("Test setup successfully complete");
}
/**
* Initialize the test cases that will be used for these tests. Each object
* will be given a random number of at-bats ranging from 0-100; if the
* number of at-bats is less than 10, we increment the number by 10 to
* insure that each test case has the minimum number of at-bats. Then each
* at-bat will be given a random result, which will be recorded by a call to
* the {@link BatterImpl.atBat()} method.
*
* @param batterImpl
* The batter object being initialized
*/
private void initBatter(BatterImpl batter) {
int numbats;
HashMap<Integer, AtBatResult> resultsTable
= new HashMap<Integer, AtBatResult>();
resultsTable.put(0, AtBatResult.strikeOut);
resultsTable.put(1, AtBatResult.fieldOut);
resultsTable.put(2, AtBatResult.base1);
resultsTable.put(3, AtBatResult.base2);
resultsTable.put(4, AtBatResult.base3);
resultsTable.put(5, AtBatResult.base4);
resultsTable.put(6, AtBatResult.walk);
resultsTable.put(7, AtBatResult.reachOnError);
resultsTable.put(8, AtBatResult.sacrifice);
numbats = dataGen.nextInt(100);
if (numbats < 10)
numbats++;
for (int i = 0; i < numbats; i++) {
batter.atBat(resultsTable.get(dataGen.nextInt(8)));
}
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link org.oreilly.javaGoodParts.examples.impl.BatterImpl#atBat(org.oreilly.javaGoodParts.examples.statistics.Batter.AtBatResult)}.
*/
@Test
public void testAtBat() {
fail("Not yet implemented");
}
/**
* Test method for {@link org.oreilly.javaGoodParts.examples.impl.BatterImpl#getAtBats()}.
*/
@Test
public void testGetAtBats() {
fail("Not yet implemented");
}
/**
* Test method for {@link org.oreilly.javaGoodParts.examples.impl.BatterImpl#getAverage()}.
*/
@Test
public void testGetAverage() {
fail("Not yet implemented");
}
/**
* Test method for {@link org.oreilly.javaGoodParts.examples.impl.BatterImpl#getOBP()}.
*/
@Test
public void testGetOBP() {
fail("Not yet implemented");
}
/**
* Test method for
* {@link org.oreilly.javaGoodParts.examples.impl.BatterImpl#getSlugging()}.
* This method will take the array of Batter objects stored in the private
* variable testBatters and made sure that all of them are consistent with
* regards to the slugging percentage, the number of at-bats, and the total
* number of bases recorded.
*/
@Test
public void testGetSlugging() {
float testAvg, calcAvg;
for (int i = 0; i < testBatters.length; i++) {
try {
testAvg = testBatters[i].getSlugging();
calcAvg = testBatters[i].getTotalBases()
/ testBatters[i].getAtBats();
assertEquals("Slugging test", testAvg, calcAvg, .02);
} catch (NotEnoughAtBatsException e) {
assertEquals("Slugging exception", true,
(10 == (e.getNeeded() + testBatters[i].getAtBats())));
}
}
}
/**
* Test method for {@link org.oreilly.javaGoodParts.examples.impl.BatterImpl#getTotalBases()}.
*/
@Test
public void testGetTotalBases() {
fail("Not yet implemented");
}
}
package org.oreilly.javaGoodParts.examples.impl;
import java.util.Comparator;
import org.oreilly.javaGoodParts.examples.statistics.NotEnoughAtBatsException;
import org.oreilly.javaGoodParts.examples.statistics.Player;
/**
* A comparitor that will rank players depending on
* their batting average. If both players have a
* batting average, the one with the higher average
* is compared as greater than the other. Players with
* insufficient at bats or who don't bat at all are
* dealt with as if their batting average is 0. If two
* players have the same batting average, they will
* be compared based on their player ids, which are
* guaranteed to be unique.
*
* This comparison will give a well-ordering to any
* collection of players, based on their batting average.
* Ties are broken by using the Id field of the player, which
* is also used to determine equality
*/
public class BattingComparitor implements Comparator<Player>{
@Override
public int compare(Player o1, Player o2) {
float o1A, o2A;
int retVal;
if (o1.getId() == o2.getId())
return 0;
if (o1.hasRole(Player.Roles.Batter)){
try {
o1A = o1.asBatter().getAverage();
} catch (NotEnoughAtBatsException e){
o1A = (float) 0.0;
}
} else
o1A = 0.0f;
if (o2.hasRole(Player.Roles.Batter)){
try {
o2A = o2.asBatter().getAverage();
} catch (NotEnoughAtBatsException e){
o2A = (float) 0.0;
}
} else
o2A = 0.0f;
if (o1A < o2A)
retVal = -1;
else if (o2A < o1A)
retVal = 1;
else
retVal = o1.getId().compareTo(o2.getId());
return retVal;
}
public boolean equals(Player o1, Player o2){
return (o1.getId()== o2.getId());
}
}
package org.oreilly.javaGoodParts.examples.impl;
import java.io.Serializable;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import org.oreilly.javaGoodParts.examples.statistics.Batter.AtBatResult;
import org.oreilly.javaGoodParts.examples.statistics.BoxScore;
import org.oreilly.javaGoodParts.examples.statistics.DidNotPlayException;
import org.oreilly.javaGoodParts.examples.statistics.Fielder.AttemptResult;
import org.oreilly.javaGoodParts.examples.statistics.Player.Position;
/**
* An implementation of the {@link BoxScore} interface, ignoring
* all of the little details like how the data that populates the private
* fields is actually placed into the object.
*/
public class BoxScoreImpl implements BoxScore, Serializable {
static private long serialVersionUID = 1;
private LinkedList<String> teams
= new LinkedList<String>();
private Hashtable<String, List<UUID>> whoPlayed
= new Hashtable<String, List<UUID>>();
private Hashtable<UUID, Position> positions
= new Hashtable<UUID, Position>();
private Hashtable<UUID, List<AtBatResult>> atBats
= new Hashtable<UUID, List<AtBatResult>>();
private Hashtable<UUID, List<AttemptResult>> fielding
= new Hashtable<UUID, List<AttemptResult>>();
private Hashtable<UUID, Integer> passedBalls
= new Hashtable<UUID, Integer>();
public BoxScoreImpl(String team1, String team2){
teams.add(team1);
teams.add(team2);
}
@Override
public List<String> getTeams() {
return(teams);
}
@Override
public List<UUID> getPlayers(String forTeam) {
return(whoPlayed.get(forTeam));
}
@Override
public Position getPosition(UUID forPlayer) throws DidNotPlayException {
if (positions.contains(forPlayer))
return positions.get(forPlayer);
else
throw new DidNotPlayException();
}
@Override
public List<AtBatResult> getBatting(UUID playerID) {
return atBats.get(playerID);
}
@Override
public List<AttemptResult> getFielding(UUID forPlayer) {
return fielding.get(forPlayer);
}
@Override
public int getPassedBalls(UUID forPlayer) {
return passedBalls.get(forPlayer).intValue();
}
}
D
/TeamImpl.java/1.3/Fri Nov 13 15:43:44 2009//
/Formatter.java/1.1/Mon Oct 19 15:53:52 2009//
/BatterImpl.java/1.1/Mon Oct 19 15:51:21 2009//
/.cvsignore/1.1/Mon Oct 19 15:50:42 2009//
/CatcherImpl.java/1.1/Mon Oct 19 15:51:21 2009//
/FielderImpl.java/1.1/Mon Oct 19 15:51:21 2009//
/BattingComparitor.java/1.1/Mon Oct 19 15:51:21 2009//
/PlayerImpl.java/1.2/Sat Oct 24 20:33:40 2009//
/CheckBatter.java/1.1/Mon Oct 19 15:51:21 2009//
/Catcher.class/1.1/Mon Oct 19 15:50:42 2009/-kb/
/CheckBatter.class/1.1/Mon Oct 19 15:50:42 2009/-kb/
/Fielder.class/1.1/Mon Oct 19 15:50:43 2009/-kb/
/Fielder$AttemptResult.class/1.1/Mon Oct 19 15:50:43 2009/-kb/
/Batter$AtBatResult.class/1.1/Mon Oct 19 15:50:42 2009/-kb/
GoodPartsExamples_collections/src/org/oreilly/javaGoodParts/examples/impl
package org.oreilly.javaGoodParts.examples.impl;
import org.oreilly.javaGoodParts.examples.statistics.NotEnoughAtBatsException;
import org.oreilly.javaGoodParts.examples.statistics.Batter;
public class BatterImpl implements Batter {
private int atBats;
private int hits;
private int bases;
private int baseOnBalls;
private int sacrifices;
private String playerName;
public BatterImpl() {
atBats = hits = bases = baseOnBalls = sacrifices = 0;
}
@Override
public void atBat(AtBatResult what) {
switch (what) {
case strikeOut:
break;
case fieldOut:
break;
case base1:
hits++;
bases++;
break;
case base2:
hits++;
bases += 2;
break;
case base3:
hits++;
bases += 3;
case base4:
hits++;
bases += 4;
case sacrifice:
sacrifices++;
return;
case walk:
baseOnBalls++;
return;
}
atBats++;
}
@Override
public int getAtBats() {
return atBats;
}
@Override
public float getAverage() throws NotEnoughAtBatsException {
if (atBats < 10) {
throw new NotEnoughAtBatsException("Insufficient at bats",
10 - atBats);
}
return (hits / atBats);
}
@Override
public float getOBP() throws NotEnoughAtBatsException {
if ((atBats + sacrifices + baseOnBalls) < 10) {
throw new NotEnoughAtBatsException("Insufficient plate appearances",
10 - atBats);
}
float obp = (hits + baseOnBalls) / (atBats + sacrifices + baseOnBalls);
return obp;
}
@Override
public float getSlugging() throws NotEnoughAtBatsException {
if (atBats < 10) {
throw new NotEnoughAtBatsException("Insufficient plate appearances",
10 - atBats);
}
return (bases / atBats);
}
@Override
public int getTotalBases() throws NotEnoughAtBatsException {
if (atBats < 10) {
throw new NotEnoughAtBatsException("Insufficient at bats",
10 - atBats);
}
return bases;
}
}
package org.oreilly.javaGoodParts.examples.impl;
import org.oreilly.javaGoodParts.examples.statistics.NotEnoughAtBatsException;
import org.oreilly.javaGoodParts.examples.statistics.Batter;
public class BatterImpl implements Batter {
private int atBats;
private int hits;
private int bases;
private int baseOnBalls;
private int sacrifices;
private String playerName;
public BatterImpl() {
atBats = hits = bases = baseOnBalls = sacrifices = 0;
}
@Override
public void atBat(AtBatResult what) {
switch (what) {
case strikeOut:
break;
case fieldOut:
break;
case base1:
hits++;
bases++;
break;
case base2:
hits++;
bases += 2;
break;
case base3:
hits++;
bases += 3;
case base4:
hits++;
bases += 4;
case sacrifice:
sacrifices++;
return;
case walk:
baseOnBalls++;
return;
}
atBats++;
}
@Override
public int getAtBats() {
return atBats;
}
@Override
public float getAverage() throws NotEnoughAtBatsException {
if (atBats < 10) {
throw new NotEnoughAtBatsException("Insufficient at bats",
10 - atBats);
}
return (hits / atBats);
}
@Override
public float getOBP() throws NotEnoughAtBatsException {
if ((atBats + sacrifices + baseOnBalls) < 10) {
throw new NotEnoughAtBatsException("Insufficient plate appearances",
10 - atBats);