OK, i figured out what i was doing wrong. I didnt have an it() method. Now it works. Thanks
here is the finished code for both my methods.Code:public Iterator it() { return products.iterator(); }
first Product.java
and now my productList.javaCode:/** * Title: Assign 2 * @author Matthew Davis * School SECC * Class Advanced Java */ public class Product{ private String code; private String name; private double price; public Product(){ } public Product(String code, String name, double price) { this.code = code; this.name = name; this.price = price; } public void setCode(String c) { code = c; } public String getCode() { return code; } public void setName(String n) { name = n; } public String getName() { return name; } public void setPrice(double p) { price = p; } public double getPrice() { return price; } public String toString() { return code + "\t" + name + "\t" + price + "\t"; //return code + " " + name + " " + price; } }
Code:/** * Title: Assign 2 * @author Matthew Davis * School SECC * Class Advanced Java */ import java.util.*; public class ProductList extends Product{ ArrayList products = new ArrayList(); public ProductList() { } public void add(Product p) {//add a product to the list products.add(p); /*String key = ((Product)p).getCode(); if(find(key) == null) { products.add(p); }*/ } public void add(String id, String name, double price) {//create a Product and add it to the list for(int i = 0 ; i<products.size(); i++) { Product prod1 = new Product(id,name,price); products.add(prod1); } } public int size() {//return how many are in the list return products.size(); } public Object get(int index) {//return the Product at position index return products.get(index); } public void remove(int index) {//remove the Product at position index products.remove(index); } public Object find(String find) {//return a Product based on the ID# Iterator it = products.iterator(); while(it.hasNext()) { Product q = (Product)it.next(); if (q.getCode().equals(find)) { return q; } } return null; } public Iterator it() { return products.iterator(); } public static void main(String[] args) { ProductList pr = new ProductList(); Product prod1 = new Product("5248","trumpet" , 50); Product prod2 = new Product("5420","Saxaphone" , 100); pr.add(prod1); pr.add(prod2); Iterator it = pr.it(); while (it.hasNext()) { Product p = (Product)it.next(); System.out.println(p.toString()); } } }




Reply With Quote