Commit 7f13704
Changed files (3)
src/Q8/BadmintonScoringWithStroke.java
@@ -11,13 +11,63 @@ public class BadmintonScoringWithStroke extends BadmintonScoring {
public BadmintonScoringWithStroke(ArrayList<Point> scores) {
super(new int[0][0]);
+ this.scores = scores;
}
public String getMostUsedStrokePlayer1() {
- return "";
+ return maxStrokeFor(Point.PLAYER1);
}
public String getMostUsedStrokePlayer2() {
- return "";
+ return maxStrokeFor(Point.PLAYER2);
+ }
+
+ private String maxStrokeFor(int player) {
+ int[] strokes = new int[] {0, 0, 0, 0, 0};
+ for (Point point : this.scores) {
+ if (point.getPlayer() != player) continue;
+
+ switch (point.getStroke()) {
+ case "slice":
+ strokes[0] += 1;
+ break;
+ case "drive":
+ strokes[1] += 1;
+ break;
+ case "smash":
+ strokes[2] += 1;
+ break;
+ case "drop":
+ strokes[3] += 1;
+ break;
+ case "net-shot":
+ strokes[4] += 1;
+ break;
+ }
+ }
+ int maxIndex = 0;
+ int maxValue = 0;
+
+ for (int i = 0; i < strokes.length; i++) {
+ if (strokes[i] > maxValue) {
+ maxIndex = i;
+ maxValue = strokes[i];
+ }
+ }
+
+ switch (maxIndex) {
+ case 0:
+ return "slice";
+ case 1:
+ return "drive";
+ case 2:
+ return "smash";
+ case 3:
+ return "drop";
+ case 4:
+ return "net-shot";
+ default:
+ return "unknown";
+ }
}
}
src/Q8/BadmintonScoringWithStrokeTest.java
@@ -9,8 +9,42 @@ import junit.framework.TestCase;
import junit.framework.TestSuite;
public class BadmintonScoringWithStrokeTest extends TestCase {
+ private BadmintonScoringWithStroke subject;
+ private ArrayList<Point> points =
+ new ArrayList<Point>() {
+ {
+ add(new Point(Point.PLAYER1, "a", 1));
+ add(new Point(Point.PLAYER1, "c", 2));
+ add(new Point(Point.PLAYER2, "d", 1));
+ add(new Point(Point.PLAYER2, "e", 2));
+ add(new Point(Point.PLAYER2, "d", 3));
+ add(new Point(Point.PLAYER2, "e", 4));
+ add(new Point(Point.PLAYER2, "d", 5));
+ add(new Point(Point.PLAYER1, "a", 3));
+ add(new Point(Point.PLAYER1, "c", 4));
+ add(new Point(Point.PLAYER2, "e", 6));
+ add(new Point(Point.PLAYER2, "e", 7));
+ add(new Point(Point.PLAYER2, "a", 8));
+ add(new Point(Point.PLAYER2, "d", 9));
+ add(new Point(Point.PLAYER2, "e", 10));
+ add(new Point(Point.PLAYER2, "e", 11));
+ add(new Point(Point.PLAYER2, "e", 12));
+ add(new Point(Point.PLAYER2, "e", 13));
+ add(new Point(Point.PLAYER2, "e", 14));
+ add(new Point(Point.PLAYER2, "e", 15));
+ add(new Point(Point.PLAYER1, "c", 5));
+ add(new Point(Point.PLAYER2, "e", 16));
+ add(new Point(Point.PLAYER2, "e", 17));
+ add(new Point(Point.PLAYER2, "e", 18));
+ add(new Point(Point.PLAYER2, "e", 19));
+ add(new Point(Point.PLAYER2, "e", 20));
+ add(new Point(Point.PLAYER2, "e", 21));
+ }
+ };
+
public BadmintonScoringWithStrokeTest(String testName) {
super(testName);
+ this.subject = new BadmintonScoringWithStroke(this.points);
}
public static Test suite() {
@@ -18,6 +52,10 @@ public class BadmintonScoringWithStrokeTest extends TestCase {
}
public void test_getMostUsedStrokePlayer1() {
- assertTrue(true);
+ assertEquals("smash", subject.getMostUsedStrokePlayer1());
+ }
+
+ public void test_getMostUsedStrokePlayer2() {
+ assertEquals("net-shot", subject.getMostUsedStrokePlayer2());
}
}
src/Q8/Point.java
@@ -1,26 +1,40 @@
package Q8;
public class Point {
+ public static final int PLAYER1 = 0;
+ public static final int PLAYER2 = 1;
private int player;
private int score;
- private static final int PLAYER1 = 0;
- private static final int PLAYER2 = 1;
private String stroke;
- public Point(int player, String stroke, int value) {
+ public Point(int player, String stroke, int score) {
this.player = player;
this.stroke = stroke;
+ this.score = score;
}
public int getPlayer() {
- return 0;
+ return this.player;
}
public int getScore() {
- return 0;
+ return this.score;
}
public String getStroke() {
- return "";
+ switch (this.stroke) {
+ case "a":
+ return "slice";
+ case "b":
+ return "drive";
+ case "c":
+ return "smash";
+ case "d":
+ return "drop";
+ case "e":
+ return "net-shot";
+ default:
+ return "unknown";
+ }
}
}