/Users/johnr/Desktop/IA_14_-_Stage_P_Upload_all_2021-04-08/CSIA14829 - Jane April 6th/src/csia14829/SortAndSearch.java
  1 package csia14829;
  2 
  3 import java.util.ArrayList;
  4 
  5 /*
  6  * To change this license header, choose License Headers in Project Properties.
  7  * To change this template file, choose Tools | Templates
  8  * and open the template in the editor.
  9  */
 10 
 11 /**
 12  *
 13  * @author 14829
 14  */
 15 public class SortAndSearch {
 16     
 17     public void switchElements(Item[] itemsQueue, int index1, int index2){
 18         Item temp = itemsQueue[index1];  
 19         itemsQueue[index1] = itemsQueue[index2];  
 20         itemsQueue[index2] = temp;
 21     }
 22     
 23     // all sorts are implementations of Bubble Sort
 24     public void sortByName(Item[] itemsQueue, int counter) {
 25         int n = counter;
 26         boolean sorted = false;
 27         while (!sorted) {
 28             n--; // since we know part of the loop has been sorted after each pass, 
 29                  //n-- makes sure we are doing one less comparison each outer pass
 30             
 31             sorted = true;  
 32             for (int i=0; i < n; i++) {
 33                 if(itemsQueue[i].getName().compareToIgnoreCase(itemsQueue[i+1].getName()) > 0) {
 34                     switchElements(itemsQueue, i, (i+1));
 35                     sorted = false; // if swapping happens, we want to continue comparing elements
 36                                     // with sorted re-set to false, the while loop continues
 37                 }
 38             }    
 39 
 40         }
 41     }
 42     // works the same way as sortByName(), except in line 50 where it is '.getItemType' instead of '.getName()'
 43     public void sortByType(Item[] itemsQueue, int counter) {
 44         int n = counter;
 45         boolean sorted = false;
 46         while (!sorted) {
 47             n--;
 48             sorted = true;
 49             for (int i=0; i < n; i++) {
 50                 if (itemsQueue[i].getItemType().compareToIgnoreCase(itemsQueue[i+1].getItemType()) > 0) {
 51                     switchElements(itemsQueue, i, (i+1));
 52                     sorted = false;
 53                 }
 54             }
 55         }
 56     }
 57 
 58     public void sortByDate(Item[] itemsQueue, int counter) {
 59         int n = counter;
 60         boolean sorted = false;
 61         while (!sorted) {
 62             n--;
 63             sorted = true;
 64             // inner (for) loop loops over each element to compare it to the next
 65             for (int i=0; i < n; i++) {
 66                 // create 6 separate Integer variables to represent dd, mm, and yy for the two dates that are being compared each pass
 67                 int date1 = Integer.parseInt((itemsQueue[i].getDate().substring(0,2)));
 68                 int month1 = Integer.parseInt((itemsQueue[i].getDate().substring(3,5)));
 69                 int year1 = Integer.parseInt((itemsQueue[i].getDate().substring(6,8)));
 70                 
 71                 int date2 = Integer.parseInt((itemsQueue[i+1].getDate().substring(0,2)));
 72                 int month2 = Integer.parseInt((itemsQueue[i+1].getDate().substring(3,5)));
 73                 int year2 = Integer.parseInt((itemsQueue[i+1].getDate().substring(6,8)));
 74                 
 75                 // year takes precedent
 76                 if (year1 > year2) {
 77                     switchElements(itemsQueue, i, (i+1));
 78                     sorted = false;
 79                
 80                 // if year is the same, then check for month
 81                 }else if(year1 == year2){
 82                     if(month1 > month2){
 83                         switchElements(itemsQueue, i, (i+1));
 84                         sorted = false;
 85                     // if month is same, then check for date
 86                     }else if((month1 == month2) & (date1 > date2)){
 87                         switchElements(itemsQueue, i, (i+1));
 88                         sorted = false;
 89                     }
 90                     
 91                 }
 92             }
 93         }
 94     }
 95     
 96     public void sortByStatus(Item[] itemsQueue, int counter) {
 97         int n = counter;
 98         boolean sorted = false;
 99         while (!sorted) {
100             n--;
101             sorted = true;  
102             for (int i=0; i < n; i++) {
103                 boolean bool1 = itemsQueue[i].getClaimedStatus();
104                 boolean  bool2 = itemsQueue[i+1].getClaimedStatus();
105                 int int1 = (bool1) ? 1 : 0; // parses boolean to integer that can be compared
106                 int int2 = (bool2) ? 1 : 0;
107                 if(int1 > int2){
108                     switchElements(itemsQueue, i, (i+1));
109                     sorted = false;
110                 }
111             }
112         }
113     }
114     
115     // sequential search algorithm with '.getName()' to search for name
116     public int sequentialSearchName(Item[] itemsQueue, String key){
117         for(int i = 0; i < itemsQueue.length; i++){
118             if(itemsQueue[i].getName().equalsIgnoreCase(key)){
119                 return i;
120             }
121         }
122         return -1;
123     }
124     
125     // sequential search algorithm to search for item type
126     public int sequentialSearchType(Item[] itemsQueue, String key){
127         for(int i = 0; i < itemsQueue.length; i++){
128             if(itemsQueue[i].getItemType().equalsIgnoreCase(key)){
129                 return i;
130             }
131         }
132         return -1;
133     }
134     
135     // sequential search algorithm to search for the date. 
136     // Here, the user must input date as a String in the form "dd-mm-yy" to search for it.
137     public int sequentialSearchDate(Item[] itemsQueue, String key){
138         for(int i = 0; i < itemsQueue.length; i++){
139             if(itemsQueue[i].getDate().equalsIgnoreCase(key)){
140                 return i;
141             }
142         }
143         return -1;
144     }
145 }
146