1 /**
2 * Purpose: Represents a CD from my music collection
3 * Author: skotthe
4 */
5
6 public class CD {
7
8 private String title;
9 private String artist;
10 private String[] songTitles;
11 private int releaseYear;
12
13
14 // Constructor
15 CD(String theTitle, String theArtist, int theReleaseYear, int noOfSongs){
16 this.title = theTitle;
17 this.artist = theArtist;
18 this.releaseYear = theReleaseYear;
19 this.songTitles = new String[noOfSongs];
20 }
21
22 //Getters
23 public String getTitle(){
24 return title;
25 }
26 public String getArtist(){
27 return artist;
28 }
29 public int getReleaseYear(){
30 return releaseYear;
31 }
32 public String[] getSongTitles(){
33 return songTitles;
34 }
35
36 // Setters
37 public void setArtist(String theArtist){
38 this.artist = theArtist;
39 }
40 public void setTitle(String theTitle){
41 this.title = theTitle;
42 }
43 public void setReleaseYear(int theReleaseYear){
44 this.releaseYear = theReleaseYear;
45 }
46 public void setSongTitles(String[] theSongTitles){
47 this.songTitles = theSongTitles;
48 }
49
50 //Instance Methods
51 public boolean addSong(String theSong){
52 //pre: String representing the song to be added
53 //post: TRUE if addition is succesful, FALSE otherwise
54 // Do not allow
55 // - duplicates (ignore case)
56 if (!hasDuplicates(theSong)){
57 return addThisSong(theSong);
58 }else{
59 return false;
60 }
61 }
62
63
64 public boolean hasDuplicates(String theSong){
65 // pre: String with the song title
66 // post: TRUE if name exists in songTitles, FALSE otherwise
67 for(int i= 0;i <= songTitles.length && songTitles[i] != null;i++){
68 if(songTitles[i].equalsIgnoreCase(theSong)){
69 return true;
70 }
71 }
72 return false;
73 }
74
75 private boolean addThisSong(String theSong){
76 //pre: String with the song title
77 //post: TRUE if addition succeeds, FALSE otherwise
78 int i = 0;
79 for(; i < songTitles.length && songTitles[i] != null;i++){
80 ;
81 }
82 if (i < songTitles.length){
83 songTitles[i]=theSong;
84 return true;
85 } else {
86 return false;
87 }
88 }
89
90 public void showInfo(){
91 // Pretty print CD Information
92 System.out.println(" **** CD Info **** ");
93 System.out.println(" CD Title :\t"+ title+"\n Artist :\t"+artist
94 +"\n Year :\t\t"+releaseYear);
95 System.out.println("\tSong List :");
96 prettyPrintSongs();
97 System.out.println(" \t\t\t\t\t**** CD Info **** ");
98 }
99
100 public void prettyPrintSongs(){
101 //Pretty print Song List
102 for(int i = 0 ; i < songTitles.length && songTitles[i]!=null;i++){
103 System.out.println("\t\t"+i+". "+songTitles[i]);
104 }
105 }
106 }
Generated with
vim2html
Copyright © 2003-2004 by Chip Cuccio
<http://norlug.org/~chipster/finger>