Skip to content

Commit

Permalink
groovy: make the example more organized and readable (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
tungbq committed Sep 9, 2023
2 parents 33528be + 1fa2346 commit bb762ad
Showing 1 changed file with 56 additions and 12 deletions.
68 changes: 56 additions & 12 deletions topics/groovy/helloworld/basic-concept.groovy
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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) {
Expand All @@ -32,47 +50,73 @@ 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
} else {
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",
"lastName": "Doe",
"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
Expand All @@ -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}")
}

0 comments on commit bb762ad

Please sign in to comment.