fix lab6 conventions + add comments

This commit is contained in:
SowinskiBraeden committed 2025-11-03 13:17:04 -08:00
1 parent fec8bfb5ca
commit 935f745836
4 files changed
+202 -98

No files matched your search

@@ -1,6 +1,22 @@
package ca.bcit.comp2522.lab06; package ca.bcit.comp2522.lab06;
/**
* EligibilityRule is a way to enforce defined rules of a
* HockeyPlayer
*
* @author Braeden Sowinski
* @author Nico Agostini
* @author Trishaan Shetty
* @author Calvin Arifianto
* @version 1.0.0
*/
@FunctionalInterface @FunctionalInterface
public interface EligibilityRule { public interface EligibilityRule {
/**
* isEligible method.
*
* @param player A hockey player
* @return True or False. True if player is eligible, false otherwise.
*/
boolean isEligible(HockeyPlayer player); boolean isEligible(HockeyPlayer player);
} }
@@ -1,46 +1,72 @@
package ca.bcit.comp2522.lab06; package ca.bcit.comp2522.lab06;
/** /**
* HockeyPlayer holds information of a * Represents a hockey player and their basic statistics, including name, position,
* hockey player and their simple stats. * year of birth, and total goals scored.
*
* <p>This class validates all input parameters to ensure
* that player data is always initialized correctly.</p>
* *
* @author Braeden Sowinski
* @author Nico Agostini * @author Nico Agostini
* @author Braeden Sowinski
* @author Trishaan Shetty * @author Trishaan Shetty
* @author Calvin Arifianto * @author Calvin Arifianto
* @version 1.0.0 * @version 1.0.0
*/ */
public class HockeyPlayer { public class HockeyPlayer
private static String FORWARD = "F"; {
private static String DEFENCE = "D"; private static final String FORWARD = "F";
private static String GOALIE = "G"; private static final String DEFENCE = "D";
private static final String GOALIE = "G";
private final String name; private final String name;
private final String position; private final String position;
private final int yearOfBirth; private final int yearOfBirth;
private final int goals; private final int goals;
/**
* Validates a string to ensure it is not null or empty.
*
* @param str the string to validate
* @throws IllegalArgumentException if the string is null or empty
*/
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: cannot be null or empty.");
} }
} }
/**
* Validates a player's position to ensure it matches one of the accepted
* values: F, D, or G.
*
* @param positionToValidate the position to validate
* @throws IllegalArgumentException if the position is invalid
*/
private void validatePosition(final String positionToValidate) private void validatePosition(final String positionToValidate)
throws IllegalArgumentException
{ {
if (positionToValidate == null
if (position != null && || !(positionToValidate.equals(FORWARD)
( position.equals(FORWARD) || positionToValidate.equals(DEFENCE)
|| position.equals(DEFENCE) || positionToValidate.equals(GOALIE)))
|| position.equals(GOALIE)))
{ {
throw new IllegalArgumentException("Position not accepted."); throw new IllegalArgumentException("Position not accepted: must be F, D, or G.");
} }
} }
/**
* Constructs a new HockeyPlayer with the specified attributes.
*
* @param name the name of the player
* @param position the player's position (F, D, or G)
* @param yearOfBirth the year the player was born
* @param goals the total number of goals scored by the player
* @throws IllegalArgumentException if the name or position are invalid
*/
public HockeyPlayer( public HockeyPlayer(
final String name, final String name,
final String position, final String position,
@@ -56,24 +82,55 @@ public class HockeyPlayer {
this.goals = goals; this.goals = goals;
} }
protected String getPlayerName(){ /**
* Returns the player's name.
*
* @return the player's name
*/
protected String getPlayerName()
{
return this.name; return this.name;
} }
protected String getPosition(){ /**
* Returns the player's position.
*
* @return the player's position
*/
protected String getPosition()
{
return this.position; return this.position;
} }
protected int getGoals(){ /**
* Returns the number of goals scored by the player.
*
* @return the number of goals
*/
protected int getGoals()
{
return this.goals; return this.goals;
} }
protected int getYearOfBirth(){ /**
* Returns the player's year of birth.
*
* @return the player's year of birth
*/
protected int getYearOfBirth()
{
return this.yearOfBirth; return this.yearOfBirth;
} }
/**
* Returns a string representation of the hockey player,
* which is the player's name.
*
* @return the player's name
*/
@Override @Override
public String toString(){ public String toString()
{
return this.name; return this.name;
} }
} }
+42 -13
View File
@@ -1,45 +1,74 @@
package ca.bcit.comp2522.lab06; package ca.bcit.comp2522.lab06;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* HockeyTeam holds information of a * Represents a hockey team containing a team name and a roster of HockeyPlayer objects.
* hockey team composed by HockeyPlayers and the team's name. *
* <p>This class validates input to ensure that a team's name
* is properly initialized and that a valid roster is provided.</p>
* *
* @author Braeden Sowinski
* @author Nico Agostini * @author Nico Agostini
* @author Braeden Sowinski
* @author Trishaan Shetty * @author Trishaan Shetty
* @author Calvin Arifianto * @author Calvin Arifianto
* @version 1.0.0 * @version 1.0.0
*/ */
public class HockeyTeam
public class HockeyTeam { {
private final String name; private final String name;
private final List<HockeyPlayer> roster; private final List<HockeyPlayer> roster;
/**
* Validates a string to ensure it is not null or empty.
*
* @param str the string to validate
* @throws IllegalArgumentException if the string is null or empty
*/
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: cannot be null or empty.");
} }
} }
public HockeyTeam(final String name, /**
final List<HockeyPlayer> roster) { * Constructs a new HockeyTeam with the specified name and roster.
*
* @param name the name of the team
* @param roster the list of HockeyPlayer objects representing the team's roster
* @throws IllegalArgumentException if the team name is invalid
*/
public HockeyTeam(
final String name,
final List<HockeyPlayer> roster
)
{
validateString(name); validateString(name);
this.name = name; this.name = name;
this.roster = roster; this.roster = roster;
} }
public String getName() { /**
* Returns the name of the team.
*
* @return the team name
*/
public String getName()
{
return this.name; return this.name;
} }
public List<HockeyPlayer> getRoster() { /**
* Returns the roster of HockeyPlayers belonging to the team.
*
* @return the team's roster as a List of HockeyPlayer objects
*/
public List<HockeyPlayer> getRoster()
{
return this.roster; return this.roster;
} }
} }
+68 -66
View File
@@ -7,21 +7,36 @@ import java.util.List;
import java.util.function.*; import java.util.function.*;
/** /**
* Main class to instantiate and test the HockeyPlayer and HockeyTeam class. * Demonstrates the use of the HockeyPlayer and HockeyTeam classes along with
* functional programming concepts such as Supplier, Predicate, Function,
* Consumer, UnaryOperator, and Comparator.
*
* <p>This class creates a sample team, performs various operations
* using functional interfaces, and prints the results to the console.</p>
* *
* @author Braeden Sowinski
* @author Nico Agostini * @author Nico Agostini
* @author Braeden Sowinski
* @author Trishaan Shetty * @author Trishaan Shetty
* @author Calvin Arifianto * @author Calvin Arifianto
* @version 1.0.0 * @version 1.0.0
*/ */
public class Main
{
private static final int CURRENT_YEAR = 2025;
private static final int HIGH_SCORE_THRESHOLD = 20;
private static final String FORWARD_POSITION = "F";
private static final int MIN_AGE = 21;
private static final int MIN_GOALS = 10;
private static final int COUNTER_STARTER = 0;
public class Main { /**
* Creates and returns a sample hockey team with several HockeyPlayer objects.
*
* @return a HockeyTeam instance representing a sample team
*/
private static HockeyTeam sampleTeam() private static HockeyTeam sampleTeam()
{ {
final List<HockeyPlayer> ps; final List<HockeyPlayer> ps;
ps = new ArrayList<>(); ps = new ArrayList<>();
ps.add(new HockeyPlayer("Alex Morgan", "F", 2002, 21)); ps.add(new HockeyPlayer("Alex Morgan", "F", 2002, 21));
@@ -33,112 +48,99 @@ public class Main {
return new HockeyTeam("BCIT Blizzards", ps); return new HockeyTeam("BCIT Blizzards", ps);
} }
public static void main(final String[] args) { /**
* The program entry point. Demonstrates creation and manipulation
final int CURRENT_YEAR = 2025; * of HockeyPlayer and HockeyTeam objects, as well as use of various
final int TWENTY_GOALS = 20; * Java functional interfaces.
final String FORWARD_POSITION = "F"; *
final int MIN_AGE = 21; * <p>This method showcases examples of Supplier, Predicate, Function,
final int MIN_GOALS = 10; * Consumer, UnaryOperator, and Comparator applied to HockeyPlayer objects.</p>
final int COUNTER_STARTER = 0; *
* @param args command-line arguments (not used)
*/
final HockeyTeam team = sampleTeam(); public static void main(final String[] args)
final List<HockeyPlayer> roster = team.getRoster(); {
final HockeyTeam team;
final List<HockeyPlayer> roster;
// SUPPLIER team = sampleTeam();
roster = team.getRoster();
Supplier<HockeyPlayer> callUpSupplier = () ->
new HockeyPlayer("John Doe","F",1998,2);
// SUPPLIER — creates a new player on demand
final Supplier<HockeyPlayer> callUpSupplier =
() -> new HockeyPlayer("John Doe", "F", 1998, 2);
roster.add(callUpSupplier.get()); roster.add(callUpSupplier.get());
// PREDICATE — filter players who are forwards and high scorers
final Predicate<HockeyPlayer> isForward =
(p) -> p.getPosition().equalsIgnoreCase(FORWARD_POSITION);
final Predicate<HockeyPlayer> highScore =
(p) -> p.getGoals() >= HIGH_SCORE_THRESHOLD;
// PREDICATE for (final HockeyPlayer p : roster)
{
Predicate<HockeyPlayer> isForward = hockeyPlayer -> hockeyPlayer.getPosition().equals(FORWARD_POSITION); if (isForward.and(highScore).test(p))
Predicate<HockeyPlayer> has20Plus = hockeyPlayer -> hockeyPlayer.getGoals() >= TWENTY_GOALS; {
for (HockeyPlayer p : roster) {
if (isForward.and(has20Plus).test(p)) {
System.out.println(p); System.out.println(p);
} }
} }
// FUNCTION — transform a HockeyPlayer into a formatted string
final Function<HockeyPlayer, String> playerFunction =
(p) -> p.getPlayerName() + "" + p.getPosition()
+ " (" + p.getGoals() + " goals)";
// FUNCTION for (final HockeyPlayer p : roster)
Function<HockeyPlayer, String> playerFunction =
p -> p.getPlayerName() + "" + p.getPosition()
+ " (" + p.getGoals()
+ " goals)";
for (HockeyPlayer p : roster)
{ {
String label = playerFunction.apply(p); System.out.println(playerFunction.apply(p));
System.out.println(label);
} }
// CONSUMER — process and print player names
// CONSUMER final Consumer<HockeyPlayer> hockeyPlayerConsumer =
(p) -> System.out.println(p.getPlayerName());
Consumer<HockeyPlayer> hockeyPlayerConsumer =
hockeyPlayer -> System.out.println(hockeyPlayer.getPlayerName());
for (final HockeyPlayer hockeyPlayer : roster) for (final HockeyPlayer hockeyPlayer : roster)
{ {
hockeyPlayerConsumer.accept(hockeyPlayer); hockeyPlayerConsumer.accept(hockeyPlayer);
} }
// UNARY OPERATOR — convert player names to uppercase
// UNARY OPERATOR final UnaryOperator<String> toUpper = (stringInput) -> stringInput.toUpperCase();
UnaryOperator<String> toUpper = stringInput -> stringInput.toUpperCase();
for (final HockeyPlayer hockeyPlayer : roster) for (final HockeyPlayer hockeyPlayer : roster)
{ {
System.out.println(toUpper.apply(hockeyPlayer.getPlayerName())); System.out.println(toUpper.apply(hockeyPlayer.getPlayerName()));
} }
// COMPARATOR — sort players by goals in descending order
// COMPARATOR - sort by goals DESC (no chaining) final Comparator<HockeyPlayer> byGoalsDesc =
Comparator<HockeyPlayer> byGoalsDesc =
(a, b) -> Integer.compare(b.getGoals(), a.getGoals()); (a, b) -> Integer.compare(b.getGoals(), a.getGoals());
Collections.sort(roster, byGoalsDesc); Collections.sort(roster, byGoalsDesc);
System.out.println("Sorted by goals (DESC):"); System.out.println("Sorted by goals (DESC):");
for (final HockeyPlayer p : roster) for (final HockeyPlayer p : roster)
{ {
System.out.println(p.getPlayerName() System.out.println(p.getPlayerName() + " - " + p.getGoals());
+ " - " + p.getGoals());
} }
// AGGREGATION — calculate total goals on the roster
int totalGoals = COUNTER_STARTER;
// AGGREGATION (loop) — total goals
int totalGoals = COUNTER_STARTER; // 0
for (final HockeyPlayer p : roster) for (final HockeyPlayer p : roster)
{ {
totalGoals += p.getGoals(); totalGoals += p.getGoals();
} }
System.out.println("Total goals on roster: " + totalGoals); System.out.println("Total goals on roster: " + totalGoals);
// FUNCTIONAL INTERFACE (EligibilityRule) // FUNCTIONAL INTERFACE (EligibilityRule)
// A player is eligible if age >= minAge AND goals >= minGoals // A player is eligible if age >= minAge AND goals >= minGoals
final EligibilityRule rule =
EligibilityRule rule = p -> (p) -> ((CURRENT_YEAR - p.getYearOfBirth()) >= MIN_AGE)
((CURRENT_YEAR - p.getYearOfBirth()) >= MIN_AGE)
&& (p.getGoals() >= MIN_GOALS); && (p.getGoals() >= MIN_GOALS);
System.out.println("Eligible players (age >= " System.out.println("Eligible players (age >= "
+ MIN_AGE + ", goals >= " + MIN_AGE + ", goals >= "
+ MIN_GOALS + "):"); + MIN_GOALS + "):");
for (HockeyPlayer p : roster)
for (final HockeyPlayer p : roster)
{ {
if (rule.isEligible(p)) if (rule.isEligible(p))
{ {