Bighuyou
Bighuyou

Reputation: 83

How to determine an object belong to a specific class

Here is my class definitions of interface Icontestant:

   interface IContestant {  
}

class TennisPlayer implements IContestant {
  String name;
  int ranking;

  TennisPlayer (String name, Integer ranking){
    this.name = name;
    this.ranking = ranking;
  }
}

class NotYetKnown  implements IContestant {

  NotYetKnown (){
  }
}

Here is my definition of classes MatchData:

    class MatchData {
  IContestant side1;
  IContestant side2;
  IContestant winner;
  IScore score;


  MatchData (IContestant side1, IContestant side2, IContestant winner, IScore score){
    this.side1 = side1;
    this.side2 = side2;
    this.winner = winner;
    this.score = score;
  }      
}

Here is my definition of ITournament

interface ITournament {
  boolean winnerPlayed ();
}

class InitMatch implements ITournament {
  MatchData matchData;  

  InitMatch (MatchData matchData){
    this.matchData = matchData;
  }

 public boolean winnerPlayed (){
    if ((this.matchData.winner == this.matchData.side1)||(this.matchData.winner == this.matchData.side2)){
        return true;
      }
      else
        return false;
    }
    else 
      return true;
  }

}

Now I need to add a condition to winnerPlayed () to check if the winner is unknown first. If it is unknown, then we can just return true before checking if winner is either side1 or side2. However, I am having hard time to get this condition correct. I am so confused in this case what to use to compare with this.matchData.winner. I get it that this.matchData.side1 is a TennisPlayer, this.matchData.side2 is another TennisPlayer, so we are comparing if this.matchData.winner is a TennisPlayer of side1 or side2. But when it comes to unknown, I start to get confused. If you could explain data structure and objects here that would be great.

Thank you.

Also, the example of TennisPlayer and Unknown looks like this:

  TennisPlayer tennisPlayer1 = new TennisPlayer("Andre Agassi", 7);
  TennisPlayer tennisPlayer2 = new TennisPlayer ("Pat", 4);
  NotYetKnown unknown = new NotYetKnown();
  MatchData matchTennis2 = new MatchData (tennisPlayer1, tennisPlayer2, notYetKnown, tennisScore3 );
  InitMatch initTennis1 = new InitMatch (matchTennis1);

Upvotes: 0

Views: 65

Answers (2)

Joakim Danielson
Joakim Danielson

Reputation: 51971

A game has two players so winner must be either side1 or side2 if the game has been played or null if the game hasn’t been played yet. Any other option would mean that you have inconsistent data.

Edit

If for some reason winner can’t be null and is always set to a NotKnownYet object then could use instanceof to check for this

if winner instanceof NotYetKnown {...

Upvotes: 1

Sometimes it works
Sometimes it works

Reputation: 1

Maybe you should give blank string as a name for an unknown player. Then have a method to retrieve players names and if the name that is retrieve is an empty string you know the player is unknown. I know this might not be exactly the solution your probably looking for but it's probably easier way of going about it.

Upvotes: 0

Related Questions