Skip to content

Commit 7cb5058

Browse files
committed
add exercise 7 tasks
1 parent ffae68f commit 7cb5058

File tree

8 files changed

+1235
-0
lines changed

8 files changed

+1235
-0
lines changed

.idea/runConfigurations/Run_All_tests_in_Execise_7.xml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package common
2+
3+
import java.nio.file.Path
4+
import java.nio.file.Paths
5+
import kotlin.io.path.readLines
6+
import kotlin.io.path.toPath
7+
8+
object FileReader {
9+
/**
10+
* Reads the contents of a file located at the specified path.
11+
*
12+
* @param path The path of the file to read.
13+
* @return A list of strings representing the lines of the file.
14+
* @throws NullPointerException if the resource at the specified path is null.
15+
*/
16+
fun readFileInResources(path: String): List<String> {
17+
val normalizedPath = path.takeIf { it.startsWith("/") } ?: "/$path"
18+
return requireNotNull(this.javaClass.getResource(normalizedPath.toString())?.toURI()?.toPath()) {
19+
"Unresolved path."
20+
}.readLines()
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package exercise7
2+
3+
import common.FileReader
4+
5+
internal fun parseFixtures(fixturesText: List<String>): List<Fixture> {
6+
TODO("Parse fixtures text to list of Fixtures.")
7+
}
8+
9+
fun main() {
10+
val fixturesText = FileReader.readFileInResources("exercise7/fixtures.csv")
11+
val fixtures: List<Fixture> = parseFixtures(fixturesText)
12+
val teams = TODO("Parse all teams from fixtures")
13+
14+
// Create league object
15+
val league: LeagueApi = TODO("Instantiate league: League(teams, fixtures)")
16+
league.displayLeagueTable()
17+
18+
league.displayLeagueTableAtFixture(13)
19+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package exercise7
2+
3+
internal interface LeagueApi {
4+
/**
5+
* Represents a list of teams participating in a league.
6+
*
7+
* @property teams The list of teams.
8+
*/
9+
val teams: List<Team>
10+
/**
11+
* Returns the list of [LeagueTableEntry] where every team result of the season is aggregated.
12+
* The League table is sorted by points descending. If more than one team has the same number of points,
13+
* sort them by goal difference.
14+
*/
15+
val leagueTable: List<LeagueTableEntry>
16+
17+
/**
18+
* Returns the team that won the league.
19+
*
20+
* @property leagueWinner The team that won the league.
21+
* The Team won the league if it has the most points in the league.
22+
* If two or more teams have a same number of points, the team with the best goal difference among them wins the league.
23+
*/
24+
val leagueWinner: Team
25+
26+
/**
27+
* Returns the team with the most wins in the league.
28+
*/
29+
val teamWithMostWins: Team
30+
31+
/**
32+
* Returns the team with the most draws in the league.
33+
*/
34+
val teamWithMostDraws: Team
35+
36+
/**
37+
* Returns the team with the most loses in a league.
38+
*/
39+
val teamWithMostLoses: Team
40+
41+
/**
42+
* Returns a team in the league with the best goal difference.
43+
*/
44+
val teamWithBestGoalDifference: Team
45+
46+
/**
47+
* Calculates the teams with the best defense based on the number of goals they have conceded.
48+
*
49+
* @param numOfTeams The number of teams to include in the result.
50+
* @return The list of teams with the best defense, sorted in ascending order based on the number of goals conceded.
51+
*/
52+
fun teamsWithBestDefence(numOfTeams: Int): List<Team>
53+
54+
/**
55+
* Calculates the teams with the best offense based on the number of goals they have scored.
56+
*
57+
* @param numOfTeams The number of teams to include in the result.
58+
* @return The list of teams with the best offense, sorted in descending order based on the number of goals scored.
59+
*/
60+
fun teamsWithBestOffense(numOfTeams: Int): List<Team>
61+
62+
/**
63+
* Calculates the number of goals that a team has scored against a specific opponent team.
64+
*
65+
* @param scorerTeam The team that has scored the goals.
66+
* @param against The opponent team.
67+
* @return The number of goals that the scorer team has scored against the opponent team.
68+
*/
69+
fun numOfGoalsTeamScoredAgainst(scorerTeam: Team, against: Team): Int
70+
71+
/**
72+
* Calculates the number of goals that a conceded team has conceded against a specific opponent team.
73+
*
74+
* @param concededTeam The team that has conceded the goals.
75+
* @param against The opponent team.
76+
* @return The number of goals that the scorer team has conceded against the opponent team.
77+
*/
78+
fun numOfGoalsTeamConcededAgainst(concededTeam: Team, against: Team): Int
79+
80+
/**
81+
* Display the league table after fixture with [fixtureId] was played.
82+
* Table should be displayed in the same format as in a method [displayLeagueTable].
83+
*/
84+
fun displayLeagueTableAtFixture(fixtureId: Int)
85+
86+
/**
87+
* Display league table with all fixtures are played.
88+
* League table should be printed in the following format:
89+
* ```
90+
* P | Team name | Games Played | Wins | Draws | Loses | GS | GC | Total Points
91+
* 1. Manchester City 38 27 5 6 83 32 86
92+
* 2. Manchester Utd 38 21 11 6 73 44 74
93+
* 3. Liverpool 38 20 9 9 68 42 69
94+
* 4. Chelsea 38 19 10 9 58 36 67
95+
* 5. Leicester City 38 20 6 12 68 50 66
96+
* 6. West Ham 38 19 8 11 62 47 65
97+
* 7. Tottenham 38 18 8 12 68 45 62
98+
* 8. Arsenal 38 18 7 13 55 39 61
99+
* 9. Leeds United 38 18 5 15 62 54 59
100+
* 10. Everton 38 17 8 13 47 48 59
101+
* 11. Aston Villa 38 16 7 15 55 46 55
102+
* 12. Newcastle Utd 38 12 9 17 46 62 45
103+
* 13. Wolves 38 12 9 17 36 52 45
104+
* 14. Crystal Palace 38 12 8 18 41 66 44
105+
* 15. Southampton 38 12 7 19 47 68 43
106+
* 16. Brighton 38 9 14 15 40 46 41
107+
* 17. Burnley 38 10 9 19 33 55 39
108+
* 18. Fulham 38 5 13 20 27 53 28
109+
* 19. West Brom 38 5 11 22 35 76 26
110+
* 20. Sheffield Utd 38 7 2 29 20 63 23
111+
* ```
112+
* where columns are ordered as following:
113+
* `P` - is a position on the board
114+
* `Team name` - name of the team
115+
* `Games Played` - total games played by team
116+
* `Wins` - total wins by team
117+
* `Draws` - total draws by team
118+
* `Loses` - total loses by team
119+
* `GS` - total goals scored by team
120+
* `GC` - total goals conceded by team
121+
* `Total Points` - total points won by team
122+
*/
123+
fun displayLeagueTable()
124+
}
125+
126+
/**
127+
* Task: Implement class [League] that implements [LeagueApi] interface. Class [League] has two properties:
128+
* @property teams - teams in the League.
129+
* @property fixtures - League fixtures with a set of games played that round.
130+
*
131+
* Task: In class init block, validate parameters of the class.
132+
* Parameters are valid if all the fixtures contain only teams from the [teams] list,
133+
* and [teams] are all mentioned in the [fixtures] list.
134+
*/
135+
136+
// TODO Implement League class that implements LeagueApi interface.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package exercise7
2+
3+
/**
4+
* Represents a team participating in a league.
5+
*
6+
* @property name The name of the team.
7+
*/
8+
internal data class Team(val name: String)
9+
10+
/**
11+
* Represents a match between two teams.
12+
*
13+
* @property homeTeam The home team.
14+
* @property awayTeam The away team.
15+
* @property homeTeamScore The score of the home team.
16+
* @property awayTeamScore The score of the away team.
17+
*/
18+
internal data class Match(
19+
val homeTeam: Team,
20+
val awayTeam: Team,
21+
val homeTeamScore: Int,
22+
val awayTeamScore: Int
23+
)
24+
25+
/**
26+
* Represents a fixture which contains a fixture ID and a list of matches.
27+
*
28+
* @property fixtureId The ID of the fixture.
29+
* @property matches The list of matches in the fixture.
30+
*/
31+
internal data class Fixture(
32+
val fixtureId: Int,
33+
val matches: List<Match>
34+
)
35+
36+
/**
37+
* Represents a team's entry in a league table.
38+
*
39+
* @param team The team participating in the league.
40+
* @param totalGamesPlayed The total number of games played by the team.
41+
* @param wins The number of wins by the team.
42+
* @param loses The number of loses by the team.
43+
* @param draws The number of draws by the team.
44+
* @param totalScoredGoals The total number of goals scored by the team.
45+
* @param totalConcededGoals The total number of goals conceded by the team.
46+
*/
47+
internal data class LeagueTableEntry(
48+
val team: Team,
49+
val totalGamesPlayed: Int,
50+
val wins: Int,
51+
val loses: Int,
52+
val draws: Int,
53+
val totalScoredGoals: Int,
54+
val totalConcededGoals: Int,
55+
) {
56+
/**
57+
* The total number of points earned by a team.
58+
* The Team earns 3 points for every win, 1 point for a draw, and zero points for a loss.
59+
*/
60+
val totalPoints: Int get() = TODO("Calculate total points")
61+
}

0 commit comments

Comments
 (0)