import java.util.ArrayList;
import java.util.Scanner;

public class Bridge 
{
	
	private ArrayList<Player> players;
	
	public Bridge() 
	{
		players = new ArrayList<Player>();
	}
	
	public static void main(String[] args) 
	{
		Bridge bg = new Bridge();
		bg.run();
	}
	
	public void run() 
	{
		System.out.println("\n\n\n");
		String name = Prompt.getString("Text file: ");
		players.add(new Player(name));
		System.out.printf("\nCalculateAverageScore = %9.2f\n", 
							players.get(0).calculateAverageScore());
		System.out.println("\n\n\n");
	}
}

class Game 
{
	private String myPartner;
	private double myScore;
	
	public Game(String partner, double score) 
	{
		myPartner = partner;
		myScore = score;
	}
	
	public String getPartner() { return myPartner; }
	public double getScore() { return myScore; }
}

class Player 
{
	private String myName;
	private double myFinalScore;
	private ArrayList<Game> myGames;	// a list of Games for this
										// Player sorted in decreasing order
										// of score
	
	private boolean DEBUG = true;
										
	public Player(String name) 
	{
		myName = name;
		/* Greenstein code to read in games */
		myGames = new ArrayList<Game>();
		Scanner input = OpenFile.openToRead(name);
		if (DEBUG)
		{
			System.out.println("\nInput from file:");
		}
		while (input.hasNext()) 
		{
			String partner = input.next();
			partner += input.next();
			double score = input.nextDouble();
			if (DEBUG)
			{
				System.out.println("   partner = " + partner + "  score = " + score);
			}
			addInOrder(partner, score);
		}
		if (DEBUG) System.out.println("\nIn ArrayList:");
		for (int a = 0; a < myGames.size(); a++)
		{
			if (DEBUG) 
			{
				System.out.println("   partner = " + myGames.get(a).getPartner() +
								"  score = " + myGames.get(a).getScore());
			}
		}
		input.close();
	}
	
	/** Add partner and score into myGames list in descending order of the score.
	 *	@param name		partner name in the game to add
	 *	@param score	bridge score with partner in game to add
	 */
	private void addInOrder(String name, double score)
	{
		int i = 0;
		while(i < myGames.size() && score < myGames.get(i).getScore())
		{
			i++;
		}
		myGames.add(i, new Game(name, score));
	}
/*
	void addInOrder(String name, double score)       //    15/15  
	{
		Game x = new Game(name, score);
		int index = 0;
		for(int i = 0; i < myGames.size(); i++)
		{
			if(x.getScore() < myGames.get(i).getScore())
			{
				index = i + 1;
			}
		}
		myGames.add(index, x);
	}
*/
/*
	private void addInOrder(String name, double score)  //  15/15
	{
		for(int i = 0; i < myGames.size(); i++)
		{
			if(score > myGames.get(i).getScore())
			{
				myGames.add(i, new Game(name, score));
				return;
			}
		}
		myGames.add(new Game(name, score));
	}
*/
/*
	private void addInOrder(String name, double score)  //  15/15
	{
		for(int i = myGames.size() - 1; i >= 0; i--)
		{
			if(score < myGames.get(i).getScore())
			{
				myGames.add(i + 1, new Game(name, score));
				return;
			}
		}
		myGames.add(0, new Game(name, score));
	}
*/
/*
	private void addInOrder(String name, double score)  //  15/15
	{
		int position = myGames.size() - 1;
		while(position >= 0 && myGames.get(position).getScore() < score)
		{
			position--;
		}
		myGames.add(position + 1, new Game(name, score));
	}
*/

	/**
	 *	Calculates average score for versatility prize.
	 *	@return		average of top scores for games with different partner names
	 *	(Hint: Use the fact that the games are stored in descending score order.)
	 */

