Skip to content

Commit

Permalink
GH-1 question-service with random questions and check answer function…
Browse files Browse the repository at this point in the history
…ality
  • Loading branch information
georglundesgaard committed Dec 17, 2019
1 parent 8087351 commit 2e43a8f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
package no.lundesgaard.extremestartup.question

import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import java.lang.String.format
import kotlin.random.Random.Default.nextInt

@RestController
class QuestionController {
@GetMapping("/")
fun getQuestionSummary(): String {
private val questions: HashMap<String, Question<Int>> = HashMap()

@GetMapping("/random")
fun randomQuestion(): String {
val r = nextInt(100)
val q = if (r < 33) AdditionQuestion() else if (r < 66) SubtractionQuestion() else MultiplicationQuestion()
return format("%s: %s? %s\n", q.id, q, q.correctAnswer())
questions[q.id] = q
return format("%s\n", q)
}

@GetMapping("/{id}")
fun question(@PathVariable id: String): String {
val q = questions[id] ?: throw MissingQuestionException(id)
return format("%s\n", q)
}

@PostMapping("/{id}")
fun checkAnswer(@PathVariable id: String, @RequestBody answer: String): String {
val q = questions[id] ?: throw MissingQuestionException(id)
return format("%s\n", answer == q.correctAnswer().toString())
}
}

@DeleteMapping("/{id}")
fun delete(@PathVariable id: String):String {
val q = questions.remove(id)
return format("%s\n", q?.id ?: "")
}

@GetMapping("/")
fun questions(): String = format("%d\n", questions.size)
}

@ResponseStatus(NOT_FOUND)
class MissingQuestionException(id: String): RuntimeException(format("Unknown question id: %s", id))
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ import kotlin.random.Random.Default.nextInt
fun questionId() = UUID.randomUUID().toString().substring(0, 8)

abstract class Question<A>(val id: String = questionId()) {
abstract fun asText(): String
abstract fun correctAnswer(): A
override fun toString() = "%s: %s".format(id, asText())
}

abstract class BinaryMathsQuestion(val n1: Int = nextInt(20), val n2: Int = nextInt(20)): Question<Int>()

class AdditionQuestion: BinaryMathsQuestion() {
override fun toString() = "what is %d plus %d".format(n1, n2)
override fun asText() = "what is %d plus %d".format(n1, n2)
override fun correctAnswer() = n1 + n2
}

class SubtractionQuestion: BinaryMathsQuestion() {
override fun toString() = "what is %d minus %d".format(n1, n2)
override fun asText() = "what is %d minus %d".format(n1, n2)
override fun correctAnswer() = n1 - n2
}

class MultiplicationQuestion: BinaryMathsQuestion() {
override fun toString() = "what is %d multiplied by %d".format(n1, n2)
override fun asText() = "what is %d multiplied by %d".format(n1, n2)
override fun correctAnswer() = n1 * n2
}
4 changes: 4 additions & 0 deletions question-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring:
jackson:
serialization:
indent_output: true

0 comments on commit 2e43a8f

Please sign in to comment.