/** * Purpose: Represents a CD from my music collection * Author: skotthe */ public class CD { private String title; private String artist; private String[] songTitles; private int releaseYear; // Constructor CD(String theTitle, String theArtist, int theReleaseYear, int noOfSongs){ this.title = theTitle; this.artist = theArtist; this.releaseYear = theReleaseYear; this.songTitles = new String[noOfSongs]; } //Getters public String getTitle(){ return title; } public String getArtist(){ return artist; } public int getReleaseYear(){ return releaseYear; } public String[] getSongTitles(){ return songTitles; } // Setters public void setArtist(String theArtist){ this.artist = theArtist; } public void setTitle(String theTitle){ this.title = theTitle; } public void setReleaseYear(int theReleaseYear){ this.releaseYear = theReleaseYear; } public void setSongTitles(String[] theSongTitles){ this.songTitles = theSongTitles; } //Instance Methods public boolean addSong(String theSong){ //pre: String representing the song to be added //post: TRUE if addition is succesful, FALSE otherwise // Do not allow // - duplicates (ignore case) if (!hasDuplicates(theSong)){ return addThisSong(theSong); }else{ return false; } } public boolean hasDuplicates(String theSong){ // pre: String with the song title // post: TRUE if name exists in songTitles, FALSE otherwise for(int i= 0;i <= songTitles.length && songTitles[i] != null;i++){ if(songTitles[i].equalsIgnoreCase(theSong)){ return true; } } return false; } private boolean addThisSong(String theSong){ //pre: String with the song title //post: TRUE if addition succeeds, FALSE otherwise int i = 0; for(; i < songTitles.length && songTitles[i] != null;i++){ ; } if (i < songTitles.length){ songTitles[i]=theSong; return true; } else { return false; } } public void showInfo(){ // Pretty print CD Information System.out.println(" **** CD Info **** "); System.out.println(" CD Title :\t"+ title+"\n Artist :\t"+artist +"\n Year :\t\t"+releaseYear); System.out.println("\tSong List :"); prettyPrintSongs(); System.out.println(" \t\t\t\t\t**** CD Info **** "); } public void prettyPrintSongs(){ //Pretty print Song List for(int i = 0 ; i < songTitles.length && songTitles[i]!=null;i++){ System.out.println("\t\t"+i+". "+songTitles[i]); } } }