-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q1.scala
39 lines (32 loc) · 1009 Bytes
/
Q1.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import scala.io.StdIn.readLine
object Q1{
// Function to get product list from user input
def getProductList: List[String] = {
var products = List.empty[String]
var input = ""
while (input != "done") {
println("Enter product name (type 'done' to finish): ")
input = readLine().trim()
if (input != "done") {
products = products :+ input
}
}
products
}
// Function to print product list with positions
def printProductList(products: List[String]): Unit = {
products.zipWithIndex.foreach { case (product, index) =>
println(s"${index + 1}. $product")
}
}
// Function to get total number of products
def getTotalProducts(products: List[String]): Int = {
products.length
}
def main(args: Array[String]): Unit = {
val productList = getProductList
println("\nList of Products:")
printProductList(productList)
println(s"\nTotal number of products: ${getTotalProducts(productList)}")
}
}