/Users/johnr/Desktop/IA_14_-_Stage_P_Upload_all_2021-04-08/IBCompSciInternalAssessment Lara April 6th/src/ibcompsciinternalassessment/SortingSearchingComplex.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 ibcompsciinternalassessment;
  7 
  8 import java.util.ArrayList;
  9 import java.util.Random;
 10 
 11 /**
 12  *
 13  * @author 21258
 14  */
 15 public class SortingSearchingComplex {
 16     
 17     public void sortByName(ArrayList<Contribution> contributionArray) {
 18         int n = contributionArray.size();
 19         boolean sorted = false;
 20         while (!sorted) { //It is the n which will result in one less comparison happening each outer pass;
 21             
 22             sorted = true;
 23             for (int i=0; i < n-1; i++) {
 24                 if (contributionArray.get(i).getDonorName().compareToIgnoreCase(contributionArray.get(i+1).getDonorName()) > 0) {
 25                     Contribution temp = contributionArray.get(i);  
 26                     contributionArray.set(i, contributionArray.get(i+1));  
 27                     contributionArray.set(i+1, temp);
 28                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
 29                                     //with sorted re-set to false again, the while loop continues
 30                }
 31            }
 32            n--;
 33         }     
 34     }
 35     
 36     
 37     public void sortByAmount(ArrayList<Contribution> contributionArray) {
 38         int n = contributionArray.size();
 39         NaturalOrderComparator sorter = new NaturalOrderComparator<String>(false);
 40         boolean sorted = false;
 41         int smallerOrBigger;
 42         while (!sorted) {
 43             
 44             sorted = true;
 45             for (int i=0; i < n-1; i++) {
 46                 smallerOrBigger = sorter.compare(contributionArray.get(i).getAmountEstimated(), contributionArray.get(i+1).getAmountEstimated());
 47                 if(smallerOrBigger == -1){
 48                     Contribution temp = contributionArray.get(i);  
 49                     contributionArray.set(i, contributionArray.get(i+1));  
 50                     contributionArray.set(i+1, temp);
 51                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
 52                                        //with sorted re-set to false again, the while loop continues
 53                 }
 54             }
 55             n--; //It is the n which will result in one less comparison happening each outer pass;
 56                   //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 57         }     
 58     
 59     }
 60     
 61     
 62     //this sort moves all nonearmarked contribution to the end of the list and then sorts them by the geographical interest
 63     //Takes in contribution array and the number of non earmarked contributions in the array
 64     //returns a sorted list
 65     public void sortByCountry(ArrayList<Contribution> contributionArray, int numNonEared) {
 66         int n = contributionArray.size();
 67         boolean sorted = false;
 68         
 69         //First loop moves all the nonearmarked contributions to the end of the list
 70         while (!sorted) {
 71             sorted = true;  
 72             for (int i=0; i < n-1; i++) {
 73                 String country = contributionArray.get(i).getGeoInterestName();
 74                 if (country.equals("non-earmarked contribution")) {
 75                     Contribution temp = contributionArray.get(i);  
 76                     contributionArray.set(i, contributionArray.get(i+1));  
 77                     contributionArray.set(i+1, temp);
 78                     sorted = false; 
 79                 }
 80             }
 81             n--;
 82         }
 83         
 84         //remaining earmarked contributions are now sorted by geographical interest (so country name)
 85         sorted = false;
 86         n = contributionArray.size()-numNonEared;
 87         while (!sorted) {
 88             sorted = true;  
 89             for (int i=0; i < n-1; i++) {
 90                 String firstCountry =contributionArray.get(i).getGeoInterestName();
 91                 String secondCountry = contributionArray.get(i+1).getGeoInterestName();
 92                 if (firstCountry.compareToIgnoreCase(secondCountry) > 0) {
 93                     Contribution temp = contributionArray.get(i);  
 94                     contributionArray.set(i, contributionArray.get(i+1));  
 95                     contributionArray.set(i+1, temp);
 96                     sorted = false; 
 97                 }
 98             }
 99             n--; 
100         }
101     
102     }
103     
104     public void sortByCountrySeverity(ArrayList<Contribution> contributionArray, int numNonEared) {
105         int n = contributionArray.size();
106         boolean sorted = false;
107         
108         //First loop moves all the nonearmarked contributions to the end of the list
109         while (!sorted) {
110             sorted = true;  
111             for (int i=0; i < n-1; i++) {
112                 String country = contributionArray.get(i).getGeoInterestName();
113                 if (country.equals("non-earmarked contribution")) {
114                     Contribution temp = contributionArray.get(i);  
115                     contributionArray.set(i, contributionArray.get(i+1));  
116                     contributionArray.set(i+1, temp);
117                     sorted = false; 
118                 }
119             }
120             n--;
121         }
122         
123         //this loop sorts the countries by severity
124         sorted = false;
125         n = contributionArray.size()-numNonEared;
126         while (!sorted) {
127             sorted = true;  
128             for (int i=0; i < n-1; i++) {
129                 int firstCountry = contributionArray.get(i).getGeoInterest().getSeverity();
130                 int secondCountry = contributionArray.get(i+1).getGeoInterest().getSeverity();
131                 
132                 if (firstCountry < secondCountry) {
133                     Contribution temp = contributionArray.get(i);  
134                     contributionArray.set(i, contributionArray.get(i+1));  
135                     contributionArray.set(i+1, temp);
136                     sorted = false; 
137                 }
138             }
139             n--;
140         }
141         
142         
143         
144         NaturalOrderComparator sorter = new NaturalOrderComparator<String>(false);
145         sorted = false;
146         int smallerOrBigger;
147         n = contributionArray.size()-numNonEared-1;
148         while (!sorted) {
149             sorted = true;
150             for (int i=0; i < n-1; i++) {
151                 smallerOrBigger = sorter.compare(contributionArray.get(i).getAmountEstimated(), contributionArray.get(i+1).getAmountEstimated());
152                 if(smallerOrBigger == -1){
153                     Contribution temp = contributionArray.get(i);  
154                     contributionArray.set(i, contributionArray.get(i+1));  
155                     contributionArray.set(i+1, temp);
156                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
157                                        //with sorted re-set to false again, the while loop continues
158                 }
159             }
160             n--; //It is the n which will result in one less comparison happening each outer pass;
161                   //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
162         }
163         
164         
165     }
166     
167     public int searchByName(ArrayList<Contribution> arr, String key){
168         int low =0;
169         int high = arr.size() - 1;
170         while(low <= high){           // Keep on looking for the key until the low and the high cross 
171             int mid = (low + high) / 2;  //each other - if that does happen, it means the key was not found.
172             if(arr.get(mid).getDonorName().equals(key))
173                 return mid;           // This is what will happen if/when we find the key in the array.
174             else if(arr.get(mid).getDonorName().compareToIgnoreCase(key) < 0)
175                 low = mid + 1;        // Since the arr[mid] value is less than the key, we can eliminate 
176             else                         //looking at the left side of the remaining elements
177                 high = mid -1;        // i.e. the arr[mid] value is greater than what we are looking for, 
178         }                               // so we can eliminate looking at the right side of the remaining elements
179     return -1;
180     }
181     
182     public int searchByCountry(ArrayList<Contribution> arr, int length, String key){
183         sortByCountry(arr, length);
184         int low =0;
185         int high = arr.size() - 1 - length;
186         while(low <= high){                                                         // Keep on looking for the key until the low and the high cross 
187             int mid = (low + high) / 2;                                             //each other - if that does happen, it means the key was not found.
188             if(arr.get(mid).getGeoInterestName().equals(key))
189                 return mid;                                                         // This is what will happen if/when we find the key in the array.
190             else if(arr.get(mid).getGeoInterestName().compareToIgnoreCase(key) < 0)
191                 low = mid + 1;                                                      // Since the arr[mid] value is less than the key, we can eliminate 
192             else                                                                    //looking at the left side of the remaining elements
193                 high = mid -1;                                                      // i.e. the arr[mid] value is greater than what we are looking for, 
194         }                                                                           // so we can eliminate looking at the right side of the remaining elements
195     return -1;
196     }
197     
198     
199     public Contribution[] ComplexSortRecs(ArrayList<Contribution> contributions) throws NullPointerException{
200         sortByCountrySeverity(contributions, Contribution.getNumberOfNonEarmarkedForms());
201         
202         Contribution[] finalArray = new  Contribution[3];       //this array will be returned to the rec. table with at most 3 contributions
203         Random rn = new Random();
204         //3 mini arrays are created to allow a selection between at most 2 contributions for each criteria
205         Contribution[] genArray = new NonEarmarkedContribution[2];
206         int genIndex = 0;
207         Contribution[] ncArray = new EarmarkedContribution[2];      //i.e. not complete contributions Array
208         int ncIndex = 0;
209         Contribution[] cArray = new EarmarkedContribution[2];       //i.e. completed contributions array
210         int cIndex = 0;
211         
212         System.out.println("WHERE IS THE ISSUE?");
213         
214         try{
215             for(int index = 0; index<contributions.size(); index++){
216                 String country = contributions.get(index).getGeoInterestName();
217                 //if contribution is not earmarked and there is space in the genArray left
218                 if (country.equals("non-earmarked contribution") && genIndex<2) {
219                     System.out.println("nonE Added");
220                     genArray[genIndex] = contributions.get(index);
221                     genIndex++;
222                 }//if contribution is earmarked, key info like donor name/amount is blank, and there is space in the ncArray left
223                 else if((contributions.get(index).getDonorName().equals("") || contributions.get(index).getAmountEstimated().equals("")) && ncIndex<2){
224                     System.out.println("nc Added");
225                     ncArray[ncIndex] = contributions.get(index);
226                     ncIndex++;
227                 }//otherwise and space in cArray
228                 else if(cIndex <2){
229                     System.out.println("c Added");
230                     cArray[cIndex] = contributions.get(index);
231                     cIndex++;
232                 }
233                 else
234                     System.out.print("Out of space in the arrays");
235             }
236 
237             //next section selects one of the 2 
238             int rndmGenIndex = rn.nextInt((genIndex));
239             int rndmNCIndex = rn.nextInt((ncIndex));
240             int rndmCIndex = rn.nextInt((cIndex));
241             System.out.println(rndmGenIndex +" "+ rndmNCIndex +" "+ rndmCIndex);
242             int fnlIndex = 0;
243             if(genIndex != 0){
244                 System.out.println(genArray[rndmGenIndex].getDonorName());
245                 finalArray[fnlIndex] = genArray[rndmGenIndex];
246                 fnlIndex++;
247             }
248             if(ncIndex !=0){
249                 System.out.println(ncArray[rndmNCIndex].getDonorName());
250                 finalArray[fnlIndex] = ncArray[rndmNCIndex];
251                 fnlIndex++;
252             }
253             if(genIndex != 0){
254                 System.out.println(cArray[rndmCIndex].getDonorName());
255                 finalArray[fnlIndex] = cArray[rndmCIndex];
256             }
257             
258             
259             System.out.println(finalArray[0].getDonorName()+"");
260             
261             return finalArray;
262         }
263         catch(NullPointerException error){
264             System.out.println("OH NO! fatal " + error);
265         }
266         
267         
268         for(int i = 0; i < finalArray.length; i++){
269             finalArray[i] = new Contribution();
270         }
271         
272         return finalArray;
273         
274     }
275     
276     
277     
278     
279 }
280