	public double calculateAverageScore()       //  15/15
	{
		double total = 0.0;
		ArrayList<String> names = new ArrayList<String>();
		for(int i = 0; i < myGames.size(); i++)
		{
			boolean present = false;
			for(int j = 0; j < names.size(); j++)
			{
				if(names.get(j).equals(myGames.get(i).getPartner()))
				{
					present = true;
				}
			}
			if(!present)
			{
				total += myGames.get(i).getScore();
				names.add(myGames.get(i).getPartner());
			}
		}
		return total/names.size();
	}

/*
	public double calculateAverageScore()       //  15/15   
	{
		String players = new String("");
		double total = 0;
		int numplayer = 0;
		for(int i = 0; i < myGames.size(); i++)
		{
			if(players.indexOf(myGames.get(i).getPartner()) == -1)
			{
				players += myGames.get(i).getPartner();
				total += myGames.get(i).getScore();
				numplayer++;
			}
		}
		return total / numplayer;
	}
*/
/*
	public double calculateAverageScore()       //  15/15
	{
		double sum = 0;
		ArrayList<String> partners = new ArrayList<String>();
		for(int i = 0; i < myGames.size(); i++)
		{
			Game game = myGames.get(i);
			if(partners.indexOf(game.getPartner()) == -1)
			{
				partners.add(game.getPartner());
				sum += game.getScore();
			}
		}
		return sum/partners.size();
	}
*/
/*
	public double calculateAverageScore()       //  15/15   
	{
		double count = 0, tot = 0;
		ArrayList<String> opp = new ArrayList<String>();
		opp.add(new String(" "));
		for(int i = 0; i < myGames.size(); i++)
		{
			for(int k = 0; k < opp.size(); k++)
			{
				if(myGames.get(i).getPartner().equals(opp.get(k)))
				{
					k = opp.size();
				}
				else if(k == opp.size() - 1)
				{
					tot += myGames.get(i).getScore();
					count++;
					opp.add(myGames.get(i).getPartner());
				}
			}
		}
		return tot / count;
	}
*/
/*
	public double calculateAverageScore()       //  15/15   
	{
		ArrayList<String> names = new ArrayList<String>();
		ArrayList<Double> total = new ArrayList<Double>();
		for(Game temp : myGames)
		{
			if(!names.contains(temp.getPartner()))
			{
				names.add(temp.getPartner());
				total.add(temp.getScore());
			}
		}
		double tot = 0;
		for(int i = 0; i < total.size(); i++)
		{
			tot += total.get(i);
		}
		System.out.println(tot);
		return tot / names.size();
	}
*/
/*
	public double calculateAverageScore()      //    15/15
	{
		double total = 0.0;
		int count = 0;
		ArrayList<Game> temp = new ArrayList<Game>();
		for(int i = 0; i < myGames.size(); i++)
		{
			temp.add(myGames.get(i));
		}
		while(temp.size() > 0)
		{
			total += temp.get(0).getScore();
			count++;
			String name = temp.get(0).getPartner();
			for(int i = 0; i < temp.size(); i++)
			{
				if(temp.get(i).getPartner().equals(name))
				{
					temp.remove(i);
					i--;
				}
			}
		}
		return total/count;
	}
*/
/*
	public double calculateAverageScore()        //      15/15
	{
		ArrayList<String> partners = new ArrayList<String>();
		double sum = 0.0;
		for (int a = 0; a < myGames.size(); a++) 
		{
			Game thisGame = myGames.get(a);
			if (! partners.contains(thisGame.getPartner())) 
			{
				partners.add(thisGame.getPartner());
				sum += thisGame.getScore();
			}
		}
		return sum / partners.size();
	}
*/
	
	/** Accessor methods */
	public String getName() { return myName; }
}

//  Save as b.txt
/*
Coppola George 61.28
Harmon Fiona 68.22
Smith Jacqueline 37.44
Smith Charlie 93.93
Harmon David 27.6
Coppola Irma 33.25
Aldo George 28.21
Coppola David 97.63
Smith David 91.45
Aldo Harriet 87.03
Aldo Jacqueline 83.68
Smith David 47.64
Smith Alan 20.05
Coppola David 59.98
Aldo Eve 35.52
Coppola Jacqueline 74.05
Smith Brian 35.01
Smith Harriet 87.58
Aldo Jacqueline 42.76
Coppola Eve 72.95
Smith George 5.32
Aldo Eve 72.27
Harmon Eve 66.62
Smith Irma 29.5
Aldo Charlie 46.57
Coppola Harriet 65.05
Coppola Karen 11.04
Smith Eve 93.81
Aldo Brian 7.01
Coppola Alan 11.81
Smith Irma 7.23
Aldo Eve 88.7
Harmon Jacqueline 55.51
Smith Irma 78.25
Aldo Karen 92.88
Harmon Brian 15.89
Smith Irma 52.35
Aldo David 95.88
Coppola George 37.37
Harmon Fiona 87.82
*/

Back to APCS Main Page