Skip to content
Snippets Groups Projects
Commit d2921d34 authored by kiady.arintsoa's avatar kiady.arintsoa :man_with_turban_tone2:
Browse files

Add formatter

parent da9e4dee
No related branches found
No related tags found
No related merge requests found
version = "3.6.1"
runner.dialect = scala3
indent.main = 4
indent.withSiteRelativeToExtends = 3
indent.commaSiteRelativeToExtends = 8
newlines.source=keep
maxColumn = 90
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = no
...@@ -24,19 +24,29 @@ trait Person{ ...@@ -24,19 +24,29 @@ trait Person{
var age: Int var age: Int
} }
class Doctor(val name: String, var age: Int, val expertises: List[Expertise]) extends Person { class Doctor(val name: String, var age: Int, val expertises: List[Expertise])
def consult(patient: Patient) = println(s"Doctor $name is consulting patient ${patient.name} with condition ${patient.condition}.") extends Person {
def consult(patient: Patient) =
println(s"Doctor $name is consulting patient ${patient.name}" +
s"with condition ${patient.condition}.")
} }
case class Patient(condition: String, val name: String, var age: Int) extends Person case class Patient(condition: String, val name: String, var age: Int)
extends Person
// Covariance is useful here because a list of doctors is a list of persons // Covariance is useful here because a list of doctors is a list of persons
// and we might want to use a list of doctors in a method that expects a list of persons // and we might want to use a list of doctors in a method that expects a list of persons
// Type restriction is useful here because we want to restrict the type of the list to only Doctors // Type restriction is useful here because we want to restrict the type of the list to only Doctors
case class UnitPersonnel[+T <: Doctor](list: List[T]) case class UnitPersonnel[+T <: Doctor](list: List[T])
class HospitalUnit(var areaOfExpertise: Expertise, var doctors: UnitPersonnel[Doctor], var patients: List[Patient]) { class HospitalUnit(
def assignDoctor(doctor: Doctor): Unit = doctors = UnitPersonnel(doctors.list :+ doctor) var areaOfExpertise: Expertise,
var doctors: UnitPersonnel[Doctor],
var patients: List[Patient]
) {
def assignDoctor(doctor: Doctor): Unit = doctors = UnitPersonnel(
doctors.list :+ doctor
)
def addPatient(patient: Patient) = { def addPatient(patient: Patient) = {
patients = patients :+ patient patients = patients :+ patient
...@@ -64,11 +74,15 @@ class Hospital { ...@@ -64,11 +74,15 @@ class Hospital {
case _ => Expertise.Surgery case _ => Expertise.Surgery
} }
def requestAppointment(patient: Patient) = { def requestAppointment(patient: Patient) = {
val unit = units.find(u => u.areaOfExpertise == processCondition(patient.condition)) val unit =
if unit.isDefined then unit.get.addPatient(patient) else throw Exception("No unit available for this condition.") units.find(u =>
u.areaOfExpertise == processCondition(patient.condition)
)
if unit.isDefined then unit.get.addPatient(patient)
else throw Exception("No unit available for this condition.")
} }
def addDoctor(d: Doctor): Unit = { def addDoctor(d: Doctor): Unit = {
for (expertise <- d.expertises) { for expertise <- d.expertises do {
val unit = units.find(_.areaOfExpertise == expertise) val unit = units.find(_.areaOfExpertise == expertise)
if unit.isDefined then { if unit.isDefined then {
doctors = doctors :+ d doctors = doctors :+ d
...@@ -79,13 +93,18 @@ class Hospital { ...@@ -79,13 +93,18 @@ class Hospital {
addUnit(d.expertises.head, d) addUnit(d.expertises.head, d)
} }
def addUnit(expertise: Expertise, doctor: Doctor) = { def addUnit(expertise: Expertise, doctor: Doctor) = {
val unit = new HospitalUnit(expertise, UnitPersonnel[Doctor](List(doctor)), List()) val unit =
new HospitalUnit(
expertise,
UnitPersonnel[Doctor](List(doctor)),
List()
)
units = units :+ unit units = units :+ unit
} }
def listDoctors = doctors.foreach(PersonSummaryPrinter.printSummary(_)) def listDoctors = doctors.foreach(PersonSummaryPrinter.printSummary(_))
object PersonSummaryPrinter extends SummaryPrinter[Person](): object PersonSummaryPrinter extends SummaryPrinter[Person]():
def printSummary(person: Person): String = s"${person.name} is ${person.age} years old." def printSummary(person: Person): String =
s"${person.name} is ${person.age} years old."
} }
...@@ -4,7 +4,10 @@ import org.scalatest.funsuite.AnyFunSuite ...@@ -4,7 +4,10 @@ import org.scalatest.funsuite.AnyFunSuite
class Ex1Suite extends AnyFunSuite { class Ex1Suite extends AnyFunSuite {
test("1. Create a list of numbers which are consecutive multiples of 3") { test("1. Create a list of numbers which are consecutive multiples of 3") {
assert(One() == List(3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81)) assert(
One() == List(3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45,
48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81)
)
} }
test("2. Generate a new list where each element is tripled") { test("2. Generate a new list where each element is tripled") {
assert(Two(List(3, 5, 2, 3, 9, 4, 2)) == List(9, 15, 6, 9, 27, 12, 6)) assert(Two(List(3, 5, 2, 3, 9, 4, 2)) == List(9, 15, 6, 9, 27, 12, 6))
......
...@@ -2,7 +2,6 @@ package helloworld ...@@ -2,7 +2,6 @@ package helloworld
import org.scalatest.funsuite.AnyFunSuite import org.scalatest.funsuite.AnyFunSuite
class HelloWorldSuite extends AnyFunSuite { class HelloWorldSuite extends AnyFunSuite {
test("Hello, world!") { test("Hello, world!") {
assert(main() == "Hello, world!") assert(main() == "Hello, world!")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment