# Calculate the Total Amount of Points using Java

## The challenge

Our football team finished the championship. The result of each match look like “x:y”. Results of all matches are recorded in the collection.

For example: `["3:1", "2:2", "0:1", ...]`

Write a function that takes such collection and counts the points of our team in the championship. Rules for counting points for each match:

• if x>y – 3 points
• if x<y – 0 point
• if x=y – 1 point

Notes:

• there are 10 matches in the championship
• 0 <= x <= 4
• 0 <= y <= 4

## Test cases

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
@Test
public void basicTests() {
assertEquals(30, TotalPoints.points(new String[]
{"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"}));

assertEquals(10, TotalPoints.points(new String[]
{"1:1","2:2","3:3","4:4","2:2","3:3","4:4","3:3","4:4","4:4"}));

assertEquals(0, TotalPoints.points(new String[]
{"0:1","0:2","0:3","0:4","1:2","1:3","1:4","2:3","2:4","3:4"}));

assertEquals(15, TotalPoints.points(new String[]
{"1:0","2:0","3:0","4:0","2:1","1:3","1:4","2:3","2:4","3:4"}));

assertEquals(12, TotalPoints.points(new String[]
{"1:0","2:0","3:0","4:4","2:2","3:3","1:4","2:3","2:4","3:4"}));
}
}
```Code language: Java (java)```

## The solution in Java

Option 1:

``````public class TotalPoints {

public static int points(String[] games) {
int points = 0;

for (String item: games) {
int x = (int) item.charAt(0);
int y = (int) item.charAt(2);
if (x>y) points += 3;
else if (x==y) points ++;
}

return points;
}
}
```Code language: Java (java)```

Option 2 (using a `stream`):

``````import java.util.Arrays;
public class TotalPoints {

public static int points(String[] games) {
return Arrays.stream(games)
.mapToInt(score -> score.charAt(0) - score.charAt(2))
.map(match -> match > 0 ? 3 : match == 0 ? 1 : 0)
.sum();
}
}
```Code language: Java (java)```
Subscribe
Notify of