My productlist class
Code:package AssignmentTwo; 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); Product prod3 = new Product("5248" , "trumpet", 50); pr.add(prod1); pr.add(prod2); pr.add(prod3); Iterator it = pr.it(); while (it.hasNext()) { Product p = (Product)it.next(); System.out.println(p.toString()); } } }




Reply With Quote