0

Here's the gist, essentially I have this method right with the exception of one conditional that I just can't seem to get. The condition is that if addGame() is called twice with the same two Strings it will not store the Game object in the games ArrayList since it will return false. I have already attempted to utilize the ArrayList contains() method to fix it, but the JUnit test I created fails each time. Here's the code for the method:

public class Conference {


    private ArrayList<Team> teams;
    private ArrayList<Player> players;
    private ArrayList<Game> games;
    public Conference(){
        teams = new ArrayList<Team>();
        players = new ArrayList<Player>();
        games = new ArrayList<Game>();
    }
public boolean addGame(String team1, String team2) {
      Game tempgame = new Game(team1, team2, 0, 0);
      Team first = new Team(team1, 0, 0, 0);
      Team second = new Team(team2, 0, 0, 0);
      if(!tempgame.getFirst().equals(tempgame.getSecond())){
          games.add(tempgame);
          first.addGamesPlayed();
          second.addGamesPlayed();
          teams.add(first);
          teams.add(second);
       return true;
      }
      return false;
}

The Game class is as follows:

package conference;
import java.util.ArrayList;

public class Game {
    private String firstTeam;
    private String secondTeam;
    private int firstTeamGoals;
    private int secondTeamGoals;
    private ArrayList<Team> team;
    public Game(String first, String second, int goals1, int goals2){
        this.firstTeam = first;
        this.secondTeam = second;
        this.firstTeamGoals = goals1;
        this.secondTeamGoals = goals2;
        team = new ArrayList<Team>();
    }
    public String getFirst(){
        return new String(firstTeam);
    }
    public String getSecond(){
        return new String(secondTeam);
    }
    public int getFirstTeamGoals(){
        return this.firstTeamGoals;
    }
    public int addFirstTeamGoals(){
        return firstTeamGoals++;
    }
    public int getSecondTeamGoals(){
        return this.secondTeamGoals;
    }
    public int addSecondTeamGoals(){
        return secondTeamGoals++;
    }
    public boolean hasMatchup(String t1, String t2){
        if(this.firstTeam.equals(t1) && this.secondTeam.equals(t2)){
            return true;
        }
        if(this.firstTeam.equals(t2) && this.secondTeam.equals(t1)){
            return true;
        }
        return false;
    }
}

And the Team class:

package conference;

public class Team {


    private String teamName;
    private int goalsScored;
    private int gamesPlayed;
    private int gamesWon;
    public Team(String name, int totalGoals, int games, int wins){
    this.teamName = name;
    this.goalsScored = totalGoals;
    this.gamesPlayed = games;
    this.gamesWon = wins;
    }

    public String getName(){
        return new String(teamName);
    }
    public int getTotalGoals(){
        return goalsScored;
    }
    public int addGoals(){
        return goalsScored++;
    }
    public int addGamesPlayed(){
        return this.gamesPlayed++;
    }
    public int getGamesPlayed(){
        return gamesPlayed;
    }
    public int addGamesWon(){
        return gamesWon++;
    }
    public int getGamesWon(){
        return gamesWon;
    }
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj == this) {
            return true;
        } else {
            if (!(obj instanceof Team)) {
                return false;
            } else {
                Team temp = (Team) obj;
                return this.teamName.equals(temp.getName());
            }
        }
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
yarcenahs
  • 17
  • 7
  • Is it that you cannot store the same two strings *ever* or is it that you cannot store the same two strings in a row? – synchronizer Feb 08 '17 at 03:16
  • Can't store the same two strings ever – yarcenahs Feb 08 '17 at 03:17
  • The strings are stored in a game object, that's why I can't have two of the same objects stored in the ArrayList ever – yarcenahs Feb 08 '17 at 03:19
  • Possible duplicate of [How do I remove repeated elements from ArrayList?](http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist) – yarcenahs Feb 12 '17 at 18:58

2 Answers2

0

Your method is likely failing because though the strings aren't equal, that doesn't mean that the teams or games list don't already have those strings. You need to loop over the games list and check if the name equals team1 or team2, if so, return false.

It's good that you compare the given teams aren't the same, and you can use your parameters to compare, but that's not the only condition you need.

if(!team1.equals(team2)) {

Also, the fact you have new String on values that are already strings makes a new object in memory, (therefore it's pointless) but the equals method should still work.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

"The strings are stored in a game object, that's why I can't have two of the same objects stored in the ArrayList ever" (Your comment.)

It seems that in exchange for more memory you could use a Set (HashSet or TreeSet) to store records of previous games played. If you're protecting against identical pairings rather than identical team names (at any point), sort the two String team names consistently and concatenate them into a key. Check whether that key already exists in the set.

EDIT: See the other solution for ArrayList only.

synchronizer
  • 1,955
  • 1
  • 14
  • 37
  • Ah, forgot one thing to mention, another constraint is I can only use ArrayList, Math and String classes. – yarcenahs Feb 08 '17 at 03:24
  • @yarcenahs *snap* ah well that's the way I would have done it. :) I guess this can be for personal "extra credit". – synchronizer Feb 08 '17 at 03:25