Created
May 16, 2021 20:36
-
-
Save jondef/d4a91723718af59e23b7e5d6ac3b25a1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package ch.zhaw.prog2.functional.streaming; | |
import ch.zhaw.prog2.functional.streaming.finance.CurrencyAmount; | |
import ch.zhaw.prog2.functional.streaming.finance.Payment; | |
import ch.zhaw.prog2.functional.streaming.humanresource.Employee; | |
import ch.zhaw.prog2.functional.streaming.humanresource.Person; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Objects; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
import java.util.stream.Collectors; | |
public class Company { | |
private final List<Employee> employeeList; | |
public Company(List<Employee> employeeList) { | |
Objects.requireNonNull(employeeList); | |
this.employeeList = employeeList; | |
} | |
/** | |
* This method is provided by lecturer - do not change | |
* Getter for all employees. | |
* | |
* @return List of employees, never {@code null} | |
*/ | |
public List<Employee> getAllEmployees() { | |
return Collections.unmodifiableList(employeeList); | |
} | |
/* | |
* Aufgabe a1) | |
*/ | |
public List<String> getDistinctFirstnamesOfEmployees() { | |
return getAllEmployees().stream().filter(Employee::isWorkingForCompany).map(Person::getFirstName).distinct().collect(Collectors.toList()); | |
} | |
/* | |
* Aufgabe a2) | |
*/ | |
public String[] getDistinctLastnamesOfEmployees() { | |
return getAllEmployees().stream().map(Person::getLastName).distinct().toArray(String[]::new); | |
} | |
/* | |
* Aufgabe b) | |
*/ | |
public List<Employee> getEmployeesWorkingForCompany() { | |
return getAllEmployees().stream().filter(Employee::isWorkingForCompany).collect(Collectors.toList()); | |
} | |
/* | |
* Aufgabe c) - Test in Klasse CompanyTestStudent | |
*/ | |
public List<Employee> getEmployeesByPredicate(Predicate<Employee> filterPredicate) { | |
return getAllEmployees().stream().filter(filterPredicate).collect(Collectors.toList()); | |
} | |
/** | |
* This method is provided by lecturer - do not change | |
* Create List of payments for employees which are selected by the employeePredicate | |
* | |
* @param employeePredicate Predicate-Function that returns true for all Employee which | |
* get a payment | |
* @return list of Payments | |
*/ | |
public List<Payment> getPayments(Predicate<Employee> employeePredicate) { | |
List<Payment> paymentList = new ArrayList<>(); | |
for(Employee employee: employeeList) { | |
if (employeePredicate.test(employee)) { | |
Payment payment = new Payment(); | |
CurrencyAmount salary = employee.getYearlySalary(); | |
int paymentsPerYear = employee.getPaymentsPerYear().getValue(); | |
salary = salary.createModifiedAmount(amount -> amount / paymentsPerYear); | |
payment.setCurrencyAmount(salary).setBeneficiary(employee).setTargetAccount(employee.getAccount()); | |
paymentList.add(payment); | |
} | |
} | |
return paymentList; | |
} | |
/* | |
* Aufgabe g1) | |
*/ | |
public List<Payment> getPayments(Predicate<Employee> employeePredicate, Function<Employee, Payment> paymentForEmployee) { | |
return null;//employeeList.stream().filter(employeePredicate).collect(Collectors.toList()); | |
} | |
/* | |
* Aufgabe g2) | |
*/ | |
public static final Function<Employee, Payment> paymentForEmployeeJanuary = employee -> { | |
return null; | |
}; | |
/* | |
* Aufgabe g3) | |
*/ | |
public static final Function<Employee, Payment> paymentForEmployeeDecember = employee -> { | |
return null; | |
}; | |
} |
This file contains hidden or 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
package ch.zhaw.prog2.functional.streaming; | |
import ch.zhaw.prog2.functional.streaming.humanresource.Employee; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import java.util.List; | |
import java.util.Random; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
/** | |
* This test class is for all test methods written by students for easier review by lecturers. | |
* In a real application these test would be in the class CompanyTest. | |
*/ | |
public class CompanyTestStudent { | |
private Company testCompany; | |
@BeforeEach | |
void setUp() { | |
Random random = new Random(CompanyTest.RANDOM_SEED); | |
CompanySupplier companySupplier = new CompanySupplier(random, CompanyTest.EMPLOYEE_COUNT); | |
testCompany = companySupplier.get(); | |
} | |
/* | |
* Aufgabe c) | |
*/ | |
@Test | |
void getEmployeesByPredicate() { | |
List<Employee> females = testCompany.getEmployeesByPredicate(Employee::isFemale); | |
List<Employee> males = testCompany.getEmployeesByPredicate(e -> !e.isFemale()); | |
assertEquals(testCompany.getAllEmployees().size(), females.size() + males.size()); | |
} | |
} |
This file contains hidden or 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
package ch.zhaw.prog2.functional.streaming.finance; | |
import ch.zhaw.prog2.functional.streaming.Company; | |
import ch.zhaw.prog2.functional.streaming.humanresource.Employee; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
public class PayrollCreator { | |
private final Company company; | |
public PayrollCreator(Company company) { | |
this.company = company; | |
} | |
/* | |
* Aufgabe d) - Test dazu exisitert in PayrollCreatorTest | |
*/ | |
public Payroll getPayrollForAll() { | |
Payroll payroll = new Payroll(); | |
payroll.addPayments(company.getPayments(Employee::isWorkingForCompany)); | |
return payroll; | |
} | |
/* | |
* Aufgabe e) - Test dazu existiert in PayrollCreatorTest | |
*/ | |
public static int payrollValueCHF(Payroll payroll) { | |
return payroll.stream().mapToInt(i -> CurrencyChange.getInNewCurrency(i.getCurrencyAmount(), Currency.getInstance("CHF")).getAmount()).sum(); | |
} | |
/* | |
* Aufgabe f) - schreiben Sie einen eigenen Test in PayrollCreatorTestStudent | |
*/ | |
public static List<CurrencyAmount> payrollAmountByCurrency(Payroll payroll) { | |
return new ArrayList<Currency>() {{ | |
add(Currency.getInstance("CHF")); | |
add(Currency.getInstance("USD")); | |
add(Currency.getInstance("GBP")); | |
add(Currency.getInstance("EUR")); | |
}}.stream() | |
.map(currency -> CurrencyChange.getInNewCurrency(new CurrencyAmount(payrollValueCHF(payroll)), currency)) | |
.collect(Collectors.toList()); | |
} | |
} |
This file contains hidden or 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
package ch.zhaw.prog2.functional.streaming.finance; | |
import ch.zhaw.prog2.functional.streaming.Company; | |
import ch.zhaw.prog2.functional.streaming.CompanySupplier; | |
import org.junit.jupiter.api.Test; | |
import java.util.List; | |
import java.util.Random; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
public class PayrollCreatorTestStudent { | |
static final long RANDOM_SEED = 5113L; | |
static final int EMPLOYEE_COUNT = 400; | |
@Test | |
void payrollAmountByCurrency() { | |
Company testCompany = new CompanySupplier(new Random(RANDOM_SEED), EMPLOYEE_COUNT).get(); | |
PayrollCreator payrollCreator = new PayrollCreator(testCompany); | |
Payroll payroll = payrollCreator.getPayrollForAll(); | |
List<CurrencyAmount> list = PayrollCreator.payrollAmountByCurrency(payroll); | |
for (CurrencyAmount curr : list) { | |
assertEquals(CurrencyChange.getInNewCurrency(new CurrencyAmount(PayrollCreator.payrollValueCHF(payroll)), curr.getCurrency()).getAmount(), curr.getAmount()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment