From 7c23f0f22ddcc648f04a1e6db9978a1a83667804 Mon Sep 17 00:00:00 2001 From: nicoagostini Date: Sun, 2 Nov 2025 15:04:57 -0800 Subject: [PATCH] lab6 ready --- .../bcit/comp2522/lab06/EligibilityRule.java | 6 + .../ca/bcit/comp2522/lab06/HockeyPlayer.java | 53 ++++--- .../ca/bcit/comp2522/lab06/HockeyTeam.java | 8 +- src/code/ca/bcit/comp2522/lab06/Main.java | 133 ++++++++++++++++++ 4 files changed, 181 insertions(+), 19 deletions(-) create mode 100644 src/code/ca/bcit/comp2522/lab06/EligibilityRule.java diff --git a/src/code/ca/bcit/comp2522/lab06/EligibilityRule.java b/src/code/ca/bcit/comp2522/lab06/EligibilityRule.java new file mode 100644 index 0000000..7001529 --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab06/EligibilityRule.java @@ -0,0 +1,6 @@ +package ca.bcit.comp2522.lab06; + +@FunctionalInterface +public interface EligibilityRule { + boolean isEligible(HockeyPlayer player); +} \ No newline at end of file diff --git a/src/code/ca/bcit/comp2522/lab06/HockeyPlayer.java b/src/code/ca/bcit/comp2522/lab06/HockeyPlayer.java index 6bc2fd0..6aa5033 100644 --- a/src/code/ca/bcit/comp2522/lab06/HockeyPlayer.java +++ b/src/code/ca/bcit/comp2522/lab06/HockeyPlayer.java @@ -10,39 +10,39 @@ package ca.bcit.comp2522.lab06; * @version 1.0.0 */ public class HockeyPlayer { - private static char FORWARD = 'F'; - private static char DEFENCE = 'D'; - private static char GOALIE = 'G'; + private static String FORWARD = "F"; + private static String DEFENCE = "D"; + private static String GOALIE = "G"; private final String name; - private final char position; - private final int yearOfBirth; - private final int goals; + private final String position; + private final int yearOfBirth; + private final int goals; private static void validateString(final String str) throws IllegalArgumentException { - if (str == null || str.isEmpty()) { + if (str == null || str.isEmpty()) + { throw new IllegalArgumentException("invalid string"); } } - private static void validatePosition(final char pos) - throws IllegalArgumentException + private void validatePosition(final String positionToValidate) { - final char posUpper = Character.toUpperCase(pos); - if (posUpper != FORWARD && - posUpper != DEFENCE && - posUpper != GOALIE - ) { - throw new IllegalArgumentException("invalid position"); + if (position != null && + ( position.equals(FORWARD) + || position.equals(DEFENCE) + || position.equals(GOALIE))) + { + throw new IllegalArgumentException("Position not accepted."); } } public HockeyPlayer( final String name, - final char position, + final String position, final int yearOfBirth, final int goals ) { @@ -54,4 +54,25 @@ public class HockeyPlayer { this.yearOfBirth = yearOfBirth; this.goals = goals; } + + protected String getPlayerName(){ + return this.name; + } + + protected String getPosition(){ + return this.position; + } + + protected int getGoals(){ + return this.goals; + } + + protected int getYearOfBirth(){ + return this.yearOfBirth; + } + + @Override + public String toString(){ + return this.name; + } } diff --git a/src/code/ca/bcit/comp2522/lab06/HockeyTeam.java b/src/code/ca/bcit/comp2522/lab06/HockeyTeam.java index c4d79e7..52d69a8 100644 --- a/src/code/ca/bcit/comp2522/lab06/HockeyTeam.java +++ b/src/code/ca/bcit/comp2522/lab06/HockeyTeam.java @@ -15,11 +15,13 @@ public class HockeyTeam { } } - public HockeyTeam(final String name) { + public HockeyTeam(final String name, + final List roster) { + validateString(name); - this.name = name; - this.roster = new ArrayList<>(); + this.name = name; + this.roster = roster; } public String getName() { diff --git a/src/code/ca/bcit/comp2522/lab06/Main.java b/src/code/ca/bcit/comp2522/lab06/Main.java index 5e1b2cf..cfe83cc 100644 --- a/src/code/ca/bcit/comp2522/lab06/Main.java +++ b/src/code/ca/bcit/comp2522/lab06/Main.java @@ -1,7 +1,140 @@ package ca.bcit.comp2522.lab06; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.function.*; + + public class Main { + + private static HockeyTeam sampleTeam() + { + final List ps; + + ps = new ArrayList<>(); + + ps.add(new HockeyPlayer("Alex Morgan", "F", 2002, 21)); + ps.add(new HockeyPlayer("Ben Carter", "D", 1999, 6)); + ps.add(new HockeyPlayer("Casey Young", "F", 2004, 28)); + ps.add(new HockeyPlayer("Drew Singh", "G", 2000, 0)); + ps.add(new HockeyPlayer("Eva Chen", "D", 2001, 5)); + + return new HockeyTeam("BCIT Blizzards", ps); + } + public static void main(final String[] args) { + final int CURRENT_YEAR = 2025; + final int TWENTY_GOALS = 20; + final String FORWARD_POSITION = "F"; + final int MIN_AGE = 21; + final int MIN_GOALS = 10; + final int COUNTER_STARTER = 0; + + + final HockeyTeam team = sampleTeam(); + final List roster = team.getRoster(); + + + // SUPPLIER + + Supplier callUpSupplier = () -> + new HockeyPlayer("John Doe","F",1998,2); + + roster.add(callUpSupplier.get()); + + + // PREDICATE + + Predicate isForward = hockeyPlayer -> hockeyPlayer.getPosition().equals(FORWARD_POSITION); + Predicate has20Plus = hockeyPlayer -> hockeyPlayer.getGoals() >= TWENTY_GOALS; + + for (HockeyPlayer p : roster) { + if (isForward.and(has20Plus).test(p)) { + System.out.println(p); + } + } + + + // FUNCTION + + Function playerFunction = + p -> p.getPlayerName() + " — " + p.getPosition() + + " (" + p.getGoals() + + " goals)"; + + for (HockeyPlayer p : roster) + { + String label = playerFunction.apply(p); + System.out.println(label); + } + + + // CONSUMER + + Consumer hockeyPlayerConsumer = + hockeyPlayer -> System.out.println(hockeyPlayer.getPlayerName()); + + for (final HockeyPlayer hockeyPlayer : roster) + { + hockeyPlayerConsumer.accept(hockeyPlayer); + } + + + // UNARY OPERATOR + + UnaryOperator toUpper = stringInput -> stringInput.toUpperCase(); + + for (final HockeyPlayer hockeyPlayer : roster) + { + System.out.println(toUpper.apply(hockeyPlayer.getPlayerName())); + } + + + // COMPARATOR - sort by goals DESC (no chaining) + + Comparator byGoalsDesc = + (a, b) -> Integer.compare(b.getGoals(), a.getGoals()); + Collections.sort(roster, byGoalsDesc); + + System.out.println("Sorted by goals (DESC):"); + + for (final HockeyPlayer p : roster) + { + System.out.println(p.getPlayerName() + + " - " + p.getGoals()); + } + + + + // AGGREGATION (loop) — total goals + + int totalGoals = COUNTER_STARTER; // 0 + for (final HockeyPlayer p : roster) + { + totalGoals += p.getGoals(); + } + System.out.println("Total goals on roster: " + totalGoals); + + + // FUNCTIONAL INTERFACE (EligibilityRule) + // A player is eligible if age >= minAge AND goals >= minGoals + + EligibilityRule rule = p -> + ((CURRENT_YEAR - p.getYearOfBirth()) >= MIN_AGE) + && (p.getGoals() >= MIN_GOALS); + + System.out.println("Eligible players (age >= " + + MIN_AGE + ", goals >= " + + MIN_GOALS + "):"); + for (HockeyPlayer p : roster) + { + if (rule.isEligible(p)) + { + System.out.println(p.getPlayerName()); + } + } } } \ No newline at end of file