Alright so like the title says I am creating an In memory CD database that has to have the abilities to add find remove and display information about the cds. So far I have been able to accomplish the adding and the finding but am having trouble doing remove and display. I am stuck and can't seem to get it to remove or display.
On a side not I would love it if someone could explain what this little bit of code means,I found the base for it on the internet and then tweaked it so that it could fit my code but don't know what it means.public void find (String search){ boolean found = false; for (MusicCD cds : numofcd ) { if (search.equals(cds.getCD())) { found = true; System.out.println("Your search was found "); break; } else{ System.out.println("Your search could not be found try again "); } }
Here is the code for my program.import java.util.Scanner; public class TestMusicDB { /** * @author Alexander Chamerlaion * @Version 1.0 * @Date 10/5/11 * This program creates a DB and stores values in it * * */ public static void main(String[] args) { System.out.println("Music Database"); Scanner input = new Scanner(System.in); String in; MusicDB mydb= new MusicDB(); while(true){ System.out.println("Welcome to your personal music database. " + "The legal commands are \nq for quit\nd for display\nf for find\na for add\nr for remove"); System.out.println("What is your command"); in=input.nextLine(); in.toLowerCase(); if (in.equals("d")){ mydb.display(); } else if(in.equals("f")){ System.out.println("Please enter the title of the CD you would like to search for: "); String search = input.nextLine(); search.toLowerCase(); mydb.find(search); } else if(in.equals("a")){ System.out.println("Please enter title of CD: "); String cdName = input.nextLine(); System.out.println("Please enter artist name: "); String artistName = input.nextLine(); System.out.println("Please enter total track number: "); int tnum = 0; tnum = input.nextInt(); System.out.println("Please enter copyright year: "); int year = 0; year = input.nextInt(); MusicCD newCD = new MusicCD(cdName, artistName,tnum,year); newCD.setCD(cdName); newCD.setArtist(artistName); newCD.setTracknum(tnum); newCD.setCright(year); mydb.add(newCD); } else if(in.equals("r")){ System.out.println("Please enter the title of the album you want to remove (must be how you entered it) "); String remove = input.nextLine(); if(remove.equals(mydb)){ //newCD.setCD(""); //newCD.setArtist(""); //newCD.setTracknum(0); //newCD.setCright(0); } else{ System.out.println("The title you entered didn't match anything saved"); } } else if(in.equals("q")){ System.out.println("Goodbye."); break; } } } }import java.util.Scanner; public class TestMusicDB { /** * @author Alexander Chamerlaion * @Version 1.0 * @Date 10/5/11 * This program creates a DB and stores values in it * * */ public static void main(String[] args) { System.out.println("Music Database"); Scanner input = new Scanner(System.in); String in; MusicDB mydb= new MusicDB(); while(true){ System.out.println("Welcome to your personal music database. " + "The legal commands are \nq for quit\nd for display\nf for find\na for add\nr for remove"); System.out.println("What is your command"); in=input.nextLine(); in.toLowerCase(); if (in.equals("d")){ mydb.display(); } else if(in.equals("f")){ System.out.println("Please enter the title of the CD you would like to search for: "); String search = input.nextLine(); search.toLowerCase(); mydb.find(search); } else if(in.equals("a")){ System.out.println("Please enter title of CD: "); String cdName = input.nextLine(); System.out.println("Please enter artist name: "); String artistName = input.nextLine(); System.out.println("Please enter total track number: "); int tnum = 0; tnum = input.nextInt(); System.out.println("Please enter copyright year: "); int year = 0; year = input.nextInt(); MusicCD newCD = new MusicCD(cdName, artistName,tnum,year); newCD.setCD(cdName); newCD.setArtist(artistName); newCD.setTracknum(tnum); newCD.setCright(year); mydb.add(newCD); } else if(in.equals("r")){ System.out.println("Please enter the title of the album you want to remove (must be how you entered it) "); String remove = input.nextLine(); if(remove.equals(mydb)){ //newCD.setCD(""); //newCD.setArtist(""); //newCD.setTracknum(0); //newCD.setCright(0); } else{ System.out.println("The title you entered didn't match anything saved"); } } else if(in.equals("q")){ System.out.println("Goodbye."); break; } } } }import java.util.Scanner; public class MusicCD { private String cd; private String artist; private int tracknum; private int cright; public MusicCD(String cdName, String artistName, int tnum, int year){ cd=cdName; artist=artistName; tracknum=tnum; cright=year; } public String getCD(){ return cd; } public void setCD(String cdName){ this.cd=cd; } public String getArtist(){ return artist; } public void setArtist(String artistName){ this.artist=artist; } public int getTracknum(){ return tracknum; } public void setTracknum(int tnum){ } public int getCright(){ return cright; } public void setCright(int year){ this.cright=cright; } }