import java.io.*;
import java.util.StringTokenizer;

/* 
 * 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
 *          BufferedReader, not the Scanner class
 */
class MedalDriverB
{
   public static void main(String[] args) throws Exception
   {
      String method;             // Scoring method
      int gold, silver, bronze;  // Number of medals

      BufferedReader bufIn = new BufferedReader(new 
                                                InputStreamReader(System.in));

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

      // Get Scoring method, loop until they enter a valid method
      do
      {
         System.out.println("Please enter the scoring method."); 
         System.out.print("(American or Canadian) >");
         method = bufIn.readLine();
         method = method.toLowerCase();
      } while (!method.equals("american") && !method.equals("canadian"));

      // Get the number of counteries competing in the Olympics
      System.out.print("Number of countries competing? > ");
      String number = bufIn.readLine();
      int num = Integer.parseInt(number);

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

      // create an array of contestants based upon the number competing
      Country contestant[] = new Country[num];

      // For all the contestant, get country and medal counts
      for (int i = 0; i < num; i++)
      {
         contestant[i] = new Country();
         System.out.print("Please enter country and medal count >");

         // All data should be entered on one line, then use
         // the StringTokenizer to pull apart and convert tokens
         // put this handles the case where that does not happen
         String line = bufIn.readLine();
         StringTokenizer tokIn = new StringTokenizer(line);
         
         contestant[i].setCountry(tokIn.nextToken());
         for (int j = 0; j < i; j++)
            if (contestant[j].equals(contestant[i]))
            {
               System.out.println("Error: Duplicate country");
               System.exit(1);
            }
         
         if (!tokIn.hasMoreTokens())
         {
            line = bufIn.readLine();
            tokIn = new StringTokenizer(line);
         }
         gold = Integer.parseInt(tokIn.nextToken());
         if (!tokIn.hasMoreTokens())
         {
            line = bufIn.readLine();
            tokIn = new StringTokenizer(line);
         }
         silver = Integer.parseInt(tokIn.nextToken());
         if (!tokIn.hasMoreTokens())
         {
            line = bufIn.readLine();
            tokIn = new StringTokenizer(line);
         }
         bronze = Integer.parseInt(tokIn.nextToken());

         contestant[i].setMedals(gold,silver,bronze);
      }

      int maxIndex = 0;                               // winnning index
      int score = contestant[0].computeScore(method); // high score
      boolean tie = false;                            // tie occured

      // loop through all the contestants (minus the first)
      for (int i = 1; i < num; i++)
      {
         // check if someone has a new high score
         // save the new information, and reset tie flag, since a 'new'
         // high score means no tie
         if (contestant[i].computeScore(method) > score)
         {
            maxIndex = i;   
            score = contestant[i].computeScore(method);
            tie = false;
         }
         else if (contestant[i].computeScore(method) == score)
            tie = true;
      }

      // print out the winners or the tie
      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 represents 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 country name
   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 counteries score
    *
    * Parameters: type -- scoring method
    * Returns:    integer representing the score for this country
    */
   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 the state of this object
   public String toString()
   {
      return this.country + " with " + this.gold + " gold medals, " +
             this.silver + " silver medals, " + this.bronze +
             " bronze medals";
   }
}
