Upload the completed verion of golfscore
This commit is contained in:
parent
421759516a
commit
954ec312e8
|
@ -1,10 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-14">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="lib" path="class"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,4 @@
|
|||
course-01,5,4,4,3,4,4,5,3,4,5,4,3,4,4,4,3,4,5
|
||||
course-02,4,4,3,4,5,3,5,4,4,4,5,3,5,4,4,3,4,4
|
||||
course-03,4,3,4,4,4,5,3,5,4,5,4,3,4,4,4,3,4,5
|
||||
course-04,4,3,4,5,4,5,3,4,4,4,5,4,3,4,5,3,4,4
|
|
|
@ -9,6 +9,7 @@ public class CSVUtility {
|
|||
|
||||
/**
|
||||
* reads all lines in given filename into an array of String
|
||||
*
|
||||
* @param filename
|
||||
* @return array of String, each element in array is each line in file
|
||||
* @throws FileNotFoundException
|
||||
|
@ -17,26 +18,36 @@ public class CSVUtility {
|
|||
// ENTER CODE HERE
|
||||
Scanner sc = new Scanner(new File(filename));
|
||||
int length = 0;
|
||||
for(length = 0;sc.hasNext();length++)sc.nextLine();
|
||||
for (length = 0; sc.hasNext(); length++)
|
||||
sc.nextLine();
|
||||
String[] filedata = new String[length];
|
||||
sc = new Scanner(new File(filename));
|
||||
sc = new Scanner(new File(filename)).useDelimiter(",");
|
||||
for(int i = 0;i<=length;i++)filedata[i]=sc.next();
|
||||
for (int i = 0; i < length; i++)
|
||||
filedata[i] = sc.nextLine();
|
||||
return filedata;
|
||||
}
|
||||
|
||||
/**
|
||||
* read given CSV file into 2D array of String
|
||||
*
|
||||
* @param filename CSV file
|
||||
* @param header true if CSV file has a header line. The first line is header. The data start from second line.
|
||||
* @param header true if CSV file has a header line. The first line is header.
|
||||
* The data start from second line.
|
||||
* @return
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public static String[][] readCSV(String filename, boolean header) throws FileNotFoundException {
|
||||
// ENTER CODE HERE
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
String[] data = readFile(filename);
|
||||
int lineOffset = header ? 1 : 0;
|
||||
int maxElement = 0;
|
||||
for (int i = lineOffset; i < data.length; i++)
|
||||
maxElement = Math.max(data[i].split(",").length, maxElement);
|
||||
String[][] csv = new String[data.length - lineOffset][maxElement];
|
||||
for (int i = lineOffset; i < data.length; i++)
|
||||
csv[i - lineOffset] = data[i].split(",");
|
||||
return csv;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -1,51 +0,0 @@
|
|||
//
|
||||
// Source code recreated from a .class file by SNLinter
|
||||
// (powered by FernFlower decompiler)
|
||||
//
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class CSVUtilityGiven {
|
||||
public CSVUtilityGiven() {
|
||||
}
|
||||
|
||||
public static String[] readFile(String var0) throws FileNotFoundException {
|
||||
Scanner var1 = new Scanner(new File(var0));
|
||||
ArrayList var2 = new ArrayList();
|
||||
|
||||
while(var1.hasNext()) {
|
||||
var2.add(var1.nextLine());
|
||||
}
|
||||
|
||||
var1.close();
|
||||
String[] var3 = new String[var2.size()];
|
||||
|
||||
for(int var4 = 0; var4 < var3.length; ++var4) {
|
||||
var3[var4] = (String)var2.get(var4);
|
||||
}
|
||||
|
||||
return var3;
|
||||
}
|
||||
|
||||
public static String[][] readCSV(String var0, boolean var1) throws FileNotFoundException {
|
||||
String[] var2 = readFile(var0);
|
||||
int var3 = var1 ? 1 : 0;
|
||||
String[][] var4 = new String[var2.length - var3][];
|
||||
int var5 = 0;
|
||||
|
||||
for(int var6 = var3; var5 < var4.length; ++var6) {
|
||||
var4[var5] = var2[var6].split(",");
|
||||
++var5;
|
||||
}
|
||||
|
||||
return var4;
|
||||
}
|
||||
|
||||
public static void main(String[] var0) throws FileNotFoundException {
|
||||
ArrayUtility.show(readCSV("course-db.csv", false));
|
||||
ArrayUtility.show(readCSV("golf-score.csv", false));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,270 @@
|
|||
|
||||
//package solution;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class GolfScore {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// uncomment each testXXX() method to test that the corresponding method works
|
||||
// properly
|
||||
// when finish uncomment run()
|
||||
|
||||
// testGetGolfers();
|
||||
|
||||
// testGetScores();
|
||||
|
||||
// testGetCourseName();
|
||||
|
||||
// testGetCoursePar();
|
||||
|
||||
// testCalculateScore();
|
||||
|
||||
// testCalculateHandicap();
|
||||
|
||||
String filename = args.length == 0 ? "golf-score-1.csv" : args[0];
|
||||
run(filename);
|
||||
}
|
||||
|
||||
public static void run(String scoreFile) {
|
||||
// 1. get golfer's names from golf-score-1.csv
|
||||
String[] golfers = getGolfers(scoreFile);
|
||||
|
||||
// 2. get golfer's scores from golf-score-1.csv
|
||||
int[][] golfScoreList = getScores(scoreFile);
|
||||
|
||||
// 3. get course name from golf-score-1.csv
|
||||
String courseName = getCourseName(scoreFile);
|
||||
|
||||
// 4. get course par from course-db.csv
|
||||
int[] coursePar = getCoursePar("course-db.csv", courseName);
|
||||
|
||||
// 5. calculate out, in, and total for each golfer
|
||||
int[][] scores = calculateScore(golfScoreList);
|
||||
|
||||
// 6. calculate handicap for each golfer
|
||||
int[] hc = calculateHandicap(golfScoreList, coursePar);
|
||||
|
||||
// 7. print the result: golfer's name, out, in, total, hc
|
||||
printResult(golfers, scores, hc);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* get golfers name from given CSV file using provided CSVUtility class golfer's
|
||||
* name are the first elements in each sub array
|
||||
*
|
||||
* Each line of CSV file is in format: golfer's
|
||||
* name,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18
|
||||
*
|
||||
* @param csvFilename csv file that contain golfers and their scores
|
||||
* @return all golfer's names in array of String
|
||||
*/
|
||||
public static String[] getGolfers(String csvFilename) {
|
||||
String[][] data;
|
||||
try {
|
||||
data = CSVUtility.readCSV(csvFilename, true);
|
||||
String[] golfers = new String[data.length];
|
||||
for (int i = 0; i < data.length; i++)
|
||||
golfers[i] = data[i][0];
|
||||
return golfers;
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* get all score from CSV file
|
||||
*
|
||||
* @param csvFilename csv file that contains golfers and their scores
|
||||
* @return 2D array of int that each element contains scores of each golfer
|
||||
*/
|
||||
public static int[][] getScores(String csvFilename) {
|
||||
// ENTER CODE HERE
|
||||
try {
|
||||
String[][] data = CSVUtility.readCSV(csvFilename, true);
|
||||
int[][] score = new int[data.length][data[0].length - 1];
|
||||
for (int i = 0; i < data.length; i++)
|
||||
for (int j = 1; j < data[i].length; j++)
|
||||
score[i][j - 1] = Integer.parseInt(data[i][j]);
|
||||
return score;
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new int[0][0];
|
||||
}
|
||||
|
||||
/**
|
||||
* gets course name for csv file. Course name is the first line.
|
||||
*
|
||||
* @param csvFilename
|
||||
* @return
|
||||
*/
|
||||
public static String getCourseName(String csvFilename) {
|
||||
try {
|
||||
return CSVUtility.readFile(csvFilename)[0];
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets course 18-hole par for given course name extracted from csv file
|
||||
*
|
||||
* @param csvFilename
|
||||
* @param courseName
|
||||
* @return
|
||||
*/
|
||||
public static int[] getCoursePar(String csvFilename, String courseName) {
|
||||
// ENTER CODE HERE
|
||||
String[][] data;
|
||||
try {
|
||||
data = CSVUtility.readCSV(csvFilename, false);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return new int[0];
|
||||
}
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
if (courseName.equals(data[i][0])) {
|
||||
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];
|
||||
}
|
||||
|
||||
/**
|
||||
* calculate handicap using System-36
|
||||
*
|
||||
* @param scores
|
||||
* @param par
|
||||
* @return handicap of all golfers
|
||||
*/
|
||||
public static int[] calculateHandicap(int[][] scores, int[] par) {
|
||||
// ENTER CODE HERE
|
||||
int hc[] = new int[scores.length];
|
||||
for (int i = 0; i < scores.length; i++)
|
||||
for (int j = 0; j < scores[i].length; j++)
|
||||
hc[i] += calculateHoleHandicap(par[j], scores[i][j]);
|
||||
return hc;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* calculate handicap for a hole from par and player's score
|
||||
*
|
||||
* @param par hole's par number (3, 4, or 5)
|
||||
* @param score player's score
|
||||
*
|
||||
* @return calculated handicap, 0 if score - par is 0, 1 if score - par is equal
|
||||
* to 1 and 2 otherwise
|
||||
*/
|
||||
public static int calculateHoleHandicap(int par, int score) {
|
||||
// ENTER CODE HERE
|
||||
if (score - par == 1)
|
||||
return 1;
|
||||
if (score - par >= 2)
|
||||
return 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* calculates: sum(hole1 to hole9) -> out sum(hole10 to hole18) -> in out + in
|
||||
* -> total
|
||||
*
|
||||
* @param golfScoreList
|
||||
* @return out, in, total for all players
|
||||
*/
|
||||
public static int[][] calculateScore(int[][] golfScoreList) {
|
||||
// ENTER CODE HERE
|
||||
int[][] playerScores = new int[golfScoreList.length][3];
|
||||
for (int i = 0; i < playerScores.length; i++) {
|
||||
for (int j = 0; j < 9; j++)
|
||||
playerScores[i][0] += golfScoreList[i][j];
|
||||
for (int j = 0; j < 9; j++)
|
||||
playerScores[i][1] += golfScoreList[i][j + 9];
|
||||
playerScores[i][2] = playerScores[i][0] + playerScores[i][1];
|
||||
}
|
||||
return playerScores;
|
||||
}
|
||||
|
||||
public static void printResult(String[] golfers, int[][] scores, int[] hc) {
|
||||
System.out.println(" Name:\tOUT\tIN\tTOTAL\tHC");
|
||||
System.out.println("------------------------------------------");
|
||||
for (int i = 0; i < golfers.length; i++) {
|
||||
int out = scores[i][0];
|
||||
int in = scores[i][1];
|
||||
int total = scores[i][2];
|
||||
System.out.format("%s:\t%d\t%d\t%d\t%d\n", golfers[i], out, in, total, hc[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// test methods
|
||||
|
||||
private static void testGetGolfers() {
|
||||
String[] golfers = getGolfers("golf-score-1.csv");
|
||||
System.out.println("Expected:");
|
||||
System.out.println("[Player01, Player02, Player03, Player04, Player05, "
|
||||
+ "Player06, Player07, Player08, Player09, Player10]");
|
||||
ArrayUtility.show(golfers);
|
||||
}
|
||||
|
||||
private static void testGetScores() {
|
||||
int[][] scores = getScores("golf-score-1.csv");
|
||||
int[][] expected = { { 4, 4, 6, 3, 4, 5, 4, 6, 6, 6, 3, 5, 4, 6, 4, 4, 3, 6 },
|
||||
{ 4, 5, 6, 4, 5, 4, 5, 4, 8, 9, 4, 5, 5, 6, 5, 4, 4, 5 },
|
||||
{ 4, 5, 5, 4, 6, 5, 5, 5, 7, 6, 3, 6, 4, 6, 5, 5, 4, 5 },
|
||||
{ 5, 6, 4, 3, 5, 4, 4, 4, 6, 5, 2, 7, 4, 7, 5, 7, 4, 6 },
|
||||
{ 4, 3, 4, 4, 4, 5, 4, 4, 5, 5, 4, 5, 5, 5, 5, 4, 5, 4 },
|
||||
{ 3, 4, 4, 3, 3, 4, 4, 4, 5, 5, 4, 5, 3, 5, 3, 4, 3, 6 },
|
||||
{ 6, 4, 6, 4, 5, 5, 5, 3, 6, 4, 4, 5, 4, 5, 5, 5, 3, 4 },
|
||||
{ 4, 6, 5, 3, 4, 6, 4, 3, 4, 4, 2, 5, 4, 5, 4, 4, 3, 4 },
|
||||
{ 5, 5, 8, 4, 5, 4, 4, 3, 5, 5, 4, 5, 3, 6, 6, 5, 4, 5 },
|
||||
{ 5, 4, 5, 3, 4, 4, 4, 2, 5, 5, 3, 6, 4, 5, 4, 6, 5, 5 } };
|
||||
ArrayUtility.show(scores);
|
||||
System.out.println("Comparing with Expected:");
|
||||
for (int i = 0; i < expected.length; i++) {
|
||||
System.out.print(Arrays.compare(expected[i], scores[i]) + " ");
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println("0 0 0 0 0 0 0 0 0 0 - expected compare result");
|
||||
}
|
||||
|
||||
private static void testGetCourseName() {
|
||||
String courseName = getCourseName("golf-score-1.csv");
|
||||
System.out.println("Expected: course-02");
|
||||
System.out.println(courseName);
|
||||
}
|
||||
|
||||
private static void testGetCoursePar() {
|
||||
int[] coursePar = getCoursePar("course-db.csv", "course-02");
|
||||
System.out.println("[4, 4, 3, 4, 5, 3, 5, 4, 4, 4, 5, 3, 5, 4, 4, 3, 4, 4] - expected");
|
||||
ArrayUtility.show(coursePar);
|
||||
}
|
||||
|
||||
private static void testCalculateScore() {
|
||||
int[][] golfScoreList = getScores("golf-score-1.csv");
|
||||
int[][] scores = calculateScore(golfScoreList);
|
||||
int[][] expected = { { 42, 41, 83 }, { 45, 47, 92 }, { 46, 44, 90 }, { 41, 47, 88 }, { 37, 42, 79 },
|
||||
{ 34, 38, 72 }, { 44, 39, 83 }, { 39, 35, 74 }, { 43, 43, 86 }, { 36, 43, 79 } };
|
||||
ArrayUtility.show(expected);
|
||||
System.out.println("---");
|
||||
ArrayUtility.show(scores);
|
||||
}
|
||||
|
||||
private static void testCalculateHandicap() {
|
||||
int[][] scores = { { 4, 4, 6, 3, 4, 5, 4, 6, 6, 6, 3, 5, 4, 6, 4, 4, 3, 6 },
|
||||
{ 4, 5, 6, 4, 5, 4, 5, 4, 8, 9, 4, 5, 5, 6, 5, 4, 4, 5 },
|
||||
{ 4, 5, 5, 4, 6, 5, 5, 5, 7, 6, 3, 6, 4, 6, 5, 5, 4, 5 } };
|
||||
int[] par = { 4, 4, 3, 4, 5, 3, 5, 4, 4, 4, 5, 3, 5, 4, 4, 3, 4, 4 };
|
||||
System.out.println("[17, 15, 19] - expected");
|
||||
ArrayUtility.show(calculateHandicap(scores, par));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue