/Users/johnr/Desktop/IA_14_-_Stage_P_Upload_all_2021-04-08/IBCompSciInternalAssessment Lara April 6th/src/ibcompsciinternalassessment/SortingSearchingComplex.java |
1
2
3
4
5
6 package ibcompsciinternalassessment;
7
8 import java.util.ArrayList;
9 import java.util.Random;
10
11
12
13 @author
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) {
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;
29
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;
52
53 }
54 }
55 n--;
56
57 }
58
59 }
60
61
62
63
64
65 public void sortByCountry(ArrayList<Contribution> contributionArray, int numNonEared) {
66 int n = contributionArray.size();
67 boolean sorted = false;
68
69
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
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
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
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;
157
158 }
159 }
160 n--;
161
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){
171 int mid = (low + high) / 2;
172 if(arr.get(mid).getDonorName().equals(key))
173 return mid;
174 else if(arr.get(mid).getDonorName().compareToIgnoreCase(key) < 0)
175 low = mid + 1;
176 else
177 high = mid -1;
178 }
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){
187 int mid = (low + high) / 2;
188 if(arr.get(mid).getGeoInterestName().equals(key))
189 return mid;
190 else if(arr.get(mid).getGeoInterestName().compareToIgnoreCase(key) < 0)
191 low = mid + 1;
192 else
193 high = mid -1;
194 }
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];
203 Random rn = new Random();
204
205 Contribution[] genArray = new NonEarmarkedContribution[2];
206 int genIndex = 0;
207 Contribution[] ncArray = new EarmarkedContribution[2];
208 int ncIndex = 0;
209 Contribution[] cArray = new EarmarkedContribution[2];
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
218 if (country.equals("non-earmarked contribution") && genIndex<2) {
219 System.out.println("nonE Added");
220 genArray[genIndex] = contributions.get(index);
221 genIndex++;
222 }
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 }
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
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