diff --git a/topics/groovy/helloworld/basic-concept.groovy b/topics/groovy/helloworld/basic-concept.groovy index e63a3f3..76dec1d 100644 --- a/topics/groovy/helloworld/basic-concept.groovy +++ b/topics/groovy/helloworld/basic-concept.groovy @@ -1,12 +1,25 @@ +// Function to print log messages in green color +def consoleLog(message) { + println "\u001B[32m[INFO] $message\u001B[0m" +} + // Greetings! -println "Welcome to groovy!" +consoleLog("Welcome to Groovy!") +//------------------------ // Working with variables -myName = "Tung" -myAge = 28 +//------------------------ + +def myName = "Tung" +def myAge = 28 println "${myName} is ${myAge} years old!" -// Working with if else +//------------------- +// Working with if-else +//------------------- + +consoleLog("Working with if-else") + def number = 10 if (number > 5) { @@ -15,7 +28,12 @@ if (number > 5) { println("The number is not greater than 5.") } -// Working with switch case +//----------------------- +// Working with switch-case +//----------------------- + +consoleLog("Working with switch-case") + def myFruit = "Banana" switch (myFruit) { @@ -32,14 +50,24 @@ switch (myFruit) { println("It's something else.") } +//-------------------- // Working with for loop +//-------------------- + +consoleLog("Working with for loop") + def fruits = ["Apple", "Banana", "Cherry", "Watermelon", "Elderberry"] for (fruit in fruits) { println("I like $fruit") } -// Working with function +//---------------------- +// Working with functions +//---------------------- + +consoleLog("Working with functions") + def factorial(n) { if (n == 0) { return 1 @@ -47,12 +75,19 @@ def factorial(n) { return n * factorial(n - 1) } } -//-- Call the function and store the result in a variable + +// Call the function and store the result in a variable def result = factorial(5) -//-- Print the result + +// Print the result println("Factorial of 5 is: $result") -// Working with Map in groovy +//----------------------- +// Working with Map in Groovy +//----------------------- + +consoleLog("Working with Map in Groovy") + // Create a map with key-value pairs def person = [ "firstName": "John", @@ -60,19 +95,28 @@ def person = [ "age": 30, "city": "New York" ] + // Access values in the map def firstName = person["firstName"] def age = person["age"] + // Modify values in the map person["city"] = "Los Angeles" + // Add a new key-value pair to the map person["email"] = "john.doe@example.com" + // Iterate over the map person.each { key, value -> println("$key: $value") } -// Working with try catch +//-------------------- +// Working with try-catch +//-------------------- + +consoleLog("Working with try-catch") + def divideNumbers(int dividend, int divisor) { try { def result = dividend / divisor @@ -86,8 +130,8 @@ def divideNumbers(int dividend, int divisor) { // Example usage: try { - def devideResult = divideNumbers(10, 2) - println("Result: $devideResult") + def divideResult = divideNumbers(10, 2) + println("Result: $divideResult") } catch (Exception e) { println("An exception occurred: ${e.message}") }