Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Alright my Java program needs to be an In Memory Data Base where the user and ad

ID: 3629979 • Letter: A

Question

Alright my Java program needs to be an In Memory Data Base where the user and add find display and remove CDS from it. I am having issues with adding more than 1 CD, doing the actual removing of the CD, and displaying more than 1 CD(I only say this is the problem because it only displays the same CD infer twice).

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.
[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;
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 q for quit d for display f for find a for add r 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;
}
}
}

}
[/CODE]
[CODE]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 " CD Title: " + cd + " Artist: " + artist + " Total Track Number: "+tracknum+" Copyright Year: "+cright;
}

}
[/CODE]
[CODE]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 ");
}
}

}
}
[/CODE]

Explanation / Answer

It looks fine.
The biggest issue is your display I believe. I think you are adding the CDs but you have the following code:

public void display() {

for (int i = 0; i < count; i++) {

System.out.println(numofcd[numCD].toString(numCD));

}

}

Hopefully you see the error that you have to use the index "i" to actually go through the cds! Also in your constructor for MusicDB you don't actually set numCD (instance variable) to the fed in numCD (argument to constructor).

You probably need this line:

this.numCD = numCD

In the constructor for MusicDB

Another thing is why does your MusicCD class have toString(int i)?

You don't use the i at all in that method so you probably can just do toString()

One last thing, you might want to consider for deleting a Music CD to possibly setting the spot in the array to NULL and then shifting all CDs after it in the DB forward 1 space rather than just setting the information to blanks or 0's.

Other than that you're set I believe!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote