Copy docker run --rm zchencow/innozverse-java:latest sh -c "
cat > /tmp/Lab03.java << 'EOF'
import java.util.*;
import java.util.function.*;
public class Lab03 {
@FunctionalInterface interface PriceTransformer {
double apply(double price, double factor);
}
@FunctionalInterface interface Validator<T> {
boolean test(T t);
default Validator<T> and(Validator<T> other) { return v -> test(v) && other.test(v); }
default Validator<T> or(Validator<T> other) { return v -> test(v) || other.test(v); }
default Validator<T> negate() { return v -> !test(v); }
}
record Product(String name, double price, int stock) {}
static Optional<Product> findCheapest(List<Product> products, double maxPrice) {
return products.stream().filter(p -> p.price() <= maxPrice)
.min(Comparator.comparingDouble(Product::price));
}
public static void main(String[] args) {
PriceTransformer discount = (price, pct) -> Math.round(price * (1 - pct) * 100) / 100.0;
PriceTransformer markup = (price, factor) -> price * factor;
System.out.println(\"Discounted: \$\" + discount.apply(864.0, 0.15));
System.out.println(\"Marked up: \$\" + markup.apply(864.0, 1.20));
Validator<Product> hasStock = p -> p.stock() > 0;
Validator<Product> affordable = p -> p.price() < 200;
Validator<Product> both = hasStock.and(affordable);
var products = List.of(
new Product(\"Surface Pro\", 864.0, 15),
new Product(\"Surface Pen\", 49.99, 80),
new Product(\"USB-C Hub\", 29.99, 0),
new Product(\"Office 365\", 99.99, 999)
);
products.stream().filter(both::test)
.forEach(p -> System.out.println(\" Affordable+stock: \" + p.name()));
// Optional chaining
var cheap = findCheapest(products, 60.0);
System.out.println(cheap.map(p -> p.name() + \" @ \$\" + p.price()).orElse(\"Not found\"));
System.out.println(findCheapest(products, 10.0).orElse(null));
cheap.ifPresent(p -> System.out.println(\"Got: \" + p.name()));
var fallback = findCheapest(products, 10.0)
.or(() -> Optional.of(new Product(\"Default\", 0.0, 0)));
System.out.println(\"Fallback: \" + fallback.get().name());
// Method references
Function<String, String> upper = String::toUpperCase;
Comparator<Product> byPrice = Comparator.comparingDouble(Product::price);
products.stream().sorted(byPrice).map(Product::name).map(upper)
.forEach(n -> System.out.print(n + \" \"));
System.out.println();
// Built-in interfaces
Predicate<Integer> isEven = n -> n % 2 == 0;
Predicate<Integer> isPos = n -> n > 0;
System.out.println(isEven.and(isPos).test(4) + \" \" + isEven.and(isPos).test(-2));
BiFunction<String, Double, String> label = (name, price) -> name + \"=\$\" + price;
System.out.println(label.apply(\"Surface Pro\", 864.0));
UnaryOperator<Double> round2 = d -> Math.round(d * 100) / 100.0;
System.out.println(round2.apply(864.999));
// Function composition
Function<Double, Double> applyTax = p -> p * 1.08;
Function<Double, Double> applyDiscount = p -> p * 0.90;
Function<Double, Double> pipeline = applyDiscount.andThen(applyTax).andThen(round2);
System.out.println(\"Pipeline \$864 → \$\" + pipeline.apply(864.0));
// Supplier & Consumer
Supplier<Product> defaultProduct = () -> new Product(\"Default\", 0.0, 0);
Consumer<Product> print = p -> System.out.println(\"Product: \" + p.name() + \" @\$\" + p.price());
Consumer<Product> logStock = p -> System.out.println(\"Stock: \" + p.stock());
Consumer<Product> both2 = print.andThen(logStock);
both2.accept(defaultProduct.get());
}
}
EOF
javac /tmp/Lab03.java -d /tmp && java -cp /tmp Lab03"