Add comments to GolfScore.java
This commit is contained in:
parent
5a270cf719
commit
8ce87145b8
Binary file not shown.
|
@ -64,17 +64,17 @@ public class GolfScore {
|
||||||
* @return all golfer's names in array of String
|
* @return all golfer's names in array of String
|
||||||
*/
|
*/
|
||||||
public static String[] getGolfers(String csvFilename) {
|
public static String[] getGolfers(String csvFilename) {
|
||||||
String[][] data;
|
|
||||||
try {
|
try {
|
||||||
data = CSVUtility.readCSV(csvFilename, true);
|
String[][] data = CSVUtility.readCSV(csvFilename, true); //Read csv to string data
|
||||||
String[] golfers = new String[data.length];
|
String[] golfers = new String[data.length]; // Make 1D array to store all golfer's name
|
||||||
for (int i = 0; i < data.length; i++)
|
for (int i = 0; i < data.length; i++) //Loop through each row
|
||||||
golfers[i] = data[i][0];
|
golfers[i] = data[i][0]; // First Column is the golfer's name, put it in golfers array
|
||||||
return golfers;
|
return golfers; //send the data back
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return new String[0];
|
return new String[0]; //If all else fail, return an empty string array
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -106,11 +106,11 @@ public class GolfScore {
|
||||||
*/
|
*/
|
||||||
public static String getCourseName(String csvFilename) {
|
public static String getCourseName(String csvFilename) {
|
||||||
try {
|
try {
|
||||||
return CSVUtility.readFile(csvFilename)[0];
|
return CSVUtility.readFile(csvFilename)[0]; //Return the first line of the CSV file
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null; //If all else fail, return null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -122,22 +122,22 @@ public class GolfScore {
|
||||||
*/
|
*/
|
||||||
public static int[] getCoursePar(String csvFilename, String courseName) {
|
public static int[] getCoursePar(String csvFilename, String courseName) {
|
||||||
// ENTER CODE HERE
|
// ENTER CODE HERE
|
||||||
String[][] data;
|
|
||||||
try {
|
try {
|
||||||
data = CSVUtility.readCSV(csvFilename, false);
|
String[][] data = CSVUtility.readCSV(csvFilename, false); // Read file and store it in data
|
||||||
|
for (int i = 0; i < data.length; i++) { //loop through
|
||||||
|
if (courseName.equals(data[i][0])) { //If the course name of the the current row is equal to courseName, read the current line
|
||||||
|
int[] pars = new int[data[i].length - 1]; //Create an array to store current course's par data
|
||||||
|
for (int j = 1; j < data[i].length; j++) //Loop through each par, start from 1 to exclude the course name
|
||||||
|
pars[j - 1] = Integer.parseInt(data[i][j]); //Put the par no. n at index n-2
|
||||||
|
return pars; //return the par for course, this end also exit the loop
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return new int[0];
|
return new int[0];
|
||||||
}
|
}
|
||||||
for (int i = 0; i < data.length; i++) {
|
|
||||||
if (courseName.equals(data[i][0])) {
|
return new int[0]; //If all else fail (File not found, etc) return an empty int array
|
||||||
int[] pars = new int[data[i].length - 1];
|
|
||||||
for (int j = 1; j < data[i].length; j++)
|
|
||||||
pars[j - 1] = Integer.parseInt(data[i][j]);
|
|
||||||
return pars;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new int[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -149,11 +149,11 @@ public class GolfScore {
|
||||||
*/
|
*/
|
||||||
public static int[] calculateHandicap(int[][] scores, int[] par) {
|
public static int[] calculateHandicap(int[][] scores, int[] par) {
|
||||||
// ENTER CODE HERE
|
// ENTER CODE HERE
|
||||||
int hc[] = new int[scores.length];
|
int hc[] = new int[scores.length]; //create a handicap array of nGolfers elements
|
||||||
for (int i = 0; i < scores.length; i++)
|
for (int i = 0; i < scores.length; i++) //Loop through each row
|
||||||
for (int j = 0; j < scores[i].length; j++)
|
for (int j = 0; j < scores[i].length; j++) //Loop through each hole
|
||||||
hc[i] += calculateHoleHandicap(par[j], scores[i][j]);
|
hc[i] += calculateHoleHandicap(par[j], scores[i][j]); //no. golfer = row = i+1; no. hole = column = j+1
|
||||||
return hc;
|
return hc; //Return the Array
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,9 +168,10 @@ public class GolfScore {
|
||||||
*/
|
*/
|
||||||
public static int calculateHoleHandicap(int par, int score) {
|
public static int calculateHoleHandicap(int par, int score) {
|
||||||
// ENTER CODE HERE
|
// ENTER CODE HERE
|
||||||
|
//Use conditional if-else chain to calculate par
|
||||||
if (score - par == 1)
|
if (score - par == 1)
|
||||||
return 1;
|
return 1;
|
||||||
if (score - par >= 2)
|
else if (score - par >= 2)
|
||||||
return 2;
|
return 2;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -186,11 +187,11 @@ public class GolfScore {
|
||||||
// ENTER CODE HERE
|
// ENTER CODE HERE
|
||||||
int[][] playerScores = new int[golfScoreList.length][3];
|
int[][] playerScores = new int[golfScoreList.length][3];
|
||||||
for (int i = 0; i < playerScores.length; i++) {
|
for (int i = 0; i < playerScores.length; i++) {
|
||||||
for (int j = 0; j < 9; j++)
|
for (int j = 0; j < 9; j++) //Sum first 9 holes
|
||||||
playerScores[i][0] += golfScoreList[i][j];
|
playerScores[i][0] += golfScoreList[i][j];
|
||||||
for (int j = 0; j < 9; j++)
|
for (int j = 0; j < 9; j++) //Sum last 9 holes
|
||||||
playerScores[i][1] += golfScoreList[i][j + 9];
|
playerScores[i][1] += golfScoreList[i][j + 9];
|
||||||
playerScores[i][2] = playerScores[i][0] + playerScores[i][1];
|
playerScores[i][2] = playerScores[i][0] + playerScores[i][1]; //all hole = index 0+1
|
||||||
}
|
}
|
||||||
return playerScores;
|
return playerScores;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue