Hi all,
Ok i've started doing a mini project about a CD collection database.
i got the program working ata stage where you can enter a single entry and the record is printed to the screen, the problem im having is i can not seem to modify the program to store an array of CD 'objects' . Each entry in the array needs to be a single object describing a CD.
I also need to add a menu of options like:

1: Input New Entry
2: Print All Entries
3: Quit

if anyone can help i would really apreciate the help.

below is my program of where i have got to so far::
If you copy and paste this you will see it works.

import javax.swing.*;


public class CdStorage
{
    public static void main (String[] args)

    {
        int menu_choice;


        CdRecord one = new CdRecord();

one.artist_name = JOptionPane.showInputDialog("Enter artist name.");
one.album_name = JOptionPane.showInputDialog("Enter album name.");
one.no_of_tracks =Integer.parseInt(JOptionPane.showInputDialog("Enter the number of tracks on the album"));


        one.printCdRecord();

        }//end main

    } // end class CdStorage




 class CdRecord
{
    public String artist_name;
    public String album_name;
    public int no_of_tracks;


    public CdRecord (String artist, String album, int tracks, int year)

    {

        artist_name = artist; //stores first argument passed into artist_name
        album_name = album; //stores second argument passed into album_name
        no_of_tracks = tracks; //stores third argument passed into no_of_tracks


        }


        public CdRecord()

        {
        artist_name = "A"; 
        album_name = "B"; 
        no_of_tracks = 0; 
            }

    public void printCdRecord ()
    {
        String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;

        System.out.println(o);
    }
    }//end class cdstorage

well, first you need to use ArrayList class, this class enables you to add and delete elements from it, check out API documentation.
after you hava an ArrayList just use Add method to add an element to your ArrayList...
an example :

ArrayList CDArrayList = new ArrayList(); //simple creation of ArrayList
CDArrayList.add(one); //"one" from your code

then when you want to use them again in your code, you typecast "object" to "CDRecord" like this,

( (CDRecord)CDArrayList.get(0) ).printRecord();

Apreciate the help but truth is i have no idea bout how to apply the changes .
Im a newbie in java and was able to get thus far by reading a java guide, but i dont understand ne further than what i have known......

well there are some points you need to fix, before going on to other subject,
generally in oop we don't make our variables "public" so make them "private" then you need to change the way you create your CDRecord object, like

CDRecord one = new CDRecord(artist, album, tracks); //you need to create artist, album and tracks variables like you did

well, i could re-write the code but that doesn't give anything, hope i could help by trying to give you some clues :)

cheers for tha help.
imma try it in a lil while..

well u could modify my program and highlight it ,, see if i can make an understanding.. lol

OK i've tried wat u said. But some weird reason i keep gettin errors for the arrays i create, tried different solutions none help. :sad:

well, let me give you a basic object creation example, and then you convert it to your code

//this is simple point class, stores two integer, which are read-only.
//so you need to specify these two integer while object creation....
public class point {
  private int x;
  private int y;

  public point(int x, int y) {
    this.x = x;
    this.y = y;  
  }
  public int getx() {
    return x;
  }

  public int gety() {
    return y;
  }

//somewhere in your main method
point testpoint = new testpoint(50,100);
System.out.println("testpoint location : " + testpoint.getx() + ", " + testpoint.gety() );

//you can also pass variables as arguments....
int x;
int y;
point testpoint2; //we only declare point, we define it later...
x = Integer.parseInt(JOptionPane.showInputDialog("X-coordinate?"));
y = Integer.parseInt(JOptionPane.showInputDialog("Y-coordinate?"));

//here comes the definition
testpoint2 = new point(x,y);
System.out.println("testpoint2 location : " + testpoint.getx() + ", " + testpoint.gety() );
}

this is basic creation of an object... practice this, then you can start creating objects and putting them into arrays.... :)

ok dude i tried the simple object coding u gave, but i keep getting errors. 6 to be exact, Identifier expected etc.......

nething pleez!!!
im gettin kind of desperate for help now!! :sad:

well, it was not the actual code, so copy & paste won't work (:
ok, here is the full!!! (:

point.java :

//this is simple point class, stores two integer, which are read-only.
//so you need to specify these two integer while object creation....
public class point {
  private int x;
  private int y;

  public point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public int getx() {
    return x;
  }

  public int gety() {
    return y;
  }
}

pointUser.java :

//this class simply creates a point, display something...
public class pointUser {
	public static void main(String[] args) {
		//some variables...
		int x,y;
		point mypoint_one = new point(15,20); //decleration and definition
		point mypoint_two; //only decleration

		System.out.println("mypoint_one : "+mypoint_one.getx()+ ", "+ mypoint_one.gety() );
		x=100;
		y=1;
		mypoint_two = new point(x,y);
		System.out.println("mypoint_two : "+mypoint_two.getx()+ ", "+ mypoint_two.gety() );
	}
}

simply create these two files, and compile pointUser, then run pointUser...
it is very simple, in this example you create points, which have read-only properties x and y. you can't hava direct access to these members, but you can get their values by calling

mypoint_one.getx(); //returns mypoint_one.x
mypoint_one.gety(); //returns mypoint_one.y

you can (if you want) give write-access by writing "setter" methods

public void setx(int x) {
  this.x = x;
}
public void sety(int y) {
  this.y = y;
}

try putting these two setter methods to point.java and then add these lines inside your "main" method in pointUser.java

mypoint_one.setx(50);
mypoint_one.sety(10);
System.out.println("mypoint_one : "+mypoint_one.getx()+ ", "+ mypoint_one.gety() );

then recompile and run pointUser.
so why are we using this? instead just making everything "public"?
because we want controlled access to members of objects, with these "getters" and "setter" we can have control over members, if we want read-only we make only getter. if we want read-write, we make getter and setter, and so on (:
another thing is construction of point, point(int, int) constructor is simple, it just assign starting values... because we don't have setters in the point class, only place we can assign values is constructors.

well, try to adopt this point class to your CDRecord class, I think this will be a good excercise...
then when you feel comfortable with simple classes, you can move to array stuff...

well, hope this helps....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.