import java.util.*;
import java.io.*;
public class Player
{
Scanner read;
PrintWriter write;
private String playerid;
private int playerscore;
int x = 0;
ArrayList<String> n = new ArrayList<String>();
ArrayList<Integer> s = new ArrayList<Integer>();
List<Player> players = new ArrayList <Player>();
public Player (String name, int score)
{
playerid = name;
playerscore = score;
}
static class ScoreComparator implements Comparator <Player>
{
public int compare(Player lf1, Player lf2)
{
int scoreA = lf1.getScore ();
int scoreB = lf2.getScore ();
if (scoreA == scoreB)
{
return 0;
}
else if (scoreA > scoreB)
{
return 1;
}
else
{
return -1;
}
}
}
public String getName()
{
return playerid;
}
public int getScore()
{
return playerscore;
}
public String toString ()
{
return playerid+"\t "+playerscore;
}
public void loadScores()
{
try
{
read = new Scanner (new FileReader ("lbout.txt"));
while (read.hasNext ())
{
n.add (read.next ());
s.add(read.nextInt ());
}
System.out.println(n);
System.out.println(s);
}
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe+": FILE NOT FOUND!");
}
catch (InputMismatchException ime)
{
System.out.println(ime+": INVALID DATA!");
}
catch (Exception e)
{
System.out.println(e);
}
}
public void evaluateScores()
{
try
{
loadScores ();
n.add (playerid);
s.add(playerscore);
// sortScores();
int u;
int max = 5;
for (u = 0; u < n.size (); ++u)
{
players.add(new Player (n.get (u),s.get (u)));
}
Collections.sort (players, Collections.reverseOrder (new Player.ScoreComparator()));
for (int j = 0; j < max; ++j)
{
System.out.println("\t "+players.get(j));
}
ArrayList <Player> arr = new ArrayList<Player>();
for (int y = 0; y < players.size (); ++y)
{
arr.add(players.get(y));
//I tried to pass the players as an argument to the GUI class but I get an error saying there's a conflict between util and awt so I copied it to an ArrayList
}
System.out.println("Print arr:" +arr);
write = new PrintWriter ("lbout.txt");
for (int z = 0; z < n.size (); ++z)
{
write.print(n.get(z)+" ");
write.println (s.get(z));
}
write.close ();
//displayScores();
}
catch (InputMismatchException ime)
{
System.out.println(ime+": INVALID DATA!");
}
catch (Exception e)
{
System.out.println(e);
}
}
}
As you can see, my code (aside from the fact that it isn't highly efficient but I'm not worrying about that at the moment, newbie here) only takes two parameters: one for the winner's name, the other the winner's score. What I'm trying to do is add not only the winner's name and score, but also the loser's, or in other words both of the players' info, but I'm not sure how I will proceed.
So far:
public class PlayerTry {
private String playeridA;
private String playeridB;
private int playerscoreA;
private int playerscoreB;
public PlayerTry (String name1, String name2, int score1, int score2)
{
playeridA = name1;
playeridB = name2;
playerscoreA = score1;
playerscoreB = score2;
}
//Actually PlayerTry is a separate class and it's some sort of my draft space in case I mess up so at least I don't mess up my original code as well.
}
Then they will be added to the n
and s
list with the other loaded info from the txt file.
I'm not sure about the getName()
, getScore()
. Should I create separate getName()
and getScore()
methods for each of the passed players' info?
Hints/suggestions would be appreciated!
EDIT: My program no longer implements the Comparable interface by the way, since I would only be needing for the players' scores to be sorted out, order of names are disregarded.