Skip to content

Instantly share code, notes, and snippets.

@borncorp
Created June 30, 2014 16:59
Show Gist options
  • Save borncorp/2626a0e63217e6c9237c to your computer and use it in GitHub Desktop.
Save borncorp/2626a0e63217e6c9237c to your computer and use it in GitHub Desktop.
Soccer
import java.util.Random;
public class Match {
private Team team1;
private Team team2;
private Team winner;
public Match(Team team1, Team team2) {
super();
this.team1 = team1;
this.team2 = team2;
}
private void getRandomWinner() {
Random myRandom = new Random();
if (myRandom.nextBoolean())
setWinner(team1);
else
setWinner(team2);
}
public Team getTeam1() {
return team1;
}
public Team getTeam2() {
return team2;
}
public Team getWinner() {
return winner;
}
public void playGame() {
System.out.println("Playing match between " + getTeam1().getName()
+ " vs " + getTeam2().getName());
getRandomWinner();
System.out.println(getWinner().getName() + " won");
}
public void setTeam1(Team team1) {
this.team1 = team1;
}
public void setTeam2(Team team2) {
this.team2 = team2;
}
public void setWinner(Team winner) {
this.winner = winner;
}
}
public class SoccerSimulator {
public static void main(String[] args) {
Team Barcelona = new Team("Barcelona");
Team RealMadrid = new Team("Real Madrid");
Team America = new Team("America");
Team Chivas = new Team("Chivas");
// Match classic= new Match(Barcelona, RealMadrid);
// classic.playGame();
Tournament myTournament = new Tournament(Barcelona, RealMadrid, America, Chivas);
myTournament.playTournament();
System.out.println("Final winner: USA");
}
}
public class Team {
private int goals;
private String name;
public int getGoals() {
return goals;
}
public String getName() {
return name;
}
public Team(String name) {
this.name=name;
}
public void scoreGoal(){
goals+=1;
}
}
public class Tournament {
private Team team1, team2, team3, team4;
public Tournament(Team team1, Team team2, Team team3, Team team4) {
super();
this.team1 = team1;
this.team2 = team2;
this.team3 = team3;
this.team4 = team4;
}
public void playTournament(){
Match match1=new Match(getTeam1(), getTeam2());
match1.playGame();
Match match2=new Match(getTeam3(), getTeam4());
match2.playGame();
System.out.println("Final Match");
Match finalmatch = new Match(match1.getWinner(), match2.getWinner());
finalmatch.playGame();
}
public Team getTeam1() {
return team1;
}
public void setTeam1(Team team1) {
this.team1 = team1;
}
public Team getTeam2() {
return team2;
}
public void setTeam2(Team team2) {
this.team2 = team2;
}
public Team getTeam3() {
return team3;
}
public void setTeam3(Team team3) {
this.team3 = team3;
}
public Team getTeam4() {
return team4;
}
public void setTeam4(Team team4) {
this.team4 = team4;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment