mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2025-12-12 12:22:12 +00:00
This commit is contained in:
parent
f6a5c4fe40
commit
563a7fac06
@ -23,6 +23,6 @@ As an alternative to downloading the code, click one of the links below to creat
|
||||
|
||||
- [Python](http://cyber-dojo.com/forker/fork/FFEB8EE18C?avatar=cheetah&tag=3)
|
||||
- [Ruby](http://cyber-dojo.com/forker/fork/9197D6B12C?avatar=cheetah&tag=3)
|
||||
- [Java](http://cyber-dojo.com/forker/fork/B22DCD17C3?avatar=buffalo&tag=11)
|
||||
- [Java](http://cyber-dojo.com/forker/fork/426FA07B60?avatar=raccoon&tag=3)
|
||||
- [C++](http://cyber-dojo.com/forker/fork/CD6FC41518?avatar=deer&tag=45)
|
||||
- [C#](http://cyber-dojo.com/forker/fork/672E047F5D?avatar=buffalo&tag=8)
|
||||
6
Tennis/java/TennisGame.java
Normal file
6
Tennis/java/TennisGame.java
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
public interface TennisGame {
|
||||
|
||||
void wonPoint(String playerName);
|
||||
String getScore();
|
||||
}
|
||||
@ -1,13 +1,12 @@
|
||||
package defactored1;
|
||||
|
||||
public class TennisGame {
|
||||
public class TennisGame1 implements TennisGame {
|
||||
|
||||
private int m_score1 = 0;
|
||||
private int m_score2 = 0;
|
||||
private String player1Name;
|
||||
private String player2Name;
|
||||
|
||||
public TennisGame(String player1Name, String player2Name) {
|
||||
public TennisGame1(String player1Name, String player2Name) {
|
||||
this.player1Name = player1Name;
|
||||
this.player2Name = player2Name;
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
package defactored2;
|
||||
|
||||
public class TennisGame
|
||||
public class TennisGame2 implements TennisGame
|
||||
{
|
||||
public int P1point = 0;
|
||||
public int P2point = 0;
|
||||
@ -10,7 +9,7 @@ public class TennisGame
|
||||
private String player1Name;
|
||||
private String player2Name;
|
||||
|
||||
public TennisGame(String player1Name, String player2Name) {
|
||||
public TennisGame2(String player1Name, String player2Name) {
|
||||
this.player1Name = player1Name;
|
||||
this.player2Name = player2Name;
|
||||
}
|
||||
@ -1,13 +1,12 @@
|
||||
package defactored3;
|
||||
|
||||
public class TennisGame {
|
||||
public class TennisGame3 implements TennisGame {
|
||||
|
||||
private int p2;
|
||||
private int p1;
|
||||
private String p1N;
|
||||
private String p2N;
|
||||
|
||||
public TennisGame(String p1N, String p2N) {
|
||||
public TennisGame3(String p1N, String p2N) {
|
||||
this.p1N = p1N;
|
||||
this.p2N = p2N;
|
||||
}
|
||||
@ -1,5 +1,3 @@
|
||||
package defactored1;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -68,9 +66,7 @@ public class TennisTest {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkAllScores() {
|
||||
TennisGame game = new TennisGame("player1", "player2");
|
||||
public void checkAllScores(TennisGame game) {
|
||||
int highestScore = Math.max(this.player1Score, this.player2Score);
|
||||
for (int i = 0; i < highestScore; i++) {
|
||||
if (i < this.player1Score)
|
||||
@ -82,16 +78,21 @@ public class TennisTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void realisticGame() {
|
||||
TennisGame game = new TennisGame("player1", "player2");
|
||||
String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"};
|
||||
String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"};
|
||||
for (int i = 0; i < 6; i++) {
|
||||
game.wonPoint(points[i]);
|
||||
assertEquals(expected_scores[i], game.getScore());
|
||||
}
|
||||
public void checkAllScoresTennisGame1() {
|
||||
TennisGame1 game = new TennisGame1("player1", "player2");
|
||||
checkAllScores(game);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkAllScoresTennisGame2() {
|
||||
TennisGame2 game = new TennisGame2("player1", "player2");
|
||||
checkAllScores(game);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkAllScoresTennisGame3() {
|
||||
TennisGame3 game = new TennisGame3("player1", "player2");
|
||||
checkAllScores(game);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,97 +0,0 @@
|
||||
package defactored2;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class TennisTest {
|
||||
|
||||
private int player1Score;
|
||||
private int player2Score;
|
||||
private String expectedScore;
|
||||
|
||||
public TennisTest(int player1Score, int player2Score, String expectedScore) {
|
||||
this.player1Score = player1Score;
|
||||
this.player2Score = player2Score;
|
||||
this.expectedScore = expectedScore;
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> getAllScores() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ 0, 0, "Love-All" },
|
||||
{ 1, 1, "Fifteen-All" },
|
||||
{ 2, 2, "Thirty-All"},
|
||||
{ 3, 3, "Forty-All"},
|
||||
{ 4, 4, "Deuce"},
|
||||
|
||||
{ 1, 0, "Fifteen-Love"},
|
||||
{ 0, 1, "Love-Fifteen"},
|
||||
{ 2, 0, "Thirty-Love"},
|
||||
{ 0, 2, "Love-Thirty"},
|
||||
{ 3, 0, "Forty-Love"},
|
||||
{ 0, 3, "Love-Forty"},
|
||||
{ 4, 0, "Win for player1"},
|
||||
{ 0, 4, "Win for player2"},
|
||||
|
||||
{ 2, 1, "Thirty-Fifteen"},
|
||||
{ 1, 2, "Fifteen-Thirty"},
|
||||
{ 3, 1, "Forty-Fifteen"},
|
||||
{ 1, 3, "Fifteen-Forty"},
|
||||
{ 4, 1, "Win for player1"},
|
||||
{ 1, 4, "Win for player2"},
|
||||
|
||||
{ 3, 2, "Forty-Thirty"},
|
||||
{ 2, 3, "Thirty-Forty"},
|
||||
{ 4, 2, "Win for player1"},
|
||||
{ 2, 4, "Win for player2"},
|
||||
|
||||
{ 4, 3, "Advantage player1"},
|
||||
{ 3, 4, "Advantage player2"},
|
||||
{ 5, 4, "Advantage player1"},
|
||||
{ 4, 5, "Advantage player2"},
|
||||
{ 15, 14, "Advantage player1"},
|
||||
{ 14, 15, "Advantage player2"},
|
||||
|
||||
{ 6, 4, "Win for player1"},
|
||||
{ 4, 6, "Win for player2"},
|
||||
{ 16, 14, "Win for player1"},
|
||||
{ 14, 16, "Win for player2"},
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkAllScores() {
|
||||
TennisGame game = new TennisGame("player1", "player2");
|
||||
int highestScore = Math.max(this.player1Score, this.player2Score);
|
||||
for (int i = 0; i < highestScore; i++) {
|
||||
if (i < this.player1Score)
|
||||
game.wonPoint("player1");
|
||||
if (i < this.player2Score)
|
||||
game.wonPoint("player2");
|
||||
}
|
||||
assertEquals(this.expectedScore, game.getScore());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void realisticGame() {
|
||||
TennisGame game = new TennisGame("player1", "player2");
|
||||
String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"};
|
||||
String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"};
|
||||
for (int i = 0; i < 6; i++) {
|
||||
game.wonPoint(points[i]);
|
||||
assertEquals(expected_scores[i], game.getScore());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,97 +0,0 @@
|
||||
package defactored3;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class TennisTest {
|
||||
|
||||
private int player1Score;
|
||||
private int player2Score;
|
||||
private String expectedScore;
|
||||
|
||||
public TennisTest(int player1Score, int player2Score, String expectedScore) {
|
||||
this.player1Score = player1Score;
|
||||
this.player2Score = player2Score;
|
||||
this.expectedScore = expectedScore;
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> getAllScores() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ 0, 0, "Love-All" },
|
||||
{ 1, 1, "Fifteen-All" },
|
||||
{ 2, 2, "Thirty-All"},
|
||||
{ 3, 3, "Forty-All"},
|
||||
{ 4, 4, "Deuce"},
|
||||
|
||||
{ 1, 0, "Fifteen-Love"},
|
||||
{ 0, 1, "Love-Fifteen"},
|
||||
{ 2, 0, "Thirty-Love"},
|
||||
{ 0, 2, "Love-Thirty"},
|
||||
{ 3, 0, "Forty-Love"},
|
||||
{ 0, 3, "Love-Forty"},
|
||||
{ 4, 0, "Win for player1"},
|
||||
{ 0, 4, "Win for player2"},
|
||||
|
||||
{ 2, 1, "Thirty-Fifteen"},
|
||||
{ 1, 2, "Fifteen-Thirty"},
|
||||
{ 3, 1, "Forty-Fifteen"},
|
||||
{ 1, 3, "Fifteen-Forty"},
|
||||
{ 4, 1, "Win for player1"},
|
||||
{ 1, 4, "Win for player2"},
|
||||
|
||||
{ 3, 2, "Forty-Thirty"},
|
||||
{ 2, 3, "Thirty-Forty"},
|
||||
{ 4, 2, "Win for player1"},
|
||||
{ 2, 4, "Win for player2"},
|
||||
|
||||
{ 4, 3, "Advantage player1"},
|
||||
{ 3, 4, "Advantage player2"},
|
||||
{ 5, 4, "Advantage player1"},
|
||||
{ 4, 5, "Advantage player2"},
|
||||
{ 15, 14, "Advantage player1"},
|
||||
{ 14, 15, "Advantage player2"},
|
||||
|
||||
{ 6, 4, "Win for player1"},
|
||||
{ 4, 6, "Win for player2"},
|
||||
{ 16, 14, "Win for player1"},
|
||||
{ 14, 16, "Win for player2"},
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkAllScores() {
|
||||
TennisGame game = new TennisGame("player1", "player2");
|
||||
int highestScore = Math.max(this.player1Score, this.player2Score);
|
||||
for (int i = 0; i < highestScore; i++) {
|
||||
if (i < this.player1Score)
|
||||
game.wonPoint("player1");
|
||||
if (i < this.player2Score)
|
||||
game.wonPoint("player2");
|
||||
}
|
||||
assertEquals(this.expectedScore, game.getScore());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void realisticGame() {
|
||||
TennisGame game = new TennisGame("player1", "player2");
|
||||
String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"};
|
||||
String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"};
|
||||
for (int i = 0; i < 6; i++) {
|
||||
game.wonPoint(points[i]);
|
||||
assertEquals(expected_scores[i], game.getScore());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,63 +0,0 @@
|
||||
package factored1;
|
||||
|
||||
public class TennisGame {
|
||||
|
||||
public static final String[] POINTS = new String[]{"Love", "Fifteen", "Thirty", "Forty"};
|
||||
|
||||
private int player2Score;
|
||||
private int player1Score;
|
||||
|
||||
private String player1Name;
|
||||
private String player2Name;
|
||||
|
||||
public TennisGame(String player1Name, String player2Name) {
|
||||
this.player1Name = player1Name;
|
||||
this.player2Name = player2Name;
|
||||
}
|
||||
|
||||
public String getScore() {
|
||||
if (someoneHasWon())
|
||||
return "Win for " + winningPlayerName();
|
||||
|
||||
if (isEndgame()) {
|
||||
if (pointsAreEven())
|
||||
return "Deuce";
|
||||
else
|
||||
return "Advantage " + winningPlayerName();
|
||||
} else {
|
||||
if (pointsAreEven())
|
||||
return POINTS[player1Score] + "-All";
|
||||
else {
|
||||
return POINTS[player1Score] + "-" + POINTS[player2Score];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean someoneHasWon() {
|
||||
return isEndgame() && (player1Score-player2Score >= 2 || player2Score-player1Score >= 2);
|
||||
}
|
||||
|
||||
private boolean pointsAreEven() {
|
||||
return player1Score == player2Score;
|
||||
}
|
||||
|
||||
private boolean isEndgame() {
|
||||
return player1Score > 3 || player2Score > 3;
|
||||
}
|
||||
|
||||
public String winningPlayerName() {
|
||||
if (player1Score > player2Score)
|
||||
return player1Name;
|
||||
else
|
||||
return player2Name;
|
||||
}
|
||||
|
||||
public void wonPoint(String playerName) {
|
||||
if (playerName == player1Name)
|
||||
this.player1Score += 1;
|
||||
else
|
||||
this.player2Score += 1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,96 +0,0 @@
|
||||
package factored1;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class TennisTest {
|
||||
|
||||
private int player1Score;
|
||||
private int player2Score;
|
||||
private String expectedScore;
|
||||
|
||||
public TennisTest(int player1Score, int player2Score, String expectedScore) {
|
||||
this.player1Score = player1Score;
|
||||
this.player2Score = player2Score;
|
||||
this.expectedScore = expectedScore;
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> getAllScores() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ 0, 0, "Love-All" },
|
||||
{ 1, 1, "Fifteen-All" },
|
||||
{ 2, 2, "Thirty-All"},
|
||||
{ 3, 3, "Forty-All"},
|
||||
{ 4, 4, "Deuce"},
|
||||
|
||||
{ 1, 0, "Fifteen-Love"},
|
||||
{ 0, 1, "Love-Fifteen"},
|
||||
{ 2, 0, "Thirty-Love"},
|
||||
{ 0, 2, "Love-Thirty"},
|
||||
{ 3, 0, "Forty-Love"},
|
||||
{ 0, 3, "Love-Forty"},
|
||||
{ 4, 0, "Win for player1"},
|
||||
{ 0, 4, "Win for player2"},
|
||||
|
||||
{ 2, 1, "Thirty-Fifteen"},
|
||||
{ 1, 2, "Fifteen-Thirty"},
|
||||
{ 3, 1, "Forty-Fifteen"},
|
||||
{ 1, 3, "Fifteen-Forty"},
|
||||
{ 4, 1, "Win for player1"},
|
||||
{ 1, 4, "Win for player2"},
|
||||
|
||||
{ 3, 2, "Forty-Thirty"},
|
||||
{ 2, 3, "Thirty-Forty"},
|
||||
{ 4, 2, "Win for player1"},
|
||||
{ 2, 4, "Win for player2"},
|
||||
|
||||
{ 4, 3, "Advantage player1"},
|
||||
{ 3, 4, "Advantage player2"},
|
||||
{ 5, 4, "Advantage player1"},
|
||||
{ 4, 5, "Advantage player2"},
|
||||
{ 15, 14, "Advantage player1"},
|
||||
{ 14, 15, "Advantage player2"},
|
||||
|
||||
{ 6, 4, "Win for player1"},
|
||||
{ 4, 6, "Win for player2"},
|
||||
{ 16, 14, "Win for player1"},
|
||||
{ 14, 16, "Win for player2"},
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkAllScores() {
|
||||
TennisGame game = new TennisGame("player1", "player2");
|
||||
int highestScore = Math.max(this.player1Score, this.player2Score);
|
||||
for (int i = 0; i < highestScore; i++) {
|
||||
if (i < this.player1Score)
|
||||
game.wonPoint("player1");
|
||||
if (i < this.player2Score)
|
||||
game.wonPoint("player2");
|
||||
}
|
||||
assertEquals(this.expectedScore, game.getScore());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void realisticGame() {
|
||||
TennisGame game = new TennisGame("player1", "player2");
|
||||
String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"};
|
||||
String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"};
|
||||
for (int i = 0; i < 6; i++) {
|
||||
game.wonPoint(points[i]);
|
||||
assertEquals(expected_scores[i], game.getScore());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user