import scala.collection.mutable.ArrayBuffer

case class Task(id: Int, description: String, priority: String)

object TaskManager {
  private val tasks = ArrayBuffer[Task]()

  def addTask(description: String, priority: String): Unit = {
    val taskId = if (tasks.isEmpty) 1 else tasks.map(_.id).max + 1
    val newTask = Task(taskId, description, priority)
    tasks += newTask
    println(s"Task added: ${newTask.id} - ${newTask.description} [Priority: ${newTask.priority}]")
  }

  def viewTasks(): Unit = {
    if (tasks.isEmpty) {
      println("No tasks available.")
    } else {
      println("ID | Description                    | Priority")
      tasks.foreach { task =>
        println(f"${task.id}%2d | ${task.description}%30s | ${task.priority}%8s")
      }
    }
  }

  def deleteTask(id: Int): Unit = {
    tasks.indexWhere(_.id == id) match {
      case -1 => println(s"Task with ID $id not found.")
      case index =>
        val deletedTask = tasks.remove(index)
        println(s"Task deleted: ${deletedTask.id} - ${deletedTask.description}")
    }
  }
}

object TaskManagerApp {
  def main(args: Array[String]): Unit = {
    println("Welcome to Task Manager!")

    var running = true
    while (running) {
      println("\nSelect an option:")
      println("1. Add Task")
      println("2. View Tasks")
      println("3. Delete Task")
      println("4. Exit")
      print("Enter your choice: ")

      scala.io.StdIn.readInt() match {
        case 1 =>
          print("Enter task description: ")
          val description = scala.io.StdIn.readLine()
          print("Enter task priority: ")
          val priority = scala.io.StdIn.readLine()
          TaskManager.addTask(description, priority)
        case 2 =>
          println("\nTasks:")
          TaskManager.viewTasks()
        case 3 =>
          print("Enter ID of the task to delete: ")
          val taskId = scala.io.StdIn.readInt()
          TaskManager.deleteTask(taskId)
        case 4 =>
          println("Exiting Task Manager.")
          running = false
        case _ =>
          println("Invalid option. Please select a valid option.")
      }
    }
  }
}
 
by

Scala Online Compiler

Write, Run & Share Scala code online using OneCompiler's Scala online compiler for free. It's one of the robust, feature-rich online compilers for Scala language, running on the latest version 2.13.8. Getting started with the OneCompiler's Scala compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Scala and start coding.

Read input from STDIN in Scala

OneCompiler's Scala online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Scala program which takes name as input and prints hello message with your name.

object Hello {
	def main(args: Array[String]): Unit = {
	  val name = scala.io.StdIn.readLine()        // Read input from STDIN
    println("Hello " + name ) 
	}
}

About Scala

Scala is both object-oriented and functional programming language by Martin Odersky in the year 2003.

Syntax help

Variables

Variable is a name given to the storage area in order to identify them in our programs.

var or val Variable-name [: Data-Type] = [Initial Value];

Loops and conditional statements

1. If family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
//code    
} 

If-else

if(conditional-expression) {  
//code if condition is true  
} else {  
//code if condition is false  
} 

Nested-If-else

if(condition-expression1) {  
//code if above condition is true  
} else if (condition-expression2) {  
//code if above condition is true  
}  
else if(condition-expression3) {  
//code if above condition is true  
}  
...  
else {  
//code if all the above conditions are false  
}  

2. For:

For loop is used to iterate a set of statements based on a criteria.

for(index <- range){  
  // code  
} 

3. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

4. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition) 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

def functionname(parameters : parameters-type) : returntype = {   //code
}

Note:

You can either use = or not in the function definition. If = is not present, function will not return any value.