PDA

View Full Version : array as class noob question


katalyst
03-27-2009, 02:06 AM
wow, normally i pick stuff up right away, but im struggling with this little problem in java..

i need to create an array as a class data type. the 'candy' array will have 4 fields, id, item name, weight, and price (all strings). i want the user to specify the array size instead of hard-coding it and then all i want is to be able to output the 4 records, one item in the array at a time.

seems so easy, but im brain-farting on the syntax and how to setup the classes and methods inside them....

can someone PLEASSSE help get me pointed in the right direction? here is what i have so far:


import staugIO.StaugIO; //import StaugIO() class

public class Program {

StaugIO io = new StaugIO();

public static void main(String[] args) {

//app code
}
}



class CaptureData {

//Declare private variables
private String itemNumber;
private String itemName;
private String itemWeight;
private String itemPrice;


}//End captureData

class ProcessData {

//Declare private variables
private int size, index;
private ProcessData[] candy;
private StaugIO io;


//First Level of Refinement Methods
public void getContent(){
size = readInt("How many items do you want to enter?");

item = new ProcessData[size] // give the null array form, dimension

for(index = 0; index < size; ++index){
item[index] = new NameOfClass();
item[index].nameOfFirstSetMethod();
item[index].nameOfSecondSetMethod();
}
displayContent();
return
}


public void displayContent(){
//type a String object to store content of array
String stringObject = item[];
for(index = 0; index < size; index ++){
nameOfStringObject = "<HTML>" + "CANDY NO. " + (index + 1) + "<br><br>" + "ITEM NO:&nbsp;&nbsp;" + item[index].nameOfFirstGetMethod() + "<br>" + "WT:&nbsp;&nbsp;" + item[index].nameOfSecondGetMethod() + "<br>" + "</HTML>";
io.writeInfo(stringObject);//print String object
}
return
}
} //End ProcessData


excuse some of my pseudocode... ANY help is greatly appreciated

servlet
03-27-2009, 07:30 AM
You can create a class 'candy' and create as many instances as you need and store them in map or array.

katalyst
03-28-2009, 09:14 PM
what would the syntax be to create the array as a class data type

this is where I'm getting confused... also, how would i set up the header for the class: class getInfo{} ? and class processInfo{}

once i get those two classes setup, do i just call getInfo.method()?

thanks!