typecode
KotlinTask Manager/TaskManager.kt
1enum class Priority { LOW, MEDIUM, HIGH, CRITICAL }
2
3data class Task(
4 val id: Int,
5 val title: String,
6 val priority: Priority = Priority.MEDIUM,
7 val completed: Boolean = false,
8 val tags: List<String> = emptyList()
9)
10
11class TaskManager {
12 private val tasks = mutableListOf<Task>()
13 private var nextId = 1
14
15 fun add(title: String, priority: Priority = Priority.MEDIUM, tags: List<String> = emptyList()): Task {
16 val task = Task(id = nextId++, title = title, priority = priority, tags = tags)
17 tasks.add(task)
18 return task
19 }
20
21 fun complete(id: Int): Boolean {
22 val index = tasks.indexOfFirst { it.id == id }
23 if (index == -1) return false
24 tasks[index] = tasks[index].copy(completed = true)
25 return true
26 }
27
28 fun byPriority(): Map<Priority, List<Task>> =
29 tasks.groupBy { it.priority }
30 .toSortedMap(compareByDescending { it.ordinal })
31
32 fun search(query: String): List<Task> =
33 tasks.filter { task ->
34 task.title.contains(query, ignoreCase = true) ||
35 task.tags.any { it.contains(query, ignoreCase = true) }
36 }
37
38 fun stats(): Map<String, Any> = mapOf(
39 "total" to tasks.size,
40 "completed" to tasks.count { it.completed },
41 "pending" to tasks.count { !it.completed },
42 "byPriority" to Priority.entries.associate { p ->
43 p.name to tasks.count { it.priority == p }
44 }
45 )
46}
0WPM
100%Accuracy
00:00Time
0%
Progress