//yuec2 yue cheng
packageexam1;//import java.io.File;
importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.util.Scanner;public classScoreBoard {
String[] players;int[][] playerScores;
Scanner input= newScanner(System.in);//do not change this method
public static voidmain(String[] args) {
ScoreBoard scoreBoard= newScoreBoard();
scoreBoard.readScores("GameScores.txt");
scoreBoard.showScores(scoreBoard.getMenuChoice());
}//do not change this method
intgetMenuChoice() {
System.out.println("*** Score Board ***");
System.out.println("1. List all the scores of a player");
System.out.println("2. Find a player's average score");
System.out.println("3. Find all highest scores");
System.out.println("4. Exit");int choice =input.nextInt();
input.nextLine();returnchoice;
}voidreadScores(String filename) {//write your code here
try{int counts=0;
Scanner sc= new Scanner((new FileReader("GameScores.txt")));while(sc.hasNextLine()) {
sc.nextLine();
counts++;}
players= newString[counts];
playerScores= new int[counts][];
sc= new Scanner((new FileReader("GameScores.txt")));for(int i=0; i< counts; i++) {
String thisline=sc.nextLine();
players[i]= thisline.split(":")[0].trim();
String[] temp= thisline.split(":")[1].trim().split(",");int size =temp.length;
playerScores[i]= new int[size];for (int j = 0; j < size; j++) {
playerScores[i][j]=Integer.valueOf(temp[j].trim());
}
}
}catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}void showScores(intchoice) {//write your code here
if (choice == 1) {
String name=input.nextLine();
findPlayerScores(name);
}else if (choice == 2) {
String name=input.nextLine();
findPlayerAverageScore(name);
}else if (choice == 3) {
findAllHighestScores();
}else if (choice == 4) {
System.out.println("Good bye!");
}return;
}int[] findPlayerScores(String name) {//write your code here
int index = -1;for (int i = 0; i < players.length; i++) {//System.out.println("players[i] = " + players[i]);
if (players[i].toLowerCase().equals(name.toLowerCase())) index =i;
}//corner case
if (index == -1) return null;//System.out.println("index = " + index);//System.out.println(players[index] +"'s scores\n");
for (int j = 0; j < playerScores[index].length; j++) {
System.out.println(playerScores[index][j]);
}returnplayerScores[index];
}floatfindPlayerAverageScore(String name) {//write your code here
int[] scores =findPlayerScores(name);float sum = 0;for(int i = 0; i < scores.length; i++) {
sum+=scores[i];
}
System.out.printf("%s's average score is %.2f", name, sum /scores.length);return sum /scores.length;
}int[] findAllHighestScores() {//write your code here
int[] highestScores = new int[players.length];int max =Integer.MIN_VALUE;for (int i = 0; i < players.length; i++) {int size =playerScores[i].length;for (int j = 0; j < size; j++) {
max=Math.max(max, playerScores[i][j]);//System.out.println("max = " + max);
}
highestScores[i]=max;
max=Integer.MIN_VALUE;
System.out.println(highestScores[i]);
}returnhighestScores;//return null;
}
}