Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement class diagram #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/kotlin/domain/CourseManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package domain

import domain.entity.Application
import domain.entity.Course
import domain.entity.Status

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package domain.entity
package domain

import domain.entity.*

class CourseTakingApplicationList(
private val id: StudentId,
Expand All @@ -15,20 +17,17 @@ class CourseTakingApplicationList(
}

fun addCourseTakingApplication(courseTakingApplication: CourseTakingApplication) {
if (checkCanAdd(courseTakingApplication)){
courseTakingApplications.add(courseTakingApplication)
updateCredits()
}else {
throw IllegalStateException("取得可能な単位数を超過しています。")
}
courseTakingApplications.add(courseTakingApplication)
updateCredits()
}

fun removeCourseTakingApplication(courseTakingApplicationId: CourseTakingApplicationId) {
courseTakingApplications.remove(getCourseTakingApplicationOfId(courseTakingApplicationId))
updateCredits()
courseTakingApplications.remove(getCourseTakingApplicationOfId(courseTakingApplicationId))
updateCredits()
}

private fun checkCanAdd(courseTakingApplication: CourseTakingApplication): Boolean {
return maxCredits >= credits + courseTakingApplication.getCourse().getCredit()
fun isWithinLimit(): Boolean {
return maxCredits >= credits
}

private fun updateCredits() {
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/domain/entity/Course.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ class Course(
return max
}

fun getCredit():Int {
fun getCredit(): Int {
return credit
}
}

data class DowAndPeriod(
val dow: String,
val dow: Dow,
val period: String
)
)

enum class Dow {
Monday, Tuesday, Wednesday, Thursday, Friday
}
34 changes: 32 additions & 2 deletions src/main/kotlin/domain/entity/CourseTakingApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class CourseTakingApplication(
}

/*抽選で当選する*/

fun confirm() {
if (state == State.UNCONFIRMED)
state = State.CONFIRMED
Expand All @@ -45,4 +44,35 @@ class CourseTakingApplication(

enum class State {
UNCONFIRMED, CONFIRMED, INVALIDATED
}
}

/*システムで扱うクラス*/
class Application(
val id: String,
val student: Student,
val course: Course,
private var status: Status,
) {
fun getStatus() = status
fun markAsRegistered() {
status = Status.REGISTERED
}

fun markAsRejected() {
status = Status.REJECTED
}
}

enum class Status {
NOT_REGISTERED,
REGISTERED,
REJECTED
}

/*永続化するデータのクラス*/
class ApplicationData(
val id: String,
val studentId: String,
val courseId: String,
private var status: Status,
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package domain.service

import domain.CourseTakingApplicationList
import domain.entity.*

interface CourseTakingApplicationService {

suspend fun applyCourseTaking(
courseTakingApplicationId: CourseTakingApplicationId, studentId: StudentId, courseId: CourseId
) : CourseTakingApplicationList
) : CourseTakingApplicationList?
suspend fun cancelCourseTaking(studentId: StudentId, courseTakingApplicationId: CourseTakingApplicationId) : CourseTakingApplicationList
suspend fun getCourseTakingApplications(studentId: StudentId): CourseTakingApplicationList
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package domain.service.impl
import data.repository.CourseTakingApplicationsRepository

import data.repository.CoursesRepository
import domain.CourseTakingApplicationList
import domain.entity.*

import domain.service.CourseTakingApplicationService

/*
* 履修の申請に関する機能を提供するクラス
*
* */
class CourseTakingApplicationServiceImpl(
private val courseTakingApplicationRepository: CourseTakingApplicationsRepository,
private val courseTakingApplicationsRepository: CourseTakingApplicationsRepository,
private val coursesRepository: CoursesRepository
) : CourseTakingApplicationService {

Expand All @@ -21,14 +23,17 @@ class CourseTakingApplicationServiceImpl(
courseTakingApplicationId: CourseTakingApplicationId,
studentId: StudentId,
courseId: CourseId
): CourseTakingApplicationList {
): CourseTakingApplicationList? {
val courseTakingApplicationList = getCourseTakingApplicationList(studentId)

val newCourseTakingApplication = createCourseTakingApplication(courseTakingApplicationId,studentId,courseId)
courseTakingApplicationList.addCourseTakingApplication(newCourseTakingApplication)
courseTakingApplicationRepository.save(newCourseTakingApplication)

return courseTakingApplicationList
if(courseTakingApplicationList.isWithinLimit()){
courseTakingApplicationsRepository.save(newCourseTakingApplication)
return courseTakingApplicationList
}else{
throw Exception("取得可能な単位数を超過しています。")
}
}

override suspend fun cancelCourseTaking(
Expand All @@ -38,7 +43,7 @@ class CourseTakingApplicationServiceImpl(
val courseTakingApplicationList = getCourseTakingApplicationList(studentId)

courseTakingApplicationList.removeCourseTakingApplication(courseTakingApplicationId)
courseTakingApplicationRepository.delete(courseTakingApplicationId)
courseTakingApplicationsRepository.delete(courseTakingApplicationId)

return courseTakingApplicationList
}
Expand All @@ -49,7 +54,7 @@ class CourseTakingApplicationServiceImpl(

/*CourseTakingApplicationListの生成*/
private suspend fun getCourseTakingApplicationList(studentId: StudentId): CourseTakingApplicationList {
val courseTakingApplications = courseTakingApplicationRepository.findByStudentId(studentId)
val courseTakingApplications = courseTakingApplicationsRepository.findByStudentId(studentId)
return CourseTakingApplicationList(
studentId,
courseTakingApplications.toMutableList(),
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/latestModel/Application.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package latestModel


/*システムで扱うクラス*/
class Application(
val id: String = "",
val student: Student = Student(),
val course: Course = Course(),
private var status: Status = Status.CREATED,
) {
fun getStatus() = status
fun markAsRegistered() {
status = Status.REGISTERED
}

fun markAsRejected() {
status = Status.REJECTED
}
}

enum class Status {
CREATED,
REGISTERED,
REJECTED
}
7 changes: 7 additions & 0 deletions src/main/kotlin/latestModel/Course.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package latestModel

class Course(
val id: String = "",
val credit: Int = 0,
val capacity: Int = 0,
)
18 changes: 18 additions & 0 deletions src/main/kotlin/latestModel/CourseManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package latestModel

class CourseManager(
private val course: Course,
private val applications:List<Application>
) {
fun canApplyWithFirstArrival():Boolean = applications.size <= course.capacity

fun selectApplications(){
/*落選したApplicationをRejected状態にする*/
applications.subList(0,3).forEach { it.markAsRejected() }
}

fun registerMembers(){
/*登録するApplicationをRegistered状態にする*/
applications.filterNot { it.getStatus() == Status.REJECTED }.forEach { it.markAsRegistered() }
}
}
38 changes: 38 additions & 0 deletions src/main/kotlin/latestModel/CourseSchedule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package latestModel


class CourseSchedule(
private val applications: List<Application>
) {
private val MAX_CREDITS = 24

private fun totalCredit(): Int = applications.filterNot { it.getStatus() == Status.REJECTED }.size

private fun checkNotOver(): Boolean = totalCredit() <= MAX_CREDITS

fun apply(student: Student, course: Course): Result<Unit> {
val newApplication = createApplication(student, course)
applications + newApplication

if (checkNotOver()) {
return Result.success(Unit)
} else {
return Result.failure(Exception("最大取得単位数を超過"))
}
}

fun cancel(applicationId: String): Result<Unit> {
val application = applications.find { it.id == applicationId }
?: return Result.failure(Exception("存在しないid"))

if (application.getStatus() == Status.CREATED) {
return Result.success(Unit)
} else {
return Result.failure(Exception("キャンセル不可"))
}
}

private fun createApplication(student: Student, course: Course): Application {
return Application("applicationId", student, course, Status.CREATED)
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/latestModel/Student.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package latestModel

class Student(
val id: String = ""
)
14 changes: 14 additions & 0 deletions src/main/kotlin/latestModel/dataStore/ApplicationsDataStore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package latestModel.dataStore

import latestModel.Application


interface ApplicationsDataStore {
fun findById(applicationId:String):Application
fun findByCourseId(courseId:String):List<Application>
fun findByStudentId(studentId:String):List<Application>

fun save(application: Application)
fun save(applications: List<Application>)
fun delete(applicationId: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package latestModel.dataStore

import latestModel.Student


interface CourseMembersDataStore {

fun save(members:List<Student>)
}
4 changes: 4 additions & 0 deletions src/main/kotlin/latestModel/dataStore/StudentDataStore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package latestModel.dataStore

interface StudentDataStore {
}
24 changes: 24 additions & 0 deletions src/main/kotlin/latestModel/useCase/apply.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package latestModel.useCase

import latestModel.*
import latestModel.dataStore.ApplicationsDataStore


fun apply(
studentId: String,
courseId: String,
) {
/*IO*/
val applications = listOf<Application>()
val myApplications = listOf<Application>()
val course = Course(courseId)
val student = Student(studentId)

/*aggregate*/
val courseSchedule = CourseSchedule(myApplications)
courseSchedule.apply(student, course)

/*IO*/
//dataStore.save(newApplication)
}

27 changes: 27 additions & 0 deletions src/main/kotlin/latestModel/useCase/applyAsFirstserve.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package latestModel.useCase

import latestModel.*
import latestModel.dataStore.ApplicationsDataStore


fun applyAsFirstServe(
studentId: String,
courseId: String,
dataStore: ApplicationsDataStore
) {
/*IO*/
val applications = listOf<Application>()
val myApplications = listOf<Application>()
val student = Student(studentId)
val course = Course(courseId)

/*aggregate*/
val courseManager = CourseManager(course, applications)
val courseSchedule = CourseSchedule(myApplications)
if (courseManager.canApplyAsFirstserve()) {
courseSchedule.apply(student, course)
}

/*IO*/
//dataStore.save(newApplication)
}
Loading