Compare commits
No commits in common. "master" and "feedback" have entirely different histories.
|
@ -1,8 +1,6 @@
|
||||||
package ch.zhaw.prog2.functional.stepik;
|
package ch.zhaw.prog2.functional.stepik;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
import java.util.function.IntPredicate;
|
import java.util.function.IntPredicate;
|
||||||
|
|
||||||
public class ComposingPredicate {
|
public class ComposingPredicate {
|
||||||
|
@ -13,8 +11,7 @@ public class ComposingPredicate {
|
||||||
* @see #disjunctAllNoStream(List)
|
* @see #disjunctAllNoStream(List)
|
||||||
*/
|
*/
|
||||||
public static IntPredicate disjunctAll(List<IntPredicate> predicates) {
|
public static IntPredicate disjunctAll(List<IntPredicate> predicates) {
|
||||||
// TODO: remove this line and implement your solution
|
throw new UnsupportedOperationException(); // TODO: remove this line and implement your solution
|
||||||
return i -> predicates.stream().anyMatch(p -> p.test(i));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -3,7 +3,6 @@ package ch.zhaw.prog2.functional.streaming;
|
||||||
import ch.zhaw.prog2.functional.streaming.finance.CurrencyAmount;
|
import ch.zhaw.prog2.functional.streaming.finance.CurrencyAmount;
|
||||||
import ch.zhaw.prog2.functional.streaming.finance.Payment;
|
import ch.zhaw.prog2.functional.streaming.finance.Payment;
|
||||||
import ch.zhaw.prog2.functional.streaming.humanresource.Employee;
|
import ch.zhaw.prog2.functional.streaming.humanresource.Employee;
|
||||||
import ch.zhaw.prog2.functional.streaming.humanresource.Person;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -41,14 +40,14 @@ public class Company {
|
||||||
* Aufgabe a1)
|
* Aufgabe a1)
|
||||||
*/
|
*/
|
||||||
public List<String> getDistinctFirstnamesOfEmployees() {
|
public List<String> getDistinctFirstnamesOfEmployees() {
|
||||||
return employeeList.stream().map(Person::getFirstName).distinct().toList();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Aufgabe a2)
|
* Aufgabe a2)
|
||||||
*/
|
*/
|
||||||
public String[] getDistinctLastnamesOfEmployees() {
|
public String[] getDistinctLastnamesOfEmployees() {
|
||||||
return employeeList.stream().map(Person::getLastName).distinct().toArray(String[]::new);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -56,14 +55,14 @@ public class Company {
|
||||||
* There might be Employees not working for the Company (e.g. temporally)
|
* There might be Employees not working for the Company (e.g. temporally)
|
||||||
*/
|
*/
|
||||||
public List<Employee> getEmployeesWorkingForCompany() {
|
public List<Employee> getEmployeesWorkingForCompany() {
|
||||||
return employeeList.stream().filter(Employee::isWorkingForCompany).toList();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Aufgabe c) - Test in Klasse CompanyTestStudent
|
* Aufgabe c) - Test in Klasse CompanyTestStudent
|
||||||
*/
|
*/
|
||||||
public List<Employee> getEmployeesByPredicate(Predicate<Employee> filterPredicate) {
|
public List<Employee> getEmployeesByPredicate(Predicate<Employee> filterPredicate) {
|
||||||
return employeeList.stream().filter(filterPredicate).toList();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,13 +99,7 @@ public class Company {
|
||||||
* @return a List of Payments based on predicate and payment function
|
* @return a List of Payments based on predicate and payment function
|
||||||
*/
|
*/
|
||||||
public List<Payment> getPayments(Predicate<Employee> employeePredicate, Function<Employee, Payment> paymentForEmployee) {
|
public List<Payment> getPayments(Predicate<Employee> employeePredicate, Function<Employee, Payment> paymentForEmployee) {
|
||||||
List<Payment> paymentList = new ArrayList<>();
|
return null;
|
||||||
for(Employee employee: employeeList) {
|
|
||||||
if (employeePredicate.test(employee)) {
|
|
||||||
paymentList.add(paymentForEmployee.apply(employee));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return paymentList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -115,12 +108,7 @@ public class Company {
|
||||||
* Function calculating Payment for January.
|
* Function calculating Payment for January.
|
||||||
*/
|
*/
|
||||||
public static final Function<Employee, Payment> paymentForEmployeeJanuary = employee -> {
|
public static final Function<Employee, Payment> paymentForEmployeeJanuary = employee -> {
|
||||||
Payment payment = new Payment();
|
return null;
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -129,11 +117,6 @@ public class Company {
|
||||||
* 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 -> {
|
public static final Function<Employee, Payment> paymentForEmployeeDecember = employee -> {
|
||||||
Payment payment = new Payment();
|
return null;
|
||||||
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,11 +1,8 @@
|
||||||
package ch.zhaw.prog2.functional.streaming.finance;
|
package ch.zhaw.prog2.functional.streaming.finance;
|
||||||
|
|
||||||
import ch.zhaw.prog2.functional.streaming.Company;
|
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.List;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This Class creates a Payroll (Lohabrechnung) for a whole Company
|
* This Class creates a Payroll (Lohabrechnung) for a whole Company
|
||||||
|
@ -27,16 +24,14 @@ public class PayrollCreator {
|
||||||
* Aufgabe d) - Test dazu exisitert in PayrollCreatorTest
|
* Aufgabe d) - Test dazu exisitert in PayrollCreatorTest
|
||||||
*/
|
*/
|
||||||
public Payroll getPayrollForAll() {
|
public Payroll getPayrollForAll() {
|
||||||
Payroll payroll = new Payroll();
|
return new Payroll();
|
||||||
payroll.addPayments(company.getPayments(Employee::isWorkingForCompany));
|
|
||||||
return payroll;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Aufgabe e) - Test dazu existiert in PayrollCreatorTest
|
* Aufgabe e) - Test dazu existiert in PayrollCreatorTest
|
||||||
*/
|
*/
|
||||||
public static int payrollValueCHF(Payroll payroll) {
|
public static int payrollValueCHF(Payroll payroll) {
|
||||||
return payroll.stream().map(payment -> CurrencyChange.getInNewCurrency(payment.getCurrencyAmount(), Currency.getInstance("CHF")).getAmount()).reduce(Integer::sum).orElse(0);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,17 +39,7 @@ public class PayrollCreator {
|
||||||
* @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) {
|
public static List<CurrencyAmount> payrollAmountByCurrency(Payroll payroll) {
|
||||||
List<CurrencyAmount> currencyAmounts = payroll.stream().map(Payment::getCurrencyAmount).toList();
|
return null;
|
||||||
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,7 +5,6 @@ import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
@ -30,8 +29,7 @@ public class CompanyTestStudent {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void getEmployeesByPredicate() {
|
void getEmployeesByPredicate() {
|
||||||
Predicate<Employee> predicate = Employee::isFemale;
|
// TODO write your test
|
||||||
assertEquals(testCompany.getAllEmployees().size(), testCompany.getEmployeesByPredicate(predicate).size() + testCompany.getEmployeesByPredicate(predicate.negate()).size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,5 @@
|
||||||
package ch.zhaw.prog2.functional.streaming.finance;
|
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.
|
* This test class is for all test methods written by students for easier review by lecturers.
|
||||||
|
@ -19,44 +7,6 @@ import static org.mockito.Mockito.*;
|
||||||
*
|
*
|
||||||
* ✅ This class should be worked on by students.
|
* ✅ This class should be worked on by students.
|
||||||
*/
|
*/
|
||||||
class PayrollCreatorTestStudent {
|
public 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