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

Initial commit

parents
/**
*
*/
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 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());
}
}
/*
*A basic implementation of the Catcher interface
*/
package org.oreilly.javaGoodParts.examples.impl;
import org.oreilly.javaGoodParts.examples.statistics.Catcher;
import org.oreilly.javaGoodParts.examples.statistics.Fielder;
/**
* A basic implementation of the Catcher
*/
public class CatcherImpl extends FielderImpl
implements Catcher{
private int passedBalls;
public CatcherImpl(){
super();
passedBalls = 0;
}
public CatcherImpl(Fielder oldStats){
attempts = oldStats.getAttempts();
putOuts = oldStats.getPutOuts();
assists = oldStats.getAssists();
errors = oldStats.getErrors();
passedBalls = 0;
}
public void PassedBall(){
passedBalls++;
}
public int getPassedBalls(){
return passedBalls;
}
}
/*
*A basic implementation of the Catcher interface
*/
package org.oreilly.javaGoodParts.examples.impl;
import org.oreilly.javaGoodParts.examples.statistics.Catcher;
import org.oreilly.javaGoodParts.examples.statistics.Fielder;
/**
* A basic implementation of the Catcher
*/
public class CatcherImpl extends FielderImpl
implements Catcher{
private int passedBalls;
public CatcherImpl(){
super();
passedBalls = 0;
}
public CatcherImpl(Fielder oldStats){
attempts = oldStats.getAttempts();
putOuts = oldStats.getPutOuts();
assists = oldStats.getAssists();
errors = oldStats.getErrors();
passedBalls = 0;
}
public void PassedBall(){
passedBalls++;
}
public int getPassedBalls(){
return passedBalls;
}
}
package org.oreilly.javaGoodParts.examples.impl;
/**
* A class that checks implementations of the Batter
* interface. The class will be initialized with an array
* of AtBatResults, will take a Batter object and feed in
* those results, and then will check the statistics that
* were generated. For simplicity sake, the current implementation
* only checks the Slugging Average
* @author waldo
*
*/
import org.oreilly.javaGoodParts.examples.statistics.NotEnoughAtBatsException;
import org.oreilly.javaGoodParts.examples.statistics.Batter;
public class CheckBatter {
private Batter.AtBatResult[] testData;
public CheckBatter(Batter.AtBatResult[] data){
testData = data;
}
public boolean SluggingTest(Batter toTest){
for (Batter.AtBatResult r : testData){
toTest.atBat(r);
}
try {
if ((toTest.getTotalBases()/toTest.getAtBats())
!= toTest.getSlugging()){
return false;
}
} catch (NotEnoughAtBatsException e){
if ((e.getNeeded() + toTest.getAtBats()) != 10)
return false;
}
return true;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.oreilly.javaGoodParts.examples.impl;
import org.oreilly.javaGoodParts.examples.statistics.Fielder;
/**
*
* @author waldo
*/
public class FielderImpl implements Fielder{
protected int attempts;
protected int putOuts;
protected int assists;
protected int errors;
public FielderImpl(){
attempts = putOuts = assists = errors = 0;
}
@Override
public void fieldTry(AttemptResult what) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void playInning() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public float getFieldAverage() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public float getRange() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getAssists() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getAttempts() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getErrors() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getPutOuts() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.oreilly.javaGoodParts.examples.impl;
import org.oreilly.javaGoodParts.examples.statistics.Fielder;
/**
*
* @author waldo
*/
public class FielderImpl implements Fielder{
protected int attempts;
protected int putOuts;
protected int assists;
protected int errors;
public FielderImpl(){
attempts = putOuts = assists = errors = 0;
}
@Override