Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default Return ArrayList function - Java

    Interface - Product

    Java Code:
    package fpt.com;
    
    import java.io.Serializable;
    
    public interface Product extends Serializable {
    	
    
    	long getId();
    
    	double getPrice();
    
    }

    Interface - ProducktList

    Java Code:
    package fpt.com;
    
    import java.io.Serializable;
    import java.util.List;
    
    
    public interface ProductList extends List<Product> , Serializable {
    	
    
    	@Override
    	boolean add(Product e);
    	
    
    	
    }

    Interface - Order

    Java Code:
    package fpt.com;
    
    
    public interface Order extends ProductList {
    	
    
    	
    	/**
    	 * @return The ProductList of the Order
    	 */
    	ProductList getProducts();
    }
    Class - ProductList

    Java Code:
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.List;
    import java.util.ListIterator;
    
    import fpt.com.Product;
    
    
    public class ProductList extends ArrayList<Product> implements fpt.com.ProductList{
    
    
    	public boolean add(Product e)
    	{
    
    		
    	    super.add(e);
    		return true;
    	}
    }


    Class - Order

    Java Code:
    
    import java.util.ArrayList;
    
    import fpt.com.Product;
    import fpt.com.ProductList;
    
    
    
    public class Order  extends ArrayList<Product>  implements fpt.com.Order {
    
    
    	
    	/**
    	 * @return The ProductList of the Order
    	 */
    	public ProductList getProducts()
    	{
    
    		
    	}


    My question is: How to write the function getProducts() in the class Order. I want the function to return the list of products,but i have no idea how to implement this function.I know that it is something very trivial,but I don't know hot to do it.
    Last edited by skp123; 04-29-2011 at 01:03 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The Order class should probably have a field that is a concrete implementation of the ProductList interface, and then you could simply return that field.

  3. #3
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    When I try this ,Eclipse says:Cannot instantiate the type ProductList

    Java Code:
    	public ProductList getProducts()
    	{
    		
    		fpt.com.ProductList list = new ProductList();
    
    	}

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What is the exact error? You have an interface and a class named ProductList, I don't really know if this is such a good idea.

    Edit: I am wondering if you are forced to do all that implementation and inheritance. It seems very unnecessary since you aren't doing anything the classes don't already do for you.
    Last edited by sunde887; 04-29-2011 at 01:24 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by skp123 View Post
    When I try this ,Eclipse says:Cannot instantiate the type ProductList

    Java Code:
    	public ProductList getProducts()
    	{
    		
    		fpt.com.ProductList list = new ProductList();
    
    	}
    Eclipse (or Java) is correct, because ProductList is an interface, not a concrete class, and you can't instantiate an interface or an abstract class, which is why I suggested you use a concrete class in my recommendation above. So first you probably should create a class that implements ProductList, and then create a field of this type in your Order class, and then return the instance in your getProducts() method.

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    The problem is, you have an interface and a class called ProductList. I suggest removing the import of fpt.com.ProductList in the Order.java file.Then you would change the return to fpt.com.ProductList

  7. #7
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Ok,so let me get this straight. I create a class named "Shop".

    Java Code:
    public class Shop {
    
    Product z = new Product();
    
    ProductList list = new ProductList();
    
    z.setPrice(10.3);
    z.setId(100000);
    list.add(z);
    
    }
    And now what to do ?

  8. #8
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    What do you want/need to do? If you have a specific problem then ask us. A question like "What should I do now?" is not quite specific and helpful since we don't know what you're up to ;)

  9. #9
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    well to go to your code, let me sort of walk you through if it might help.

    You have interface called ProductList. This is fine. When you implement this interface you should name it something else e.g.

    class oProductList implements ProductList

    and write the implementing code. After writing the implementing code you can create objects of that class i.e. oProductList

    and you ask now what do i do... well you tell us :) what you want to do?

  10. #10
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Ok,so in the class Shop I created a list with one product. I want the function getProducts() to return this list. That's what I am trying to do.

  11. #11
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    First of all, that code needs to be in a constructor/method. Second of all, the "list" variable is right there. Why do you want to call getProducts() when you already have access to the the ProductList object?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why are you using all of these interfaces? Myself I'd simply create an ArrayList<Product>. Is that a stipulation of some assignment? If so, you might want to post the entire assignment.

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Like I said above, I edited in so you might have missed it.

    It seems like you just did a workaround so you would have to display the parameterized generic argument in arraylist. You could have the product class maintain an arraylist of products in a much simpler and more straightforward way.

  14. #14
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Yes,it is an assignment.I am forced to do it this way.

    I have a package called fpt.com with interfaces

    Java Code:
    Product 
    
    package fpt.com;
    
    import java.io.Serializable;
    
    public interface Product extends Serializable {
    	
    	/**
    	 * 
    	 * @return The id for the product.
    	 */
    	long getId();
    
    	/**
    	 * @return The price of the product
    	 */
    	double getPrice();
    	
    	
    	/**
    	 * Alters the price of the product.
    	 * @param price The new price
    	 */
    	void setPrice(double price);
    	}
    
    
    ProductList
    
    
    package fpt.com;
    
    import java.io.Serializable;
    import java.util.List;
    
    
    public interface ProductList extends List<Product> , Serializable {
    	
    	/**
    	 *
    	 * Adds a Product to the Order and recalculates the sum;
    	 * @param e the product
    	 */
    	@Override
    	boolean add(Product e);
    	
    	Product  findProductById(int id);
    	
    	Product  findProductByName(String name);
    	
    }
    
    
    Order 
    
    
    package fpt.com;
    
    
    public interface Order extends ProductList {
    	
    	/**
    	 * @return The sum of the Order
    	 */
    	double getSum();
    	
    	/**
    	 * @return The ProductList of the Order
    	 */
    	ProductList getProducts();
    	
    	
    	/**
    	 * @return The overall quantity of Products in the Order
    	 */
    	int getQuantity();
    	
    }
    This is the task

    Create classes "Product, ProductList and Order ",which implement the interfaces from the package fpt.com.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So your first order of business is to create concrete classes from each interface, i.e.,

    Java Code:
    class ConcreteProduct implements Product {
     //.... 
    }
    
    class ConcreteProductList implements ProductList {
      //....
    }
    
    etc...

  16. #16
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Well,I did it

    Java Code:
    
    public class Product implements fpt.com.Product{
    
    	private double price;
    	private int quantity;
    	private long id;
    
    
    	
    	public long getId()
    	{
    		return this.id;
    	}
    
    
    	public double getPrice()
    	{
    		return this.price;
    	}
    	
    	public void setId(long id)
    	{
    		this.id=id;
    	}
    
    public	void setPrice(double price)
    	{
    		this.price=price;
    	}
    	
    }
    
    
    
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.List;
    import java.util.ListIterator;
    
    import fpt.com.Product;
    
    
    public class ProductList extends ArrayList<Product> implements fpt.com.ProductList{
    
    	private static final long serialVersionUID = 1L;
    	int idIndex;
    	 int nameIndex;
    	 
    
    	public boolean add(Product e)
    	{	
    	    super.add(e);
    		return true;
    	}
    	
    public 	Product  findProductById(int id){
    
      Product x;
    		for(int i=0;i<this.size();i++)
    			{
    			if (super.get(i).getId()==id)
    				{
    				idIndex=i;
    				break;
    				}
    			}
    		x=get(idIndex);
    		return x;
    
    	}
    		
    
    public	Product  findProductByName(String name){
    		  Product x;	
    
    			for(int i=0;i<this.size();i++)
    				{
    				if (super.get(i).getName()==name)
    					{
    					nameIndex=i;
    					break;
    					}
    				}
    			x=get(nameIndex);
    			return x;
    	}
    
    }
    
    
    
    import java.util.ArrayList;
    
    import fpt.com.Product;
    
    
    public class Order  extends ArrayList<Product>  implements fpt.com.Order {
    
    	
    	/**
    	 * @return The sum of the Order
    	 */
    	public double getSum()
    	{
    		double sum=0;
    
    			for(int i=0;i<this.size();i++)
    				sum=sum+super.get(i).getPrice();
    
    		return sum;
    		
    	}
    	
    	/**
    	 * @return The ProductList of the Order
    	 */
    	public ProductList getProducts()
    	{
    		//ProductList list = new ProductList();
    
    	}
    
    }



    And now I'm having difficulty writing the getProducts() function
    Last edited by skp123; 04-29-2011 at 02:17 AM.

  17. #17
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Shouldn't the Order class have an instance variable of ProductList? Otherwise, what's the point of that class? And why are you extending ArrayList in Order too?

  18. #18
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Yes it should have an instance variable of ProductList.And it should be like this

    Java Code:
    import java.util.ArrayList;
    
    import fpt.com.Product;
    
    
    public class Order extends ArrayList<Product> implements fpt.com.Order {
    
    	public ProductList list = new ProductList();
    	
    	/**
    	 * @return The sum of the Order
    	 */
    	public double getSum()
    	{
    		double sum=0;
    
    			for(int i=0;i<list.size();i++)
    				sum=sum+list.get(i).getPrice();
    		return sum;
    		
    	}
    	
    	/**
    	 * @return The ProductList of the Order
    	 */
    	public ProductList getProducts()
    	{
    
    		return list;
    	
    	}
    
    }


    And thank you for asking about Order extending ArrayList. If I don't extend ArrayList , Eclipse makes me implement all the inherited List<Product> functions.

  19. #19
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    But why even extend it? I don't see you using any of its methods or using Order as an ArrayList.

  20. #20
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Yes,you are absolutely right.There is no sense to extend ArrayList,because i don't need it in Order .But if I do it this way,red cross shows up on the left saying

    Multiple markers at this line
    - The type Order must implement the inherited abstract method List<Product>.get(int)
    - The type Order must implement the inherited abstract method List<Product>.indexOf(Object)

    ...
    Java Code:
    
    import java.util.ArrayList;
    
    import fpt.com.Product;
    
    
    public class Order  implements fpt.com.Order {
    
    	public ProductList list = new ProductList();
    	
    	/**
    	 * @return The sum of the Order
    	 */
    	public double getSum()
    	{
    		double sum=0;
    
    			for(int i=0;i<list.size();i++)
    				sum=sum+list.get(i).getPrice();
    		return sum;
    		
    	}
    	
    
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Help in test function for ArrayList
    By swalf in forum New To Java
    Replies: 5
    Last Post: 02-18-2011, 06:14 PM
  2. Filling an array from the return value of the function
    By alex1988 in forum Java Applets
    Replies: 7
    Last Post: 02-02-2011, 09:29 AM
  3. getSession() Function Return Null value
    By dalchndr@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 11-04-2009, 07:24 AM
  4. Search function for ArrayList?
    By javanoobie in forum New To Java
    Replies: 11
    Last Post: 04-17-2009, 08:38 PM
  5. how to return an object from an arraylist
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 06:57 PM

Posting Permissions

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