Logout

Full GUI Sort & Search With Arrays - (New Currculum)

MoviesGUI2222.java
/Users/adelaide/Public/Netbeans - All JSR Projects/Movie Database 2/src/movie/database/pkg2/MoviesGUI2222.java
  1 package movie.database.pkg2;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.BufferedWriter;
  5 import java.io.FileReader;
  6 import java.io.FileWriter;
  7 import java.io.IOException;
  8 import java.util.StringTokenizer;
  9 import javax.swing.JFileChooser;
 10 
 11 /**
 12  *
 13  * @author John Rayworth
 14  */
 15 public class MoviesGUI2222 extends javax.swing.JFrame {
 16 
 17     public MoviesGUI2222() {
 18         initComponents();
 19         myInitComponents();
 20 
 21     }
 22     private Movie2222[] movieArray = new Movie2222[100];
 23     private int counter = 0;
 24 
 25     private void myInitComponents() {
 26 
 27         for (int i = 0; i < movieArray.length; i++) {
 28             movieArray[i] = new Movie2222();
 29         }
 30         buttonGroup1.add(haveSeenItYesRadioButton);
 31         buttonGroup1.add(haveSeenItNoRadioButton);
 32         //displayTable.
 33         searchResultTable.setAutoCreateRowSorter(true);
 34     }
 35 
561 
562     private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                             
563         System.exit(0);
564     }                                            
565 
566     private void EntryButtonMouseReleased(java.awt.event.MouseEvent evt) {                                          
567         boolean haveSeenIt = false;
568         if (haveSeenItYesRadioButton.isSelected()) {
569             haveSeenIt = true;
570         }
571 
572         movieArray[counter] = new Movie2222(movieNameTF.getText(), genreNameTF.getText(), haveSeenIt);
573         counter++;
574         movieNameTF.setText("");
575         genreNameTF.setText("");
576     }                                         
577 
578     private void searchButtonMouseReleased(java.awt.event.MouseEvent evt) {                                           
579 
580         SortAndSearchMovies2222 s = new SortAndSearchMovies2222();
581 
582         if (searchComboBox.getSelectedItem().equals("Movie Name")) {
583             s.selectionSortByMovieName(movieArray);
584             int result = s.binarySearchOfMovieName(movieArray, searchInputTF.getText());
585             if (result == -1) {
586                 searchResultTF.setText("Not found");
587             }
588             searchResultTF.setText(movieArray[result].getGenre() + " --" + movieArray[result].getHaveSeenIt());
589 
590         } 
591         
592         else if (searchComboBox.getSelectedItem().equals("Genre")) {
593             Movie2222[] genreResultArray = s.sequentialSearchOfGenre(movieArray, searchInputTF.getText());
594             for (int i = 0; i < genreResultArray.length; i++) {
595                 if (!genreResultArray[i].getMovieName().equals("not set yet")) {
596                     searchResultTable.setValueAt(genreResultArray[i].getMovieName(), i, 0);
597                     searchResultTable.setValueAt(genreResultArray[i].getGenre(), i, 1);
598                     searchResultTable.setValueAt(genreResultArray[i].getHaveSeenIt(), i, 2);
599                 }
600             }
601 
602         } else {
603             s.selectionSortByHaveSeenIt(movieArray);
604             int result = s.binarySearchOfHaveSeenIt(movieArray, searchInputTF.getText());
605             if (result == -1) {
606                 searchResultTF.setText("Not found");
607             }
608             searchResultTF.setText(movieArray[result].getMovieName() + " --" + movieArray[result].getGenre());
609 
610         }
611 
612     }                                          
613 
614     private void haveSeenItYesRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                         
615         // TODO add your handling code here:
616     }                                                        
617 
618     private void saveMenuItemMouseReleased(java.awt.event.MouseEvent evt) {                                           
619 
620         String stringToSaveToFile = counter + ":";//the number of elements added.
621         for (int i = 0; i < movieArray.length; i++) {
622             stringToSaveToFile = stringToSaveToFile + movieArray[i].getMovieName() + ":" + movieArray[i].getGenre() + ":" + movieArray[i].getHaveSeenIt() + ":";
623         }
624 
625         JFileChooser fileChooser = new JFileChooser();
626         fileChooser.showSaveDialog(this);
627         try {
628             FileWriter fw = new FileWriter(fileChooser.getSelectedFile());
629             BufferedWriter bw = new BufferedWriter(fw);
630             bw.write(stringToSaveToFile);
631             bw.close();
632         } catch (IOException ex) {
633             System.out.println("Problem with FileWriter part in saveMenuItemMouseReleased");
634         }
635     }                                          
636 
637     private void openMenuItemMouseReleased(java.awt.event.MouseEvent evt) {                                           
638         JFileChooser fileChooser = new JFileChooser();
639         fileChooser.showOpenDialog(this);
640         try {
641             FileReader fr = new FileReader(fileChooser.getSelectedFile());
642             BufferedReader br = new BufferedReader(fr);
643             String readIn = br.readLine();
644             System.out.println("readIn: " + readIn);
645             StringTokenizer st = new StringTokenizer(readIn, ":");
646             counter = Integer.parseInt(st.nextToken());
647 
648             int i = 0;
649             while (st.hasMoreTokens()) {
650                 String movieName = st.nextToken();
651 
652                 String genre = st.nextToken();
653                 boolean haveSeen = Boolean.parseBoolean(st.nextToken());
654                 movieArray[i] = new Movie2222(movieName, genre, haveSeen);
655                 i++;
656             }
657             /*for (int k = 0; k < movieArray.length; k++) {
658                 System.out.println(movieArray[k].getMovieName()); // Mistake in class: make sure using k here, not i
659                 System.out.println(movieArray[k].getGenre());
660                 System.out.println(movieArray[k].getHaveSeenIt());
661             }*/
662         } catch (IOException ex) {
663             System.out.println("Problem with reading in the file.");
664         }
665 
666     }                                          
667 
668     private void jTabbedPane1MouseReleased(java.awt.event.MouseEvent evt) {
669         for (int i = 0; i < movieArray.length; i++) {
670             if (!movieArray[i].getMovieName().equals("not set yet")) {
671                 displayTable.setValueAt(movieArray[i].getMovieName(), i, 0);
672                 displayTable.setValueAt(movieArray[i].getGenre(), i, 1);
673                 displayTable.setValueAt(movieArray[i].getHaveSeenIt(), i, 2);
674             }
675         }
676 
677     }
678 
679     /**
680      * @param args the command line arguments
681      */
682     public static void main(String args[]) {
683         /* Set the Nimbus look and feel */
684         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
685         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
686          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
687          */
688         try {
689             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
690                 if ("Nimbus".equals(info.getName())) {
691                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
692                     break;
693                 }
694             }
695         } catch (ClassNotFoundException ex) {
696             java.util.logging.Logger.getLogger(MoviesGUI2222.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
697         } catch (InstantiationException ex) {
698             java.util.logging.Logger.getLogger(MoviesGUI2222.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
699         } catch (IllegalAccessException ex) {
700             java.util.logging.Logger.getLogger(MoviesGUI2222.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
701         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
702             java.util.logging.Logger.getLogger(MoviesGUI2222.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
703         }
704         //</editor-fold>
705 
706         /* Create and display the form */
707         java.awt.EventQueue.invokeLater(new Runnable() {
708             public void run() {
709                 new MoviesGUI2222().setVisible(true);
710             }
711         });
712     }

/Users/adelaide/Public/Netbeans - All JSR Projects/Movie Database 2/src/movie/database/pkg2/SortAndSearchMovies2222.java
  1 
  2 package movie.database.pkg2;
  3 
  4 public class SortAndSearchMovies2222 {
  5 
  6     public void selectionSortByMovieName(Movie2222[] movieArray) {
  7         for (int i = 0; i < movieArray.length - 1; i++) {
  8             int minIndex = i;      // Assumed index of smallest remaining value.
  9             for (int j = i + 1; j < movieArray.length; j++) {
 10                 if (movieArray[j].getMovieName().compareTo(movieArray[minIndex].getMovieName()) < 0) {
 11                     minIndex = j;  // Remember index of new minimum
 12                 }
 13             }
 14             if (minIndex != i) {
 15                 //Exchange current element with smallest remaining.
 16                 //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 17                 Movie2222 temp = movieArray[i];
 18                 movieArray[i] = movieArray[minIndex];
 19                 movieArray[minIndex] = temp;
 20             }
 21         }
 22     }
 23     
 24     public void selectionSortByGenre(Movie2222[] movieArray) {
 25         for (int i = 0; i < movieArray.length - 1; i++) {
 26             int minIndex = i;      // Assumed index of smallest remaining value.
 27             for (int j = i + 1; j < movieArray.length; j++) {
 28                 if (movieArray[j].getGenre().compareTo(movieArray[minIndex].getGenre()) < 0) {
 29                     minIndex = j;  // Remember index of new minimum
 30                 }
 31             }
 32             if (minIndex != i) {
 33                 //Exchange current element with smallest remaining.
 34                 //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 35                 Movie2222 temp = movieArray[i];
 36                 movieArray[i] = movieArray[minIndex];
 37                 movieArray[minIndex] = temp;
 38             }
 39         }
 40     }
 41    
 42     
 43     
 44     
 45     
 46     
 47     
 48     public void selectionSortByHaveSeenIt(Movie2222[] movieArray) {
 49         for (int i = 0; i < movieArray.length - 1; i++) {
 50             int minIndex = i;      // Assumed index of smallest remaining value.
 51             for (int j = i + 1; j < movieArray.length; j++) {
 52                 if (movieArray[j].getHaveSeenIt() == true) {
 53                     minIndex = j;  // Remember index of new minimum
 54                 }
 55             }
 56             if (minIndex != i) {
 57                 //Exchange current element with smallest remaining.
 58                 //But note that this only happens once each outer loop iteration, at the end of the inner loop's looping
 59                 Movie2222 temp = movieArray[i];
 60                 movieArray[i] = movieArray[minIndex];
 61                 movieArray[minIndex] = temp;
 62             }
 63         }
 64     }
 65  
 66     
 67     public int binarySearchOfMovieName(Movie2222 arr[], String key) {
 68         int low = 0;
 69         int high = arr.length - 1;
 70         while (low <= high) {
 71             int mid = (low + high) / 2;
 72             if (arr[mid].getMovieName().equals(key)) {
 73                 return mid;
 74             } else if (arr[mid].getMovieName().compareTo(key) < 0) {
 75                 low = mid + 1;
 76             } else {
 77                 high = mid - 1;
 78             }
 79         }
 80         return -1;
 81     }
 82     
 83     public int binarySearchOfGenre(Movie2222 arr[], String key) {
 84         int low = 0;
 85         int high = arr.length - 1;
 86         while (low <= high) {
 87             int mid = (low + high) / 2;
 88             if (arr[mid].getGenre().equals(key)) {
 89                 return mid;
 90             } else if (arr[mid].getGenre().compareTo(key) < 0) {
 91                 low = mid + 1;
 92             } else {
 93                 high = mid - 1;
 94             }
 95         }
 96         return -1;
 97     }
 98        
 99     
100     public Movie2222[] sequentialSearchOfGenre(Movie2222 [] arrTakenIn, String key) {
101 
102         int arraySize = arrTakenIn.length;
103         Movie2222[] resultArray = new Movie2222[arraySize];
104         for(int i = 0; i < resultArray.length; i++){
105             resultArray[i] = new Movie2222();
106         }
107         int counter = 0;
108         for(int i = 0; i < arrTakenIn.length; i++){
109             if(arrTakenIn[i].getGenre().equals(key)){
110                 resultArray[counter] = arrTakenIn[i];
111                 counter++;
112             }
113         }
114  
115         return resultArray;
116 
117     }
118     
119     
120     public int binarySearchOfHaveSeenIt(Movie2222 arr[], String key) {
121         
122         boolean haveSeenIt = false;
123         if(key.equalsIgnoreCase("yes")){
124             haveSeenIt = true;
125         }
126         
127         int low = 0;
128         int high = arr.length - 1;
129         while (low <= high) {
130             int mid = (low + high) / 2;
131             if (arr[mid].getHaveSeenIt() == haveSeenIt) {//So if looking for false, return false, if looking for true, return true.
132                 return mid;
133             } else if (arr[mid].getHaveSeenIt() == false) {//This means we must be looking for true, but we landed on a false, so:
134                 low = mid + 1;
135             } else {//This means we must be looking for false, but landed on a true, so:
136                 high = mid - 1;
137             }
138         }
139         return -1;
140     }
141 
142     
143 }
144 

 

/Users/adelaide/Public/Netbeans - All JSR Projects/Movie Database 2/src/movie/database/pkg2/Movie2222.java
 1 
 2 package movie.database.pkg2;
 3 
 4 /**
 5  *
 6  * @author John Rayworth
 7  */
 8 public class Movie2222 {
 9     
10     
11     String movieName = "not set yet";
12     String genre = "not set yet";
13     boolean haveSeenIt = false;
14     
15     
16     public Movie2222(){
17         
18     }
19     
20     public Movie2222(String movieName, String genre, boolean haveSeenIt){
21         this.movieName = movieName;
22         this.genre = genre;
23         this.haveSeenIt = haveSeenIt;
24     }
25     
26     public String getMovieName(){
27         return movieName;
28     }
29     
30     public String getGenre(){
31         return genre;
32     }
33     
34     public boolean getHaveSeenIt(){
35         return haveSeenIt;
36     }
37     
38     public String toString(){
38a        //Remember to implement this each time too, as an example of polymorphism.
38b    }
39 }
40