Results 1 to 20 of 24
Thread: Return ArrayList function - Java
- 04-29-2011, 12:58 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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
Class - ProductListJava Code:package fpt.com; public interface Order extends ProductList { /** * @return The ProductList of the Order */ ProductList getProducts(); }
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.
-
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.
- 04-29-2011, 01:16 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
When I try this ,Eclipse says:Cannot instantiate the type ProductList
Java Code:public ProductList getProducts() { fpt.com.ProductList list = new ProductList(); }
- 04-29-2011, 01:21 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
-
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.
- 04-29-2011, 01:29 AM #6
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
- 04-29-2011, 01:32 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
Ok,so let me get this straight. I create a class named "Shop".
And now what to do ?Java Code:public class Shop { Product z = new Product(); ProductList list = new ProductList(); z.setPrice(10.3); z.setId(100000); list.add(z); }
- 04-29-2011, 01:35 AM #8
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 ;)
- 04-29-2011, 01:40 AM #9
Member
- Join Date
- Nov 2010
- Posts
- 44
- Rep Power
- 0
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?
- 04-29-2011, 01:41 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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.
- 04-29-2011, 01:43 AM #11
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?
-
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.
- 04-29-2011, 01:43 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-29-2011, 01:53 AM #14
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
Yes,it is an assignment.I am forced to do it this way.
I have a package called fpt.com with interfaces
This is the taskJava 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(); }
Create classes "Product, ProductList and Order ",which implement the interfaces from the package fpt.com.
-
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...
- 04-29-2011, 02:08 AM #16
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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() functionLast edited by skp123; 04-29-2011 at 02:17 AM.
- 04-29-2011, 02:33 AM #17
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?
- 04-29-2011, 02:47 AM #18
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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.
- 04-29-2011, 04:15 AM #19
But why even extend it? I don't see you using any of its methods or using Order as an ArrayList.
- 04-29-2011, 10:10 AM #20
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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; } }
Similar Threads
-
Help in test function for ArrayList
By swalf in forum New To JavaReplies: 5Last Post: 02-18-2011, 06:14 PM -
Filling an array from the return value of the function
By alex1988 in forum Java AppletsReplies: 7Last Post: 02-02-2011, 09:29 AM -
getSession() Function Return Null value
By dalchndr@gmail.com in forum Advanced JavaReplies: 0Last Post: 11-04-2009, 07:24 AM -
Search function for ArrayList?
By javanoobie in forum New To JavaReplies: 11Last Post: 04-17-2009, 08:38 PM -
how to return an object from an arraylist
By elizabeth in forum New To JavaReplies: 1Last Post: 07-30-2007, 06:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks