Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner
- import kotlin.system.exitProcess
- val scanner = Scanner(System.`in`)
- class Program1{
- var studentName: MutableList<String> = ArrayList() //don't make it private so we can access it in main function
- fun addStudent(){
- print("\nEnter student name: ")
- val name = readLine()!!
- studentName.add(name)
- }
- fun deleteStudent(){
- val result = studentName.toMutableList()
- /*if student in list is 1 and
- *when delete function is called it automatically removes the only student in list
- *without giving the specific index
- */
- studentName = if(studentName.size == 1) {
- result.removeAt(0)
- result
- } else {
- print("Enter index to delete: ")
- val index = scanner.nextInt()
- result.removeAt(index)
- result
- }
- }
- fun displayStudent(){
- println("List of students: ")
- var index = 0
- for (name in studentName) {
- println("[$index]$name")
- index += 1
- }
- }
- }
- fun main(){
- val call = Program1()
- while(true){
- println("\n[1]Add Student [2]Delete Student [3] Display All Student [Other Key]Exit ")
- print("Answer: ")
- when (scanner.next().single()) {
- '1' -> call.addStudent()
- '2' -> if(call.studentName.isNotEmpty()){call.deleteStudent()} else println("No student in list")
- '3' -> if(call.studentName.isNotEmpty()){call.displayStudent()} else println("No student in list")
- else -> exitProcess(0) //break the infinite loop (exit app)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement