import java.util.Scanner;
 
/* 
 * Program: MedalDriver - program that computes the winner of the Olympic
 *                        games, based upon two different scoring methods
 * Author:  William Kreahling
 * Date:    Feb 2006
 *
 * NOTE:    This version of the solution makes use of the the
 *          Scanner class
 */

class MedalDriver
{
   public static void main(String[] args)
   {
      String method;             // scoring method
      int gold, silver, bronze;  // medals

      Scanner scanIn = new Scanner(System.in);

      System.out.println("Olympic Medal Scorer\n");

      // Loop until a valid scoring method is input
      do
      {
         System.out.println("Please enter the scoring method."); 
         System.out.print("(American or Canadian) >");
         method = scanIn.next();
         method = method.toLowerCase();
      } while (!method.equals("american") && !method.equals("canadian"));

      // get number of counteries and create an array
      System.out.print("Number of countries competing? > ");
      int num = scanIn.nextInt();

      if (num <= 0)
      {
         System.out.println("Cannot compute score for " + num + " countries");
         System.exit(1);
      }

      Country contestant[] = new Country[num];

      // get names and medal counts from user
      for (int i = 0; i < num; i++)
      {
         contestant[i] = new Country();
         System.out.print("Please enter country and medal count >");
         contestant[i].setCountry(scanIn.next());
         for (int j = 0; j < i; j++)
         {
            if (contestant[j].equals(contestant[i]))
            {
               System.out.println("Error: duplicate country name");
               System.exit(1); 
            }
         }

         gold = scanIn.nextInt();
         silver = scanIn.nextInt();
         bronze = scanIn.nextInt();

         contestant[i].setMedals(gold,silver,bronze);
      }
      int maxIndex = 0;                               // winning index
      int score = contestant[0].computeScore(method); // high score
      boolean tie = false;                            // tie occured?

      for (int i = 1; i < num; i++)
      {
         // check for new high scores
         if (contestant[i].computeScore(method) > score)
         {
            maxIndex = i;   
            score = contestant[i].computeScore(method);
            tie = false;
         }
         // check for ties
         else if (contestant[i].computeScore(method) == score)
            tie = true;
      }

      // print winner(s)
      if (! tie)
         System.out.println("The winner is: " + contestant[maxIndex]);
      else
      {
         System.out.println("The winners are:");
         for (int i = 0; i < num; i++)
         {
            if (contestant[i].computeScore(method) == score)
               System.out.println("\t" + contestant[i]);
         }

      }
   }
}

// class that will represent counteries in the Olympics
class Country
{
   
   private String country;    // Country name
   private int gold;          // medals won
   private int silver;
   private int bronze;

   // default contructor
   public Country()
   {
      this.gold = this.silver = this.bronze = 0;
   }

   // Setter method for county
   public void setCountry(String country)
   {
      this.country = country;
   }

   // Setter method for medals
   public void setMedals(int gold, int silver, int bronze)
   {
      this.gold = gold;
      this.silver = silver;
      this.bronze = bronze;
   }

   /*
    * computeScore - determines a numerical value for this country's score
    *
    * Parameters: type -- scoring method
    * Returns:    integer representing the total score
    */
   public int computeScore(String type)
   {
      if (type.equals("american"))
         return this.gold * 100 + this.silver * 10 + this.bronze;
      else if (type.equals("canadian"))
         return this.gold + this.silver + this.bronze;
      else
      {
         System.out.println("Undefined scoring method");
         System.exit(1);
      }
      return 0;
   }

   public boolean equals(Country anotherCountry)
   {
      return this.country.equals(anotherCountry.country);
   }

   // Prints state of this object
   public String toString()
   {
      return this.country + " with " + this.gold + " gold medals, " +
             this.silver + " silver medals, " + this.bronze +
             " bronze medals";
   }

}
