GildedRose-Refactoring-Kata/Tennis/java/defactored2/TennisGame.java
2011-08-24 11:08:32 +02:00

138 lines
2.4 KiB
Java

package defactored2;
public class TennisGame
{
public int P1point = 0;
public int P2point = 0;
public String P1res = "";
public String P2res = "";
private String player1Name;
private String player2Name;
public TennisGame(String player1Name, String player2Name) {
this.player1Name = player1Name;
this.player2Name = player2Name;
}
public String getScore(){
String score = "";
if (P1point == P2point && P1point < 4)
{
if (P1point==0)
score = "Love";
if (P1point==1)
score = "Fifteen";
if (P1point==2)
score = "Thirty";
if (P1point==3)
score = "Forty";
score += "-All";
}
if (P1point==P2point && P1point>3)
score = "Deuce";
if (P1point > 0 && P2point==0)
{
if (P1point==1)
P1res = "Fifteen";
if (P1point==2)
P1res = "Thirty";
if (P1point==3)
P1res = "Forty";
P2res = "Love";
score = P1res + "-" + P2res;
}
if (P2point > 0 && P1point==0)
{
if (P2point==1)
P2res = "Fifteen";
if (P2point==2)
P2res = "Thirty";
if (P2point==3)
P2res = "Forty";
P1res = "Love";
score = P1res + "-" + P2res;
}
if (P1point>P2point && P1point < 4)
{
if (P1point==2)
P1res="Thirty";
if (P1point==3)
P1res="Forty";
if (P2point==1)
P2res="Fifteen";
if (P2point==2)
P2res="Thirty";
score = P1res + "-" + P2res;
}
if (P2point>P1point && P2point < 4)
{
if (P2point==2)
P2res="Thirty";
if (P2point==3)
P2res="Forty";
if (P1point==1)
P1res="Fifteen";
if (P1point==2)
P1res="Thirty";
score = P1res + "-" + P2res;
}
if (P1point > P2point && P2point >= 3)
{
score = "Advantage player1";
}
if (P2point > P1point && P1point >= 3)
{
score = "Advantage player2";
}
if (P1point>=4 && P2point>=0 && (P1point-P2point)>=2)
{
score = "Win for player1";
}
if (P2point>=4 && P1point>=0 && (P2point-P1point)>=2)
{
score = "Win for player2";
}
return score;
}
public void SetP1Score(int number){
for (int i = 0; i < number; i++)
{
P1Score();
}
}
public void SetP2Score(int number){
for (int i = 0; i < number; i++)
{
P2Score();
}
}
public void P1Score(){
P1point++;
}
public void P2Score(){
P2point++;
}
public void wonPoint(String player) {
if (player == "player1")
P1Score();
else
P2Score();
}
}