Kotlin, with its expressive syntax, concise code, and null safety, is an excellent choice for building applications that need efficient and maintainable code. In this blog post, we will learn how to build a simple student management system in Koltin.
Key Features
Student Management: Add, view, update, and remove student records.
Course Enrollment: Enroll a student in a course or unenroll them.
View Course Students: View a list of students enrolled in a specific course.
Implementation
1. Class Definitions
We'll start with the Student and Course classes.data class Student(val studentId: Int,
var name: String,
val enrolledCourses: MutableList<Course> = mutableListOf()
)
data class Course(val courseId: Int,
val name: String
)
2. Student Management System
This class will manage our system's main operations.
class StudentManagementSystem {
private val students = mutableListOf<Student>()
private val courses = mutableListOf<Course>()
fun addStudent(student: Student) {
students.add(student)
}
fun removeStudent(studentId: Int) {
val student = students.find { it.studentId == studentId }
students.remove(student)
}
fun updateStudentName(studentId: Int, newName: String) {
val student = students.find { it.studentId == studentId }
student?.name = newName
}
fun addCourse(course: Course) {
courses.add(course)
}
fun enrollStudentInCourse(studentId: Int, courseId: Int) {
val student = students.find { it.studentId == studentId }
val course = courses.find { it.courseId == courseId }
if (student != null && course != null && course !in student.enrolledCourses) {
student.enrolledCourses.add(course)
}
}
fun unenrollStudentFromCourse(studentId: Int, courseId: Int) {
val student = students.find { it.studentId == studentId }
val course = courses.find { it.courseId == courseId }
student?.enrolledCourses?.remove(course)
}
fun viewStudentsInCourse(courseId: Int): List<Student> {
val course = courses.find { it.courseId == courseId }
return students.filter { course in it.enrolledCourses }
}
}
3. Main Function
This function provides a menu-driven interface for the system.
fun main() {
val system = StudentManagementSystem()
loop@ while (true) {
println("\nOptions:\n1. Add Student\n2. Remove Student\n3. Update Student Name\n4. Add Course\n5. Enroll Student in Course\n6. Unenroll Student from Course\n7. View Students in Course\n8. Exit")
when (readLine()?.toIntOrNull()) {
1 -> {
println("Enter Student ID: ")
val id = readLine()!!.toInt()
println("Enter Student Name: ")
val name = readLine()!!
system.addStudent(Student(id, name))
println("Student added.")
}
2 -> {
println("Enter Student ID to remove: ")
val id = readLine()!!.toInt()
system.removeStudent(id)
println("Student removed.")
}
3 -> {
println("Enter Student ID: ")
val id = readLine()!!.toInt()
println("Enter new Student Name: ")
val name = readLine()!!
system.updateStudentName(id, name)
println("Student name updated.")
}
4 -> {
println("Enter Course ID: ")
val id = readLine()!!.toInt()
println("Enter Course Name: ")
val name = readLine()!!
system.addCourse(Course(id, name))
println("Course added.")
}
5 -> {
println("Enter Student ID: ")
val studentId = readLine()!!.toInt()
println("Enter Course ID: ")
val courseId = readLine()!!.toInt()
system.enrollStudentInCourse(studentId, courseId)
println("Student enrolled in course.")
}
6 -> {
println("Enter Student ID: ")
val studentId = readLine()!!.toInt()
println("Enter Course ID to unenroll: ")
val courseId = readLine()!!.toInt()
system.unenrollStudentFromCourse(studentId, courseId)
println("Student unenrolled from course.")
}
7 -> {
println("Enter Course ID: ")
val courseId = readLine()!!.toInt()
val studentsInCourse = system.viewStudentsInCourse(courseId)
println("Students enrolled in this course:")
for (student in studentsInCourse) {
println("${student.studentId} - ${student.name}")
}
}
8 -> {
println("Exiting...")
break@loop
}
else -> println("Invalid choice!")
}
}
Complete Working Code with Output
To test the program:
- Copy the complete code.
- Use a Kotlin compiler or IDE to compile the code and run the program.
- Follow the menu options to add books, register members, issue, and return books.
data class Student(val studentId: Int, var name: String, val enrolledCourses: MutableList<Course> = mutableListOf())
data class Course(val courseId: Int, val name: String)
class StudentManagementSystem {
private val students = mutableListOf<Student>()
private val courses = mutableListOf<Course>()
fun addStudent(student: Student) {
students.add(student)
}
fun removeStudent(studentId: Int) {
val student = students.find { it.studentId == studentId }
students.remove(student)
}
fun updateStudentName(studentId: Int, newName: String) {
val student = students.find { it.studentId == studentId }
student?.name = newName
}
fun addCourse(course: Course) {
courses.add(course)
}
fun enrollStudentInCourse(studentId: Int, courseId: Int) {
val student = students.find { it.studentId == studentId }
val course = courses.find { it.courseId == courseId }
if (student != null && course != null && course !in student.enrolledCourses) {
student.enrolledCourses.add(course)
}
}
fun unenrollStudentFromCourse(studentId: Int, courseId: Int) {
val student = students.find { it.studentId == studentId }
val course = courses.find { it.courseId == courseId }
student?.enrolledCourses?.remove(course)
}
fun viewStudentsInCourse(courseId: Int): List<Student> {
val course = courses.find { it.courseId == courseId }
return students.filter { course in it.enrolledCourses }
}
}
fun main() {
val system = StudentManagementSystem()
loop@ while (true) {
println("\nOptions:\n1. Add Student\n2. Remove Student\n3. Update Student Name\n4. Add Course\n5. Enroll Student in Course\n6. Unenroll Student from Course\n7. View Students in Course\n8. Exit")
when (readLine()?.toIntOrNull()) {
1 -> {
println("Enter Student ID: ")
val id = readLine()!!.toInt()
println("Enter Student Name: ")
val name = readLine()!!
system.addStudent(Student(id, name))
println("Student added.")
}
2 -> {
println("Enter Student ID to remove: ")
val id = readLine()!!.toInt()
system.removeStudent(id)
println("Student removed.")
}
3 -> {
println("Enter Student ID: ")
val id = readLine()!!.toInt()
println("Enter new Student Name: ")
val name = readLine()!!
system.updateStudentName(id, name)
println("Student name updated.")
}
4 -> {
println("Enter Course ID: ")
val id = readLine()!!.toInt()
println("Enter Course Name: ")
val name = readLine()!!
system.addCourse(Course(id, name))
println("Course added.")
}
5 -> {
println("Enter Student ID: ")
val studentId = readLine()!!.toInt()
println("Enter Course ID: ")
val courseId = readLine()!!.toInt()
system.enrollStudentInCourse(studentId, courseId)
println("Student enrolled in course.")
}
6 -> {
println("Enter Student ID: ")
val studentId = readLine()!!.toInt()
println("Enter Course ID to unenroll: ")
val courseId = readLine()!!.toInt()
system.unenrollStudentFromCourse(studentId, courseId)
println("Student unenrolled from course.")
}
7 -> {
println("Enter Course ID: ")
val courseId = readLine()!!.toInt()
val studentsInCourse = system.viewStudentsInCourse(courseId)
println("Students enrolled in this course:")
for (student in studentsInCourse) {
println("${student.studentId} - ${student.name}")
}
}
8 -> {
println("Exiting...")
break@loop
}
else -> println("Invalid choice!")
}
}
}
Output
Once the system is up and running, you can add students, courses, enroll students in courses, view them, and execute various operations as provided in the menu. The console will interactively guide you through these operations.
Here is the output for your reference:
Options:
1. Add Student
2. Remove Student
3. Update Student Name
4. Add Course
5. Enroll Student in Course
6. Unenroll Student from Course
7. View Students in Course
8. Exit
Enter your choice: 1
Enter Student ID: 101
Enter Student Name: Ramesh
Student added.
Options:
1. Add Student
2. Remove Student
3. Update Student Name
4. Add Course
5. Enroll Student in Course
6. Unenroll Student from Course
7. View Students in Course
8. Exit
Enter your choice: 4
Enter Course ID: 501
Enter Course Name: Learn Kotlin
Course added.
Options:
1. Add Student
2. Remove Student
3. Update Student Name
4. Add Course
5. Enroll Student in Course
6. Unenroll Student from Course
7. View Students in Course
8. Exit
Enter your choice: 5
Enter Student ID: 101
Enter Course ID: 501
Student Ramesh enrolled in Learn Kotlin.
Options:
1. Add Student
2. Remove Student
3. Update Student Name
4. Add Course
5. Enroll Student in Course
6. Unenroll Student from Course
7. View Students in Course
8. Exit
Enter your choice: 7
Enter Course ID: 501
Students enrolled in Learn Kotlin:
1. Ramesh
Options:
1. Add Student
2. Remove Student
3. Update Student Name
4. Add Course
5. Enroll Student in Course
6. Unenroll Student from Course
7. View Students in Course
8. Exit
Enter your choice: 8
Exiting...
Conclusion
In this blog post, we learned how to build a simple student management system in Koltin. You can change this project or add more features as per your requirements.
Comments
Post a Comment
Leave Comment