solved lab 06

This commit is contained in:
schrom01 2022-05-22 17:31:58 +02:00
parent 15768ee04d
commit b90a3687a0
5 changed files with 121 additions and 34 deletions

View File

@ -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<IntPredicate> 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));
}
/**

View File

@ -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<Employee> employeeList;
@ -40,14 +41,14 @@ public class Company {
* Aufgabe a1)
*/
public List<String> 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<Employee> getEmployeesWorkingForCompany() {
return null;
return employeeList.stream().filter(Employee::isWorkingForCompany).toList();
}
/*
* Aufgabe c) - Test in Klasse CompanyTestStudent
*/
public List<Employee> getEmployeesByPredicate(Predicate<Employee> 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<Payment> getPayments(Predicate<Employee> employeePredicate, Function<Employee, Payment> paymentForEmployee) {
return null;
List<Payment> 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<Employee, Payment> 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<Employee, Payment> 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;
};
}

View File

@ -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<CurrencyAmount> payrollAmountByCurrency(Payroll payroll) {
return null;
List<CurrencyAmount> 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();
}

View File

@ -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<Employee> predicate = Employee::isFemale;
assertEquals(testCompany.getAllEmployees().size(), testCompany.getEmployeesByPredicate(predicate).size() + testCompany.getEmployeesByPredicate(predicate.negate()).size());
}
}

View File

@ -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<Payment> payments = new ArrayList<>();
for(int i = 0; i < 3; i++){
payments.add(mock(Payment.class));
}
List<CurrencyAmount> 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<CurrencyAmount> 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());
}
}