Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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++;

Copy link
Owner

@breakponchito breakponchito Aug 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eliminar espacio innecesario

} 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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) {

Copy link
Owner

@breakponchito breakponchito Aug 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eliminar espacio innecesario

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());
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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) {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eliminar espacio innecesario

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;
}
}
Original file line number Diff line number Diff line change
@@ -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) {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eliminar espacio innecesario

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 {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eliminar espacio innecesario

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;
}
}
Loading