Created
December 11, 2022 22:06
-
-
Save MahmoudMabrok/ab15b18e756dcea208a27798915e350a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(){ | |
val company = Company("DevMaster") | |
val department = Department("Programming") | |
val employeeAhmed = Employee("Mahmoud", 26) | |
val employeeMahmoud = Employee("Ammar", 1) | |
val employeeMariam = Employee("Mariam", 23) | |
department.addEmployee(employeeAhmed) | |
department.addEmployee(employeeMahmoud) | |
department.addEmployee(employeeMariam) | |
company.addDepartment(department) | |
val companyDSL = buildCompany("DevMaster") { | |
department("Programming") { | |
employee { | |
name = "Mahmoud" | |
age = 26 | |
} | |
employee { | |
name = "Ammar" | |
age = 1 | |
} | |
employee { | |
name = "Mariam" | |
age = 23 | |
} | |
} | |
} | |
company.print() | |
companyDSL.print() | |
} | |
fun buildCompany(name: String, build: Company.() -> Unit): Company { | |
val company = Company(name) | |
build.invoke(company) | |
return company | |
} | |
class Company(private val name: String){ | |
private val departments = mutableListOf<Department>() | |
fun department(departName: String , build:Department.() -> Unit){ | |
val department = Department(departName) | |
build.invoke(department) | |
departments.add(department) | |
} | |
fun addDepartment(department: Department){ | |
departments.add(department) | |
} | |
fun print(){ | |
println(name) | |
departments.forEach { | |
println("Dep Name: ${it.name}") | |
it.print() | |
} | |
} | |
} | |
class Department(val name: String){ | |
private val employees = mutableListOf<Employee>() | |
fun employee(build:Employee.() -> Unit){ | |
val employee = Employee() | |
build.invoke(employee) | |
employees.add(employee) | |
} | |
fun addEmployee(employee: Employee){ | |
employees.add(employee) | |
} | |
fun print() { | |
println("Dep Employees: ${employees.joinToString { it.name }}") | |
} | |
} | |
data class Employee(var name:String = "" , var age:Int = 0 ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment