Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
fix(school): Use require instead of check or if throw fro CalendarEve…
Browse files Browse the repository at this point in the history
…ntFactory, Class, ClassImpl, Professor, School and Student
  • Loading branch information
AndreaBrighi committed Jul 12, 2023
1 parent d8361e2 commit eb69a10
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface Class {
* @throws IllegalArgumentException if the name is blank
*/
fun create(name: String): Class {
check(name.isNotBlank()) { "name cannot be blank" }
require(name.isNotBlank()) { "name cannot be blank" }
return ClassImpl(name)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ interface School {
* @throws IllegalArgumentException if the name or the city is blank
*/
fun create(name: String, city: String): School {
check(name.isNotBlank()) { "name cannot be blank" }
check(city.isNotBlank()) { "city cannot be blank" }
require(name.isNotBlank()) { "name cannot be blank" }
require(city.isNotBlank()) { "city cannot be blank" }
return SchoolImpl(name, city)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ object CalendarEventFactory {
subject: Subject,
module: String? = null,
): WeekLesson {
check(subject.isNotBlank()) {
require(subject.isNotBlank()) {
"subject cannot be blank"
}
check(!startTime.isAfter(endTime)) {
require(!startTime.isAfter(endTime)) {
"startTime cannot be after endTime"
}
check(!fromDate.isAfter(toDate)) {
require(!fromDate.isAfter(toDate)) {
"fromDate cannot be after toDate"
}
return WeekLessonImpl(
Expand Down Expand Up @@ -152,10 +152,10 @@ object CalendarEventFactory {
subject: Subject,
module: String? = null,
): DateLesson {
check(subject.isBlank()) {
require(subject.isNotBlank()) {
"subject cannot be blank"
}
check(startTime.isAfter(endTime)) {
require(!startTime.isAfter(endTime)) {
"startTime cannot be after endTime"
}
return DateLessonImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ internal data class ClassImpl(
* @throws IllegalArgumentException if the student is already in the class or if the student is not in the class
*/
override fun addStudent(student: Student): Class {
if (students.contains(student)) {
error("student is already in this class")
} else {
return copy().apply {
students = students + student
}
require(!students.contains(student)) {
"student is already in this class"
}
return copy().apply {
students = students + student
}
}

Expand All @@ -51,21 +50,20 @@ internal data class ClassImpl(
* @throws IllegalArgumentException if the professor is not in the class or if the professor doesn't teach the subjects in the class
*/
override fun addProfessor(professor: Professor, subjects: Set<Subject>): Class {
if (subjects.isEmpty()) {
error("subjects cannot be empty")
require(subjects.isNotEmpty()) {
"subjects cannot be empty"
}
val newClass = if (professors.contains(professor)) {
val oldSubjects = professorTeachSubjects[professor] ?: error("professor is not in this class")
copy().apply {
professorTeachSubjects = professorTeachSubjects + (professor to oldSubjects + subjects)
}
} else {
val newClass = if (professors.contains(professor)) {
val oldSubjects = professorTeachSubjects[professor] ?: error("professor is not in this class")
copy().apply {
professorTeachSubjects = professorTeachSubjects + (professor to oldSubjects + subjects)
}
} else {
copy().apply {
professors = professors + professor
professorTeachSubjects = professorTeachSubjects + (professor to subjects)
}
copy().apply {
professors = professors + professor
professorTeachSubjects = professorTeachSubjects + (professor to subjects)
}
return newClass
}
return newClass
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ interface Professor : Person {
name: String,
surname: String,
): Professor {
check(email.isNotBlank()) { "email cannot be blank" }
check(name.isNotBlank()) { "name cannot be blank" }
check(surname.isNotBlank()) { "surname cannot be blank" }
require(email.isNotBlank()) { "email cannot be blank" }
require(name.isNotBlank()) { "name cannot be blank" }
require(surname.isNotBlank()) { "surname cannot be blank" }
return ProfessorImpl(email, name, surname)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ interface Student : Person {
name: String,
surname: String,
): Student {
check(email.isNotBlank()) { "email cannot be blank" }
check(name.isNotBlank()) { "name cannot be blank" }
check(surname.isNotBlank()) { "surname cannot be blank" }
require(email.isNotBlank()) { "email cannot be blank" }
require(name.isNotBlank()) { "name cannot be blank" }
require(surname.isNotBlank()) { "surname cannot be blank" }
return StudentImpl(email, name, surname)
}
}
Expand Down

0 comments on commit eb69a10

Please sign in to comment.