From b90a3687a08e4b2907ac11cf65c2143468d577f4 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 22 May 2022 17:31:58 +0200 Subject: [PATCH] solved lab 06 --- .../functional/stepik/ComposingPredicate.java | 5 +- .../prog2/functional/streaming/Company.java | 53 ++++++++++++------ .../streaming/finance/PayrollCreator.java | 33 ++++++++--- .../streaming/CompanyTestStudent.java | 8 ++- .../finance/PayrollCreatorTestStudent.java | 56 ++++++++++++++++++- 5 files changed, 121 insertions(+), 34 deletions(-) diff --git a/code/Stepik/src/main/java/ch/zhaw/prog2/functional/stepik/ComposingPredicate.java b/code/Stepik/src/main/java/ch/zhaw/prog2/functional/stepik/ComposingPredicate.java index 2fd75ab..aab705d 100644 --- a/code/Stepik/src/main/java/ch/zhaw/prog2/functional/stepik/ComposingPredicate.java +++ b/code/Stepik/src/main/java/ch/zhaw/prog2/functional/stepik/ComposingPredicate.java @@ -1,6 +1,8 @@ package ch.zhaw.prog2.functional.stepik; import java.util.List; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.IntPredicate; public class ComposingPredicate { @@ -11,7 +13,8 @@ public class ComposingPredicate { * @see #disjunctAllNoStream(List) */ public static IntPredicate disjunctAll(List predicates) { - throw new UnsupportedOperationException(); // TODO: remove this line and implement your solution + // TODO: remove this line and implement your solution + return i -> predicates.stream().anyMatch(p -> p.test(i)); } /** diff --git a/code/Streaming/src/main/java/ch/zhaw/prog2/functional/streaming/Company.java b/code/Streaming/src/main/java/ch/zhaw/prog2/functional/streaming/Company.java index ead3449..61e7cd1 100644 --- a/code/Streaming/src/main/java/ch/zhaw/prog2/functional/streaming/Company.java +++ b/code/Streaming/src/main/java/ch/zhaw/prog2/functional/streaming/Company.java @@ -3,6 +3,7 @@ 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; @@ -14,9 +15,9 @@ import java.util.stream.Collectors; /** - * This classe models a Company with all its Employees. + * This classe models a Company with all its Employees. * There might be Employees not working for the Company (e.g. temporally) - * ✅ This class should be worked on by students. + * ✅ This class should be worked on by students. */ public class Company { private final List employeeList; @@ -40,14 +41,14 @@ public class Company { * Aufgabe a1) */ public List getDistinctFirstnamesOfEmployees() { - return null; + return employeeList.stream().map(Person::getFirstName).distinct().toList(); } /* * Aufgabe a2) */ public String[] getDistinctLastnamesOfEmployees() { - return null; + return employeeList.stream().map(Person::getLastName).distinct().toArray(String[]::new); } /* @@ -55,14 +56,14 @@ public class Company { * There might be Employees not working for the Company (e.g. temporally) */ public List getEmployeesWorkingForCompany() { - return null; + return employeeList.stream().filter(Employee::isWorkingForCompany).toList(); } /* * Aufgabe c) - Test in Klasse CompanyTestStudent */ public List getEmployeesByPredicate(Predicate filterPredicate) { - return null; + return employeeList.stream().filter(filterPredicate).toList(); } /** @@ -88,35 +89,51 @@ public class Company { return paymentList; } - + /** * Aufgabe g1) - * - * This Method calculates a List of Payments using a (delegate) Function. - * @param employeePredicate - predicate for Employees eligible for a Payements - * @param paymentForEmployee - (delegate) Function Calculating a Payment for an Employee + * + * This Method calculates a List of Payments using a (delegate) Function. + * @param employeePredicate - predicate for Employees eligible for a Payements + * @param paymentForEmployee - (delegate) Function Calculating a Payment for an Employee * @return a List of Payments based on predicate and payment function */ public List getPayments(Predicate employeePredicate, Function paymentForEmployee) { - return null; + List paymentList = new ArrayList<>(); + for(Employee employee: employeeList) { + if (employeePredicate.test(employee)) { + paymentList.add(paymentForEmployee.apply(employee)); + } + } + return paymentList; } /** * Aufgabe g2) - * - * Function calculating Payment for January. + * + * Function calculating Payment for January. */ public static final Function paymentForEmployeeJanuary = employee -> { - return null; + 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()); + return payment; }; /* * Aufgabe g3) - * - * Fuction calculating Payment for December, where Employees having 13 Payments per year will get the double amount. + * + * Fuction calculating Payment for December, where Employees having 13 Payments per year will get the double amount. */ public static final Function paymentForEmployeeDecember = employee -> { - return null; + Payment payment = new Payment(); + CurrencyAmount salary = employee.getYearlySalary(); + int paymentsPerYear = employee.getPaymentsPerYear().getValue(); + salary = salary.createModifiedAmount(amount -> paymentsPerYear == 13 ? amount * 2 / paymentsPerYear : amount / paymentsPerYear); + payment.setCurrencyAmount(salary).setBeneficiary(employee).setTargetAccount(employee.getAccount()); + return payment; }; } diff --git a/code/Streaming/src/main/java/ch/zhaw/prog2/functional/streaming/finance/PayrollCreator.java b/code/Streaming/src/main/java/ch/zhaw/prog2/functional/streaming/finance/PayrollCreator.java index d4f43b3..d2ba2c8 100644 --- a/code/Streaming/src/main/java/ch/zhaw/prog2/functional/streaming/finance/PayrollCreator.java +++ b/code/Streaming/src/main/java/ch/zhaw/prog2/functional/streaming/finance/PayrollCreator.java @@ -1,19 +1,22 @@ 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.Currency; import java.util.List; +import java.util.stream.Stream; /** - * This Class creates a Payroll (Lohabrechnung) for a whole Company - * and supplies some Utility Methods for a a Payroll. - * ✅ This class should be worked on by students. + * This Class creates a Payroll (Lohabrechnung) for a whole Company + * and supplies some Utility Methods for a a Payroll. + * ✅ This class should be worked on by students. */ public class PayrollCreator { private final Company company; /** - * Opens a Payroll for a company. + * Opens a Payroll for a company. * @param company */ public PayrollCreator(Company company) { @@ -24,22 +27,34 @@ public class PayrollCreator { * Aufgabe d) - Test dazu exisitert in PayrollCreatorTest */ public Payroll getPayrollForAll() { - return new Payroll(); + 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 0; + return payroll.stream().map(payment -> CurrencyChange.getInNewCurrency(payment.getCurrencyAmount(), Currency.getInstance("CHF")).getAmount()).reduce(Integer::sum).orElse(0); } - /** + /** * Aufgabe f) - schreiben Sie einen eigenen Test in PayrollCreatorTestStudent - * @return a List of total amounts in this currency for each currency in the payroll + * @return a List of total amounts in this currency for each currency in the payroll */ public static List payrollAmountByCurrency(Payroll payroll) { - return null; + List currencyAmounts = payroll.stream().map(Payment::getCurrencyAmount).toList(); + return currencyAmounts.stream() + .map(CurrencyAmount::getCurrency) + .distinct() + .map(currency -> new CurrencyAmount( + currencyAmounts.stream() + .filter(currencyAmount -> currencyAmount.getCurrency().equals(currency)) + .map(CurrencyAmount::getAmount) + .reduce(Integer::sum) + .orElse(0), currency)) + .toList(); } diff --git a/code/Streaming/src/test/java/ch/zhaw/prog2/functional/streaming/CompanyTestStudent.java b/code/Streaming/src/test/java/ch/zhaw/prog2/functional/streaming/CompanyTestStudent.java index 658f556..8adcf08 100644 --- a/code/Streaming/src/test/java/ch/zhaw/prog2/functional/streaming/CompanyTestStudent.java +++ b/code/Streaming/src/test/java/ch/zhaw/prog2/functional/streaming/CompanyTestStudent.java @@ -5,14 +5,15 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.Random; +import java.util.function.Predicate; 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. - * - * ✅ This class should be worked on by students. + * + * ✅ This class should be worked on by students. */ public class CompanyTestStudent { private Company testCompany; @@ -29,7 +30,8 @@ public class CompanyTestStudent { */ @Test void getEmployeesByPredicate() { - // TODO write your test + Predicate predicate = Employee::isFemale; + assertEquals(testCompany.getAllEmployees().size(), testCompany.getEmployeesByPredicate(predicate).size() + testCompany.getEmployeesByPredicate(predicate.negate()).size()); } } diff --git a/code/Streaming/src/test/java/ch/zhaw/prog2/functional/streaming/finance/PayrollCreatorTestStudent.java b/code/Streaming/src/test/java/ch/zhaw/prog2/functional/streaming/finance/PayrollCreatorTestStudent.java index 7b98c08..d502327 100644 --- a/code/Streaming/src/test/java/ch/zhaw/prog2/functional/streaming/finance/PayrollCreatorTestStudent.java +++ b/code/Streaming/src/test/java/ch/zhaw/prog2/functional/streaming/finance/PayrollCreatorTestStudent.java @@ -1,12 +1,62 @@ package ch.zhaw.prog2.functional.streaming.finance; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; + +import java.util.ArrayList; +import java.util.Currency; +import java.util.List; +import java.util.stream.Stream; + +import static java.util.Currency.getInstance; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; /** * 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 PayrollCreatorTest. - * - * ✅ This class should be worked on by students. + * + * ✅ This class should be worked on by students. */ -public class PayrollCreatorTestStudent { +class PayrollCreatorTestStudent { + Payroll payroll; + @BeforeEach + void setup(){ + payroll = new Payroll(); + } + + @Test + void payrollAmountByCurrency() { + + Payroll payroll = mock(Payroll.class); + + List payments = new ArrayList<>(); + for(int i = 0; i < 3; i++){ + payments.add(mock(Payment.class)); + } + + List currencyAmounts = new ArrayList<>(); + for(int i = 0; i < 3; i++){ + currencyAmounts.add(mock(CurrencyAmount.class)); + when(payments.get(i).getCurrencyAmount()).thenReturn(currencyAmounts.get(i)); + } + + when(payroll.stream()).thenReturn(payments.stream()); + + when(currencyAmounts.get(0).getCurrency()).thenReturn(getInstance("CHF")); + when(currencyAmounts.get(0).getAmount()).thenReturn(10); + + when(currencyAmounts.get(1).getCurrency()).thenReturn(getInstance("CHF")); + when(currencyAmounts.get(1).getAmount()).thenReturn(15); + + when(currencyAmounts.get(2).getCurrency()).thenReturn(getInstance("USD")); + when(currencyAmounts.get(2).getAmount()).thenReturn(117); + + List sumCurrencyAmounts = PayrollCreator.payrollAmountByCurrency(payroll); + assertEquals(25, sumCurrencyAmounts.stream().filter(currencyAmount -> currencyAmount.getCurrency().equals(getInstance("CHF"))).findFirst().get().getAmount()); + assertEquals(117, sumCurrencyAmounts.stream().filter(currencyAmount -> currencyAmount.getCurrency().equals(getInstance("USD"))).findFirst().get().getAmount()); + + } }