/Users/e1813314/Desktop/IA Final Submission 000307-0066/Product/Athletics Awards Netbeans Project/src/iacomsci_allan/SortingAndSearching.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package iacomsci_allan;
  7 
  8 import java.util.ArrayList;
  9 
 10 /**
 11  *
 12  * @author 13314
 13  */
 14 public class SortingAndSearching {
 15 
 16     //Method to sort the file of rosters to have JV set first.
 17     public void sortByYear(ArrayList<Roster> ArrayList) {
 18 //        holderMain = c.getHolderArrayList();
 19         boolean finished = false;
 20 
 21         int n = ArrayList.size();
 22         while (!finished) {
 23             n--; //It is the n which will result in one less comparison happening each outer pass;
 24             //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 25             finished = true;
 26             for (int i = 0; i < n; i++) {
 27                 if (ArrayList.get(i).getYear() > ArrayList.get(i + 1).getYear()) {
 28                     Roster temp = ArrayList.get(i);
 29                     ArrayList.set(i, ArrayList.get(i + 1));
 30                     ArrayList.set(i + 1, temp);
 31                     finished = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
 32                     //with sorted re-set to false again, the while loop continues
 33                 }
 34             }
 35         }
 36         //return ArrayList;
 37     }
 38 
 39     public ArrayList<Roster> sortBySeason(ArrayList<Roster> ArrayList) {
 40         boolean finished = false;
 41 
 42         int n = ArrayList.size();
 43         while (!finished) {
 44             n--; //It is the n which will result in one less comparison happening each outer pass;
 45             //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 46             finished = true;
 47             for (int i = 0; i < n; i++) {
 48                 if (ArrayList.get(i).getSeason() > ArrayList.get(i + 1).getSeason()) {
 49                     Roster temp = ArrayList.get(i);
 50                     ArrayList.set(i, ArrayList.get(i + 1));
 51                     ArrayList.set(i + 1, temp);
 52                     finished = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
 53                     //with sorted re-set to false again, the while loop continues
 54                 }
 55             }
 56         }
 57         return ArrayList;
 58     }
 59 
 60     public ArrayList<Roster> sortBySport(ArrayList<Roster> ArrayList) {
 61         boolean finished = false;
 62 
 63         int n = ArrayList.size();
 64         while (!finished) {
 65             n--; //It is the n which will result in one less comparison happening each outer pass;
 66             //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 67             finished = true;
 68             for (int i = 0; i < n; i++) {
 69                 if (ArrayList.get(i).getSport().compareToIgnoreCase(ArrayList.get(i + 1).getSport()) > 0) {
 70                     Roster temp = ArrayList.get(i);
 71                     ArrayList.set(i, ArrayList.get(i + 1));
 72                     ArrayList.set(i + 1, temp);
 73                     finished = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
 74                     //with sorted re-set to false again, the while loop continues
 75                 }
 76             }
 77         }
 78         return ArrayList;
 79     }
 80 
 81     public void sortPlayersInRoster(ArrayList<Roster> arrL) {
 82         boolean finished = false;
 83 
 84         int n = arrL.size();
 85         while (!finished) {
 86 //            n--; //It is the n which will result in one less comparison happening each outer pass;
 87             //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 88             for (int i = 0; i < n; i++) {
 89                 //Nexted for loop used to access the arrayList within the first arrayList
 90                 for (int j = 0; j < arrL.get(i).getArrayListOfPlayers().size(); j++) {
 91                     //Check to see if the end of the list has been reached
 92                     if (j+1 == arrL.get(i).getArrayListOfPlayers().size())
 93                     {
 94                         //Sentinel to escape the while loop
 95                         finished = true;
 96                     }
 97                     else
 98                     {
 99                     //Checking to see if the next name is less than the previous name
100                     if (arrL.get(i).getArrayListOfPlayers().get(j).getFirstName().compareToIgnoreCase(arrL.get(i).getArrayListOfPlayers().get(j + 1).getFirstName()) > 0) {
101                         //Temporary holder to swap the indices
102                         Player temp = arrL.get(i).getArrayListOfPlayers().get(j);
103                         arrL.get(i).getArrayListOfPlayers().set(j, arrL.get(i).getArrayListOfPlayers().get(j + 1));
104                         arrL.get(i).getArrayListOfPlayers().set(j + 1, temp);
105                         finished = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
106 
107                     }
108                     //Checking the last name in case the first names are the same.
109                     if (arrL.get(i).getArrayListOfPlayers().get(j).getFirstName().compareToIgnoreCase(arrL.get(i).getArrayListOfPlayers().get(j + 1).getFirstName()) == 0) {
110                             if (arrL.get(i).getArrayListOfPlayers().get(j).getLastName().compareToIgnoreCase(arrL.get(i).getArrayListOfPlayers().get(j + 1).getLastName()) > 0) {
111                                 Player temp = arrL.get(i).getArrayListOfPlayers().get(j);
112                                 arrL.get(i).getArrayListOfPlayers().set(j, arrL.get(i).getArrayListOfPlayers().get(j + 1));
113                                 arrL.get(i).getArrayListOfPlayers().set(j + 1, temp);
114                                 finished = false;
115                             }
116                         }
117                     
118                     
119                 }
120                 }
121 
122             }
123             
124         }
125 
126     }
127 
128     //Method to search for a player within all of the rosters
129     public ArrayList<String> searchPlayer(ArrayList<Roster> ar, String f) {
130         //Constructors for the holder variables
131         ArrayList<String> data = new ArrayList();
132         int counterVarsitySeasons= 0;
133         int counterIasasSeasons = 0;
134         String seasons = "";
135         String sports = "";
136         //Initial for loop to iterate through each roster
137         for (int i = 0; i < ar.size(); i++)
138         {
139         //While loop used to loop the binary search until the whole arrayList has been searched 
140         int low = 0;
141         int high = ar.get(i).getArrayListOfPlayers().size()- 1;
142         while (low <= high) {               // Keep on looking for the key until the low and the high cross each other - if that does happen, it means the key was not found.
143             int mid = (low + high) / 2;
144             //Checking to see if the searched name has been found
145             if (ar.get(i).getArrayListOfPlayers().get(mid).getFirstName().equalsIgnoreCase(f)){
146                 counterVarsitySeasons++;
147                 sports += ar.get(i).getSport() + " (" + ar.get(i).getYear() + "), ";
148                 seasons += ar.get(i).getSeason();
149                 //Checking to see if the counter is at 1, so that the data below is written only once
150                 if (counterVarsitySeasons == 1)
151                 {
152                 data.add(ar.get(i).getArrayListOfPlayers().get(mid).getFirstName());
153                 data.add(ar.get(i).getArrayListOfPlayers().get(mid).getLastName());
154                 data.add(Integer.toString(ar.get(i).getArrayListOfPlayers().get(mid).getGrade()));
155                     System.out.println("check");
156                 }
157                 //Casting the player element as Varsity to check if the player is IASAS
158                 Varsity player = (Varsity)ar.get(i).getArrayListOfPlayers().get(mid);
159                 if(player.getIsIasas())
160                 {
161                     //Counter incremented to display the number of IASAS seasons
162                     counterIasasSeasons++;
163                 }
164                 break;
165                 
166                 // This is what will happen if/when we find the key in the array.
167             } else if (ar.get(i).getArrayListOfPlayers().get(mid).getFirstName().compareToIgnoreCase(f) < 0) {
168                 low = mid + 1;            // since the arr[mid] value is less than the key, we can eliminate looking at the left side of the remaining elements
169             } else {
170                 high = mid - 1;            // i.e. the arr[mid] value is greater than what we are looking for, so we can eliminate looking at the right side of the remaining elements
171             }
172         }
173         
174         }
175         data.add(sports);
176         data.add(Integer.toString(counterVarsitySeasons));
177         data.add(Integer.toString(counterIasasSeasons));
178         data.add(seasons);
179         return data;
180         
181     }
182 }
183 
184 
185 
186 //    //Method to search for any roster asked by the user
187 //    /*Preconditions:
188 //    Array read from the file must be sorted by JV and Varsity.
189 //     */
190 //    public void searchRoster() {
191 //
192 //    }
193 //
194 //    /*Preconditions:
195 //    Rosters read from the file must each be sorted alphabetically.
196 //     */
197 //    public void searchPlayer() {
198 //
199 //    }
200 
201