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
+210 -106

No files matched your search

@@ -1,6 +1,22 @@
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
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);
}
@@ -1,51 +1,77 @@
package ca.bcit.comp2522.lab06;
/**
* HockeyPlayer holds information of a
* hockey player and their simple stats.
* Represents a hockey player and their basic statistics, including name, position,
* 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 Braeden Sowinski
* @author Trishaan Shetty
* @author Calvin Arifianto
* @version 1.0.0
*/
public class HockeyPlayer {
private static String FORWARD = "F";
private static String DEFENCE = "D";
private static String GOALIE = "G";
public class HockeyPlayer
{
private static final String FORWARD = "F";
private static final String DEFENCE = "D";
private static final String GOALIE = "G";
private final String name;
private final String position;
private final int yearOfBirth;
private final int goals;
private final int yearOfBirth;
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)
throws IllegalArgumentException
throws IllegalArgumentException
{
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)
throws IllegalArgumentException
{
if (position != null &&
( position.equals(FORWARD)
|| position.equals(DEFENCE)
|| position.equals(GOALIE)))
if (positionToValidate == null
|| !(positionToValidate.equals(FORWARD)
|| positionToValidate.equals(DEFENCE)
|| positionToValidate.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(
final String name,
final String position,
final int yearOfBirth,
final int goals
final String name,
final String position,
final int yearOfBirth,
final int goals
) {
validateString(name);
validatePosition(position);
@@ -56,24 +82,55 @@ public class HockeyPlayer {
this.goals = goals;
}
protected String getPlayerName(){
/**
* Returns the player's name.
*
* @return the player's name
*/
protected String getPlayerName()
{
return this.name;
}
protected String getPosition(){
/**
* Returns the player's position.
*
* @return the player's position
*/
protected String getPosition()
{
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;
}
protected int getYearOfBirth(){
/**
* Returns the player's year of birth.
*
* @return the player's year of birth
*/
protected int getYearOfBirth()
{
return this.yearOfBirth;
}
/**
* Returns a string representation of the hockey player,
* which is the player's name.
*
* @return the player's name
*/
@Override
public String toString(){
public String toString()
{
return this.name;
}
}
+43 -14
View File
@@ -1,45 +1,74 @@
package ca.bcit.comp2522.lab06;
import java.util.ArrayList;
import java.util.List;
/**
* HockeyTeam holds information of a
* hockey team composed by HockeyPlayers and the team's name.
* Represents a hockey team containing a team name and a roster of HockeyPlayer objects.
*
* <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 Braeden Sowinski
* @author Trishaan Shetty
* @author Calvin Arifianto
* @version 1.0.0
*/
public class HockeyTeam {
public class HockeyTeam
{
private final String name;
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)
throws IllegalArgumentException
{
if (str == null || str.isEmpty()) {
throw new IllegalArgumentException("invalid string");
if (str == null || str.isEmpty())
{
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);
this.name = name;
this.name = name;
this.roster = roster;
}
public String getName() {
/**
* Returns the name of the team.
*
* @return the team name
*/
public String getName()
{
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;
}
}
+68 -66
View File
@@ -7,21 +7,36 @@ import java.util.List;
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 Braeden Sowinski
* @author Trishaan Shetty
* @author Calvin Arifianto
* @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()
{
final List<HockeyPlayer> ps;
ps = new ArrayList<>();
ps.add(new HockeyPlayer("Alex Morgan", "F", 2002, 21));
@@ -33,112 +48,99 @@ public class Main {
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<HockeyPlayer> roster = team.getRoster();
// SUPPLIER
Supplier<HockeyPlayer> callUpSupplier = () ->
new HockeyPlayer("John Doe","F",1998,2);
/**
* The program entry point. Demonstrates creation and manipulation
* of HockeyPlayer and HockeyTeam objects, as well as use of various
* Java functional interfaces.
*
* <p>This method showcases examples of Supplier, Predicate, Function,
* Consumer, UnaryOperator, and Comparator applied to HockeyPlayer objects.</p>
*
* @param args command-line arguments (not used)
*/
public static void main(final String[] args)
{
final HockeyTeam team;
final List<HockeyPlayer> roster;
team = sampleTeam();
roster = team.getRoster();
// SUPPLIER — creates a new player on demand
final Supplier<HockeyPlayer> callUpSupplier =
() -> new HockeyPlayer("John Doe", "F", 1998, 2);
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
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)) {
for (final HockeyPlayer p : roster)
{
if (isForward.and(highScore).test(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
Function<HockeyPlayer, String> playerFunction =
p -> p.getPlayerName() + "" + p.getPosition()
+ " (" + p.getGoals()
+ " goals)";
for (HockeyPlayer p : roster)
for (final HockeyPlayer p : roster)
{
String label = playerFunction.apply(p);
System.out.println(label);
System.out.println(playerFunction.apply(p));
}
// CONSUMER
Consumer<HockeyPlayer> hockeyPlayerConsumer =
hockeyPlayer -> System.out.println(hockeyPlayer.getPlayerName());
// CONSUMER — process and print player names
final Consumer<HockeyPlayer> hockeyPlayerConsumer =
(p) -> System.out.println(p.getPlayerName());
for (final HockeyPlayer hockeyPlayer : roster)
{
hockeyPlayerConsumer.accept(hockeyPlayer);
}
// UNARY OPERATOR
UnaryOperator<String> toUpper = stringInput -> stringInput.toUpperCase();
// UNARY OPERATOR — convert player names to uppercase
final 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 =
// COMPARATOR — sort players by goals in descending order
final 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());
System.out.println(p.getPlayerName() + " - " + p.getGoals());
}
// AGGREGATION (loop) — total goals
int totalGoals = COUNTER_STARTER; // 0
// AGGREGATION — calculate total goals on the roster
int totalGoals = COUNTER_STARTER;
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)
final 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)
for (final HockeyPlayer p : roster)
{
if (rule.isEligible(p))
{