diff --git a/.gitignore b/.gitignore index 2d7e4dc..be7d37d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -*.class +*.class +save.txt +data.txt diff --git a/HockeyPlayer.java b/HockeyPlayer.java index a844e84..07dedb2 100644 --- a/HockeyPlayer.java +++ b/HockeyPlayer.java @@ -41,22 +41,29 @@ public class HockeyPlayer { public int getGamesPlayed() { return gamesWon + gamesLost; } - - public double getPointsPerGame(){ + + public double getPointsPerGame() { return (double) getPoints() / (double) getGamesPlayed(); } public String toString() { String s = String.format( - "Name: %s%nGoals: %d\tAssists: %d\tPoints: %d\tPoints Per Game: %d%nGames Won: %d\tGames Lost: %d\tGames" - + " Played: %d%n", - name, goals, assists, getPoints(), getPointsPerGame(), gamesWon, gamesLost, getGamesPlayed()); + "Name: %s%nGoals: %d\tAssists: %d\tPoints: %d\tPoints Per Game: %f%nGames Won: %d" + + "\tGames Lost: %d\tGames Played: %d%n", + name, + goals, + assists, + getPoints(), + getPointsPerGame(), + gamesWon, + gamesLost, + getGamesPlayed()); return s; } public String getData() { - return String.format("%s %d %d %d %d", name, goals, assists, gamesWon, gamesLost); + return String.format("%s %d %d %d %d%n", name, goals, assists, gamesWon, gamesLost); } public boolean equals(Object o) { @@ -69,7 +76,7 @@ public class HockeyPlayer { return this.name.compareTo(p.name); } - public void addGame(int goals, int assists, boolean gameWon){ + public void addGame(int goals, int assists, boolean gameWon) { this.goals += goals; this.assists += assists; if (gameWon) gamesWon++; diff --git a/HockeyStats.java b/HockeyStats.java index 4c8ca39..47512bd 100644 --- a/HockeyStats.java +++ b/HockeyStats.java @@ -16,6 +16,7 @@ public class HockeyStats { } public static void loadData(String fileName) throws Exception { + players.clear(); File f = new File(fileName); br = new BufferedReader(new FileReader(f)); st = new StringTokenizer(br.readLine()); @@ -30,12 +31,13 @@ public class HockeyStats { Integer.parseInt(st.nextToken()), // gamesWon Integer.parseInt(st.nextToken()))); // gamesLost } + br.close(); } public static void saveData(String fileName) throws Exception { FileWriter fw = new FileWriter(fileName); - String s = players.createSaveData(); - fw.write(s); + String save = players.createSaveData(); + fw.write(save); fw.close(); System.out.println("Data saved to " + fileName); } @@ -43,21 +45,23 @@ public class HockeyStats { public static boolean showMenu() throws Exception { System.out.print( """ - <----------------------------> - Hockey Stats Manager Menu + <----------------------------> + Hockey Stats Manager Menu - Select an option: + Select an option: - (1) Load Data from Files - (2) Open Data Editor - (3) Open Stats View Menu - (4) Quit + (1) Load Data from Files + (2) Save Data + (3) Open Data Editor + (4) Open Stats View Menu + (5) Quit - <----------------------------> + <----------------------------> """); int n; try { n = s.nextInt(); + s.nextLine(); } catch (Exception e) { System.out.println("Please choose one of the available menu options!"); return false; @@ -68,16 +72,19 @@ public class HockeyStats { loadData("data.txt"); break; case 2: + saveData("save.txt"); + break; + case 3: while (true) { if (showDataMenu()) break; } break; - case 3: + case 4: while (true) { if (showStatsMenu()) break; } break; - case 4: + case 5: System.out.println("Thank you!"); close = true; break; @@ -96,9 +103,9 @@ public class HockeyStats { Select an option: - (1) Print Data - (2) Save Data - (3) null + (1) Add Player + (2) Remove Player + (3) Add Game to Player (4) Return to main menu <----------------------------> @@ -107,25 +114,64 @@ public class HockeyStats { int k; try { k = s.nextInt(); + s.nextLine(); } catch (Exception e) { System.out.println("Please choose one of the available menu options!"); return false; } - switch (k) { - case 1: - System.out.println(players.toString()); - break; - case 2: - saveData("save.txt"); - break; - case 3: - break; - case 4: - dataExit = true; - break; - default: - System.out.println("Please choose one of the available menu options!"); - break; + try { + switch (k) { + case 1: + System.out.println("Enter The Following Stats:"); + System.out.println("\nPlayer Name:"); + String newName = s.nextLine(); + System.out.println("Player Goals Scored:"); + int newGoals = s.nextInt(); + System.out.println("Player Assists:"); + int newAssists = s.nextInt(); + System.out.println("Games Won:"); + int newGamesWon = s.nextInt(); + System.out.println("Games Lost:"); + int newGamesLost = s.nextInt(); + s.nextLine(); + players.addPlayer( + new HockeyPlayer(newName, newGoals, newAssists, newGamesWon, newGamesLost)); + break; + case 2: + System.out.println("Enter the name of the player you want to remove:"); + String rmName = s.nextLine(); + System.out.println( + players.removePlayer(rmName) + ? rmName + " was successfully removed." + : "Player not found."); + break; + case 3: + System.out.println("Enter The Following Stats:"); + System.out.println("\nPlayer Name:"); + String addName = s.nextLine(); + System.out.println("Player Goals Scored:"); + int addGoals = s.nextInt(); + System.out.println("Player Assists:"); + int addAssists = s.nextInt(); + s.nextLine(); + System.out.println("Games Won? (yes/no)"); + boolean addGameWon = s.nextLine().equalsIgnoreCase("yes"); + HockeyPlayer found = players.findPlayer(addName); + if (found == null) { + System.out.println("Player not found."); + } else { + found.addGame(addGoals, addAssists, addGameWon); + } + break; + case 4: + dataExit = true; + break; + default: + System.out.println("Please choose one of the available menu options!"); + break; + } + } catch (Exception e) { + System.out.println("Please enter the right kind of data"); } return dataExit; } @@ -133,39 +179,66 @@ public class HockeyStats { public static boolean showStatsMenu() { System.out.print( """ - <----------------------------> - Hockey Stats Viewer Menu + <----------------------------> + Hockey Stats Viewer - Select an option: + Select an option: - (1) Stats - (2) Stats - (3) Stats - (4) Stats + (1) Print Data + (2) Find Player + (3) Sort By Name + (4) Sort By Points + (5) Show by Goals > num + (6) Return to Main Menu - <----------------------------> + <----------------------------> """); boolean statsExit = false; int k; try { k = s.nextInt(); + s.nextLine(); } catch (Exception e) { System.out.println("Please choose one of the available menu options!"); return false; } - switch (k) { - case 1: - break; - case 2: - break; - case 3: - break; - case 4: - statsExit = true; - break; - default: - System.out.println("Please choose one of the available menu options!"); - break; + try { + switch (k) { + case 1: + System.out.println(players.toString()); + break; + case 2: + System.out.println("Enter the name of the player you want to find:"); + String findName = s.nextLine(); + HockeyPlayer p = players.findPlayer(findName); + System.out.println(p == null ? "Player not found." : p.toString()); + break; + case 3: + players.sortByNames(); + System.out.println("Sorted by names"); + break; + case 4: + players.sortByPoints(); + System.out.println("Sorted by points"); + break; + case 5: + System.out.println("Enter the min goals to show:"); + int minGoals = s.nextInt(); + s.nextLine(); + PlayerList pl = players.getPlayersGoalsAbove(minGoals); + System.out.println(pl.toString()); + break; + case 6: + statsExit = true; + break; + default: + System.out.println("Please choose one of the available menu options!"); + break; + } + } catch (Exception e) { + System.out.println("k = " + k); + System.out.println(e); + System.out.println("Please enter the right kind of data"); } return statsExit; } diff --git a/Planning/csaFinalProjUML.drawio b/Planning/csaFinalProjUML.drawio index e1aae91..9985ed2 100644 --- a/Planning/csaFinalProjUML.drawio +++ b/Planning/csaFinalProjUML.drawio @@ -1,6 +1,6 @@ - + @@ -41,12 +41,12 @@ - + - + @@ -55,29 +55,32 @@ - + - + - + - + - + + + + - + - + - + diff --git a/PlayerList.java b/PlayerList.java index 760689b..e696b1a 100644 --- a/PlayerList.java +++ b/PlayerList.java @@ -7,11 +7,16 @@ public class PlayerList { pList = new ArrayList<>(); } + public void clear() { + pList.clear(); + } + public void addPlayer(HockeyPlayer p) { pList.add(p); } - public boolean removePlayer(HockeyPlayer p) { + public boolean removePlayer(String name) { + HockeyPlayer p = findPlayer(name); return pList.remove(p); } diff --git a/data.txt b/data.txt deleted file mode 100644 index 07b6c69..0000000 --- a/data.txt +++ /dev/null @@ -1,2 +0,0 @@ -1 -Connor McDavid 9 8 7 6 diff --git a/save.txt b/save.txt deleted file mode 100644 index 7cd61ed..0000000 --- a/save.txt +++ /dev/null @@ -1,2 +0,0 @@ -1 -Connor McDavid 9 8 7 6 \ No newline at end of file