From 2e35c9b49447e3f7f48b58ff4c75260934b62e5f Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 9 Sep 2023 15:52:53 +0700 Subject: [PATCH 1/2] groovy: improvement for more readable --- topics/groovy/helloworld/basic-concept.groovy | 51 ++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/topics/groovy/helloworld/basic-concept.groovy b/topics/groovy/helloworld/basic-concept.groovy index e63a3f3..775bd30 100644 --- a/topics/groovy/helloworld/basic-concept.groovy +++ b/topics/groovy/helloworld/basic-concept.groovy @@ -1,12 +1,18 @@ // Greetings! -println "Welcome to groovy!" +println "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 +//------------------- + def number = 10 if (number > 5) { @@ -15,7 +21,10 @@ if (number > 5) { println("The number is not greater than 5.") } -// Working with switch case +//----------------------- +// Working with switch-case +//----------------------- + def myFruit = "Banana" switch (myFruit) { @@ -32,14 +41,20 @@ switch (myFruit) { println("It's something else.") } +//-------------------- // 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 +//---------------------- + def factorial(n) { if (n == 0) { return 1 @@ -47,12 +62,17 @@ 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 +//----------------------- + // Create a map with key-value pairs def person = [ "firstName": "John", @@ -60,19 +80,26 @@ 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 +//-------------------- + def divideNumbers(int dividend, int divisor) { try { def result = dividend / divisor @@ -86,8 +113,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}") } From 1fa234605a50e387fab56c5c74827b1df7960237 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 9 Sep 2023 15:57:18 +0700 Subject: [PATCH 2/2] groovy: decorate the console --- topics/groovy/helloworld/basic-concept.groovy | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/topics/groovy/helloworld/basic-concept.groovy b/topics/groovy/helloworld/basic-concept.groovy index 775bd30..76dec1d 100644 --- a/topics/groovy/helloworld/basic-concept.groovy +++ b/topics/groovy/helloworld/basic-concept.groovy @@ -1,5 +1,10 @@ +// 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 @@ -13,6 +18,8 @@ println "${myName} is ${myAge} years old!" // Working with if-else //------------------- +consoleLog("Working with if-else") + def number = 10 if (number > 5) { @@ -25,6 +32,8 @@ if (number > 5) { // Working with switch-case //----------------------- +consoleLog("Working with switch-case") + def myFruit = "Banana" switch (myFruit) { @@ -45,6 +54,8 @@ switch (myFruit) { // Working with for loop //-------------------- +consoleLog("Working with for loop") + def fruits = ["Apple", "Banana", "Cherry", "Watermelon", "Elderberry"] for (fruit in fruits) { @@ -55,6 +66,8 @@ for (fruit in fruits) { // Working with functions //---------------------- +consoleLog("Working with functions") + def factorial(n) { if (n == 0) { return 1 @@ -73,6 +86,8 @@ println("Factorial of 5 is: $result") // Working with Map in Groovy //----------------------- +consoleLog("Working with Map in Groovy") + // Create a map with key-value pairs def person = [ "firstName": "John", @@ -100,6 +115,8 @@ person.each { key, value -> // Working with try-catch //-------------------- +consoleLog("Working with try-catch") + def divideNumbers(int dividend, int divisor) { try { def result = dividend / divisor