sort by goals
This commit is contained in:
@@ -280,8 +280,9 @@ public class HockeyStats {
|
||||
(2) Find Player
|
||||
(3) Sort By Name
|
||||
(4) Sort By Points
|
||||
(5) Show by Goals > num
|
||||
(6) Return to Main Menu
|
||||
(5) Sort By Goals
|
||||
(6) Show by Goals > num
|
||||
(7) Return to Main Menu
|
||||
|
||||
<---------------------------->
|
||||
""";
|
||||
@@ -327,6 +328,10 @@ public class HockeyStats {
|
||||
JOptionPane.showMessageDialog(null, "Sorted by points");
|
||||
break;
|
||||
case 5:
|
||||
players.sortByGoals();
|
||||
JOptionPane.showMessageDialog(null, "Sorted by goals");
|
||||
break;
|
||||
case 6:
|
||||
String minInput = JOptionPane.showInputDialog(null, "Enter the min goals to show:");
|
||||
if (minInput == null) {
|
||||
break;
|
||||
@@ -343,7 +348,7 @@ public class HockeyStats {
|
||||
JScrollPane showScrollPane = new JScrollPane(showTextArea);
|
||||
JOptionPane.showMessageDialog(null, showScrollPane);
|
||||
break;
|
||||
case 6:
|
||||
case 7:
|
||||
statsExit = true;
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -129,6 +129,21 @@ public class PlayerList {
|
||||
}
|
||||
}
|
||||
|
||||
/** Sorts the players in the list by total goals using an Insertion Sort algorithm. */
|
||||
public void sortByGoals() {
|
||||
int n = pList.size();
|
||||
for (int i = 0; i < n; i++) {
|
||||
HockeyPlayer key = pList.get(i);
|
||||
int j = i - 1;
|
||||
|
||||
while (j >= 0 && pList.get(j).getGoals() < key.getGoals()) {
|
||||
pList.set(j + 1, pList.get(j));
|
||||
j--;
|
||||
}
|
||||
pList.set(j + 1, key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Locates a player and updates their stats with results from a single game.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user