You are currently viewing SIMPLE TO-DO LIST

SIMPLE TO-DO LIST

class toDo(userName:String){
val name = userName // assigning ‘userName’ to a variable so that it can be used

fun showFeatures(){ println(“””Hello $name, welcome to “TO-DO LIST beta”


FEATURES - ⦾ Add ⦾ Delete ⦾ View ⦾ Modify ⦾ Quit """)

}

var userTask = mutableListOf<String>() // list which store tasks fun addTasks(){ print(“Enter the task: “) val inputTasks = readLine()!! // asking user to enter the task which he wanna add userTask.add(inputTasks) // adding given task to ‘userTask’ list println(“[task successfully added !]n”) }

fun modifyTasks(){ print(“Enter task no. which you wanna modify: “) val inputTaskNo = readLine()!! val taskNo = inputTaskNo.toInt() // converting given tasknumber from String to Int if (taskNo > userTask.size){ // if given taskno > length of list println(“This task doesn’t exists”) } else if (taskNo < 1){ println(“This task doesn’t exists”) } else{ // if given taskno is within range print(“Enter the new modified task: “) val newTaskInput = readLine()!! userTask[taskNo-1] = newTaskInput /* LHS = finding index of taskno by subtracting 1 from taskno because taskno starts from 1 RHS = assigning the new modified value*/ println(“[task successfully modified !]n”) }
}

fun viewTasks(){ if (userTask.size == 0){ // if ‘userTask’ list is empty println(“Your tasklist is empty, add something first to view”) } else{ // creating space for (i in 1..6){ println() } println(” |—————————————-|”) println(” | TO-DO LIST beta |”)
println(” |—————————————-|”) var pos = 1 // tasknumber or taskno for (i in userTask){ println(“$pos• $i”) pos++ } for (i in 1..6){ println() } }
}

fun deleteTasks(){ print(“Enter task no which you wanna delete: “) val inputDeleteTask = readLine()!! val deleteTask = inputDeleteTask.toInt() if (deleteTask > userTask.size){ println(“This task doesn’t exists”) } else if (deleteTask < 1){ println(“This task doesn’t exists”) } else{ userTask.removeAt(deleteTask-1) println(“[task successfully deleted !]n”) }

} }

fun main(){ print(“Enter your name: “) val UserName = readLine()!! // taking username val instance = toDo(UserName) // creating object and passing user’s name instance.showFeatures()

while (true){ print(“choose 1 from features: “) val chosen = readLine()!! if (chosen==”Add”){ instance.addTasks() } else if (chosen==”Modify”){ instance.modifyTasks() } else if (chosen==”View”){ instance.viewTasks() } else if (chosen==”Delete”){ instance.deleteTasks() }
else if (chosen==”Quit”){ print(“see you next time, bye !”) break }
}
}

submitted by /u/THE__REVOLUTIONIZER
[link] [comments]