groovy-lecture/assignments/solutions/mitarbeiter.groovy

63 lines
1.5 KiB
Groovy
Raw Normal View History

2024-06-09 20:55:46 +02:00
class Employee {
def name
def salary
Employee(name, salary) {
this.name = name
this.salary = salary
}
String toString() {
return "Name: ${name}, Salary: ${salary}"
}
}
class EmployeeManager {
def employees = []
def addEmployee(name, salary) {
employees << new Employee(name, salary)
}
def updateSalary(name, newSalary) {
employees.find { it.name == name }?.salary = newSalary
}
def removeEmployee(name) {
employees.removeAll { it.name == name }
}
def adjustSalaries(Closure adjustmentLogic) {
employees.each { it.salary = adjustmentLogic(it.salary) }
}
def filterEmployees(Closure criteria) {
return employees.findAll(criteria)
}
}
// Beispielnutzung der Klasse EmployeeManager
def manager = new EmployeeManager()
// Hinzufügen von Mitarbeitern
manager.addEmployee("Alice", 50000)
manager.addEmployee("Bob", 60000)
manager.addEmployee("Charlie", 55000)
// Aktualisieren des Gehalts eines Mitarbeiters
manager.updateSalary("Alice", 52000)
// Entfernen eines Mitarbeiters
manager.removeEmployee("Charlie")
// Durchführen von Gehaltsanpassungen für alle Mitarbeiter
manager.adjustSalaries { salary -> salary * 1.1 } // 10% Gehaltserhöhung
// Filtern von Mitarbeitern basierend auf einem Kriterium
def highEarners = manager.filterEmployees { it.salary > 55000 }
println("Alle Mitarbeiter:")
manager.employees.each { println it }
println("\nHigh Earners:")
highEarners.each { println it }