diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Game.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Game.java new file mode 100644 index 0000000..3529e77 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Game.java @@ -0,0 +1,116 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module10.practice1; + +import com.cert.oca8.course.devdamunguia95.module10.practice1.utility.GameUtils; + +/** + * + * @author dmunguias + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public Game() { + } + + public Game(Team homeTeam, Team awayTeam) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int) (Math.random() * (maxGoals + 1)); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGamesGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(homeTeam.getTeamName() + + " vs " + awayTeam.getTeamName() + "\n"); + for (Goal currGame : this.getGoals()) { + if (currGame.getTheTeam() == homeTeam) { + homeTeamGoals++; + + } else { + awayTeamGoals++; + } + returnString.append("Goal scored after " + + currGame.getTheTime() + " mins by " + + currGame.getThePlayer().getPlayerName() + " of " + + currGame.getTheTeam().getTeamName() + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("it's a draw!"); + homeTeam.incPointsTotal(1); + awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + awayTeam.incPointsTotal(1); + } + returnString.append("(" + homeTeamGoals + " - " + + awayTeamGoals + ") \n"); + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Goal.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Goal.java new file mode 100644 index 0000000..4eda943 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Goal.java @@ -0,0 +1,56 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module10.practice1; +/** + * + * @author dmunguias + */ +public class Goal { + private Team theTeam; + private Player thePlayer; + private double theTime; + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/League.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/League.java new file mode 100644 index 0000000..915e32f --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/League.java @@ -0,0 +1,73 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module10.practice1; + +/** + * + * @author dmunguias + */ +public class League { + + public static void main(String[] args) { + League league = new League(); + Team[] theTeams = league.createTeams(); + Game[] theGames = league.createGames(theTeams); + + for (Game theGame : theGames) { + theGame.playGame(); + System.out.println(theGame.getDescription()); + } + league.showBestTeam(theTeams); + } + + public Team[] createTeams() { + // TODO code application logic here + Player player1 = new Player("George Eliot"); + Player player2 = new Player("Graham Greene"); + Player player3 = new Player("Geoffrey Chaucer"); + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team("The Greens", thePlayers); + //Second Team + Team team2 = new Team("The Reeds", new Player[3]); + team2.getPlayerArray()[0] = new Player("Robert Service"); + team2.getPlayerArray()[1] = new Player("Robbie Burns"); + team2.getPlayerArray()[2] = new Player("Rafael Sabatini"); + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + Game theGame = new Game(theTeams[0], theTeams[1]); + Game theGame2 = new Game(theTeams[1], theTeams[0]); + Game theGame3 = new Game(theTeams[0], theTeams[1]); + Game theGame4 = new Game(theTeams[1], theTeams[0]); + Game[] theGames = {theGame, theGame2, theGame3, theGame4}; + return theGames; + } + + public void showBestTeam(Team[] theTeams) { + + System.out.println("\nTeams Poinst"); + + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + ":" + + currTeam.getPointsTotal()); + } + + Team currBestTeam = theTeams[0]; + + for (Team currTeam : theTeams) { + currBestTeam = currTeam.getPointsTotal() > + currBestTeam.getPointsTotal()? + currTeam : currBestTeam; + } + System.out.println("Winner of League is " + + currBestTeam.getTeamName()); + } + +} \ No newline at end of file diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Player.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Player.java new file mode 100644 index 0000000..b482a26 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Player.java @@ -0,0 +1,32 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module10.practice1; +/** + * + * @author dmunguias + */ +public class Player { + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() { + } + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Team.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Team.java new file mode 100644 index 0000000..9e61838 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/Team.java @@ -0,0 +1,76 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module10.practice1; + +/** + * + * @author dmunguias + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + + public Team() { + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] playerArray) { + this(teamName); + this.playerArray = playerArray; + } + + public void incPointsTotal(int pointsTotal) { + + this.pointsTotal += pointsTotal; + } + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/utility/GameUtils.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/utility/GameUtils.java new file mode 100644 index 0000000..bbdc8f4 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice1/utility/GameUtils.java @@ -0,0 +1,38 @@ +package com.cert.oca8.course.devdamunguia95.module10.practice1.utility; + +import com.cert.oca8.course.devdamunguia95.module10.practice1.Game; +import com.cert.oca8.course.devdamunguia95.module10.practice1.Goal; + +/** + * + * @author dmunguias + */ +public class GameUtils { + + public static Game addGamesGoals(Game currGame) { + + for (int i = 0; i < currGame.getGoals().length; i++) { + Goal goals = new Goal(); + int numberOfTeam = (int) (Math.random() * 2); + if (numberOfTeam != 0) { + + int arrayPlayerLength = currGame.getAwayTeam().getPlayerArray().length; + int numberOfPlayer = (int) (Math.random() * arrayPlayerLength); + + goals.setThePlayer(currGame.getAwayTeam().getPlayerArray()[numberOfPlayer]); + goals.setTheTeam(currGame.getAwayTeam()); + goals.setTheTime((int) (Math.random() * 90)); + + } else { + + int arrayPlayerLength = currGame.getHomeTeam().getPlayerArray().length; + int numberOfPlayer = (int) (Math.random() * arrayPlayerLength); + goals.setThePlayer(currGame.getHomeTeam().getPlayerArray()[numberOfPlayer]); + goals.setTheTeam(currGame.getHomeTeam()); + goals.setTheTime((int) (Math.random() * 90)); + } + currGame.getGoals()[i] = goals; + } + return currGame; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Game.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Game.java new file mode 100644 index 0000000..c108c0e --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Game.java @@ -0,0 +1,118 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module10.practice2; + +import com.cert.oca8.course.devdamunguia95.module10.practice2.utility.GameUtils; + +/** + * + * @author dmunguias + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public Game() { + } + + public Game(Team homeTeam, Team awayTeam) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int) (Math.random() * (maxGoals + 1)); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGamesGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(homeTeam.getTeamName() + + " vs " + awayTeam.getTeamName() + "\n"); + for (Goal currGame : this.getGoals()) { + if (currGame.getTheTeam() == homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + returnString.append("Goal scored after " + + currGame.getTheTime() + " mins by " + + currGame.getThePlayer().getPlayerName() + " of " + + currGame.getTheTeam().getTeamName() + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("it's a draw!"); + homeTeam.incPointsTotal(1); + awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + awayTeam.incPointsTotal(1); + } + returnString.append("(" + homeTeamGoals + " - " + + awayTeamGoals + ") \n"); + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Goal.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Goal.java new file mode 100644 index 0000000..922ab00 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Goal.java @@ -0,0 +1,57 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module10.practice2; + +/** + * + * @author dmunguias + */ +public class Goal { + private Team theTeam; + private Player thePlayer; + private double theTime; + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/League.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/League.java new file mode 100644 index 0000000..3e0c3b6 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/League.java @@ -0,0 +1,81 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module10.practice2; + +/** + * + * @author dmunguias + */ +public class League { + + public static void main(String[] args) { + League league = new League(); + Team[] theTeams = league.createTeams(); + Game[] theGames = league.createGames(theTeams); + + for (Game theGame : theGames) { + theGame.playGame(); + System.out.println(theGame.getDescription()); + } + league.showBestTeam(theTeams); + } + + public Team[] createTeams() { + // TODO code application logic here + Player player1 = new Player("George Eliot"); + Player player2 = new Player("Graham Greene"); + Player player3 = new Player("Geoffrey Chaucer"); + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team("The Greens", thePlayers); + //Second Team + Team team2 = new Team("The Reeds", new Player[3]); + team2.getPlayerArray()[0] = new Player("Robert Service"); + team2.getPlayerArray()[1] = new Player("Robbie Burns"); + team2.getPlayerArray()[2] = new Player("Rafael Sabatini"); + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + Game theGame = new Game(theTeams[0], theTeams[1]); + Game theGame2 = new Game(theTeams[1], theTeams[0]); + Game theGame3 = new Game(theTeams[0], theTeams[1]); + Game theGame4 = new Game(theTeams[1], theTeams[0]); + Game[] theGames = {theGame, theGame2, theGame3, theGame4}; + return theGames; + } + + public void showBestTeam(Team[] theTeams) { + + System.out.println("\nTeams Poinst"); + + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + ":" + + currTeam.getPointsTotal() + ":" + + currTeam.getGoalsTotal()); + } + Team currBestTeam = theTeams[0]; + + for (Team currTeam : theTeams) { + + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()) { + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() + == currBestTeam.getPointsTotal()) { + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()) { + currBestTeam = currTeam; + } + + } + } + + System.out.println("Winner of League is " + + currBestTeam.getTeamName()); + + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Player.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Player.java new file mode 100644 index 0000000..269c1df --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Player.java @@ -0,0 +1,33 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module10.practice2; + +/** + * + * @author dmunguias + */ +public class Player { + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() { + } + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Team.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Team.java new file mode 100644 index 0000000..070f691 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/Team.java @@ -0,0 +1,96 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module10.practice2; + +/** + * + * @author dmunguias + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public Team() { + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] playerArray) { + this(teamName); + this.playerArray = playerArray; + } + + public void incPointsTotal(int pointsTotal) { + + this.pointsTotal += pointsTotal; + } + + public void incGoalsTotal(int goal) { + + this.goalsTotal = this.goalsTotal + goal; + } + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the golasTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param golasTotal the golasTotal to set + */ + public void setGoalsTotal(int golasTotal) { + this.goalsTotal = golasTotal; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/utility/GameUtils.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/utility/GameUtils.java new file mode 100644 index 0000000..3c72362 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module10/practice2/utility/GameUtils.java @@ -0,0 +1,38 @@ +package com.cert.oca8.course.devdamunguia95.module10.practice2.utility; + +import com.cert.oca8.course.devdamunguia95.module10.practice2.Game; +import com.cert.oca8.course.devdamunguia95.module10.practice2.Goal; + +/** + * + * @author dmunguias + */ +public class GameUtils { + + public static Game addGamesGoals(Game currGame) { + + for (int i = 0; i < currGame.getGoals().length; i++) { + Goal goals = new Goal(); + int numberOfTeam = (int) (Math.random() * 2); + if (numberOfTeam != 0) { + + int arrayPlayerLength = currGame.getAwayTeam().getPlayerArray().length; + int numberOfPlayer = (int) (Math.random() * arrayPlayerLength); + + goals.setThePlayer(currGame.getAwayTeam().getPlayerArray()[numberOfPlayer]); + goals.setTheTeam(currGame.getAwayTeam()); + goals.setTheTime((int) (Math.random() * 90)); + + } else { + + int arrayPlayerLength = currGame.getHomeTeam().getPlayerArray().length; + int numberOfPlayer = (int) (Math.random() * arrayPlayerLength); + goals.setThePlayer(currGame.getHomeTeam().getPlayerArray()[numberOfPlayer]); + goals.setTheTeam(currGame.getHomeTeam()); + goals.setTheTime((int) (Math.random() * 90)); + } + currGame.getGoals()[i] = goals; + } + return currGame; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Game.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Game.java new file mode 100644 index 0000000..e4b5d7f --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Game.java @@ -0,0 +1,131 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.soocer; + +import com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.utility.GameUtils; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +/** + * + * @author Administrator + */ +public class Game { + private LocalDateTime localDateTime; + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime localDateTime) { + this.localDateTime=localDateTime; + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + this.localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE)+ "\n"); + + for (Goal currGoal: this.getGoals()) { + + if (currGoal.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName()+"\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Goal.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Goal.java new file mode 100644 index 0000000..d4ea394 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Goal.java @@ -0,0 +1,63 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.soocer; + +import java.time.LocalDateTime; + +/** + * + * @author Administrator + */ +public class Goal { + private LocalDateTime localDateTime; + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/League.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/League.java new file mode 100644 index 0000000..b75e572 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/League.java @@ -0,0 +1,88 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.soocer; +import com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.utility.PlayerDatabase; +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +/** + * + * @author Administrator + */ +public class League { + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + Team[] theTeams = theLeague.createTeams("The Robins, The Crows, The Swallows",3); + Game[] theGames = theLeague.createGames(theTeams); + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame : theGames) { + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + theLeague.showBestTeam(theTeams); + } + + public Team[] createTeams(String teamName, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + StringTokenizer teamNameTokens=new StringTokenizer(teamName,","); + + Team[] theTeams=new Team[teamNameTokens.countTokens()]; + + for (int i = 0; i < theTeams.length; i++) { + theTeams[i]= new Team(teamNameTokens.nextToken(),playerDB.getTeam(teamSize)); + } + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames=0; + ArrayList theGames= new ArrayList(); + for (Team homeTeam : theTeams) { + for (Team awayTeam : theTeams) { + if (homeTeam!=awayTeam) { + daysBetweenGames+=7; + theGames.add(new Game(homeTeam,awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + } + } + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\n"+"Team Points"); + + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + " : " + + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > + currBestTeam.getPointsTotal() ? currTeam : currBestTeam; + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()) + { + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()) + { + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()) { + currBestTeam = currTeam; + } + } + } + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + } + public String getLeagueAnnouncement(Game[] theGames){ + Period thePeriod=Period.between(theGames[0].getLocalDateTime().toLocalDate(), + theGames[theGames.length-1].getLocalDateTime().toLocalDate()); + return "The league is Scheduled to run for "+ thePeriod.getMonths() + + " month(s), and " + thePeriod.getDays() + " day(s)\n"; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Player.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Player.java new file mode 100644 index 0000000..dd99b9c --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.soocer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Team.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Team.java new file mode 100644 index 0000000..f13fb22 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/soocer/Team.java @@ -0,0 +1,95 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.soocer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/utility/GameUtils.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/utility/GameUtils.java new file mode 100644 index 0000000..29a4a31 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/utility/GameUtils.java @@ -0,0 +1,41 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.utility; + +import java.util.Arrays; +import com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.soocer.Game; +import com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.soocer.Goal; +/** + * + * @author ksomervi + */ +public class GameUtils { + + public static void addGameGoals(Game currGame) { + + //System.out.println(currGame.awayTeam + " : " + currGame.homeTeam); + + // Or possibly throw an Exception? + if (currGame.getGoals() == null) { + currGame.setGoals(new Goal[(int) (Math.random() * 10)]); // If goals not initialized max will be 9 + } + + //System.out.println(currGame.goals.length); + int i = 0; + for (Goal currGoal : currGame.getGoals()) { + currGoal = new Goal(); + currGoal.setTheTeam(Math.random() > 0.5 ? currGame.getHomeTeam() : currGame.getAwayTeam()); + currGoal.setThePlayer(currGoal.getTheTeam().getPlayerArray()[(int) (Math.random() * currGoal.getTheTeam().getPlayerArray().length)]); + currGoal.setTheTime((int) (Math.random() * 90)); + currGame.getGoals()[i] = currGoal; + i++; + } + Arrays.sort(currGame.getGoals(), (g1, g2) -> Double.valueOf(g1.getTheTime()).compareTo(Double.valueOf(g2.getTheTime()))); + + } + + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/utility/PlayerDatabase.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/utility/PlayerDatabase.java new file mode 100644 index 0000000..52cc1de --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module11/arrayloopsdates/practice1/utility/PlayerDatabase.java @@ -0,0 +1,81 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.utility; + +import com.cert.oca8.course.devdamunguia95.module11.arrayloopsdates.practice1.soocer.Player; +import java.util.ArrayList; +import java.util.StringTokenizer; + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + /* Practice 11-2. Declare an ArrayList here */ + private ArrayList players; + + /* Practice 11-2. Add Constructor here */ + public PlayerDatabase() { + StringTokenizer authorToken = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorToken.hasMoreElements()) { + players.add(new Player((String) authorToken.nextElement())); + + } + } + /* Practice 11-2. Add getTeam() method here */ + + public Player[] getTeam(int numberOfPlayers){ + + Player[] teamsPlayer=new Player[numberOfPlayers]; + + for (int i = 0; i < numberOfPlayers; i++) { + int playerIndex= (int) (Math.random()*players.size()); + teamsPlayer[i]=players.get(playerIndex); + } + + + return teamsPlayer; + } + String authorList + = "Agatha Christie," + + "Alan Patton," + + "Alexander Solzhenitsyn," + + "Arthur Conan Doyle," + + "Anthony Trollope," + + "Baroness Orczy," + + "Brendan Behan," + + "Brian Moore," + + "Boris Pasternik," + + "Charles Dickens," + + "Charlotte Bronte," + + "Dorothy Parker," + + "Emile Zola," + + "Frank O'Connor," + + "Geoffrey Chaucer," + + "George Eliot," + + "G. K. Chesterton," + + "Graham Green," + + "Henry James," + + "James Joyce," + + "J. M. Synge," + + "J. R. Tolkien," + + "Jane Austin," + + "Leo Tolstoy," + + "Liam O'Flaherty," + + "Marcel Proust," + + "Mark Twain," + + "Oscar Wilde," + + "O. Henry," + + "Samuel Beckett," + + "Sean O'Casey," + + "William Shakespeare," + + "William Makepeace Thackeray," + + "W. B. Yeats," + + "Wilkie Collins"; + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Game.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Game.java new file mode 100644 index 0000000..ab105f7 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Game.java @@ -0,0 +1,141 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice1.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +/** + * + * @author Administrator + */ +public class Game { + private LocalDateTime localDateTime; + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime localDateTime) { + this.localDateTime=localDateTime; + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + + public void playGame() { + ArrayList eventList = new ArrayList(); + Goal currEvent; + for (int i = 1; i <= 90; i++) { + + if (Math.random()>0.95) { + // System.out.println(i); + currEvent=new Goal(); + currEvent.setTheTeam(Math.random()>0.5?homeTeam:awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam().getPlayerArray() + [(int) (Math.random()* currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + } + this.goals=new Goal[eventList.size()]; + eventList.toArray(goals); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + " " + + this.localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE)+ "\n"); + + for (Goal currGoal: this.getGoals()) { + if (currGoal.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName()+"\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append("(" + homeTeamGoals + " - " + + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Goal.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Goal.java new file mode 100644 index 0000000..29ab98b --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Goal.java @@ -0,0 +1,62 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice1.soccer; +import java.time.LocalDateTime; + +/** + * + * @author Administrator + */ +public class Goal { + private LocalDateTime localDateTime; + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/League.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/League.java new file mode 100644 index 0000000..1515a35 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/League.java @@ -0,0 +1,97 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice1.soccer; + +import com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice1.utility.PlayerDatabase; +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,"+ + "The Swallows", 3); + Game[] theGames = theLeague.createGames(theTeams); + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame : theGames) { + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + theLeague.showBestTeam(theTeams); + } + + public Team[] createTeams(String teamName, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + StringTokenizer teamNameTokens = new StringTokenizer(teamName, ","); + + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + + for (int i = 0; i < theTeams.length; i++) { + theTeams[i] = new Team(teamNameTokens.nextToken(), + playerDB.getTeam(teamSize)); + } + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + ArrayList theGames = new ArrayList(); + for (Team homeTeam : theTeams) { + for (Team awayTeam : theTeams) { + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, + LocalDateTime.now().plusDays(daysBetweenGames))); + } + } + } + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\n" + "Team Points"); + + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + " : " + + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > + currBestTeam.getPointsTotal() ? currTeam : currBestTeam; + if (currTeam.getPointsTotal() > + currBestTeam.getPointsTotal()) { + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == + currBestTeam.getPointsTotal()) { + if (currTeam.getGoalsTotal() > + currBestTeam.getGoalsTotal()) { + currBestTeam = currTeam; + } + } + } + System.out.println("Winner of the League is " + + currBestTeam.getTeamName()); + } + + public String getLeagueAnnouncement(Game[] theGames) { + Period thePeriod = Period.between( + theGames[0].getLocalDateTime().toLocalDate(), + theGames[theGames.length - 1].getLocalDateTime().toLocalDate()); + return "The league is Scheduled to run for " + thePeriod.getMonths() + + " month(s), and " + thePeriod.getDays() + " day(s)\n"; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Player.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Player.java new file mode 100644 index 0000000..b1a5fc7 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice1.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Team.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Team.java new file mode 100644 index 0000000..f259198 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/soccer/Team.java @@ -0,0 +1,95 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice1.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/utility/PlayerDatabase.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/utility/PlayerDatabase.java new file mode 100644 index 0000000..18dd037 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice1/utility/PlayerDatabase.java @@ -0,0 +1,72 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice1.utility; + +import com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice1.soccer.Player; +import java.util.ArrayList; +import java.util.StringTokenizer; + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + private ArrayList players; + + public PlayerDatabase() { + StringTokenizer authorToken = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorToken.hasMoreElements()) { + players.add(new Player((String) authorToken.nextElement())); + + } + } + public Player[] getTeam(int numberOfPlayers){ + + Player[] teamsPlayer=new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++) { + int playerIndex= (int) (Math.random()*players.size()); + teamsPlayer[i]=players.get(playerIndex); + } + return teamsPlayer; + } + String authorList + = "Agatha Christie," + + "Alan Patton," + + "Alexander Solzhenitsyn," + + "Arthur Conan Doyle," + + "Anthony Trollope," + + "Baroness Orczy," + + "Brendan Behan," + + "Brian Moore," + + "Boris Pasternik," + + "Charles Dickens," + + "Charlotte Bronte," + + "Dorothy Parker," + + "Emile Zola," + + "Frank O'Connor," + + "Geoffrey Chaucer," + + "George Eliot," + + "G. K. Chesterton," + + "Graham Green," + + "Henry James," + + "James Joyce," + + "J. M. Synge," + + "J. R. Tolkien," + + "Jane Austin," + + "Leo Tolstoy," + + "Liam O'Flaherty," + + "Marcel Proust," + + "Mark Twain," + + "Oscar Wilde," + + "O. Henry," + + "Samuel Beckett," + + "Sean O'Casey," + + "William Shakespeare," + + "William Makepeace Thackeray," + + "W. B. Yeats," + + "Wilkie Collins"; +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Game.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Game.java new file mode 100644 index 0000000..971eac5 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Game.java @@ -0,0 +1,140 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author Administrator + */ +public class Game { + + private LocalDateTime localDateTime; + private Team homeTeam; + private Team awayTeam; + private GameEvent[] gameEvents; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent = null; + for (int i = 1; i <= 90; i++) { + if (Math.random() > 0.95) { + currEvent = Math.random() > 0.6 ? new Goal() : new Possession(); + currEvent.setTheTeam(Math.random() > 0.5 ? homeTeam : awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam().getPlayerArray() + [(int) (Math.random() * + currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + } + this.gameEvents = new GameEvent[eventList.size()]; + eventList.toArray(gameEvents); + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + " " + + this.localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE) + + "\n"); + + for (GameEvent currEvent : this.getEvents()) { + if (currEvent.getTheTeam() == homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + returnString.append(currEvent + "After " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append("(" + homeTeamGoals + " - " + + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the gameEvents + */ + public GameEvent[] getEvents() { + return gameEvents; + } + + /** + * @param gameEvents the gameEvents to set + */ + public void setEvents(GameEvent[] gameEvents) { + this.gameEvents = gameEvents; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/GameEvent.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/GameEvent.java new file mode 100644 index 0000000..1a83410 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/GameEvent.java @@ -0,0 +1,58 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.soccer; + +/** + * + * @author dmunguias + */ +public abstract class GameEvent { + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Goal.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Goal.java new file mode 100644 index 0000000..36daac3 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Goal.java @@ -0,0 +1,16 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.soccer; + +/* + * + * @author Administrator + */ +public class Goal extends GameEvent{ + + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/League.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/League.java new file mode 100644 index 0000000..521fda1 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/League.java @@ -0,0 +1,96 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.soccer; + +import com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.utility.PlayerDatabase; +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +/** + * + * @author Administrator + */ +public class League { + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,"+ + "The Swallows", 3); + Game[] theGames = theLeague.createGames(theTeams); + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame : theGames) { + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + theLeague.showBestTeam(theTeams); + } + + public Team[] createTeams(String teamName, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + StringTokenizer teamNameTokens = new StringTokenizer(teamName, ","); + + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + + for (int i = 0; i < theTeams.length; i++) { + theTeams[i] = new Team(teamNameTokens.nextToken(), + playerDB.getTeam(teamSize)); + } + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + ArrayList theGames = new ArrayList(); + for (Team homeTeam : theTeams) { + for (Team awayTeam : theTeams) { + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, + LocalDateTime.now().plusDays(daysBetweenGames))); + } + } + } + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\n" + "Team Points"); + + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + " : " + + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > + currBestTeam.getPointsTotal() ? currTeam : currBestTeam; + if (currTeam.getPointsTotal() > + currBestTeam.getPointsTotal()) { + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == + currBestTeam.getPointsTotal()) { + if (currTeam.getGoalsTotal() > + currBestTeam.getGoalsTotal()) { + currBestTeam = currTeam; + } + } + } + System.out.println("Winner of the League is " + + currBestTeam.getTeamName()); + } + + public String getLeagueAnnouncement(Game[] theGames) { + Period thePeriod = Period.between( + theGames[0].getLocalDateTime().toLocalDate(), + theGames[theGames.length - 1].getLocalDateTime().toLocalDate()); + return "The league is Scheduled to run for " + thePeriod.getMonths() + + " month(s), and " + thePeriod.getDays() + " day(s)\n"; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Player.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Player.java new file mode 100644 index 0000000..2536619 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Possession.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Possession.java new file mode 100644 index 0000000..ad7ef01 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Possession.java @@ -0,0 +1,14 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.soccer; + +/** + * + * @author dmunguias + */ +public class Possession extends GameEvent { + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Team.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Team.java new file mode 100644 index 0000000..21e756c --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/soccer/Team.java @@ -0,0 +1,95 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/utility/PlayerDatabase.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/utility/PlayerDatabase.java new file mode 100644 index 0000000..050a4f1 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module12/hierarchy/practice2/utility/PlayerDatabase.java @@ -0,0 +1,72 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.utility; + +import com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.soccer.Player; +import java.util.ArrayList; +import java.util.StringTokenizer; + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + private ArrayList players; + + public PlayerDatabase() { + StringTokenizer authorToken = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorToken.hasMoreElements()) { + players.add(new Player((String) authorToken.nextElement())); + + } + } + public Player[] getTeam(int numberOfPlayers){ + + Player[] teamsPlayer=new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++) { + int playerIndex= (int) (Math.random()*players.size()); + teamsPlayer[i]=players.get(playerIndex); + } + return teamsPlayer; + } + String authorList + = "Agatha Christie," + + "Alan Patton," + + "Alexander Solzhenitsyn," + + "Arthur Conan Doyle," + + "Anthony Trollope," + + "Baroness Orczy," + + "Brendan Behan," + + "Brian Moore," + + "Boris Pasternik," + + "Charles Dickens," + + "Charlotte Bronte," + + "Dorothy Parker," + + "Emile Zola," + + "Frank O'Connor," + + "Geoffrey Chaucer," + + "George Eliot," + + "G. K. Chesterton," + + "Graham Green," + + "Henry James," + + "James Joyce," + + "J. M. Synge," + + "J. R. Tolkien," + + "Jane Austin," + + "Leo Tolstoy," + + "Liam O'Flaherty," + + "Marcel Proust," + + "Mark Twain," + + "Oscar Wilde," + + "O. Henry," + + "Samuel Beckett," + + "Sean O'Casey," + + "William Shakespeare," + + "William Makepeace Thackeray," + + "W. B. Yeats," + + "Wilkie Collins"; +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Game.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Game.java new file mode 100644 index 0000000..7bc403f --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Game.java @@ -0,0 +1,142 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice1; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author Administrator + */ +public class Game { + + private LocalDateTime localDateTime; + private Team homeTeam; + private Team awayTeam; + private GameEvent[] gameEvents; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent = null; + for (int i = 1; i <= 90; i++) { + if (Math.random() > 0.95) { + currEvent = Math.random() > 0.8 ? new Goal() : new Possession(); + currEvent.setTheTeam(Math.random() > 0.8 ? homeTeam : awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam().getPlayerArray() + [(int) (Math.random() + * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + } + this.gameEvents = new GameEvent[eventList.size()]; + eventList.toArray(gameEvents); + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + " " + + this.localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE) + + "\n"); + + for (GameEvent currEvent : this.getEvents()) { + if (currEvent instanceof Goal) { + + if (currEvent.getTheTeam() == homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + } + returnString.append(currEvent + "After " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append("(" + homeTeamGoals + " - " + + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the gameEvents + */ + public GameEvent[] getEvents() { + return gameEvents; + } + + /** + * @param gameEvents the gameEvents to set + */ + public void setEvents(GameEvent[] gameEvents) { + this.gameEvents = gameEvents; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/GameEvent.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/GameEvent.java new file mode 100644 index 0000000..c21eadd --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/GameEvent.java @@ -0,0 +1,60 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice1; + +import com.cert.oca8.course.devdamunguia95.module12.hierarchy.practice2.soccer.*; + +/** + * + * @author dmunguias + */ +public abstract class GameEvent { + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Goal.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Goal.java new file mode 100644 index 0000000..036058b --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Goal.java @@ -0,0 +1,21 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice1; + +/* + * + * @author Administrator + */ +public class Goal extends GameEvent{ + + @Override + public String toString() { + return "Goal "; + } + + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/League.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/League.java new file mode 100644 index 0000000..37d1904 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/League.java @@ -0,0 +1,96 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice1; + +import com.cert.oca8.course.devdamunguia95.module13.interfaces.practice1.utility.PlayerDatabase; +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +/** + * + * @author Administrator + */ +public class League { + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,"+ + "The Swallows", 3); + Game[] theGames = theLeague.createGames(theTeams); + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame : theGames) { + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + theLeague.showBestTeam(theTeams); + } + + public Team[] createTeams(String teamName, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + StringTokenizer teamNameTokens = new StringTokenizer(teamName, ","); + + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + + for (int i = 0; i < theTeams.length; i++) { + theTeams[i] = new Team(teamNameTokens.nextToken(), + playerDB.getTeam(teamSize)); + } + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + ArrayList theGames = new ArrayList(); + for (Team homeTeam : theTeams) { + for (Team awayTeam : theTeams) { + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, + LocalDateTime.now().plusDays(daysBetweenGames))); + } + } + } + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\n" + "Team Points"); + + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + " : " + + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > + currBestTeam.getPointsTotal() ? currTeam : currBestTeam; + if (currTeam.getPointsTotal() > + currBestTeam.getPointsTotal()) { + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == + currBestTeam.getPointsTotal()) { + if (currTeam.getGoalsTotal() > + currBestTeam.getGoalsTotal()) { + currBestTeam = currTeam; + } + } + } + System.out.println("Winner of the League is " + + currBestTeam.getTeamName()); + } + + public String getLeagueAnnouncement(Game[] theGames) { + Period thePeriod = Period.between( + theGames[0].getLocalDateTime().toLocalDate(), + theGames[theGames.length - 1].getLocalDateTime().toLocalDate()); + return "The league is Scheduled to run for " + thePeriod.getMonths() + + " month(s), and " + thePeriod.getDays() + " day(s)\n"; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Player.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Player.java new file mode 100644 index 0000000..d2378ca --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Player.java @@ -0,0 +1,36 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice1; +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Possession.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Possession.java new file mode 100644 index 0000000..4375455 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Possession.java @@ -0,0 +1,21 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice1; + +/** + * + * @author dmunguias + */ +public class Possession extends GameEvent { + + @Override + public String toString() { + return "Possession "; //To change body of generated methods, choose Tools | Templates. + } + + +} + diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Team.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Team.java new file mode 100644 index 0000000..6f8b689 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/Team.java @@ -0,0 +1,95 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice1; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/utility/PlayerDatabase.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/utility/PlayerDatabase.java new file mode 100644 index 0000000..0272d21 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice1/utility/PlayerDatabase.java @@ -0,0 +1,72 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice1.utility; + +import com.cert.oca8.course.devdamunguia95.module13.interfaces.practice1.Player; +import java.util.ArrayList; +import java.util.StringTokenizer; + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + private ArrayList players; + + public PlayerDatabase() { + StringTokenizer authorToken = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorToken.hasMoreElements()) { + players.add(new Player((String) authorToken.nextElement())); + + } + } + public Player[] getTeam(int numberOfPlayers){ + + Player[] teamsPlayer=new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++) { + int playerIndex= (int) (Math.random()*players.size()); + teamsPlayer[i]=players.get(playerIndex); + } + return teamsPlayer; + } + String authorList + = "Agatha Christie," + + "Alan Patton," + + "Alexander Solzhenitsyn," + + "Arthur Conan Doyle," + + "Anthony Trollope," + + "Baroness Orczy," + + "Brendan Behan," + + "Brian Moore," + + "Boris Pasternik," + + "Charles Dickens," + + "Charlotte Bronte," + + "Dorothy Parker," + + "Emile Zola," + + "Frank O'Connor," + + "Geoffrey Chaucer," + + "George Eliot," + + "G. K. Chesterton," + + "Graham Green," + + "Henry James," + + "James Joyce," + + "J. M. Synge," + + "J. R. Tolkien," + + "Jane Austin," + + "Leo Tolstoy," + + "Liam O'Flaherty," + + "Marcel Proust," + + "Mark Twain," + + "Oscar Wilde," + + "O. Henry," + + "Samuel Beckett," + + "Sean O'Casey," + + "William Shakespeare," + + "William Makepeace Thackeray," + + "W. B. Yeats," + + "Wilkie Collins"; +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Game.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Game.java new file mode 100644 index 0000000..befbab1 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Game.java @@ -0,0 +1,142 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice2; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author Administrator + */ +public class Game { + + private LocalDateTime localDateTime; + private Team homeTeam; + private Team awayTeam; + private GameEvent[] gameEvents; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent = null; + for (int i = 1; i <= 90; i++) { + if (Math.random() > 0.95) { + currEvent = Math.random() > 0.8 ? new Goal() : new Possession(); + currEvent.setTheTeam(Math.random() > 0.8 ? homeTeam : awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam().getPlayerArray() + [(int) (Math.random() + * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + } + this.gameEvents = new GameEvent[eventList.size()]; + eventList.toArray(gameEvents); + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + " " + + this.localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE) + + "\n"); + + for (GameEvent currEvent : this.getEvents()) { + if (currEvent instanceof Goal) { + + if (currEvent.getTheTeam() == homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + } + returnString.append(currEvent + "After " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append("(" + homeTeamGoals + " - " + + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the gameEvents + */ + public GameEvent[] getEvents() { + return gameEvents; + } + + /** + * @param gameEvents the gameEvents to set + */ + public void setEvents(GameEvent[] gameEvents) { + this.gameEvents = gameEvents; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/GameEvent.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/GameEvent.java new file mode 100644 index 0000000..4e6c5a0 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/GameEvent.java @@ -0,0 +1,57 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice2; +/** + * + * @author dmunguias + */ +public abstract class GameEvent { + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Goal.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Goal.java new file mode 100644 index 0000000..7b9dc91 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Goal.java @@ -0,0 +1,21 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice2; + +/* + * + * @author Administrator + */ +public class Goal extends GameEvent{ + + @Override + public String toString() { + return "Goal "; + } + + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/League.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/League.java new file mode 100644 index 0000000..c11d6a2 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/League.java @@ -0,0 +1,98 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice2; + +import com.cert.oca8.course.devdamunguia95.module13.interfaces.practice2.utility.PlayerDatabase; +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.StringTokenizer; +/** + * + * @author Administrator + */ +public class League { + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,"+ + "The Swallows", 3); + Game[] theGames = theLeague.createGames(theTeams); + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame : theGames) { + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + theLeague.showBestTeam(theTeams); + } + + public Team[] createTeams(String teamName, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + StringTokenizer teamNameTokens = new StringTokenizer(teamName, ","); + + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + + for (int i = 0; i < theTeams.length; i++) { + theTeams[i] = new Team(teamNameTokens.nextToken(), + playerDB.getTeam(teamSize)); + } + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + ArrayList theGames = new ArrayList(); + for (Team homeTeam : theTeams) { + for (Team awayTeam : theTeams) { + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, + LocalDateTime.now().plusDays(daysBetweenGames))); + } + } + } + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Arrays.sort(theTeams); + Team currBestTeam = theTeams[0]; + System.out.println("\n" + "Team Points"); + + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + " : " + + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > + currBestTeam.getPointsTotal() ? currTeam : currBestTeam; + if (currTeam.getPointsTotal() > + currBestTeam.getPointsTotal()) { + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == + currBestTeam.getPointsTotal()) { + if (currTeam.getGoalsTotal() > + currBestTeam.getGoalsTotal()) { + currBestTeam = currTeam; + } + } + } + System.out.println("Winner of the League is " + + currBestTeam.getTeamName()); + } + + public String getLeagueAnnouncement(Game[] theGames) { + Period thePeriod = Period.between( + theGames[0].getLocalDateTime().toLocalDate(), + theGames[theGames.length - 1].getLocalDateTime().toLocalDate()); + return "The league is Scheduled to run for " + thePeriod.getMonths() + + " month(s), and " + thePeriod.getDays() + " day(s)\n"; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Player.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Player.java new file mode 100644 index 0000000..10e1643 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Player.java @@ -0,0 +1,36 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice2; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Possession.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Possession.java new file mode 100644 index 0000000..6a7580c --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Possession.java @@ -0,0 +1,21 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice2; + +/** + * + * @author dmunguias + */ +public class Possession extends GameEvent { + + @Override + public String toString() { + return "Possession "; //To change body of generated methods, choose Tools | Templates. + } + + +} + diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Team.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Team.java new file mode 100644 index 0000000..357e57b --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/Team.java @@ -0,0 +1,108 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice2; + +/** + * + * @author Administrator + */ + public class Team implements Comparable { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals) { + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points) { + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() { + } + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + + @Override + public int compareTo(Object theTeam) { + int returnValue = -1; + if (this.getPointsTotal() < ((Team) theTeam).getPointsTotal()) { + return -1; + } else if (this.getPointsTotal() + == ((Team) theTeam).getPointsTotal()) { + if (this.getGoalsTotal() < ((Team) theTeam).getGoalsTotal()) { + returnValue = 1; + } + } + return returnValue; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/utility/PlayerDatabase.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/utility/PlayerDatabase.java new file mode 100644 index 0000000..ae2864e --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice2/utility/PlayerDatabase.java @@ -0,0 +1,72 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice2.utility; + +import com.cert.oca8.course.devdamunguia95.module13.interfaces.practice2.Player; +import java.util.ArrayList; +import java.util.StringTokenizer; + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + private ArrayList players; + + public PlayerDatabase() { + StringTokenizer authorToken = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorToken.hasMoreElements()) { + players.add(new Player((String) authorToken.nextElement())); + + } + } + public Player[] getTeam(int numberOfPlayers){ + + Player[] teamsPlayer=new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++) { + int playerIndex= (int) (Math.random()*players.size()); + teamsPlayer[i]=players.get(playerIndex); + } + return teamsPlayer; + } + String authorList + = "Agatha Christie," + + "Alan Patton," + + "Alexander Solzhenitsyn," + + "Arthur Conan Doyle," + + "Anthony Trollope," + + "Baroness Orczy," + + "Brendan Behan," + + "Brian Moore," + + "Boris Pasternik," + + "Charles Dickens," + + "Charlotte Bronte," + + "Dorothy Parker," + + "Emile Zola," + + "Frank O'Connor," + + "Geoffrey Chaucer," + + "George Eliot," + + "G. K. Chesterton," + + "Graham Green," + + "Henry James," + + "James Joyce," + + "J. M. Synge," + + "J. R. Tolkien," + + "Jane Austin," + + "Leo Tolstoy," + + "Liam O'Flaherty," + + "Marcel Proust," + + "Mark Twain," + + "Oscar Wilde," + + "O. Henry," + + "Samuel Beckett," + + "Sean O'Casey," + + "William Shakespeare," + + "William Makepeace Thackeray," + + "W. B. Yeats," + + "Wilkie Collins"; +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Game.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Game.java new file mode 100644 index 0000000..2f6f374 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Game.java @@ -0,0 +1,142 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice3; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author Administrator + */ +public class Game { + + private LocalDateTime localDateTime; + private Team homeTeam; + private Team awayTeam; + private GameEvent[] gameEvents; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent = null; + for (int i = 1; i <= 90; i++) { + if (Math.random() > 0.95) { + currEvent = Math.random() > 0.8 ? new Goal() : new Possession(); + currEvent.setTheTeam(Math.random() > 0.8 ? homeTeam : awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam().getPlayerArray() + [(int) (Math.random() + * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + } + this.gameEvents = new GameEvent[eventList.size()]; + eventList.toArray(gameEvents); + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + " " + + this.localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE) + + "\n"); + + for (GameEvent currEvent : this.getEvents()) { + if (currEvent instanceof Goal) { + currEvent.getThePlayer().incGoalsScored(); + if (currEvent.getTheTeam() == homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + } + returnString.append(currEvent + "After " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append("(" + homeTeamGoals + " - " + + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the gameEvents + */ + public GameEvent[] getEvents() { + return gameEvents; + } + + /** + * @param gameEvents the gameEvents to set + */ + public void setEvents(GameEvent[] gameEvents) { + this.gameEvents = gameEvents; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/GameEvent.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/GameEvent.java new file mode 100644 index 0000000..ecae3e1 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/GameEvent.java @@ -0,0 +1,58 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice3; + +/** + * + * @author dmunguias + */ +public abstract class GameEvent { + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Goal.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Goal.java new file mode 100644 index 0000000..8ca4657 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Goal.java @@ -0,0 +1,21 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice3; + +/* + * + * @author Administrator + */ +public class Goal extends GameEvent{ + + @Override + public String toString() { + return "Goal "; + } + + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/League.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/League.java new file mode 100644 index 0000000..9bc17b4 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/League.java @@ -0,0 +1,117 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice3; + +import com.cert.oca8.course.devdamunguia95.module13.interfaces.practice3.utility.PlayerDatabase; +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.StringTokenizer; +import java.util.Collections; +import java.util.Comparator; +/** + * + * @author Administrator + */ +public class League { + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,"+ + "The Swallows", 3); + Game[] theGames = theLeague.createGames(theTeams); + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame : theGames) { + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + theLeague.showBestTeam(theTeams); + theLeague.showBestPlayers(theTeams); + } + + public Team[] createTeams(String teamName, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + StringTokenizer teamNameTokens = new StringTokenizer(teamName, ","); + + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + + for (int i = 0; i < theTeams.length; i++) { + theTeams[i] = new Team(teamNameTokens.nextToken(), + playerDB.getTeam(teamSize)); + } + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + ArrayList theGames = new ArrayList(); + for (Team homeTeam : theTeams) { + for (Team awayTeam : theTeams) { + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, + LocalDateTime.now().plusDays(daysBetweenGames))); + } + } + } + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Arrays.sort(theTeams); + Team currBestTeam = theTeams[0]; + System.out.println("\n" + "Team Points"); + + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + " : " + + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > + currBestTeam.getPointsTotal() ? currTeam : currBestTeam; + if (currTeam.getPointsTotal() > + currBestTeam.getPointsTotal()) { + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == + currBestTeam.getPointsTotal()) { + if (currTeam.getGoalsTotal() > + currBestTeam.getGoalsTotal()) { + currBestTeam = currTeam; + } + } + } + System.out.println("Winner of the League is " + + currBestTeam.getTeamName()); + } + + public String getLeagueAnnouncement(Game[] theGames) { + Period thePeriod = Period.between( + theGames[0].getLocalDateTime().toLocalDate(), + theGames[theGames.length - 1].getLocalDateTime().toLocalDate()); + return "The league is Scheduled to run for " + thePeriod.getMonths() + + " month(s), and " + thePeriod.getDays() + " day(s)\n"; + } + + public void showBestPlayers(Team[] theTeams){ + System.out.println("\n\nBest Players"); + ArrayList thePlayers= new ArrayList(); + for (Team currTeam : theTeams) { + thePlayers.addAll(Arrays.asList(currTeam.getPlayerArray())); + } + + Collections.sort(thePlayers, (p1,p2)-> + Double.valueOf(p2.getGoalScored()).compareTo(Double.valueOf(p1.getGoalScored()))); + + for (Player currPlayer : thePlayers) { + System.out.println(currPlayer.getPlayerName()+ " : " + + currPlayer.getGoalScored()); + } + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Player.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Player.java new file mode 100644 index 0000000..09edd68 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Player.java @@ -0,0 +1,49 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice3; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + private int goalScored; + + public void incGoalsScored(){ + this.goalScored++; + } + + public int getGoalScored() { + return goalScored; + } + + public void setGoalScored(int goalScored) { + this.goalScored = goalScored; + } + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Possession.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Possession.java new file mode 100644 index 0000000..fa1a316 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Possession.java @@ -0,0 +1,21 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice3; + +/** + * + * @author dmunguias + */ +public class Possession extends GameEvent { + + @Override + public String toString() { + return "Possession "; //To change body of generated methods, choose Tools | Templates. + } + + +} + diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Team.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Team.java new file mode 100644 index 0000000..ee46346 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/Team.java @@ -0,0 +1,108 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice3; + +/** + * + * @author Administrator + */ + public class Team implements Comparable { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals) { + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points) { + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() { + } + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + + @Override + public int compareTo(Object theTeam) { + int returnValue = -1; + if (this.getPointsTotal() < ((Team) theTeam).getPointsTotal()) { + return -1; + } else if (this.getPointsTotal() + == ((Team) theTeam).getPointsTotal()) { + if (this.getGoalsTotal() < ((Team) theTeam).getGoalsTotal()) { + returnValue = 1; + } + } + return returnValue; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/utility/PlayerDatabase.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/utility/PlayerDatabase.java new file mode 100644 index 0000000..49b034d --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module13/interfaces/practice3/utility/PlayerDatabase.java @@ -0,0 +1,72 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module13.interfaces.practice3.utility; + +import com.cert.oca8.course.devdamunguia95.module13.interfaces.practice3.Player; +import java.util.ArrayList; +import java.util.StringTokenizer; + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + private ArrayList players; + + public PlayerDatabase() { + StringTokenizer authorToken = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorToken.hasMoreElements()) { + players.add(new Player((String) authorToken.nextElement())); + + } + } + public Player[] getTeam(int numberOfPlayers){ + + Player[] teamsPlayer=new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++) { + int playerIndex= (int) (Math.random()*players.size()); + teamsPlayer[i]=players.get(playerIndex); + } + return teamsPlayer; + } + String authorList + = "Agatha Christie," + + "Alan Patton," + + "Alexander Solzhenitsyn," + + "Arthur Conan Doyle," + + "Anthony Trollope," + + "Baroness Orczy," + + "Brendan Behan," + + "Brian Moore," + + "Boris Pasternik," + + "Charles Dickens," + + "Charlotte Bronte," + + "Dorothy Parker," + + "Emile Zola," + + "Frank O'Connor," + + "Geoffrey Chaucer," + + "George Eliot," + + "G. K. Chesterton," + + "Graham Green," + + "Henry James," + + "James Joyce," + + "J. M. Synge," + + "J. R. Tolkien," + + "Jane Austin," + + "Leo Tolstoy," + + "Liam O'Flaherty," + + "Marcel Proust," + + "Mark Twain," + + "Oscar Wilde," + + "O. Henry," + + "Samuel Beckett," + + "Sean O'Casey," + + "William Shakespeare," + + "William Makepeace Thackeray," + + "W. B. Yeats," + + "Wilkie Collins"; +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Game.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Game.java new file mode 100644 index 0000000..26be005 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Game.java @@ -0,0 +1,142 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module14.Exception.practice1; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author Administrator + */ +public class Game { + + private LocalDateTime localDateTime; + private Team homeTeam; + private Team awayTeam; + private GameEvent[] gameEvents; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent = null; + for (int i = 1; i <= 90; i++) { + if (Math.random() > 0.95) { + currEvent = Math.random() > 0.8 ? new Goal() : new Possession(); + currEvent.setTheTeam(Math.random() > 0.8 ? homeTeam : awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam().getPlayerArray() + [(int) (Math.random() + * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + } + this.gameEvents = new GameEvent[eventList.size()]; + eventList.toArray(gameEvents); + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + " " + + this.localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE) + + "\n"); + + for (GameEvent currEvent : this.getEvents()) { + if (currEvent instanceof Goal) { + currEvent.getThePlayer().incGoalsScored(); + if (currEvent.getTheTeam() == homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + } + returnString.append(currEvent + "After " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append("(" + homeTeamGoals + " - " + + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the gameEvents + */ + public GameEvent[] getEvents() { + return gameEvents; + } + + /** + * @param gameEvents the gameEvents to set + */ + public void setEvents(GameEvent[] gameEvents) { + this.gameEvents = gameEvents; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/GameEvent.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/GameEvent.java new file mode 100644 index 0000000..faccc3e --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/GameEvent.java @@ -0,0 +1,58 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module14.Exception.practice1; + +/** + * + * @author dmunguias + */ +public abstract class GameEvent { + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Goal.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Goal.java new file mode 100644 index 0000000..f7e46a8 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Goal.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.cert.oca8.course.devdamunguia95.module14.Exception.practice1; +/* + * + * @author Administrator + */ +public class Goal extends GameEvent{ + + @Override + public String toString() { + return "Goal "; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/League.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/League.java new file mode 100644 index 0000000..13df54c --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/League.java @@ -0,0 +1,123 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module14.Exception.practice1; + +import com.cert.oca8.course.devdamunguia95.module14.Exception.practice1.utility.PlayerDatabase; +import com.cert.oca8.course.devdamunguia95.module14.Exception.practice1.utility.PlayerDatabaseException; +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.StringTokenizer; +import java.util.Collections; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + try { + League theLeague = new League(); + Team[] theTeams = theLeague.createTeams("The Robins,The Crows," + + "The Swallows, The Owls", 11); + Game[] theGames = theLeague.createGames(theTeams); + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame : theGames) { + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + theLeague.showBestTeam(theTeams); + theLeague.showBestPlayers(theTeams); + } catch (Exception e) { + + e.printStackTrace(System.err); + } + } + + public Team[] createTeams(String teamName, int teamSize) throws PlayerDatabaseException { + + PlayerDatabase playerDB = new PlayerDatabase(); + StringTokenizer teamNameTokens = new StringTokenizer(teamName, ","); + + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + + for (int i = 0; i < theTeams.length; i++) { + theTeams[i] = new Team(teamNameTokens.nextToken(), + playerDB.getTeam(teamSize)); + } + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + ArrayList theGames = new ArrayList(); + for (Team homeTeam : theTeams) { + for (Team awayTeam : theTeams) { + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, + LocalDateTime.now().plusDays(daysBetweenGames))); + } + } + } + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Arrays.sort(theTeams); + Team currBestTeam = theTeams[0]; + System.out.println("\n" + "Team Points"); + + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + " : " + + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() + > currBestTeam.getPointsTotal() ? currTeam : currBestTeam; + if (currTeam.getPointsTotal() + > currBestTeam.getPointsTotal()) { + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() + == currBestTeam.getPointsTotal()) { + if (currTeam.getGoalsTotal() + > currBestTeam.getGoalsTotal()) { + currBestTeam = currTeam; + } + } + } + System.out.println("Winner of the League is " + + currBestTeam.getTeamName()); + } + + public String getLeagueAnnouncement(Game[] theGames) { + Period thePeriod = Period.between( + theGames[0].getLocalDateTime().toLocalDate(), + theGames[theGames.length - 1].getLocalDateTime().toLocalDate()); + return "The league is Scheduled to run for " + thePeriod.getMonths() + + " month(s), and " + thePeriod.getDays() + " day(s)\n"; + } + + public void showBestPlayers(Team[] theTeams) { + System.out.println("\n\nBest Players"); + ArrayList thePlayers = new ArrayList(); + for (Team currTeam : theTeams) { + thePlayers.addAll(Arrays.asList(currTeam.getPlayerArray())); + } + + Collections.sort(thePlayers, (p1, p2) + -> Double.valueOf(p2.getGoalScored()).compareTo(Double.valueOf(p1.getGoalScored()))); + + for (Player currPlayer : thePlayers) { + System.out.println(currPlayer.getPlayerName() + " : " + + currPlayer.getGoalScored()); + } + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Player.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Player.java new file mode 100644 index 0000000..fbf7268 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Player.java @@ -0,0 +1,49 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module14.Exception.practice1; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + private int goalScored; + + public void incGoalsScored(){ + this.goalScored++; + } + + public int getGoalScored() { + return goalScored; + } + + public void setGoalScored(int goalScored) { + this.goalScored = goalScored; + } + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Possession.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Possession.java new file mode 100644 index 0000000..e269b6a --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Possession.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module14.Exception.practice1; + +/** + * + * @author dmunguias + */ +public class Possession extends GameEvent { + + @Override + public String toString() { + return "Possession "; //To change body of generated methods, choose Tools | Templates. + } +} + diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Team.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Team.java new file mode 100644 index 0000000..b028f25 --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/Team.java @@ -0,0 +1,108 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module14.Exception.practice1; + +/** + * + * @author Administrator + */ + public class Team implements Comparable { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals) { + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points) { + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() { + } + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + + @Override + public int compareTo(Object theTeam) { + int returnValue = -1; + if (this.getPointsTotal() < ((Team) theTeam).getPointsTotal()) { + return -1; + } else if (this.getPointsTotal() + == ((Team) theTeam).getPointsTotal()) { + if (this.getGoalsTotal() < ((Team) theTeam).getGoalsTotal()) { + returnValue = 1; + } + } + return returnValue; + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/utility/PlayerDatabase.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/utility/PlayerDatabase.java new file mode 100644 index 0000000..dec512a --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/utility/PlayerDatabase.java @@ -0,0 +1,82 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module14.Exception.practice1.utility; + +import com.cert.oca8.course.devdamunguia95.module14.Exception.practice1.Player; +import java.util.ArrayList; +import java.util.StringTokenizer; + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase() { + StringTokenizer authorToken = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorToken.hasMoreElements()) { + players.add(new Player((String) authorToken.nextElement())); + + } + } + + public Player[] getTeam(int numberOfPlayers)throws PlayerDatabaseException{ + try { + Player[] teamsPlayer = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++) { + int playerIndex = (int) (Math.random() * players.size()); + teamsPlayer[i] = players.get(playerIndex); + players.remove(playerIndex); + + + } + return teamsPlayer; + }catch(IndexOutOfBoundsException ie){ + throw new PlayerDatabaseException("No enogth players in " + + "the database for the temas resquested ."); + } + + } + String authorList + = "Agatha Christie," + + "Alan Patton," + + "Alexander Solzhenitsyn," + + "Arthur Conan Doyle," + + "Anthony Trollope," + + "Baroness Orczy," + + "Brendan Behan," + + "Brian Moore," + + "Boris Pasternik," + + "Charles Dickens," + + "Charlotte Bronte," + + "Dorothy Parker," + + "Emile Zola," + + "Frank O'Connor," + + "Geoffrey Chaucer," + + "George Eliot," + + "G. K. Chesterton," + + "Graham Green," + + "Henry James," + + "James Joyce," + + "J. M. Synge," + + "J. R. Tolkien," + + "Jane Austin," + + "Leo Tolstoy," + + "Liam O'Flaherty," + + "Marcel Proust," + + "Mark Twain," + + "Oscar Wilde," + + "O. Henry," + + "Samuel Beckett," + + "Sean O'Casey," + + "William Shakespeare," + + "William Makepeace Thackeray," + + "W. B. Yeats," + + "Wilkie Collins"; +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/utility/PlayerDatabaseException.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/utility/PlayerDatabaseException.java new file mode 100644 index 0000000..34803bf --- /dev/null +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module14/Exception/practice1/utility/PlayerDatabaseException.java @@ -0,0 +1,16 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.cert.oca8.course.devdamunguia95.module14.Exception.practice1.utility; + +/** + * + * @author dmunguias + */ +public class PlayerDatabaseException extends Exception{ + public PlayerDatabaseException(String message){ + super(message); + } +} diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module9/practice2/Game.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module9/practice2/Game.java index ef97423..978b176 100644 --- a/src/main/java/com/cert/oca8/course/devdamunguia95/module9/practice2/Game.java +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module9/practice2/Game.java @@ -36,11 +36,11 @@ public void playGame() { public String getDescription() { StringBuilder returnString = new StringBuilder(); - for (Goal goal : this.getGoals()) { + for (Goal currGame : this.getGoals()) { returnString.append("Goal scored after " - + goal.getTheTime() + " mins by " - + goal.getThePlayer().getPlayerName() + " of " - + goal.getTheTeam().getTeamName() + "\n"); + + currGame.getTheTime() + " mins by " + + currGame.getThePlayer().getPlayerName() + " of " + + currGame.getTheTeam().getTeamName() + "\n"); } return returnString.toString(); } diff --git a/src/main/java/com/cert/oca8/course/devdamunguia95/module9/practice2/Team.java b/src/main/java/com/cert/oca8/course/devdamunguia95/module9/practice2/Team.java index 172706a..cc14254 100644 --- a/src/main/java/com/cert/oca8/course/devdamunguia95/module9/practice2/Team.java +++ b/src/main/java/com/cert/oca8/course/devdamunguia95/module9/practice2/Team.java @@ -4,14 +4,16 @@ * and open the template in the editor. */ package com.cert.oca8.course.devdamunguia95.module9.practice2; + /** * * @author dmunguias */ public class Team { + private String teamName; private Player[] playerArray; - + public Team() { } @@ -20,27 +22,31 @@ public Team(String teamName) { } public Team(String teamName, Player[] playerArray) { - this(teamName); + this(teamName); this.playerArray = playerArray; } + /** * @return the teamName */ public String getTeamName() { return teamName; } + /** * @param teamName the teamName to set */ public void setTeamName(String teamName) { this.teamName = teamName; } + /** * @return the playerArray */ public Player[] getPlayerArray() { return playerArray; } + /** * @param playerArray the playerArray to set */