import java.io.*;
/**
* The HallOfFame class represents a hall of fame that
* saves and retrieves high scores for a game.  The HallOfFame
* has five entries, each containing a name and a score. 
* A person can be added if their score is
* within the range of high scores, or higher.  New scores are
* inserted into the array in the appropriate high-to-low
* position (dropping the lowest high score and name from the
* arrays if there are already 5 high scores in the hall of
* fame).  
*/
public class HallOfFame implements Serializable
{
    /**
    * An array of names corresponding 1-to-1 to an array of scores.
    */
    private String[] names;
    /**
    * An array of scores, ordered from highest to lowest, corresponding 1-to-1
    * to an array of names.
    */
    private int[] scores;


    /**
    * Creates a default HallOfFame object with 5 names "-" and score 0.
    */
    public HallOfFame()
    {
        names = new String[5];
        for (int i = 0; i < 5; i++)
            names[i] = "-";
        scores = new int[5];
        for (int i = 0; i < 5; i++)
            scores[i] = 0;
    }

    /**
    * Returns a score at a given position.
    * @param pos The array position of the requested score
    * @return The int scores
    */
    public int getScore(int pos)
    {
        return scores[pos];
    }

    /**
    * Returns a name at a given position.
    * @param pos The array position of the requested name
    * @return The String name.
    */
    public String getName(int pos)
    {
        return names[pos];
    }

    /**
    * Private Helper method to assign a score to a given position.
    * @param pos The array position to place the score
    * @param score The player's score
    */
    void setScore(int pos, int score)
    {
        scores[pos] = score;
    }



    /**
    * Private Helper method to assign a name to a given position.
    * @param pos The array position to place the name
    * @param name The player's name
    */
    void setName(int pos, String name)
    {
        names[pos] = name;
    }

  
    /**
    * Receives a score, and name, to add to the Hall of Fame arrays.
    * <br><b>Pre-condition:</b> hall of fame constructed
    * <br><b>Pre-condition:</b> isScoreWorthy (newScoreP) is TRUE
    * <br><b>Post-condition:</b> 
    *                  newScoreP, newNameP are inserted into the Hall of Fame.
    * <br><b>Post-condition:</b> 
    *                  Hall of Fame is sorted in descending order and has 5 items.
    * @param mewScoreP int score to add to the hall of fame 
    * @param newNameP  String player name to add to the hall of fame
    */
    public void addScore(int newScoreP, String newNameP)
    {
        int newScore = newScoreP;
        String newName = newNameP;
        boolean updated = false;
        String nameTemp;
        int scoreTemp;
        // Beginning at first index of score array
        // FOR index of score array = start to finish
        for (int i = 0; i < scores.length; i++)
        {
            if (!updated)
            {
                // IF current index of scores is empty
                if (scores[i] == 0)
                {
                    scores[i] = newScore;
                    names[i] = newName;
                    updated = true;
                } // ENDIF
                // ELSE IF newScore belongs at this spot
                else if (newScore > scores[i])
                {
                    //    THEN
                    //       FOR index of score array = index to finish
                    for (int j = i; j < scores.length; j++)
                    {   // swap the array items
                        scoreTemp = scores[j];
                        nameTemp = names[j];
                        scores[j] = newScore;
                        names[j] = newName;
                        newScore = scoreTemp;
                        newName = nameTemp;

                    } // ENDFOR

                    updated = true;

                }  // ENDIF
            }  //    ENDIF

        }  // ENDFOR

    }


    /**
    * Checks to see if user's score is higher than scores in array or if the
    * array is empty
    * @return True if the user's score is worthy of entering the Hall of Fame
    */
    public boolean isScoreWorthy(int currentScore)
    {
        // INITIALIZE flag as false
        boolean flag = false;

        // Examine all scores in the array 
        for (int i = 0; i < scores.length; i++)
        {
            // IF Score belongs in this spot 
            if (currentScore > scores[i] || scores[i] == 0)
            {
                return true;
            } // ENDIF
        } // ENDFOR

        return flag;
    }

    // *** saveToDisk and loadFromDisk methods omitted ***

    /**
    * Checks if the object being passed in is equal to this Hall of Fame object
    * <br><b>Pre-condition:</b> hall of fame constructed, ready for checking &
    * input.
    * <br><b>Post-condition:</b> none.
    * @param op2 The object being compared
    * @return a boolean value indiciating whether the object was equal to this Hall
    * of Fame object
    */
    public boolean equals (Object op2)
    {
        // IF op2 is an instance of HallOfFame
        if (op2 instanceof HallOfFame)
        {
            // Copy op2 into theHOF
            HallOfFame theHOF = (HallOfFame)op2;
            // FOR the beginning int 1 to the last index of scores
            for (int i = 0; i < scores.length; i++)
            {
                // IF the current index of scores is not equal to the current
                // index of theHOF
                if (this.scores[i] != theHOF.getScore(i))
                    // RETURN false
                    return false;
                // IF the current index of names is not equal to the current
                // index of theHOF
                if (this.names[i] != theHOF.getName(i))
                    // RETURN false
                    return false;
            } //ENDFOR
        } // ENDIF
        //RETURN true

        return true;
    }



}