Results 1 to 13 of 13

Thread: Need help bad!!

Threaded View

  1. #13

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Re: Need help bad!!

    OK, i figured out what i was doing wrong. I didnt have an it() method. Now it works. Thanks

    Code:
    public Iterator it()
        {
            return products.iterator();
        }
    here is the finished code for both my methods.

    first Product.java
    Code:
    /**
     * 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;
    		}
    
    
    }
    and now my productList.java

    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());
                }
       
    			
    		}
    	}
    Last edited by ddmeightball; Feb 3rd, 2005 at 11:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width