Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Last active October 14, 2017 20:54
Show Gist options
  • Save gakuzzzz/8e082f77f45dad35a2fe41ce428f6a6d to your computer and use it in GitHub Desktop.
Save gakuzzzz/8e082f77f45dad35a2fe41ce428f6a6d to your computer and use it in GitHub Desktop.
Scala入門者向けハンズオン #2 解答
package app.model
object TicketRepo {
private var tickets: Map[TicketId, Ticket] = Map(
1 -> new Issue(1, "First Issue"),
2 -> new Issue(2, "Seccond Issue"),
3 -> new Bug(3, "Fisrt Bug", TicketStatus.Open, "sample"),
4 -> new Bug(4, "Closed Issue", TicketStatus.Fixed, "fixed")
)
def findAll: Seq[Ticket] = tickets.values.toVector
def findById(id: TicketId): Option[Ticket] = tickets.get(id)
/** Issueを新規に作成して登録します。作成したIssueを返します。 */
def createIssue(title: String): Issue = {
val newId = if (tickets.isEmpty) 1 else tickets.keys.max + 1
val issue = Issue(newId, title)
tickets = tickets + (newId -> issue)
issue
}
/** 引数で渡したstatusに一致するBugを全て返します。 */
def findBugsByStatus(status: TicketStatus): Seq[Bug] =
findAll.collect { case t@Bug(_, _, `status`, _) => t }
/** 引数で指定したIDのチケットのステータスをFixedに更新します。
* 更新が行われたらtrueを返し、該当するチケットが無かったり
* すでにFixedだった場合には false を返します。
*/
def fix(id: TicketId): Boolean = {
val opened = findById(id).filter(_.status != TicketStatus.Fixed)
opened.foreach { t =>
tickets = tickets + (t.id -> t.fix)
}
opened.isDefined
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment