/**
 * Repesent a high score, such as in an arcade game.
 * Includes the player's name and their score (which must be
 * positive).  Once a HighScore object is created, the player's
 * name cannot be changed.
 *
 * @author Zach Tomaszewski
 */
public class HighScore implements Comparable<HighScore> {

  private String name;
  private int score;


  /**
   * Constructs a HighScore with the given name and score.  If the given
   * score is < 0, it will be treated as 0.
   */
  public HighScore(String name, int score) {
    this.name = name;
    this.setScore(score);
  }

  /**
   * Constructs a new HighScore with the given name and a score of 0.
   */
  public HighScore(String n) {
    this(n, 0);
  }

  public String getName() {
    return this.name;
  }

  public int getScore() {
    return this.score;
  }

  /**
   * Changes this HighScore's score to the given value.  If this value is
   * less than 0, it is treated as 0.
   */
  public void setScore(int s) {
    if (s < 0) {
      this.score = 0;
    }else {
      this.score = s;
    }
  }

  /**
   * Orders two HighScore objects based on their score, with the higher
   * score coming first.  That is, the natural order of HighScores is
   * from highest-to-lowest score.
   *
   * The name is ignored in the sorting.
   */
  public int compareTo(HighScore obj) {
    //
    // Normally, left.compareTo(right) should return:
    //   < 0 (negative) if left is less (comes before) right
    //   > 0 (positive) if left is greater (comes after) right
    //  == 0 (zero) if left equals right
    //
    // However, since HighScores are to be sorted highest-to-lowest,
    // these rules are reversed here.
    //
    if (this.score < obj.score) {
      return 1;
    }else if (this.score > obj.score) {
      return -1;
    }else {
      return 0;
    }

    //Shortcut:
    //I also could have implemented this method in one line:
    //  return obj.score - this.score;
  }


  /**
   * Returns this HighScore's name and score separated by a tab.
   */
  public String toString() {
    return this.name + "\t" + this.score;
  }

}
