From 33c91a809c94fa9ec941ef547abfde0f6742960d Mon Sep 17 00:00:00 2001 From: Dawid Urbaniak Date: Fri, 21 Jun 2024 13:32:24 +0200 Subject: [PATCH] Inital commit --- .gitignore | 29 +++++++++++ .idea/.gitignore | 8 +++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 +++ Paradygmaty_Programowania_2.iml | 11 ++++ src/BiConsumerType.java | 20 ++++++++ src/BiFunctionType.java | 8 +++ src/BiPredicateType.java | 7 +++ src/BinaryOperatorType.java | 6 +++ src/ConsumerType.java | 13 +++++ src/FunctionType.java | 8 +++ src/Main.java | 89 +++++++++++++++++++++++++++++++++ src/PredicateType.java | 6 +++ src/PremierLeaguePlayer.java | 64 ++++++++++++++++++++++++ src/SupplierType.java | 8 +++ src/ToIntType.java | 7 +++ src/UnaryType.java | 7 +++ 17 files changed, 305 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 Paradygmaty_Programowania_2.iml create mode 100644 src/BiConsumerType.java create mode 100644 src/BiFunctionType.java create mode 100644 src/BiPredicateType.java create mode 100644 src/BinaryOperatorType.java create mode 100644 src/ConsumerType.java create mode 100644 src/FunctionType.java create mode 100644 src/Main.java create mode 100644 src/PredicateType.java create mode 100644 src/PremierLeaguePlayer.java create mode 100644 src/SupplierType.java create mode 100644 src/ToIntType.java create mode 100644 src/UnaryType.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..69ace3f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..78b8941 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Paradygmaty_Programowania_2.iml b/Paradygmaty_Programowania_2.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Paradygmaty_Programowania_2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/BiConsumerType.java b/src/BiConsumerType.java new file mode 100644 index 0000000..571283b --- /dev/null +++ b/src/BiConsumerType.java @@ -0,0 +1,20 @@ +import java.util.Arrays; +import java.util.function.BiConsumer; + +public class BiConsumerType { + public BiConsumer goalsOnTheSameTime() { + return (i,k) -> { + if (i.length != k.length) { + System.out.println("False"); + } else { + for (int j = 0; j < i.length; j++) { + if (i[j] != k[j]) { + System.out.println("False"); + return; + } + } + System.out.println("True"); + } + }; + } +} diff --git a/src/BiFunctionType.java b/src/BiFunctionType.java new file mode 100644 index 0000000..82e499b --- /dev/null +++ b/src/BiFunctionType.java @@ -0,0 +1,8 @@ +import java.util.Arrays; +import java.util.function.BiFunction; + +public class BiFunctionType { + public BiFunction sumOfTwoPlayersGoals() { + return (array1, array2) -> Arrays.stream(array1).sum() + Arrays.stream(array2).sum(); + } +} diff --git a/src/BiPredicateType.java b/src/BiPredicateType.java new file mode 100644 index 0000000..12abbda --- /dev/null +++ b/src/BiPredicateType.java @@ -0,0 +1,7 @@ +import java.util.function.BiPredicate; + +public class BiPredicateType { + public BiPredicate goalsMoreThanMatches() { + return (g, m) -> g > m; + } +} diff --git a/src/BinaryOperatorType.java b/src/BinaryOperatorType.java new file mode 100644 index 0000000..6d78ae9 --- /dev/null +++ b/src/BinaryOperatorType.java @@ -0,0 +1,6 @@ +import java.util.function.BinaryOperator; +public class BinaryOperatorType { + public BinaryOperator passesPerGoal() { + return (g, p) -> p/g; + } +} diff --git a/src/ConsumerType.java b/src/ConsumerType.java new file mode 100644 index 0000000..a17eb48 --- /dev/null +++ b/src/ConsumerType.java @@ -0,0 +1,13 @@ +import java.util.function.Consumer; + +public class ConsumerType { + public Consumer isDefender() { + return (i) -> { + if (i.equals("defender")) { + System.out.println("Yes"); + } else { + System.out.println("No"); + } + }; + } +} diff --git a/src/FunctionType.java b/src/FunctionType.java new file mode 100644 index 0000000..7c5d293 --- /dev/null +++ b/src/FunctionType.java @@ -0,0 +1,8 @@ +import java.util.Arrays; +import java.util.function.Function; + +public class FunctionType { + public Function goalsSum() { + return sum -> Arrays.stream(sum).sum(); + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..3eb1cdc --- /dev/null +++ b/src/Main.java @@ -0,0 +1,89 @@ +import java.util.Date; +import java.util.function.*; + +public class Main { + public static void main(String[] args) { + PremierLeaguePlayer playerOne = new PremierLeaguePlayer(34, "defender"); + PremierLeaguePlayer playerTwo = new PremierLeaguePlayer(30, "forward"); + + System.out.print("Scores goals on the same time: "); + BiConsumerType biConsumerType = new BiConsumerType(); + biConsumerType.goalsOnTheSameTime().accept(playerOne.getMinutesOfGoals(), playerOne.getMinutesOfGoals()); + System.out.println("---------------------------"); + + System.out.print("Sum goals of both players: "); + BiFunctionType biFunctionType = new BiFunctionType(); + BiFunction sum = biFunctionType.sumOfTwoPlayersGoals(); + int goalsSum = sum.apply(playerOne.getGoalsInMatches(), playerTwo.getGoalsInMatches()); + System.out.println(goalsSum); + System.out.println("---------------------------"); + + BinaryOperatorType binaryOperatorType = new BinaryOperatorType(); + BinaryOperator passesPerGoalRatio = binaryOperatorType.passesPerGoal(); + + Double playerOnePassesPerGoal = passesPerGoalRatio.apply((double) playerOne.getTotalGoals(), (double) playerOne.getTotalPasses()); + System.out.print("Passes per goal for Player One: "); + System.out.println(playerOnePassesPerGoal); + Double playerTwoPassesPerGoal = passesPerGoalRatio.apply((double) playerTwo.getTotalGoals(), (double) playerTwo.getTotalPasses()); + System.out.print("Passes per goal for Player Two: "); + System.out.println(playerTwoPassesPerGoal); + System.out.println("---------------------------"); + + BiPredicateType biPredicateType = new BiPredicateType(); + BiPredicate goalsMoreThanMatchesBoolean = biPredicateType.goalsMoreThanMatches(); + System.out.print("Player One has more goals than matches: "); + boolean playerOneMoreGoalsThanMatches = goalsMoreThanMatchesBoolean.test(playerOne.getTotalGoals(), playerOne.getMatchesPlayed()); + System.out.println(playerOneMoreGoalsThanMatches); + System.out.print("Player Two has more goals than matches: "); + boolean playerTwoMoreGoalsThanMatches = goalsMoreThanMatchesBoolean.test(playerTwo.getTotalGoals(), playerTwo.getMatchesPlayed()); + System.out.println(playerTwoMoreGoalsThanMatches); + System.out.println("---------------------------"); + + ConsumerType consumerType = new ConsumerType(); + Consumer isDefenderFunction = consumerType.isDefender(); + System.out.print("Is Player One a defender: "); + isDefenderFunction.accept(playerOne.getPosition()); + System.out.print("Is Player Two a defender: "); + isDefenderFunction.accept(playerTwo.getPosition()); + System.out.println("---------------------------"); + + FunctionType functionType = new FunctionType(); + Function goalsSumFunction = functionType.goalsSum(); + int playerOneGoals = goalsSumFunction.apply(playerOne.getGoalsInMatches()); + System.out.println("Goals sum for Player One: " + playerOneGoals); + int playerTwoGoals = goalsSumFunction.apply(playerTwo.getGoalsInMatches()); + System.out.println("Goals sum for Player Two: " + playerTwoGoals); + System.out.println("---------------------------"); + + PredicateType predicateType = new PredicateType(); + Predicate hasAtLeastThreeMatchPlayedFunction = predicateType.hasAtLeastThreeMatchPlayed(); + boolean player1HasAtLeastThreeMatches = hasAtLeastThreeMatchPlayedFunction.test(playerOne.getMatchesPlayed()); + System.out.println("Player One played at least three matches: " + player1HasAtLeastThreeMatches); + boolean player2HasAtLeastThreeMatches = hasAtLeastThreeMatchPlayedFunction.test(playerTwo.getMatchesPlayed()); + System.out.println("Player Two played at least three matches: " + player2HasAtLeastThreeMatches); + System.out.println("---------------------------"); + + ToIntType toIntType = new ToIntType(); + ToIntFunction secondsPlayedFunction = toIntType.secondsPlayed(); + int playerOneSecondsPlayed = secondsPlayedFunction.applyAsInt(playerOne.getMinutesPlayed()); + System.out.println("Player One minutes played in seconds: " + playerOneSecondsPlayed); + int playerTwoSecondsPlayed = secondsPlayedFunction.applyAsInt(playerTwo.getMinutesPlayed()); + System.out.println("Player Two minutes played in seconds: " + playerTwoSecondsPlayed); + System.out.println("---------------------------"); + + UnaryType unaryType = new UnaryType(); + UnaryOperator goalsPerMatchFunction = unaryType.goalsPerMatch(); + double playerOneGoalsPerMatch = goalsPerMatchFunction.apply((double) playerOne.getTotalGoals()); + System.out.println("Player One goals per match: " + playerOneGoalsPerMatch); + double playerTwoGoalsPerMatch = goalsPerMatchFunction.apply((double)playerTwo.getTotalGoals()); + System.out.println("Player Two goals per match: " + playerTwoGoalsPerMatch); + System.out.println("---------------------------"); + + SupplierType supplierType = new SupplierType(); + Supplier dateSupplier = supplierType.reportDate(); + Date reportDate = dateSupplier.get(); + System.out.println("Report Date: " + reportDate); + } + + +} \ No newline at end of file diff --git a/src/PredicateType.java b/src/PredicateType.java new file mode 100644 index 0000000..66a39fd --- /dev/null +++ b/src/PredicateType.java @@ -0,0 +1,6 @@ +import java.util.function.Predicate; +public class PredicateType { + public Predicate hasAtLeastThreeMatchPlayed() { + return i -> i >= 3; + } +} diff --git a/src/PremierLeaguePlayer.java b/src/PremierLeaguePlayer.java new file mode 100644 index 0000000..abc8677 --- /dev/null +++ b/src/PremierLeaguePlayer.java @@ -0,0 +1,64 @@ +import java.util.Arrays; +import java.util.Random; + +public class PremierLeaguePlayer { + private int[] goalsInMatches; + private int minutesPlayed; + private int[] minutesOfGoals; + private int totalPasses; + private int matchesPlayed; + private int totalGoals; + private String position; + + public PremierLeaguePlayer(int numberOfMatches, String position) { + Random random = new Random(); + + this.goalsInMatches = new int[numberOfMatches]; + for (int i = 0; i < numberOfMatches; i++) { + this.goalsInMatches[i] = random.nextInt(5); + } + + this.minutesPlayed = random.nextInt(90 * numberOfMatches); + + this.minutesOfGoals = new int[numberOfMatches]; + for (int i = 0; i < numberOfMatches; i++) { + if(this.goalsInMatches[i] > 0) { + this.minutesOfGoals[i] = random.nextInt(91); + } + } + + this.totalPasses = random.nextInt(250 * numberOfMatches); + this.matchesPlayed = numberOfMatches; + this.totalGoals = Arrays.stream(this.goalsInMatches).sum(); + this.position = position; + } + + public int getMatchesPlayed() { + return matchesPlayed; + } + + public int getMinutesPlayed() { + return minutesPlayed; + } + + public int getTotalGoals() { + return totalGoals; + } + + public int getTotalPasses() { + return totalPasses; + } + + public int[] getGoalsInMatches() { + return goalsInMatches; + } + + public int[] getMinutesOfGoals() { + return minutesOfGoals; + } + + public String getPosition() { + return position; + } + +} diff --git a/src/SupplierType.java b/src/SupplierType.java new file mode 100644 index 0000000..faeee1d --- /dev/null +++ b/src/SupplierType.java @@ -0,0 +1,8 @@ +import java.util.Date; +import java.util.function.Supplier; + +public class SupplierType { + public Supplier reportDate() { + return Date::new; + } +} diff --git a/src/ToIntType.java b/src/ToIntType.java new file mode 100644 index 0000000..7d61ac8 --- /dev/null +++ b/src/ToIntType.java @@ -0,0 +1,7 @@ +import java.util.function.ToIntFunction; + +public class ToIntType { + public ToIntFunction secondsPlayed() { + return m -> m*60; + } +} diff --git a/src/UnaryType.java b/src/UnaryType.java new file mode 100644 index 0000000..37d0b06 --- /dev/null +++ b/src/UnaryType.java @@ -0,0 +1,7 @@ +import java.util.function.UnaryOperator; + +public class UnaryType { + public UnaryOperator goalsPerMatch() { + return x -> x / 38; + } +}