/Users/18812/Desktop/Computer Science IA/Chun - IA/src/recipeOrganizer/SortAndSearchRecipes.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 recipeOrganizer;
 7 
 8 /**
 9  *
10  * @author 18812
11  */
12 public class SortAndSearchRecipes {
13     public int count(RecipeClass[] recipes){
14         int notDefaultCount = 0;
15         for(int i = 0; i < recipes.length; i++){
16             if(recipes[i].getTimeRequired() != 999999999){
17                 notDefaultCount++;
18             }
19         }
20         return notDefaultCount;
21     }
22     
23     public void sortByRecipeNameAtoZ(RecipeClass[] recipes) {
24         int n = count(recipes);
25         boolean sorted = false;
26         while (!sorted) {
27             n--;
28             sorted = true;  
29             for (int i=0; i < n; i++) {
30                 if (recipes[i].getRecipeName().compareTo(recipes[i+1].getRecipeName()) > 0) {
31                     RecipeClass temp = recipes[i];  
32                     recipes[i] = recipes[i+1];  
33                     recipes[i+1] = temp;
34                     sorted = false; 
35                 }
36             }
37         }
38     }
39     
40     public void sortByNewest(RecipeClass[] recipes) {
41         int n = count(recipes);
42         boolean sorted = false;
43         while (!sorted) {
44             n--;
45             sorted = true;  
46             for (int i=0; i < n; i++) {
47                 if (recipes[i].getRecordedDate().compareTo(recipes[i+1].getRecordedDate()) > 0) {
48                     RecipeClass temp = recipes[i];  
49                     recipes[i] = recipes[i+1];  
50                     recipes[i+1] = temp;
51                     sorted = false; 
52                 }
53             }
54         }
55     }
56     
57     public int searchByName(RecipeClass arr[], String recipes){
58         for(int i = 0; i < arr.length; i++){
59             if(arr[i].getRecipeName().equals(recipes)){
60                 return i;
61             }
62         }
63         return -1;
64     }
65     
66     public int searchByDate(RecipeClass arr[], String recipes){
67         for (int i = 0 ; i < arr.length ; i++)
68         {
69             if (arr[i].getRecordedDate().equals(recipes));
70             {
71                 return i;
72             }
73         }
74         return -1;
75     }
76     
77     public int searchByCategory(RecipeClass arr[], String recipes){
78         for ( int i = 0 ; i < arr.length ; i++)
79         {
80             if (arr[i].getCategory().equals(recipes));
81             {
82                 return i;
83             }
84         }
85         return -1;
86     }
87 }
88