Student Management System in Kotlin with Source Code

Let's create a basic Student Management System in Kotlin that provides functionalities for CRUD (Create, Read, Update, and Delete) operations.

Student Management System in Kotlin with Source Code

Here is the complete Kotlin program for the Student Management System:
data class Student(val rollNo: Int, var name: String, var marks: Double)

fun main() {
    val students = mutableMapOf<Int, Student>()

    while (true) {
        println("""
            1. Add Student
            2. Display Students
            3. Update Student
            4. Delete Student
            5. Exit
        """.trimIndent())

        print("Enter your choice: ")
        when (readLine()!!.toInt()) {
            1 -> {
                print("Enter roll number: ")
                val rollNo = readLine()!!.toInt()
                print("Enter name: ")
                val name = readLine()!!
                print("Enter marks: ")
                val marks = readLine()!!.toDouble()

                students[rollNo] = Student(rollNo, name, marks)
                println("Student added successfully!")
            }
            2 -> {
                println("\nAll Students:")
                students.values.forEach {
                    println("Roll No: ${it.rollNo}, Name: ${it.name}, Marks: ${it.marks}")
                }
            }
            3 -> {
                print("Enter roll number to update: ")
                val rollNo = readLine()!!.toInt()
                val student = students[rollNo]
                if (student != null) {
                    print("Enter new name: ")
                    student.name = readLine()!!
                    print("Enter new marks: ")
                    student.marks = readLine()!!.toDouble()
                    println("Student updated successfully!")
                } else {
                    println("Student not found!")
                }
            }
            4 -> {
                print("Enter roll number to delete: ")
                val rollNo = readLine()!!.toInt()
                if (students.remove(rollNo) != null) {
                    println("Student deleted successfully!")
                } else {
                    println("Student not found!")
                }
            }
            5 -> return
        }
    }
}

Explanation: 

A Student data class is defined to hold each student's details. 

The main function uses a students mutable map, where the key is the roll number (unique for each student) and the value is a Student. W

e've implemented a menu-driven approach for the user to interact with the system. The user can add, display, update, delete students, or exit the application.

Output:

When executed, the program will show a menu, allowing the user to: 
  1. Add a student. 
  2. Display all students' details. 
  3. Update a student's details using the roll number. 
  4. Delete a student using the roll number. 
  5. Exit the application. 
Upon each operation, the user will see the relevant messages (e.g., "Student added successfully!") and will be redirected back to the main menu until they decide to exit.

Here is the complete output for your reference:
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 1
Enter roll number: 100
Enter name: Ramesh
Enter marks: 60
Student added successfully!
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 2
All Students:
Roll No: 100, Name: Ramesh, Marks: 60.0
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 3
Enter roll number to update: 100
Enter new name: Ram
Enter new marks: 80
Student updated successfully!1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 2
All Students:
Roll No: 100, Name: Ram, Marks: 80.01. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 4
Enter roll number to delete: 100
Student deleted successfully!
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 5
This Kotlin-based Student Management System is a simple and concise example of CRUD operations. Further enhancements and features can be added based on specific requirements.

Comments