Skip to content

Commit

Permalink
Alterations
Browse files Browse the repository at this point in the history
  • Loading branch information
rosebbc committed Jul 17, 2024
1 parent 27a3213 commit 4bf2589
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 45 deletions.
6 changes: 5 additions & 1 deletion election-api-scala/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The API has 3 endpoints:

During your assessment we will ask you to work though the task in `tasks.md` with a pair. Please do not work on or complete these prior to the assessment.

:warning: If you make any changes to the code, please ensure you return it to it's initial (HEAD) state before your assessment.
__Warning:__ If you make any changes to the code, please ensure you return it to it's initial (HEAD) state before your assessment.

## Prerequisites
- Java 11
Expand All @@ -36,6 +36,10 @@ During your assessment we will ask you to work though the task in `tasks.md` wit

`sbt run`

You may get a port error when you try to run this. If so, run the following command:

`export PLAY_HTTP_PORT=9001`

### To Test

`sbt test`
3 changes: 1 addition & 2 deletions election-api-scala/app/controllers/ResultsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class ResultsController @Inject()(val controllerComponents: ControllerComponents
}

def getScoreboard: Action[AnyContent] = Action {

Ok(Json.toJson(Scoreboard(0)))
Ok(Json.toJson(None))
}
}
6 changes: 4 additions & 2 deletions election-api-scala/app/model/PartyResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ case class ConstituencyResult(id: Int, name: String, seqNo: Int, partyResults: S

case class ApiResponse (error: String, message: String)


case class Scoreboard(declared:Int)
// TODO: this class should hold:
// - the overall winner (if there is one)
// - the seats that each party wins in Parliament
case class Scoreboard(winner: String)
16 changes: 5 additions & 11 deletions election-api-scala/tasks.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
### Task

The product owner has asked that we implement the scoreboard endpoint.

First version: First Past the Post
#### First Past the Post

For each constituency a winner can be declared:
1. if we have votes for that constituency
2. The winning party is which ever party received a plurality of votes, e.g. whoever receives the most votes
For each constituency a winner can be declared if we have votes for at least one party in that constituency.
The winning party is which ever party received a plurality of votes, e.g. whoever receives the most votes.

Someone wins in the UK if they receive half of the constituency seats in Parliament (325).
So if a party has received 325 or more seats it can be declared the winner overall.
Expand All @@ -16,16 +16,10 @@ The scoreboard should show:
- The seats for each party
- The overall winner (i.e. the party with 325 or more seats) if there is one

There is a case class [Scoreboard](app/model/PartyResult.scala) ready to be extended and an
There is a case class [Scoreboard](app/model/PartyResult.scala) which you should alter to show the above results and an
endpoint controller to put your implementation: [ResultsController](app/controllers/ResultsController.scala). You will also need to complete
the [tests](test/controllers/ResultsControllerSpec.scala) as part of your implementation.

Bonus information for the scoreboard:
- The total votes for each party
- The total share of the vote for each party. So the percentage of votes for each party.


### Possible other implementations

- Absolute majority required. Someone needs 50% + 1 votes or a run off is triggered (check the data that's probably all constituencies)
- Allocate the seats from the total declarations based on % of vote share
48 changes: 19 additions & 29 deletions election-api-scala/test/controllers/ResultsControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,37 @@ class ResultsControllerSpec extends PlaySpec with GuiceOneAppPerTest with Inject

"Test first 5 results" in {
val scoreboard = runXResults(5)
scoreboard match {
case Some(sc) => matchScoreboard(scoreboard = sc, LabSeats = 4, LDSeats = 1)
case None => fail("Didn't return a scoreboard")
}
scoreboard must not be empty
// LAB = 4
// LD = 1
// winner = noone
}

"First 100 results" in {
val scoreboard = runXResults(100)
scoreboard match {
case Some(sc) => matchScoreboard(scoreboard = sc, LabSeats = 56, LDSeats = 12, ConSeats = 31)
case None => fail("Didn't return a scoreboard")
}
scoreboard must not be empty
// LD == 12
// LAB == 56
// CON == 31
// winner = noone
}

"First 554 results" in {
val scoreboard = runXResults(554)
scoreboard match {
case Some(sc) => matchScoreboard(scoreboard = sc, LabSeats = 325, LDSeats = 52, ConSeats = 167, winner = Some("LAB"))
case None => fail("Didn't return a scoreboard")
}
scoreboard must not be empty
// LD == 52
// LAB = 325
// CON = 167
// winner = LAB
}

"All results" in {
val scoreboard = runXResults(650)
scoreboard match {
case Some(sc) => matchScoreboard(scoreboard = sc, LabSeats = 349, LDSeats = 62, ConSeats = 210, winner = Some("LAB"))
case None => fail("Didn't return a scoreboard")
}
}

def matchScoreboard(scoreboard: Scoreboard, LabSeats: Int = 0,
LDSeats: Int = 0, ConSeats: Int = 0, winner: Option[String] = None) {
var ld, lab, con = 0

//TODO: set the seats by party

ld mustEqual LDSeats
lab mustEqual LabSeats
con mustEqual ConSeats
// something mustEqual winner
fail("Implement me")
scoreboard must not be empty
// LD == 62
// LAB == 349
// CON == 210
// winner = LAB
}

def runXResults(number: Int): Option[Scoreboard] = {
Expand Down

0 comments on commit 4bf2589

Please sign in to comment.