ArrayList problem ....i can remove my object from my arrayList
i am going to insert a cd to the arrayList , remove a cd from the arrayList, searchTheTitle, and display it...But i stuck on my remove function...i can `t remove the data that i want from the array..pls help..
import java.util.ArrayList;
import java.io.*;
publicclass MusicCdStore
{
ArrayList<MusicCd> MusicCdList;
publicvoid insertCd()
{
MusicCdList =new ArrayList<MusicCd>( );
readOperation theRo =new readOperation();
MusicCd theCd;
int muiseCdsYearOfRelease;
String muiseCdsTitle;
while(true)
{
String continueInsertCd ="Y";
do
{
muiseCdsTitle = theRo.readString("Please enter your CD`s title : ");
muiseCdsYearOfRelease = theRo.readInt("Please enter your CD`s year of release : ");
MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsYearOfRelease));
MusicCdList.trimToSize();
continueInsertCd = theRo.readString("Do you have another Cd ? (Y/N) : ");
}while(continueInsertCd.equals("Y") || continueInsertCd.equals("y") );
if(continueInsertCd.equals("N") || continueInsertCd.equals("n"));
{
//MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsYearOfRelease));
break;
}
//System.out.println("You `ve an invalid input " + continueInsertCd + " Please enter (Y/N) only!!");
}
}
publicvoid displayAllCd()
{
System.out.println("\nOur CD collection is: \n" );
System.out.println(toString());
}
public String toString( )
{
String result=" ";
for( MusicCd tempCd : MusicCdList)
{
result += tempCd.toString() +"\n";
}
return result;
}
publicvoid searchingMusicCd()
{
readOperation theRo =new readOperation();
String keyword = theRo.readString("Enter a CD `s Title you are going to search : ") ;
ArrayList<MusicCd> results = searchForTitle(keyword );
System.out.println("The search results for " + keyword +" are:" );
for(MusicCd tempCd : results)
System.out.println( tempCd.toString() );
}
//removeCd function << -here
publicvoid removeCd()
{
readOperation theRo =new readOperation();
String keyword = theRo.readString("Please enter CD `s title you are going to remove : ") ;
ArrayList<MusicCd> removeMusicCdResult =searchForRemoveCdsTitle(keyword);
System.out.println("The CD that you just removed is " + keyword );
for(MusicCd tempCd : removeMusicCdResult)
System.out.println( tempCd.toString() );
}
private 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;
}
private ArrayList<MusicCd> searchForRemoveCdsTitle(String searchString)
{
MusicCd tempCd =new MusicCd();
tempCd.setTitle(searchString);
ArrayList<MusicCd> removeCdsResult =new ArrayList<MusicCd>();
for(MusicCd currentMusicCd : MusicCdList)
{
if((currentMusicCd.getTitle()).equals(tempCd.getTitle()))
removeCdsResult.remove(currentMusicCd);
}
removeCdsResult.trimToSize();
return removeCdsResult;
}
}
//import java.io.Serializable;
publicclass MusicCd
{
private String musicCdsTitle;
privateint yearOfRelease;
public MusicCd()
{
musicCdsTitle ="";
yearOfRelease = 1900;
}
public MusicCd(String newMusicCdsTitle)
{
musicCdsTitle = newMusicCdsTitle;
//yearOfRelease = newYearOfRelease;
}
public MusicCd(String newMusicCdsTitle,int newYearOfRelease)
{
musicCdsTitle = newMusicCdsTitle;
yearOfRelease = newYearOfRelease;
}
public String getTitle()
{
return musicCdsTitle;
}
publicint getYearOfRelease()
{
return yearOfRelease;
}
publicvoid setTitle(String newMusicCdsTitle)
{
musicCdsTitle = newMusicCdsTitle;
}
publicvoid setYearOfRelease(int newYearOfRelease)
{
yearOfRelease = newYearOfRelease;
}
/*
public boolean equalsName(MusicCd otherCd)
{
if(otherCd == null)
return false;
else
return (musicCdsTitle.equals(otherCd.musicCdsTitle));
}
*/
public String toString()
{
return("Music Cd`s Title: " + musicCdsTitle +"\t"
+"Year of release: " + yearOfRelease +"\t");
}
}
import java.util.*;
publicclass readOperation{
public String readString(String userInstruction)
{
String aString =null;
try
{
Scanner scan =new Scanner(System.in);
System.out.print(userInstruction);
aString = scan.nextLine();
}
catch (NoSuchElementException e)
{
//if no line was found
System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);
}
catch (IllegalStateException e)
{
// if this scanner is closed
System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
}
return aString;
}
publicchar readTheFirstChar(String userInstruction)
{
char aChar =' ';
String strSelection =null;
try
{
//char charSelection;
Scanner scan =new Scanner(System.in);
System.out.print(userInstruction);
strSelection = scan.next();
aChar = strSelection.charAt(0);
}
catch (NoSuchElementException e)
{
//if no line was found
System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);
}
catch (IllegalStateException e)
{
// if this scanner is closed
System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
}
return aChar;
}
publicint readInt(String userInstruction){
int aInt = 0;
try{
Scanner scan =new Scanner(System.in);
System.out.print(userInstruction);
aInt = scan.nextInt();
}catch (InputMismatchException e){
System.out.println("\nInputMismatchException error occurred (the next token does not match the Integer regular expression, or is out of range) " + e);
}catch (NoSuchElementException e){
System.out.println("\nNoSuchElementException error occurred (input is exhausted)" + e);
}catch (IllegalStateException e){
System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
}
return aInt;
}
}
import java.util.*;
publicclass MusicCdStoreEngine{
publicstaticvoid main(String[] args)
{
MusicCdStore mcs =new MusicCdStore( );
mcs.insertCd();
//display the Cd that you just insert
mcs.displayAllCd();
mcs.removeCd();
mcs.displayAllCd();
//mcs.searchingMusicCd();
}
}
//Acutally result
//Please enter your CD`s title : ivan
//Please enter your CD`s year of release : 1992
//Do you have another Cd ? (Y/N) : y
//Please enter your CD`s title : hero
//Please enter your CD`s year of release : 1992
//Do you have another Cd ? (Y/N) : n
//Our CD collection is:
// Music Cd`s Title: ivanYear of release: 1992
//Music Cd`s Title: heroYear of release: 1992
//Please enter CD `s title you are going to remove : hero
//The CD that you just removed is hero
//
//Our CD collection is:
// Music Cd`s Title: ivanYear of release: 1992
//Music Cd`s Title: heroYear of release: 1992
//
//Enter a CD `s Title you are going to search : hero
//The search results for hero are:
//Music Cd`s Title: heroYear of release: 1992
//>Exit code: 0
/////////////////////////////////////////////////////
//Expected result
//Please enter your CD`s title : ivan
//Please enter your CD`s year of release : 1992
//Do you have another Cd ? (Y/N) : y
//Please enter your CD`s title : hero
//Please enter your CD`s year of release : 1992
//Do you have another Cd ? (Y/N) : n
//Our CD collection is:
// Music Cd`s Title: ivanYear of release: 1992
//Music Cd`s Title: heroYear of release: 1992
//Please enter CD `s title you are going to remove : hero
//The CD that you just removed is hero
//
//Our CD collection is:
// Music Cd`s Title: ivanYear of release: 1992
//Music Cd`s Title: heroYear of release: 1992<<-- it is not supposed to display cos i have deleted it from from array
//
//Enter a CD `s Title you are going to search : hero
//The search results for hero are:
//Music Cd`s Title: heroYear of release: 1992<<-- i should have get this reuslt...cos it is already delete from my array
//>Exit code: 0

