Initial commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Gradle build configuration for specific lab module / exercise
|
||||
*/
|
||||
// enabled plugins
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
// Project/Module information
|
||||
description = 'Lab06 Stepik'
|
||||
group = 'ch.zhaw.prog2'
|
||||
version = '2022.1'
|
||||
|
||||
// Dependency configuration
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Junit 5 dependencies
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.+'
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.+'
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.+'
|
||||
// Mockito dependencies
|
||||
testImplementation 'org.mockito:mockito-core:4.3.+'
|
||||
}
|
||||
|
||||
// Test task configuration
|
||||
test {
|
||||
// Use JUnit platform for unit tests
|
||||
useJUnitPlatform()
|
||||
// Output results of individual tests
|
||||
testLogging {
|
||||
events "PASSED", "SKIPPED", "FAILED"
|
||||
}
|
||||
}
|
||||
|
||||
// Java plugin configuration
|
||||
java {
|
||||
// By default the Java version of the gradle process is used as source/target version.
|
||||
// This can be overridden, to ensure a specific version. Enable only if required.
|
||||
sourceCompatibility = JavaVersion.VERSION_17 // ensure Java source code compatibility
|
||||
targetCompatibility = JavaVersion.VERSION_17 // version of the created byte-code
|
||||
|
||||
// Java compiler specific options
|
||||
compileJava {
|
||||
// source files should be UTF-8 encoded
|
||||
options.encoding = 'UTF-8'
|
||||
// for more options see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package ch.zhaw.prog2.functional.stepik;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.IntPredicate;
|
||||
|
||||
public class ComposingPredicate {
|
||||
|
||||
/**
|
||||
* Write a solution which is using streams.
|
||||
*
|
||||
* @see #disjunctAllNoStream(List)
|
||||
*/
|
||||
public static IntPredicate disjunctAll(List<IntPredicate> predicates) {
|
||||
throw new UnsupportedOperationException(); // TODO: remove this line and implement your solution
|
||||
}
|
||||
|
||||
/**
|
||||
* Classical implementation provided by lecturer to help you solve this exercise.
|
||||
* <p>
|
||||
* This solution works, but you have to search a solution using streams which will lead you
|
||||
* to a solution with less lines of code.
|
||||
*/
|
||||
public static IntPredicate disjunctAllNoStream(List<IntPredicate> predicates) {
|
||||
IntPredicate disjunct = x -> false;
|
||||
for (IntPredicate currentPredicate : predicates) {
|
||||
disjunct = disjunct.or(currentPredicate);
|
||||
}
|
||||
return disjunct;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package ch.zhaw.prog2.functional.stepik;
|
||||
|
||||
import ch.zhaw.prog2.functional.stepik.ComposingPredicate;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.IntPredicate;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Do not modify this test class
|
||||
*/
|
||||
class ComposingPredicateTest {
|
||||
private static final IntPredicate isEven = x -> x % 2 == 0;
|
||||
private static final IntPredicate isDividableBy3 = x -> x % 3 == 0;
|
||||
private static final List<IntPredicate> predicateList = List.of(isEven, isDividableBy3);
|
||||
private List<Integer> expected;
|
||||
private IntStream testIntegers;
|
||||
|
||||
/*
|
||||
* This tests your solution
|
||||
*/
|
||||
@Test
|
||||
@Disabled("This exercise is not mandatory. Enable it, if you solve this exercise.")
|
||||
void disjunctAll() {
|
||||
assertDoesNotThrow(
|
||||
() -> ComposingPredicate.disjunctAll(List.of(x -> true)),
|
||||
"You have to implement ComposingPredicate.disjunctAll"
|
||||
);
|
||||
IntPredicate alwaysTrue = ComposingPredicate.disjunctAll(List.of(x -> true));
|
||||
assertTrue(alwaysTrue.test(1), "Test with one predicate only");
|
||||
|
||||
IntPredicate dividableBy2Or3 = ComposingPredicate.disjunctAll(predicateList);
|
||||
assertArrayEquals(expected.toArray(), testIntegers.filter(dividableBy2Or3).boxed().toArray());
|
||||
}
|
||||
|
||||
/*
|
||||
* This tests the given classical solution without streams
|
||||
*/
|
||||
@Test
|
||||
void disjunctAllNoStream() {
|
||||
IntPredicate alwaysTrue = ComposingPredicate.disjunctAllNoStream(List.of(x -> true));
|
||||
assertTrue(alwaysTrue.test(1), "Test with one predicate only");
|
||||
|
||||
IntPredicate dividableBy2Or3 = ComposingPredicate.disjunctAllNoStream(predicateList);
|
||||
assertArrayEquals(expected.toArray(), testIntegers.filter(dividableBy2Or3).boxed().toArray());
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
testIntegers = IntStream.range(1, 10);
|
||||
expected = List.of(2, 3, 4, 6, 8, 9);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Gradle build configuration for specific lab module / exercise
|
||||
*/
|
||||
// enabled plugins
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
// Project/Module information
|
||||
description = 'Lab06 Streaming'
|
||||
group = 'ch.zhaw.prog2'
|
||||
version = '2022.1'
|
||||
|
||||
// Dependency configuration
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Junit 5 dependencies
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.+'
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.+'
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.+'
|
||||
// Mockito dependencies
|
||||
testImplementation 'org.mockito:mockito-core:4.3.+'
|
||||
}
|
||||
|
||||
// Test task configuration
|
||||
test {
|
||||
// Use JUnit platform for unit tests
|
||||
useJUnitPlatform()
|
||||
// Output results of individual tests
|
||||
testLogging {
|
||||
events "PASSED", "SKIPPED", "FAILED"
|
||||
}
|
||||
}
|
||||
|
||||
// Java plugin configuration
|
||||
java {
|
||||
// By default the Java version of the gradle process is used as source/target version.
|
||||
// This can be overridden, to ensure a specific version. Enable only if required.
|
||||
sourceCompatibility = JavaVersion.VERSION_17 // ensure Java source code compatibility
|
||||
targetCompatibility = JavaVersion.VERSION_17 // version of the created byte-code
|
||||
|
||||
// Java compiler specific options
|
||||
compileJava {
|
||||
// source files should be UTF-8 encoded
|
||||
options.encoding = 'UTF-8'
|
||||
// for more options see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
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 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;
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
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 null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Aufgabe a2)
|
||||
*/
|
||||
public String[] getDistinctLastnamesOfEmployees() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Aufgabe b)
|
||||
* There might be Employees not working for the Company (e.g. temporally)
|
||||
*/
|
||||
public List<Employee> getEmployeesWorkingForCompany() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Aufgabe c) - Test in Klasse CompanyTestStudent
|
||||
*/
|
||||
public List<Employee> getEmployeesByPredicate(Predicate<Employee> filterPredicate) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aufgabe g2)
|
||||
*
|
||||
* Function calculating Payment for January.
|
||||
*/
|
||||
public static final Function<Employee, Payment> paymentForEmployeeJanuary = employee -> {
|
||||
return null;
|
||||
};
|
||||
|
||||
/*
|
||||
* Aufgabe g3)
|
||||
*
|
||||
* 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;
|
||||
};
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import java.util.Currency;
|
||||
|
||||
/**
|
||||
* Information needed to transfer money: IBAN-Number and Currency
|
||||
*/
|
||||
public class BankAccount {
|
||||
public static final String RELAXED_IABN_REGEX = "[A-Z][A-Z][0-9][0-9][A-Z0-9]{1,30}";
|
||||
private Currency currency = Currency.getInstance("CHF");
|
||||
private String ibanNumber;
|
||||
|
||||
/**
|
||||
* Check if {@link #setIbanNumber(String) setIbanNumber} will accept the given ibanNumber.
|
||||
*
|
||||
* @param ibanNumber the IBAN Number to check
|
||||
* @return true, if ibanNumber will be accepted
|
||||
*/
|
||||
public static boolean isIbanAccepted(String ibanNumber) {
|
||||
return removeSpaces(ibanNumber).matches(RELAXED_IABN_REGEX);
|
||||
}
|
||||
|
||||
private static String removeSpaces(String in) {
|
||||
return in.replace(" ", "");
|
||||
}
|
||||
|
||||
public Currency getCurrency() {
|
||||
return currency;
|
||||
}
|
||||
|
||||
public BankAccount setCurrency(Currency currency) {
|
||||
this.currency = currency;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getIbanNumber() {
|
||||
return ibanNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method
|
||||
*
|
||||
* @param ibanNumber must be acceptable, see {@link #isIbanAccepted(String)}
|
||||
* @return this
|
||||
* @throws IllegalIbanNumber if ibanNumber can not be accepted
|
||||
*/
|
||||
public BankAccount setIbanNumber(String ibanNumber) throws IllegalIbanNumber {
|
||||
if (isIbanAccepted(ibanNumber)) {
|
||||
this.ibanNumber = ibanNumber;
|
||||
} else {
|
||||
throw new IllegalArgumentException("IBAN is not accepted");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("BankAccount{");
|
||||
sb.append("currency=").append(currency);
|
||||
sb.append(", ibanNumber='").append(ibanNumber).append('\'');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import java.util.Currency;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.function.IntUnaryOperator;
|
||||
|
||||
/**
|
||||
* Bind currency to the amount
|
||||
*/
|
||||
public class CurrencyAmount {
|
||||
public static final Currency CHF = Currency.getInstance("CHF");
|
||||
private final int amount;
|
||||
private final Currency currency;
|
||||
|
||||
public CurrencyAmount(int amount, Currency currency) {
|
||||
this.amount = amount;
|
||||
this.currency = currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use {@link #CHF} as currency
|
||||
*
|
||||
* @param amount
|
||||
*/
|
||||
public CurrencyAmount(int amount) {
|
||||
this(amount, CHF);
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public Currency getCurrency() {
|
||||
return currency;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new CurrencyAmount based on this object, modified by the modifying function.
|
||||
* @param modifyFunction - function to modify this amount.
|
||||
* @return the new modified CurrencyAmount.
|
||||
*/
|
||||
public CurrencyAmount createModifiedAmount(IntUnaryOperator modifyFunction) {
|
||||
int newAmount = modifyFunction.applyAsInt(this.amount);
|
||||
return new CurrencyAmount(newAmount, this.currency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", CurrencyAmount.class.getSimpleName() + "[", "]")
|
||||
.add("amount=" + amount)
|
||||
.add("currency=" + currency)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import java.util.Currency;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Helper class to change currency
|
||||
*/
|
||||
public class CurrencyChange {
|
||||
private static final Map<String, Double> CURRENCYISOCODE_FACTOR_TO_CHF = Map.of(
|
||||
"CHF", 1.00,
|
||||
"USD", 1.04,
|
||||
"GBP", 0.83,
|
||||
"EUR", 0.94
|
||||
);
|
||||
|
||||
private static double factorFor(String fromIsoCode, String toIsoCode) {
|
||||
return CURRENCYISOCODE_FACTOR_TO_CHF.get(toIsoCode) / CURRENCYISOCODE_FACTOR_TO_CHF.get(fromIsoCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to a new Currency
|
||||
*
|
||||
* Example:
|
||||
* <code>
|
||||
* CurrencyAmount old = new CurrencyAmount (12345, Currency.getInstance("EUR"));
|
||||
* CurrencyAmount newCurrencyAmount = CurrencyChange.getInNewCurrency(old, Currency.getInstance("USD"));
|
||||
* </code>
|
||||
*
|
||||
* @param currencyAmount an amount in given currency
|
||||
* @param newCurrency the target currency
|
||||
* @return new instance with an equivalent value but in the new currency
|
||||
*/
|
||||
public static CurrencyAmount getInNewCurrency(CurrencyAmount currencyAmount, Currency newCurrency) {
|
||||
Objects.requireNonNull(currencyAmount);
|
||||
Objects.requireNonNull(newCurrency);
|
||||
double factor = factorFor(currencyAmount.getCurrency().getCurrencyCode(), newCurrency.getCurrencyCode());
|
||||
long newAmount = Math.round(currencyAmount.getAmount() * factor);
|
||||
return new CurrencyAmount((int) newAmount, newCurrency);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
/**
|
||||
* Thrown when an illegal IBAN (International Bank Account Number) is provided.
|
||||
*/
|
||||
public class IllegalIbanNumber extends Throwable {
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import ch.zhaw.prog2.functional.streaming.humanresource.Person;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* All information needed to pay an amount to a BankAccount of a Person.
|
||||
*/
|
||||
public class Payment {
|
||||
private BankAccount targetAccount;
|
||||
private CurrencyAmount currencyAmount;
|
||||
private Person beneficiary;
|
||||
|
||||
public BankAccount getTargetAccount() {
|
||||
return targetAccount;
|
||||
}
|
||||
|
||||
public Payment setTargetAccount(BankAccount targetAccount) {
|
||||
this.targetAccount = targetAccount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Payment setTargetAccount(Optional<BankAccount> targetAccount) {
|
||||
return setTargetAccount(targetAccount.orElse(null));
|
||||
}
|
||||
|
||||
public CurrencyAmount getCurrencyAmount() {
|
||||
return currencyAmount;
|
||||
}
|
||||
|
||||
public Payment setCurrencyAmount(CurrencyAmount currencyAmount) {
|
||||
this.currencyAmount = currencyAmount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Person getBeneficiary() {
|
||||
return beneficiary;
|
||||
}
|
||||
|
||||
public Payment setBeneficiary(Person beneficiary) {
|
||||
this.beneficiary = beneficiary;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Payment.class.getSimpleName() + "[", "]")
|
||||
.add("targetAccount=" + targetAccount)
|
||||
.add("currencyAmount=" + currencyAmount)
|
||||
.add("beneficiary=" + beneficiary)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
public enum PaymentsPerYear {
|
||||
TWELVE(12),
|
||||
THIRTEEN(13);
|
||||
|
||||
private final int value;
|
||||
|
||||
PaymentsPerYear(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import ch.zhaw.prog2.functional.streaming.humanresource.Person;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* A Payroll (Lohnabrechnung) is principally a List of Payments for the Company.
|
||||
* In this Payroll only one Payment for each beneficiary is allowed
|
||||
*/
|
||||
public class Payroll {
|
||||
private final List<Payment> paymentList = new ArrayList<>();
|
||||
|
||||
public List<Payment> getPaymentList() {
|
||||
return Collections.unmodifiableList(paymentList);
|
||||
}
|
||||
|
||||
/**
|
||||
* This Method will add more Payments to this Payroll an throw an IllegalArgumentException
|
||||
* if we already have a Payment beloging to the same Person in this Payroll.
|
||||
* @param morePayments
|
||||
*/
|
||||
public void addPayments(List<Payment> morePayments) {
|
||||
if (hasSameBeneficiaryInefficient(morePayments)) {
|
||||
throw new IllegalArgumentException("Duplicate Beneficiary detected");
|
||||
} else {
|
||||
paymentList.addAll(morePayments);
|
||||
}
|
||||
}
|
||||
|
||||
// this method is inefficient and should be rewritten by staff (not students)
|
||||
private boolean hasSameBeneficiaryInefficient(List<Payment> paymentListToVerify) {
|
||||
boolean res = false;
|
||||
for (Payment payment : paymentListToVerify) {
|
||||
Person beneficiary = payment.getBeneficiary();
|
||||
for (Payment checkPayment : paymentList) {
|
||||
if (beneficiary.equals(checkPayment.getBeneficiary())) {
|
||||
res = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public Stream<Payment> stream() {
|
||||
return paymentList.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Payroll.class.getSimpleName() + "[", "]")
|
||||
.add("paymentList=" + paymentList)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import ch.zhaw.prog2.functional.streaming.Company;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @param company
|
||||
*/
|
||||
public PayrollCreator(Company company) {
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
/*
|
||||
* Aufgabe d) - Test dazu exisitert in PayrollCreatorTest
|
||||
*/
|
||||
public Payroll getPayrollForAll() {
|
||||
return new Payroll();
|
||||
}
|
||||
|
||||
/*
|
||||
* Aufgabe e) - Test dazu existiert in PayrollCreatorTest
|
||||
*/
|
||||
public static int payrollValueCHF(Payroll payroll) {
|
||||
return 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
|
||||
*/
|
||||
public static List<CurrencyAmount> payrollAmountByCurrency(Payroll payroll) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package ch.zhaw.prog2.functional.streaming.humanresource;
|
||||
|
||||
import ch.zhaw.prog2.functional.streaming.finance.BankAccount;
|
||||
import ch.zhaw.prog2.functional.streaming.finance.CurrencyAmount;
|
||||
import ch.zhaw.prog2.functional.streaming.finance.PaymentsPerYear;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Employee extends Person {
|
||||
private CurrencyAmount yearlySalary;
|
||||
private PaymentsPerYear paymentsPerYear = PaymentsPerYear.THIRTEEN;
|
||||
private BankAccount account;
|
||||
private boolean isWorkingForCompany;
|
||||
|
||||
public Employee(String firstName, String lastName) {
|
||||
super(firstName, lastName);
|
||||
}
|
||||
|
||||
/**
|
||||
* There might be Employees not working for the Company (e.g. temporally)
|
||||
*/
|
||||
public boolean isWorkingForCompany() {
|
||||
return isWorkingForCompany;
|
||||
}
|
||||
|
||||
/**
|
||||
* There might be Employees not working for the Company (e.g. temporally)
|
||||
* @param workingForCompany true if working
|
||||
* @return
|
||||
*/
|
||||
public Employee setWorkingForCompany(boolean workingForCompany) {
|
||||
isWorkingForCompany = workingForCompany;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CurrencyAmount getYearlySalary() {
|
||||
return yearlySalary;
|
||||
}
|
||||
|
||||
public Employee setYearlySalary(CurrencyAmount yearlySalary) {
|
||||
this.yearlySalary = yearlySalary;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PaymentsPerYear getPaymentsPerYear() {
|
||||
return paymentsPerYear;
|
||||
}
|
||||
|
||||
public Employee setPaymentsPerYear(PaymentsPerYear paymentsPerYear) {
|
||||
this.paymentsPerYear = paymentsPerYear;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Optional<BankAccount> getAccount() {
|
||||
return Optional.ofNullable(account);
|
||||
}
|
||||
|
||||
public Employee setAccount(BankAccount account) {
|
||||
this.account = account;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("Employee{");
|
||||
sb.append("yearlySalaryCHF=").append(yearlySalary);
|
||||
sb.append(", paymentsPerYear=").append(paymentsPerYear);
|
||||
sb.append(", account=").append(account);
|
||||
sb.append(", person=").append(super.toString());
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package ch.zhaw.prog2.functional.streaming.humanresource;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Information about a person.
|
||||
*/
|
||||
public class Person {
|
||||
private final String firstName;
|
||||
private final String lastName;
|
||||
private boolean isFemale;
|
||||
private boolean isAlive = true;
|
||||
private UUID uuid;
|
||||
private Person father;
|
||||
private Person mother;
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
constructNonFinalFields();
|
||||
}
|
||||
|
||||
private void constructNonFinalFields() {
|
||||
this.uuid = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return isAlive;
|
||||
}
|
||||
|
||||
public Person setAlive(boolean alive) {
|
||||
isAlive = alive;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Person getFather() {
|
||||
return father;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param father, can be {@code null}
|
||||
* @return this
|
||||
*/
|
||||
public Person setFather(Person father) {
|
||||
this.father = father;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Person getMother() {
|
||||
return mother;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mother, can be {@code null}
|
||||
* @return this
|
||||
*/
|
||||
public Person setMother(Person mother) {
|
||||
this.mother = mother;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return persons name, never {@code null}
|
||||
*/
|
||||
public String getName() {
|
||||
return firstName + " " + lastName;
|
||||
}
|
||||
|
||||
public boolean isFemale() {
|
||||
return isFemale;
|
||||
}
|
||||
|
||||
public Person setFemale(boolean female) {
|
||||
isFemale = female;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Person.class.getSimpleName() + "[", "]")
|
||||
.add("firstName='" + firstName + "'")
|
||||
.add("lastName='" + lastName + "'")
|
||||
.add("isFemale=" + isFemale)
|
||||
.add("isAlive=" + isAlive)
|
||||
.add("uuid=" + uuid)
|
||||
.add("father=" + father)
|
||||
.add("mother=" + mother)
|
||||
.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package ch.zhaw.prog2.functional.streaming;
|
||||
|
||||
import ch.zhaw.prog2.functional.streaming.humanresource.Employee;
|
||||
import ch.zhaw.prog2.functional.streaming.humanresource.EmployeeSupplier;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CompanySupplier implements Supplier<Company> {
|
||||
private final int employeeCount;
|
||||
private final EmployeeSupplier employeeSupplier;
|
||||
|
||||
public CompanySupplier(Random random, int employeeCount) {
|
||||
Objects.requireNonNull(random);
|
||||
this.employeeCount = employeeCount;
|
||||
employeeSupplier = new EmployeeSupplier(random);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Company get() {
|
||||
List<Employee> employeeList = Stream.generate(employeeSupplier).limit(employeeCount).collect(Collectors.toList());
|
||||
return new Company(employeeList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package ch.zhaw.prog2.functional.streaming;
|
||||
|
||||
import ch.zhaw.prog2.functional.streaming.finance.Payment;
|
||||
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 java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
|
||||
class CompanyTest {
|
||||
// These variables are not private because some tests are in the class CompanyTestStudent.
|
||||
static final long RANDOM_SEED = 113L;
|
||||
static final int EMPLOYEE_COUNT = 500;
|
||||
private Company testCompany;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
Random random = new Random(RANDOM_SEED);
|
||||
CompanySupplier companySupplier = new CompanySupplier(random, EMPLOYEE_COUNT);
|
||||
testCompany = companySupplier.get();
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> new Company(null), "null in constuctor not allowed");
|
||||
List<Employee> employeeList = testCompany.getAllEmployees();
|
||||
assertNotNull(employeeList);
|
||||
assertEquals(EMPLOYEE_COUNT, testCompany.getAllEmployees().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllEmployees() {
|
||||
assertEquals(EMPLOYEE_COUNT, testCompany.getAllEmployees().size(), "testCompany has given count of employees");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPayments() {
|
||||
List<Payment> paymentList = testCompany.getPayments(employee -> false);
|
||||
assertNotNull(paymentList, "You have to implement the tested method");
|
||||
assertEquals(0, paymentList.size(), "No payments if Predicate evaluates always true");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDistinctFirstnamesOfEmployees() {
|
||||
List<String> names = testCompany.getDistinctFirstnamesOfEmployees();
|
||||
assertNotNull(names, "You have to implement the tested method");
|
||||
assertEquals(28, names.size(), "default company has given number of distinct first names");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDistinctLastnamesOfEmployees() {
|
||||
String[] names = testCompany.getDistinctLastnamesOfEmployees();
|
||||
assertNotNull(names, "You have to implement the tested method");
|
||||
assertEquals(21, names.length, "default company has given number of distinct last names");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getEmployeesWorkingForCompany() {
|
||||
List<Employee> workingEmployees = testCompany.getEmployeesWorkingForCompany();
|
||||
assertNotNull(workingEmployees, "You have to implement the tested method");
|
||||
assertTrue(workingEmployees.size() >= 400, "default company has at least 400 working employees");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetPayments() {
|
||||
Payment dummyPayment = mock(Payment.class);
|
||||
List<Payment> paymentList = testCompany.getPayments(employee -> false, employee -> dummyPayment);
|
||||
assertNotNull(paymentList, "You have to implement the tested method");
|
||||
assertEquals(List.of(), paymentList, "no employees");
|
||||
|
||||
Predicate<Employee> allEmployee = employee -> true;
|
||||
paymentList = testCompany.getPayments(allEmployee, employee -> dummyPayment);
|
||||
assertEquals(EMPLOYEE_COUNT, paymentList.size(), "every employee gets payment");
|
||||
assertTrue(paymentList.stream().allMatch(payment -> payment == dummyPayment), "all payments are dummy payments");
|
||||
|
||||
long januaryAmountSum = getAmountSum(testCompany.getPayments(allEmployee, Company.paymentForEmployeeJanuary));
|
||||
long decemberAmountSum = getAmountSum(testCompany.getPayments(allEmployee, Company.paymentForEmployeeDecember));
|
||||
long sumByMonth = 11 * januaryAmountSum + decemberAmountSum;
|
||||
|
||||
Function<Employee, Payment> yearlySalary = employee -> new Payment()
|
||||
.setCurrencyAmount(employee.getYearlySalary())
|
||||
.setBeneficiary(employee);
|
||||
|
||||
paymentList = testCompany.getPayments(employee -> true, yearlySalary);
|
||||
long yearlyAmountSum = CompanyTest.this.getAmountSum(paymentList);
|
||||
assertEquals(sumByMonth, yearlyAmountSum, EMPLOYEE_COUNT * 12, "sum of monthly payments have to match yearly sum");
|
||||
}
|
||||
|
||||
private long getAmountSum(List<Payment> paymentList) {
|
||||
return paymentList.stream()
|
||||
.mapToInt(payment -> payment.getCurrencyAmount().getAmount())
|
||||
.sum();
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
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.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.
|
||||
*
|
||||
* ✅ This class should be worked on by students.
|
||||
*/
|
||||
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() {
|
||||
// TODO write your test
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import java.util.Currency;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class BankAccountSupplier implements Supplier<BankAccount> {
|
||||
private final Random random;
|
||||
private final CurrencySupplier currencySupplier;
|
||||
|
||||
public BankAccountSupplier(Random random) {
|
||||
Objects.requireNonNull(random);
|
||||
this.random = new Random(random.nextLong());
|
||||
currencySupplier = new CurrencySupplier(random);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BankAccount get() {
|
||||
BankAccount bankAccount = new BankAccount();
|
||||
Currency currency = currencySupplier.get();
|
||||
try {
|
||||
StringBuilder iban = new StringBuilder(currency.getCurrencyCode().substring(0, 2));
|
||||
iban.append(random.nextInt(90) + 10).append(" ");
|
||||
iban.append(random.nextInt(1_000_000_000)).append(" ");
|
||||
iban.append(random.nextInt(1_000_000_000));
|
||||
bankAccount.setCurrency(currency).setIbanNumber(iban.toString());
|
||||
} catch (IllegalIbanNumber illegalIbanNumber) {
|
||||
illegalIbanNumber.printStackTrace();
|
||||
}
|
||||
return bankAccount;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class BankAccountSupplierTest {
|
||||
private static final long RANDOM_SEED = 119L;
|
||||
|
||||
@Test
|
||||
void get() {
|
||||
Random random = new Random(RANDOM_SEED);
|
||||
BankAccountSupplier bankAccountSupplier = new BankAccountSupplier(random);
|
||||
int sampleSize = 10;
|
||||
long distinct = Stream.generate(bankAccountSupplier).limit(sampleSize).limit(sampleSize)
|
||||
.map(account -> account.getIbanNumber()).distinct().count();
|
||||
assertEquals(sampleSize, distinct, "all generated iban number have to differ");
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class BankAccountTest {
|
||||
|
||||
@Test
|
||||
void isIbanAccepted_valid() {
|
||||
List<String> accepted = List.of("BE71 0961 2345 6769", "FR76 3000 6000 0112 3456 7890 189" ,
|
||||
"DE91 1000 0000 0123 4567 89", "CH10 00230 00A109822346");
|
||||
accepted.forEach(iban ->
|
||||
assertTrue(BankAccount.isIbanAccepted(iban), iban + " is a valid IBAN"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isIbanAccepted_invalid() {
|
||||
List<String> invalid = List.of("BE71 0961 2345 6769 9999 8888 7777 6666 123", "BE71", "CH10");
|
||||
invalid.forEach(iban ->
|
||||
assertFalse(BankAccount.isIbanAccepted(iban), iban + " is not a valid IBAN"));
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class CurrencyAmountSupplier implements Supplier<CurrencyAmount> {
|
||||
private final Random random;
|
||||
private final int maxAmount;
|
||||
private final CurrencySupplier currencySupplier;
|
||||
|
||||
public CurrencyAmountSupplier(Random random, int maxAmount) {
|
||||
Objects.requireNonNull(random);
|
||||
this.random = random;
|
||||
this.maxAmount = maxAmount;
|
||||
this.currencySupplier = new CurrencySupplier(random);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CurrencyAmount get() {
|
||||
return new CurrencyAmount(random.nextInt(maxAmount), currencySupplier.get());
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class CurrencyAmountTest {
|
||||
public static final int TEST_AMOUNT = 4321;
|
||||
|
||||
@Test
|
||||
void constructor() {
|
||||
CurrencyAmount currencyAmount = new CurrencyAmount(TEST_AMOUNT);
|
||||
assertEquals(CurrencyAmount.CHF, currencyAmount.getCurrency());
|
||||
assertEquals(TEST_AMOUNT, currencyAmount.getAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createModifiedAmount() {
|
||||
CurrencyAmount currencyAmount = new CurrencyAmount(TEST_AMOUNT);
|
||||
int factor = 17;
|
||||
CurrencyAmount newAmount = currencyAmount.createModifiedAmount(x -> x * factor);
|
||||
assertEquals(TEST_AMOUNT * factor, newAmount.getAmount());
|
||||
assertEquals(currencyAmount.getCurrency(), newAmount.getCurrency());
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Currency;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class CurrencyChangeTest {
|
||||
private static final CurrencyAmount gbp113 = new CurrencyAmount(113, Currency.getInstance("GBP"));
|
||||
private static final Currency CURRENCY_CHF = Currency.getInstance("CHF");
|
||||
private static final Currency CURRENCY_USD = Currency.getInstance("USD");
|
||||
private static final Currency CURRENCY_GBP = Currency.getInstance("GBP");
|
||||
|
||||
@Test
|
||||
void getInNewCurrency() {
|
||||
CurrencyAmount unchanged = CurrencyChange.getInNewCurrency(gbp113, CURRENCY_GBP);
|
||||
assertEquals(gbp113.getAmount(), unchanged.getAmount(), "no conversion keeps value");
|
||||
assertEquals(CURRENCY_GBP, unchanged.getCurrency(), "target currency is GBP");
|
||||
|
||||
CurrencyAmount newCurrencyAmount = CurrencyChange.getInNewCurrency(gbp113, CURRENCY_CHF);
|
||||
// TODO fix magic numbers - CurrencyChange should get map with factors in constructor
|
||||
assertEquals(113 / 0.83, newCurrencyAmount.getAmount() * 1.0, 0.5);
|
||||
assertEquals("CHF", newCurrencyAmount.getCurrency().getCurrencyCode());
|
||||
|
||||
newCurrencyAmount = CurrencyChange.getInNewCurrency(gbp113, CURRENCY_USD);
|
||||
// TODO fix magic numbers - CurrencyChange should get map with factors in constructor
|
||||
assertEquals(113 / 0.83 * 1.04, newCurrencyAmount.getAmount() * 1.0, 0.5);
|
||||
assertEquals("USD", newCurrencyAmount.getCurrency().getCurrencyCode());
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import java.util.Currency;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class CurrencySupplier implements Supplier<Currency> {
|
||||
// Java 14 knows 228 Currencies - we only want some of them
|
||||
private static final Set<String> CURRENCY_ISOCODES = Set.of("CHF", "EUR", "GBP", "USD");
|
||||
private static final Currency[] CURRENCIES = Currency.getAvailableCurrencies().stream()
|
||||
.filter(currency -> CurrencySupplier.CURRENCY_ISOCODES.contains(currency.getCurrencyCode()))
|
||||
.toArray(Currency[]::new);
|
||||
private final Random random;
|
||||
|
||||
public CurrencySupplier(Random random) {
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Currency get() {
|
||||
return CURRENCIES[random.nextInt(CURRENCIES.length)];
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class CurrencySupplierTest {
|
||||
private static final long RANDOM_SEED = 1173L;
|
||||
|
||||
@Test
|
||||
void get() {
|
||||
Random random = new Random(RANDOM_SEED);
|
||||
CurrencySupplier currencySupplier = new CurrencySupplier(random);
|
||||
int sampleSize = 10;
|
||||
long distinct = Stream.generate(currencySupplier).limit(sampleSize).distinct().count();
|
||||
assertTrue(distinct > 2, "At least two different currencies expected");
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
class PaymentTest {
|
||||
private Payment payment;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
payment = new Payment();
|
||||
}
|
||||
|
||||
@Test
|
||||
void setTargetAccount() {
|
||||
Optional<BankAccount> account = Optional.empty();
|
||||
payment.setTargetAccount(account);
|
||||
assertNull(payment.getTargetAccount());
|
||||
|
||||
BankAccount realAccount = new BankAccount();
|
||||
account = Optional.of(realAccount);
|
||||
payment.setTargetAccount(account);
|
||||
assertEquals(realAccount, payment.getTargetAccount(), "get stored account value");
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import ch.zhaw.prog2.functional.streaming.Company;
|
||||
import ch.zhaw.prog2.functional.streaming.CompanySupplier;
|
||||
import ch.zhaw.prog2.functional.streaming.humanresource.Employee;
|
||||
import ch.zhaw.prog2.functional.streaming.humanresource.EmployeeSupplier;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PayrollCreatorTest {
|
||||
// not private, so they can be used student test class PayRollCreatorTestStudent
|
||||
static final long RANDOM_SEED = 5113L;
|
||||
static final int EMPLOYEE_COUNT = 400;
|
||||
|
||||
@Test
|
||||
void getPayrollForAll() {
|
||||
EmployeeSupplier employeeSupplier = new EmployeeSupplier(new Random(RANDOM_SEED));
|
||||
// non working employee
|
||||
Employee employee = employeeSupplier.get();
|
||||
employee.setWorkingForCompany(true);
|
||||
PayrollCreator payrollCreatorOneEmployee = new PayrollCreator(new Company(List.of(employee)));
|
||||
Payroll payroll = payrollCreatorOneEmployee.getPayrollForAll();
|
||||
assertEquals(1, payroll.getPaymentList().size(), "one working employee, one payment");
|
||||
employee.setWorkingForCompany(false);
|
||||
payroll = payrollCreatorOneEmployee.getPayrollForAll();
|
||||
assertEquals(0, payroll.getPaymentList().size(), "no working employees, no payments");
|
||||
}
|
||||
|
||||
@Test
|
||||
void payrollValueCHF() {
|
||||
Company testCompany = new CompanySupplier(new Random(RANDOM_SEED), EMPLOYEE_COUNT).get();
|
||||
PayrollCreator payrollCreator = new PayrollCreator(testCompany);
|
||||
Payroll payroll = payrollCreator.getPayrollForAll();
|
||||
int paysum = PayrollCreator.payrollValueCHF(payroll);
|
||||
assertTrue(paysum > 100000);
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public class PayrollCreatorTestStudent {
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ch.zhaw.prog2.functional.streaming.finance;
|
||||
|
||||
import ch.zhaw.prog2.functional.streaming.humanresource.Person;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class PayrollTest {
|
||||
private static final Person adam = new Person("Adam", "First");
|
||||
|
||||
@Test
|
||||
void addPayments() {
|
||||
Payment firstPayment = new Payment().setBeneficiary(adam);
|
||||
Payroll payroll = new Payroll();
|
||||
List<Payment> paymentList = new ArrayList<>(1);
|
||||
paymentList.add(firstPayment);
|
||||
payroll.addPayments(paymentList);
|
||||
assertIterableEquals(paymentList, payroll.getPaymentList());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
payroll.addPayments(paymentList), "detect duplicate beneficiary");
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package ch.zhaw.prog2.functional.streaming.humanresource;
|
||||
|
||||
import ch.zhaw.prog2.functional.streaming.finance.BankAccountSupplier;
|
||||
import ch.zhaw.prog2.functional.streaming.finance.CurrencyAmountSupplier;
|
||||
import ch.zhaw.prog2.functional.streaming.finance.PaymentsPerYear;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
import java.util.function.IntPredicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class EmployeeSupplier implements Supplier<Employee> {
|
||||
public static final short PERCENTAGE_OF_WORKING_EMPLOYEES = 90;
|
||||
public static final short PERCENTAGE_FEMALE = 50;
|
||||
private static final short PERCENTAGE_13_MONTH_PAYMENT = 50;
|
||||
private static final int SALARY_MAX = 100000;
|
||||
private static final String[] FIRSTNAMES = {
|
||||
"Lowri", "Molly", "Ria", "Irene", "Hazel", "Yasmin", "Alexia",
|
||||
"Kenneth", "Yasin", "Gerald", "Ciaran", "Rocco", "Glenn", "Bailey",
|
||||
"Evelyn", "Penelope", "Darcie", "Ellie-May", "Rhonda", "Lana", "Heather",
|
||||
"Raphael", "Oscar", "Liam", "Robert", "Declan", "Leroy", "Aiden"
|
||||
};
|
||||
private static final String[] LASTNAMES = {
|
||||
"Lamb", "Evans", "Rowe", "Ford", "Paul", "Turner", "Miller",
|
||||
"Peters", "Wang", "Davis", "Burton", "Faulkner", "Griffiths", "Owens",
|
||||
"O'Reilly", "Jacobs", "Sherman", "Howells", "Walters", "Warner", "Schroeder"
|
||||
};
|
||||
private final Random random;
|
||||
private final IntPredicate randomTrueForPercentage;
|
||||
private final CurrencyAmountSupplier currencyAmountSupplier;
|
||||
private final BankAccountSupplier accountSupplier;
|
||||
|
||||
public EmployeeSupplier(Random random) {
|
||||
Objects.requireNonNull(random);
|
||||
this.random = new Random(random.nextLong());
|
||||
accountSupplier = new BankAccountSupplier(new Random(random.nextLong()));
|
||||
currencyAmountSupplier = new CurrencyAmountSupplier(new Random(random.nextLong()), SALARY_MAX);
|
||||
randomTrueForPercentage = percentTrue -> this.random.nextInt(100) + 1 <= percentTrue;
|
||||
}
|
||||
|
||||
private String selectOne(String[] values) {
|
||||
int index = random.nextInt(values.length);
|
||||
return values[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Employee get() {
|
||||
PaymentsPerYear paymentsPerYear = randomTrueForPercentage.test(PERCENTAGE_13_MONTH_PAYMENT) ?
|
||||
PaymentsPerYear.THIRTEEN : PaymentsPerYear.TWELVE;
|
||||
|
||||
Employee newEmployee = new Employee(selectOne(FIRSTNAMES), selectOne(LASTNAMES))
|
||||
.setWorkingForCompany(randomTrueForPercentage.test(PERCENTAGE_OF_WORKING_EMPLOYEES))
|
||||
.setPaymentsPerYear(paymentsPerYear)
|
||||
.setYearlySalary(currencyAmountSupplier.get())
|
||||
.setAccount(accountSupplier.get());
|
||||
newEmployee.setFemale(randomTrueForPercentage.test(PERCENTAGE_FEMALE));
|
||||
return newEmployee;
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ch.zhaw.prog2.functional.streaming.humanresource;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EmployeeSupplierTest {
|
||||
private static final long RANDOM_SEED = 42L;
|
||||
|
||||
@Test
|
||||
void get() {
|
||||
Random random = new Random(RANDOM_SEED);
|
||||
EmployeeSupplier employeeSupplier = new EmployeeSupplier(random);
|
||||
Employee firstEmployee = employeeSupplier.get();
|
||||
Employee secondEmployee = employeeSupplier.get();
|
||||
assertNotEquals(firstEmployee.getName(), secondEmployee.getName(), "Generated Employees have to differ");
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package ch.zhaw.prog2.functional.streaming.humanresource;
|
||||
|
||||
import ch.zhaw.prog2.functional.streaming.finance.BankAccount;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EmployeeTest {
|
||||
private static final String TEST_FIRSTNAME = "Valeria";
|
||||
private static final String TEST_LASTNAME = "Sherman";
|
||||
private final Employee defaultEmployee = new Employee(TEST_FIRSTNAME, TEST_LASTNAME);
|
||||
|
||||
@Test
|
||||
void getYearlySalary() {
|
||||
assertNull(defaultEmployee.getYearlySalary());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAccount() {
|
||||
Optional<BankAccount> account = defaultEmployee.getAccount();
|
||||
assertTrue(account.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void setAccount() {
|
||||
BankAccount account = new BankAccount();
|
||||
Employee employee = new Employee(TEST_FIRSTNAME, TEST_LASTNAME);
|
||||
employee.setAccount(account);
|
||||
assertEquals(account, employee.getAccount().orElse(null));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
:source-highlighter: coderay
|
||||
:icons: font
|
||||
|
||||
= Lösungsblatt zum Praktikum Functional Programming
|
||||
|
||||
Diese Datei ist als Hilfsmittel für Sie gedacht.
|
||||
Sie brauchen die Datei nicht zu verwenden, wenn Sie nicht möchten.
|
||||
|
||||
== 1. Die Functional Interfaces
|
||||
|
||||
[loweralpha]
|
||||
. Welche Interfaces aus dem Package `java.util.function` können Sie alles nutzen, um
|
||||
- die mathematische Funktion f(x) = x ^ 2 - 3 für Zahlen des Typs `long` abzubilden?
|
||||
[numbered]
|
||||
.. Lösung 1
|
||||
.. Lösung 2
|
||||
.. Lösung 3
|
||||
.. Lösung 4
|
||||
.. Lösung 5
|
||||
|
||||
- um den Zinsfaktor (double) für n (int) Jahre bei einem Zinssatz von p Prozent (float) zu berechnen mit der Formel
|
||||
zf = (1 + p / 100)^n ?
|
||||
[numbered]
|
||||
.. Lösung 1
|
||||
.. Lösung 2
|
||||
|
||||
- ein Objekt vom Typ `Person` (ohne Parameter) zu generieren?
|
||||
[numbered]
|
||||
.. Lösung
|
||||
|
||||
- Welche Eigenschaft muss eine Funktion haben, damit Sie ein eigenes Interface schreiben müssen,
|
||||
also keines der in `java.util.function` vorhandenen Interfaces verwenden können?
|
||||
[numbered]
|
||||
.. Lösung
|
||||
|
||||
. Welche der Aussagen stimmen für ein funktionales Interface?
|
||||
** [x] Ankreuzen mit x in [ ]
|
||||
** [ ] Es ist ein Java-Interface (Schlüsselwort `interface` im Code)
|
||||
** [ ] Es hat **genau eine** abstrakte Methode
|
||||
** [ ] Das Interface **muss** mit `@FunctionalInterface` markiert sein
|
||||
** [ ] Es hat **keine** default-Methoden (Schlüsselwort `default`)
|
||||
. Welche Aussagen stimmen?
|
||||
** [ ] Zu **jedem** funktionalen Interface können Lambda-Ausdrücke (_lambda expressions_) geschrieben werden
|
||||
** [ ] Ein Lambda-Ausdruck kann **ohne** passendes funktionales Interface erstellt werden
|
||||
** [ ] Eine Variable vom Typ `Optional` kann nie `null` sein.
|
||||
|
||||
|
||||
== 2. Übungen auf der Stepik-Plattform
|
||||
|
||||
=== Übungen zu Functional Interface und Lambda Expression
|
||||
. Identify the correct lambdas and method references
|
||||
Korrekt sind
|
||||
* ...
|
||||
|
||||
. Writing simple lambda expressions
|
||||
+
|
||||
[source, Java]
|
||||
----
|
||||
// java function
|
||||
----
|
||||
|
||||
. Too many arguments
|
||||
+
|
||||
[source, Java]
|
||||
----
|
||||
// java function
|
||||
----
|
||||
|
||||
=== Übungen mit Streams
|
||||
|
||||
. Composing predicates
|
||||
+
|
||||
[source, Java]
|
||||
----
|
||||
// java code
|
||||
----
|
||||
|
||||
== 3. Design Pattern _Chain of responsibility_
|
||||
|
||||
[source, Java]
|
||||
----
|
||||
// java code
|
||||
----
|
||||
|
||||
== 4. Company Payroll
|
||||
|
||||
Lösung im Code-Repository.
|
||||
Reference in New Issue
Block a user