Last active
March 27, 2019 22:10
-
-
Save eciavatta/6ea3e8b1aed9d8f43667d1ff17b8c569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ExamsManagerTest extends App { | |
trait ExamsManager { | |
def createNewCall(call: String): Unit | |
def addStudentResult(call: String, student: String, result: ExamResult): Unit | |
def getAllStudentsFromCall(call: String): Set[String] | |
def getEvaluationsMapFromCall(call: String): Map[String, Int] | |
def getResultsMapFromStudent(student: String): Map[String, String] | |
def getBestResultFromStudent(student: String): Option[Int] | |
} | |
object ExamsManager { | |
def apply: ExamsManager = new ExamsManagerImpl | |
private class ExamsManagerImpl extends ExamsManager { | |
private var calls: Map[String, Map[String, ExamResult]] = Map.empty | |
override def createNewCall(call: String): Unit = { | |
require(!(calls contains call)) | |
calls = calls + (call -> Map.empty) | |
} | |
override def addStudentResult(call: String, student: String, result: ExamResult): Unit = { | |
require(!(calls(call) contains student)) | |
calls = calls map { | |
case (`call`, studentMap) => (call, studentMap + (student -> result)) | |
case t => t | |
} | |
} | |
override def getAllStudentsFromCall(call: String): Set[String] = calls(call).keySet | |
override def getEvaluationsMapFromCall(call: String): Map[String, Int] = | |
calls(call).filter(_._2.isInstanceOf[ExamSucceeded]).map(t => t._1 -> t._2.valuation.get) | |
override def getResultsMapFromStudent(student: String): Map[String, String] = | |
calls.filter(_._2.contains(student)).map(t => t._1 -> t._2(student).toString) | |
override def getBestResultFromStudent(student: String): Option[Int] = | |
calls.filter(_._2.contains(student)).map(_._2(student).valuation).max | |
} | |
} | |
sealed trait ExamResult { | |
def valuation: Option[Int] = None | |
def cumLaude: Boolean = false | |
} | |
case object ExamFailed extends ExamResult { | |
override val toString: String = "FAILED" | |
} | |
case object ExamRetired extends ExamResult { | |
override val toString: String = "RETIRED" | |
} | |
case class ExamSucceeded(private val evaluation: Int, override val cumLaude: Boolean) extends ExamResult { | |
override val valuation: Option[Int] = Some(evaluation) | |
override val toString: String = s"SUCCEEDED($evaluation${if (cumLaude) "L" else ""})" | |
} | |
object ExamResult { | |
val failed: ExamResult = ExamFailed | |
val retired: ExamResult = ExamRetired | |
val succeededCumLaude: ExamResult = ExamSucceeded(30, cumLaude = true) | |
def succeeded(evaluation: Int): ExamResult = { | |
require(evaluation >= 18 && evaluation <= 30) | |
ExamSucceeded(evaluation, cumLaude = false) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment