1  /**
  2   * Represent my CD Collection with CDs
  3   * Author : skotthe
  4   */
  5  public class CDCollection{
  6
  7    private CD[] theCollection;
  8
  9    // Constructor
 10    CDCollection(int collectionSize){
 11      this.theCollection = new CD[collectionSize];
 12    }
 13
 14    //Accesors
 15    //Setter
 16    public void setTheCollection(CD[] newCollection){
 17      theCollection = newCollection;
 18    }
 19
 20    //getter
 21    public CD[] getTheCollection(){
 22      return theCollection;
 23    }
 24
 25
 26    public void showInfo(){
 27      //pretty print each CD in turn 
 28      System.out.println(" #### The Collection ####");
 29      for (int i = 0; i < theCollection.length && theCollection[i] !=null;i++){
 30        System.out.println(" CD "+ i+" :");
 31        theCollection[i].showInfo();
 32      }
 33      System.out.println("\t\t\t\t #### The Collection ####");
 34    }
 35
 36
 37    public boolean addCD(CD newCD){
 38      // pre: a CD
 39      // post: True on success, false otherwise
 40      //  DO not allow duplicate cds 
 41      if (!hasDuplicates(newCD)){
 42        return addThisCD(newCD);
 43      }else{
 44        return false;
 45      }
 46    }
 47
 48    private boolean addThisCD(CD aCD){
 49      // pre: aCD that is not already in the collection 
 50      // post: True if addition was succesful, false otherwise
 51      int i = 0;
 52      for(;i < theCollection.length && theCollection[i]!=null;i++){
 53        ;
 54      }
 55      if (i < theCollection.length){
 56        theCollection[i] = aCD;
 57        return true;
 58      }else{
 59        return false;
 60      }
 61    }
 62
 63    private boolean hasDuplicates(CD aCD){
 64      //pre: aCD 
 65      //post: True if aCD is exactly the same as a CD in the collection 
 66      //      False otherwise.
 67        for (int i =0; i < theCollection.length && theCollection[i]!=null;i++){
 68        if (sameCD(theCollection[i],aCD)){
 69          return true;
 70        }
 71      }
 72      return false;
 73    }
 74
 75    private boolean sameCD(CD first, CD second){
 76      //pre: two CDs
 77      //post: True if exactly the same i.e., same :
 78      //              title, artist, year, number of songs,
 79      //              order of songs, title of each song is the same, 
 80      boolean titles = first.getTitle().equalsIgnoreCase(second.getTitle());
 81      boolean artists = first.getArtist().equalsIgnoreCase(second.getArtist());
 82      boolean releases = first.getReleaseYear() == second.getReleaseYear();
 83      boolean duplicateSongs = hasIdenticalSongs(first,second);
 84
 85      return (titles && artists && releases && duplicateSongs);
 86    }
 87
 88    private boolean hasIdenticalSongs(CD one, CD two){
 89      //pre: two CDs (check if they are identical)
 90      //post: TRUE : if duplicate songs exists
 91      //      FALSE: otherwise
 92      String[] oneSongs = one.getSongTitles();
 93      String[] twoSongs = two.getSongTitles();
 94      if (oneSongs.length != twoSongs.length){
 95        return false;
 96      } else {
 97        for (int i = 0; i < oneSongs.length; i++){
 98          if (!two.hasDuplicates(oneSongs[i])){
 99            return false;
100          }
101        }
102        return true;
103      }
104    }
105  }

Generated with vim2html
Copyright © 2003-2004 by Chip Cuccio <http://norlug.org/~chipster/finger>