ArrayList problem ....
I am going to construct a Simple Muise Qrganizer Application.I want to add the Muise that i have to my application ..However, i can`t display it out properly...i think there is an problem on my arrayList..could anyone give me some suggestion...thc ..i have three class : MusicCd,
MusicCdStore, and MusicCdStoreEngine...MusicCd is for a CD data,
MusicCdStore is where i introduce the the arrayList, and MusicCdStoreEngine is the main function..
import java.util.*;
publicclass MusicCdStoreEngine{
publicstaticvoid main(String[] args)
{
boolean quit =false;
readOperation theRo =new readOperation();
MusicCdStore mcs =new MusicCdStore();
finalint SENTINEL = -1;
String sentinel ="quit";
int inputValue = 0;
String muiseCdsTitle;
int muiseCdsYearOfRelease;
while(inputValue != SENTINEL)
{
//inserting
muiseCdsTitle = theRo.readString("Please enter your CD`s title : ");
muiseCdsYearOfRelease = theRo.readInt("Please enter your CD`s year of release : ");
mcs =new MusicCdStore(muiseCdsTitle, muiseCdsYearOfRelease);
inputValue = theRo.readInt("Do you want to display the Cd? press -1 to Display or other value to continue...");
}
//System.out.println("Our CD collection is:" );
System.out.println(mcs.toString());
}
//Actually Result: (it is not suppose to have that)
//Please enter your CD`s title : We are the world
//Please enter your CD`s year of release : 1984
//Do you want to display the Cd? press -1 to Display...8
//Please enter your CD`s title : Go go go
//Please enter your CD`s year of release : 1992
//Do you want to display the Cd? press -1 to Display...-1
// Music Cd`s Title: Go go ogoYear of release: 1992
//Expected Result: ( suppose to have that)
//Please enter your CD`s title : We are the world
//Please enter your CD`s year of release : 1984
//Do you want to display the Cd? press -1 to Display...8
//Please enter your CD`s title : Go go go
//Please enter your CD`s year of release : 1992
//Do you want to display the Cd? press -1 to Display...-1
//Music Cd`s Title: We are the world Year of release: 1984 <<--(I can`t this result in Actually Result.
// .it seems that it doesn`t add anything to my arraylist)
// Music Cd`s Title: Go go ogoYear of release: 1992<<--(i only get this result)
//From these two result, it seems that my araylis(in my MusicCdStore) is out of order....could anyone show me what problewm with my code..pls help
}
[code]
import java.util.ArrayList;
publicclass MusicCdStore
{
private ArrayList<MusicCd> MusicCdList;
public MusicCdStore()
{
/*
MusicCdList = new ArrayList<MusicCd>( );
MusicCdList.add(new MusicCd("Jessica"));
MusicCdList.add(new MusicCd("Get to the World"));
MusicCdList.trimToSize();
*/
}
public MusicCdStore(String newMusicCdsTitle,int newYearOfRelease)
{
MusicCdList =new ArrayList<MusicCd>( );
MusicCdList.add(new MusicCd(newMusicCdsTitle, newYearOfRelease));
//MusicCdList.add(new MusicCd(newMusicCdsTitle, newYearOfRelease));
//MusicCdList.add(new MusicCd("Get to the World"));
MusicCdList.trimToSize();
}
public String toString( )
{
String result=" ";
for( MusicCd tempCd : MusicCdList)
{
result += tempCd.toString() +"\n";
}
return result;
}
public ArrayList<MusicCd> searchForTitle(String searchString)
{
ArrayList<MusicCd> searchResult =new ArrayList<MusicCd>();
for(MusicCd currentMusicCd : MusicCdList)
{
if((currentMusicCd.getTitle()).indexOf(searchString) != -1)
searchResult.add(currentMusicCd);
}
searchResult.trimToSize();
return searchResult;
}
}
publicclass MusicCd
{
private String musicCd;
privateint yearOfRelease;
public MusicCd()
{
musicCd ="";
yearOfRelease = 1900;
}
public MusicCd(String newMusicCd,int newYearOfRelease)
{
musicCd = newMusicCd;
yearOfRelease = newYearOfRelease;
}
public String getTitle()
{
return musicCd;
}
publicint getYearOfRelease()
{
return yearOfRelease;
}
publicvoid setTitle(String newMusicCd)
{
musicCd = newMusicCd;
}
publicvoid setYearOfRelease(int newYearOfRelease)
{
yearOfRelease = newYearOfRelease;
}
public String toString()
{
return("Music Cd`s Title: " + musicCd +"\t"
+"Year of release: " + yearOfRelease +"\t");
}
}

