Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: In Memory CD Database

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default In Memory CD Database

    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,
    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 ");
    			}
    		}
    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.

    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;
    	}
     
    }


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: In Memory CD Database

    Updated Code
    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;
    		int numCD=0;
    		MusicDB mydb= new MusicDB(numCD);
     
    		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);
    				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;
    			}
    		}
    	}
     
    }
    public class MusicDB {
    	private MusicCD[] numofcd;  
    	private int count = 0;
    	public int length;
    	public MusicDB(int numCD){
    		numofcd = new MusicCD[numCD]; 
    		length = numCD;
     
    	}
     
    	public void add(MusicCD cd){
    		if(count < length){
    			numofcd[count] = cd;
    			count++;
    		}
    		else{
    			System.out.println("Your database is full");
    		}
    	}
    	public void display(){
    		for (MusicCD cds : numofcd ) {          		        
    			System.out.println(numofcd[length]+cds.toString());
    		}
    	}
     
    	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 ");
    			}
    		}
     
     
     
    	}
     
     
    	public void remove(String search){
    		boolean found = false;
    		for (MusicCD cds : numofcd ) {          		        
    			if (search.equals(cds.getCD())) {    
    				found = true;
    				System.out.println("Your search was found ");
    				//setArtist("");
    				break;
    			}
    			else{
    				System.out.println("Your search could not be found try again ");
    			}
    		}
     
    	}
    }
    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;
    	}
    	public String toString(){
    		return "\nCD Title: " + cd + " \nArtist: " + artist + " \nTotal Track Number: "+tracknum+" \nCopyright Year: "+cright; 
    		}
     
    }

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: In Memory CD Database

    Quote Originally Posted by Laxman2809 View Post
    On a side not I would love it if someone could explain what this little bit of code 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 ");
    			}
    		}
    It's a method called 'find' which takes the String argument 'search'
    This method would be called like so: find("bob marley");

    It sets the boolean(true/false) variable 'found' to false.

    Then it loops through the list of CDs and if it matches the String 'search' it sets the 'found' variable to true and prints out "Your search was found" in the console.

    break; stops the loop.

    else (It if doesn't find the String), it prints "Your search could not be found try again" to the console.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: In Memory CD Database

    Ok thank you for that explanations, but now I have an issue with my code. My remove function doesn't see to be working. Now I think it has to do with the fact it is not setting that CDs information back to empty or 0. Also it only stores 1 cd informations, even if i enter a second 1 cd with different information.
    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;
    	System.out.println("How many CDs would you like to enter: ");
    	int numCD=input.nextInt();
     
    		MusicDB mydb= new MusicDB(numCD);
     
    		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);
    				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();
    				mydb.remove(remove);
     
    			}
    			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;
    	}
    	public String toString(int i){
    		return "\nCD Title: " + cd + " \nArtist: " + artist + " \nTotal Track Number: "+tracknum+" \nCopyright Year: "+cright; 
    		}
     
    }
    public class MusicDB {
    	private MusicCD[] numofcd;  
    	private int count;
    	public int length;
    	int numCD=0;
     
     
    	public MusicDB(int numCD){
     
    		numofcd = new MusicCD[numCD]; 
    		length = numCD;
     
    		count = 0;
     
    	}
     
    	public void add(MusicCD cd){
    		if(count < length){
    			numofcd[count] = cd;
    			count++;
    		}
    		else{
    			System.out.println("Your database is full");
    		}
    	}
    	public void display(){
    		for(int i = 0; i<count; i++){                         
    			System.out.println(numofcd[numCD].toString(numCD));
    		}
    	}
     
    	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 ");
    			}
    		}
     
     
     
    	}
    	public void remove(String search){
    		boolean found = false;
    		for (MusicCD cds : numofcd ) {          		        
    			if (search.equals(cds.getCD())) {    
    				found = true;
    				System.out.println("Your search was found ");
    				cds.setCD("");
    				cds.setArtist("");
    				cds.setTracknum(0);
    				cds.setCright(0);
    				break;
    			}
    			else{
    				System.out.println("Your search could not be found try again ");
    			}
    		}
     
    	}
    }
    Last edited by Laxman2809; October 7th, 2011 at 11:19 AM.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: In Memory CD Database

    Also another issue seems to be that it after I enter 1 CD it only stores that information

Similar Threads

  1. SwingWorker - max memory?
    By fractalorbit in forum AWT / Java Swing
    Replies: 1
    Last Post: September 15th, 2011, 02:58 PM
  2. Memory Handling -de allocate memory in Java
    By 19world in forum Java Theory & Questions
    Replies: 4
    Last Post: June 15th, 2010, 04:05 AM
  3. Out of Memory Error
    By wasaki in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 31st, 2010, 03:37 PM
  4. free memory of bufferedimage
    By mr_empty in forum Java SE APIs
    Replies: 2
    Last Post: January 19th, 2010, 05:14 AM
  5. Java memory management
    By trueacumen in forum Java Theory & Questions
    Replies: 5
    Last Post: August 12th, 2009, 02:40 AM