Merge pull request #6 from SowinskiBraeden/lab6-nico

lab6 ready
This commit is contained in:
SowinskiBraeden authored and GitHub committed 2025-11-02 15:26:08 -08:00
commit 57721f292d
4 files changed
+181 -19

No files matched your search

@@ -0,0 +1,6 @@
package ca.bcit.comp2522.lab06;
@FunctionalInterface
public interface EligibilityRule {
boolean isEligible(HockeyPlayer player);
}
@@ -10,39 +10,39 @@ package ca.bcit.comp2522.lab06;
* @version 1.0.0 * @version 1.0.0
*/ */
public class HockeyPlayer { public class HockeyPlayer {
private static char FORWARD = 'F'; private static String FORWARD = "F";
private static char DEFENCE = 'D'; private static String DEFENCE = "D";
private static char GOALIE = 'G'; private static String GOALIE = "G";
private final String name; private final String name;
private final char position; private final String position;
private final int yearOfBirth; private final int yearOfBirth;
private final int goals; private final int goals;
private static void validateString(final String str) private static void validateString(final String str)
throws IllegalArgumentException throws IllegalArgumentException
{ {
if (str == null || str.isEmpty()) { if (str == null || str.isEmpty())
{
throw new IllegalArgumentException("invalid string"); throw new IllegalArgumentException("invalid string");
} }
} }
private static void validatePosition(final char pos) private void validatePosition(final String positionToValidate)
throws IllegalArgumentException
{ {
final char posUpper = Character.toUpperCase(pos);
if (posUpper != FORWARD && if (position != null &&
posUpper != DEFENCE && ( position.equals(FORWARD)
posUpper != GOALIE || position.equals(DEFENCE)
) { || position.equals(GOALIE)))
throw new IllegalArgumentException("invalid position"); {
throw new IllegalArgumentException("Position not accepted.");
} }
} }
public HockeyPlayer( public HockeyPlayer(
final String name, final String name,
final char position, final String position,
final int yearOfBirth, final int yearOfBirth,
final int goals final int goals
) { ) {
@@ -54,4 +54,25 @@ public class HockeyPlayer {
this.yearOfBirth = yearOfBirth; this.yearOfBirth = yearOfBirth;
this.goals = goals; 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;
}
} }
@@ -15,11 +15,13 @@ public class HockeyTeam {
} }
} }
public HockeyTeam(final String name) { public HockeyTeam(final String name,
final List<HockeyPlayer> roster) {
validateString(name); validateString(name);
this.name = name; this.name = name;
this.roster = new ArrayList<>(); this.roster = roster;
} }
public String getName() { public String getName() {
+133
View File
@@ -1,7 +1,140 @@
package ca.bcit.comp2522.lab06; 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 { public class Main {
private static HockeyTeam sampleTeam()
{
final List<HockeyPlayer> 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) { 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<HockeyPlayer> roster = team.getRoster();
// SUPPLIER
Supplier<HockeyPlayer> callUpSupplier = () ->
new HockeyPlayer("John Doe","F",1998,2);
roster.add(callUpSupplier.get());
// PREDICATE
Predicate<HockeyPlayer> isForward = hockeyPlayer -> hockeyPlayer.getPosition().equals(FORWARD_POSITION);
Predicate<HockeyPlayer> has20Plus = hockeyPlayer -> hockeyPlayer.getGoals() >= TWENTY_GOALS;
for (HockeyPlayer p : roster) {
if (isForward.and(has20Plus).test(p)) {
System.out.println(p);
}
}
// FUNCTION
Function<HockeyPlayer, String> playerFunction =
p -> p.getPlayerName() + "" + p.getPosition()
+ " (" + p.getGoals()
+ " goals)";
for (HockeyPlayer p : roster)
{
String label = playerFunction.apply(p);
System.out.println(label);
}
// CONSUMER
Consumer<HockeyPlayer> hockeyPlayerConsumer =
hockeyPlayer -> System.out.println(hockeyPlayer.getPlayerName());
for (final HockeyPlayer hockeyPlayer : roster)
{
hockeyPlayerConsumer.accept(hockeyPlayer);
}
// UNARY OPERATOR
UnaryOperator<String> toUpper = stringInput -> stringInput.toUpperCase();
for (final HockeyPlayer hockeyPlayer : roster)
{
System.out.println(toUpper.apply(hockeyPlayer.getPlayerName()));
}
// COMPARATOR - sort by goals DESC (no chaining)
Comparator<HockeyPlayer> 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());
}
}
} }
} }