solved lab 06
This commit is contained in:
parent
15768ee04d
commit
b90a3687a0
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -99,7 +100,13 @@ public class Company {
|
|||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -108,7 +115,12 @@ public class Company {
|
|||
* 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;
|
||||
};
|
||||
|
||||
/*
|
||||
|
@ -117,6 +129,11 @@ public class Company {
|
|||
* 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;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
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
|
||||
|
@ -24,14 +27,16 @@ 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -39,7 +44,17 @@ public class PayrollCreator {
|
|||
* @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();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ 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;
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,17 @@
|
|||
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.
|
||||
|
@ -7,6 +19,44 @@ package ch.zhaw.prog2.functional.streaming.finance;
|
|||
*
|
||||
* ✅ 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());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue