Hey guys,
I'll post the code, and then the error message below. Essentially I'm getting an error message on the add and it's a bit confusing, since I've imported ArrayList and add is a method in there as well.
My question: What am I missing? If someone could post what I'm missing, I'd appreciate it. No trolls please.
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
public class CDCollection {
private ArrayList collection = null;
private double totalCost;
//-----------------------------------------------------------------
// Constructor: Creates an initially empty collection.
//-----------------------------------------------------------------
public CDCollection() {
collection = new ArrayList(100);
totalCost = 0.0;
}
//-----------------------------------------------------------------
// Adds a CD to the collection, increasing the size of the
// collection if necessary.
//-----------------------------------------------------------------
public void addCD(String title, String artist, double cost, int tracks) {
collection.add(new CD(title, artist, cost, tracks));
totalCost += cost;
}
//-----------------------------------------------------------------
// Returns a report describing the CD collection.
//-----------------------------------------------------------------
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
report += "My CD Collection\n\n";
report += "Number of CDs: " + collection.size() + "\n";
report += "Total cost: " + fmt.format(totalCost) + "\n";
if (collection.size() != 0) {
report += "Average cost: " + fmt.format(totalCost / collection.size());
}
report += "\n\nCD List:\n\n";
report += CDCollection.createReport(collection);
return report;
}
/**
*
* @param al
* @return
*/
public static String createReport(ArrayList al) {
StringBuffer sb = new StringBuffer();
Iterator iter = al.iterator();
while (iter.hasNext()) {
CD aCD = (CD) iter.next();
sb.append(aCD.toString());
sb.append(System.getProperty("line.separator"));
}
return sb.toString();
}
}
and the error message:
CDCollection.java:36: cannot find symbol
symbol: method add(CD)
location: class java.util.ArrayList<java.lang.String>
collection.add(new CD(title, artist, cost, tracks));
Any help would be very appreciated folks.