Skip to content

Commit

Permalink
Added significant methods for some classes, namely a method to sort t…
Browse files Browse the repository at this point in the history
…he Standings
  • Loading branch information
mariuskilian committed Aug 26, 2017
1 parent 062d449 commit f808595
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 11 deletions.
8 changes: 8 additions & 0 deletions football-prediction/src/main/java/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public Game(Team homeTeam, Team awayTeam) {
this.awayTeam = awayTeam;
}

public void setPredictedScore(Score score) {
predictedScore = score;
}

public void setFinalScore(Score score) {
finalScore = score;
}

public Team getHomeTeam() {
return homeTeam;
}
Expand Down
2 changes: 1 addition & 1 deletion football-prediction/src/main/java/Gameday.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Gameday {
public Gameday(Game game[]) {
this.game = game;
for(Game g : game) {
Calculations.predictGame(g);
g.setPredictedScore(Calculations.predictGame(g));
}
}

Expand Down
2 changes: 1 addition & 1 deletion football-prediction/src/main/java/Season.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public Season() {
awayStandings = new Table(team, false, true);
}

}
}
102 changes: 93 additions & 9 deletions football-prediction/src/main/java/Table.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,122 @@
import java.util.ArrayList;

/**
* Created by marius on 25/08/2017.
*/
public class Table {

private Spot spot[];
private Spot table[];
private boolean includeHome;
private boolean includeAway;

Table(Team team[], boolean includeHome, boolean includeAway) {
spot = new Spot[team.length];
table = new Spot[team.length];
this.includeHome = includeHome;
this.includeAway = includeAway;

for (int i = 0; i < team.length; i++) {
spot[i] = new Spot(team[i], includeHome, includeAway);
table[i] = new Spot(team[i], includeHome, includeAway, i);
}
}

public void sortTable() {
Spot tmp[] = new Spot[table.length];
ArrayList<Spot> highestLeft = new ArrayList(); // Array list because several teams might share standing

// Update all the Stats
for (Spot s : table) {
s.calcStats(includeHome, includeAway);
}

while (!sortedAll()) { // Checks if the whole Table has been sorted or not
for (int i = 0; i < table.length; i++) {

if (table[i] != null) {

if (highestLeft.get(0) == null) {
highestLeft.clear();
highestLeft.add(table[i]);
} else {

// Booleans for comparing standing
boolean samePoints = table[i].points == highestLeft.get(0).points;
boolean sameGoalDiff = table[i].goalDiff == highestLeft.get(0).goalDiff;
boolean sameScored = table[i].goalsScored == highestLeft.get(0).goalsScored;

if (table[i].points > highestLeft.get(0).points ||
(samePoints && table[i].goalDiff > highestLeft.get(0).goalDiff) ||
(samePoints && sameGoalDiff && table[i].goalsScored > highestLeft.get(0).goalsScored)) {
highestLeft.clear();
highestLeft.add(table[i]);
} else if (samePoints && sameGoalDiff && sameScored) {
highestLeft.add(table[i]);
}
}
}

}

int nextStandingIndex = 0;
int standing = 1;
for (int i = 0; tmp[i] != null; i++) {
nextStandingIndex = i + 1;
}
if (nextStandingIndex != 0) {
standing = tmp[nextStandingIndex - 1].standing + 1;
}

int tmpArrayListIndex = 0;
Spot s = highestLeft.get(tmpArrayListIndex);
while (s != null) {
s.setStanding(standing);
tmp[nextStandingIndex++] = s;
table[s.index] = null;
s = highestLeft.get(++tmpArrayListIndex);
}
}

for (int i = 0; i < table.length; i++) {
table[i] = tmp[i];
}
}

private boolean sortedAll() {
for (int i = 0; i < table.length; i++) {
if (table[i] != null) {
return false;
}
}
return true;
}

}

class Spot {

int index;
int standing;
Team team;
int wins;
int losses;
int ties;
int goalsScored;
int goalsRecieved;
int goalDiff;
int points;

Spot(Team team, boolean includeHome, boolean includeAway) {
Spot(Team team, boolean includeHome, boolean includeAway, int index) {
this.team = team;
this.index = index;
calcStats(includeHome, includeAway);
}

void calcStats(boolean includeHome, boolean includeAway) {
wins = 0;
losses = 0;
ties = 0;
goalsScored = 0;
goalsRecieved = 0;
points = 0;
calcStats(includeHome, includeAway);
}

void calcStats(boolean includeHome, boolean includeAway) {
if (includeHome) {
wins += team.getHomeWins();
losses += team.getHomeLosses();
Expand All @@ -52,7 +131,12 @@ void calcStats(boolean includeHome, boolean includeAway) {
goalsScored += team.getAwayScored();
goalsRecieved += team.getAwayRecieved();
}
points += (3 * wins) + ties;
goalDiff = goalsScored - goalsRecieved;
points = (3 * wins) + ties;
}

void setStanding(int standing) {
this.standing = standing;
}

}

0 comments on commit f808595

Please sign in to comment.