Created
August 19, 2024 02:51
-
-
Save maichanchinh/2470d15aa50f6f7bfceb481a78bf6174 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlin.math.round | |
| enum class AcademicLevel { | |
| EXCELLENT, GOOD, AVERAGE, BELOW_AVERAGE | |
| } | |
| interface Printable { | |
| fun getInfo(): String | |
| } | |
| abstract class Person(open val name: String) : Printable { | |
| abstract fun calculateAge(): Int | |
| } | |
| class Student( | |
| override val name: String, | |
| val birthYear: Int, | |
| private val grades: MutableList<Double> = mutableListOf() | |
| ) : Person(name) { | |
| val averageGrade: Double | |
| get() = if (grades.isNotEmpty()) grades.average() else 0.0 | |
| override fun calculateAge(): Int = 2024 - birthYear | |
| fun addGrade(grade: Double) { | |
| require(grade in 0.0..10.0) { "Điểm phải nằm trong khoảng từ 0 đến 10" } | |
| grades.add(grade) | |
| } | |
| override fun getInfo(): String = "Học sinh: $name, Tuổi: ${calculateAge()}, Điểm TB: ${round(averageGrade * 100) / 100}" | |
| companion object { | |
| fun createDefaultStudent() = Student("Học sinh mặc định", 2000) | |
| } | |
| } | |
| fun List<Student>.getTopStudent(): Student? = this.maxByOrNull { it.averageGrade } | |
| fun processStudents(students: List<Student>, criteria: (Student) -> Boolean): List<Student> { | |
| return students.filter(criteria) | |
| } | |
| fun main() { | |
| val students = listOf( | |
| Student("An", 2000).apply { | |
| addGrade(8.5) | |
| addGrade(9.0) | |
| addGrade(7.5) | |
| }, | |
| Student("Bình", 2001).apply { | |
| addGrade(7.0) | |
| addGrade(7.5) | |
| addGrade(8.0) | |
| }, | |
| Student("Cường", 2002).apply { | |
| addGrade(9.0) | |
| addGrade(9.5) | |
| addGrade(10.0) | |
| }, | |
| Student.createDefaultStudent().apply { | |
| addGrade(6.0) | |
| addGrade(6.5) | |
| addGrade(7.0) | |
| } | |
| ) | |
| println("Thông tin học sinh:") | |
| students.forEach { println(it.getInfo()) } | |
| println("\nHọc sinh có điểm trung bình cao nhất:") | |
| students.getTopStudent()?.let { println(it.getInfo()) } ?: println("Không có học sinh nào") | |
| println("\nHọc sinh có điểm trung bình trên 8.0:") | |
| val highPerformers = processStudents(students) { it.averageGrade > 8.0 } | |
| highPerformers.forEach { println(it.getInfo()) } | |
| println("\nPhân loại học sinh:") | |
| students.forEach { student -> | |
| val academicLevel = when { | |
| student.averageGrade >= 9.0 -> AcademicLevel.EXCELLENT | |
| student.averageGrade >= 8.0 -> AcademicLevel.GOOD | |
| student.averageGrade >= 7.0 -> AcademicLevel.AVERAGE | |
| else -> AcademicLevel.BELOW_AVERAGE | |
| } | |
| println("${student.name}: $academicLevel") | |
| } | |
| try { | |
| students[0].addGrade(11.0) | |
| } catch (e: IllegalArgumentException) { | |
| println("\nLỗi: ${e.message}") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Student(val name: String, var age: Int) { | |
| fun introduce() = "Xin chào, tôi là $name và tôi $age tuổi." | |
| } | |
| fun sumEvenNumbers(numbers: List<Int>): Int { | |
| var sum = 0 | |
| for (num in numbers) { | |
| if (num % 2 == 0) { | |
| sum += num | |
| } | |
| } | |
| return sum | |
| } | |
| fun main() { | |
| val students = listOf( | |
| Student("An", 20), | |
| Student("Bình", 21), | |
| Student("Cường", 19) | |
| ) | |
| println("Danh sách học sinh:") | |
| for (student in students) { | |
| println(student.introduce()) | |
| } | |
| val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
| val evenSum = sumEvenNumbers(numbers) | |
| println("\nTổng các số chẵn trong danh sách $numbers là: $evenSum") | |
| println("\nPhân loại học sinh theo độ tuổi:") | |
| for (student in students) { | |
| val category = when { | |
| student.age < 20 -> "Trẻ" | |
| student.age in 20..22 -> "Trung bình" | |
| else -> "Lớn" | |
| } | |
| println("${student.name} thuộc nhóm tuổi: $category") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment