开发者

Java Generics Assignment

I have a Assignment question about Java Generics. The given class is Product.java. It has abstract class Product, then ComputerPart, Peripheral, Fruit, Cheese, and Service extends Product.

  1. Carefully study the class structure in Products.java.
  2. Design a generic container called GenericOrder that acts as a collection of an arbitrary number of objects in Products.java. Design a mechanism that gives each instance of the container a unique identifier. Implement as many methods as necessary. You must use Java generics features.
  3. Design and implement a subclass of GenericOrder called ComputerOrder that takes an arbitrary number of different classes of ComputerPart objects, Peripheral objects, and Service objects. Implement as many methods as necessary.
  4. Design and implement a subclass of GenericOrder called PartyTrayOrder that takes an arbitrary number of different classes of Cheese objects, Fruit objects, and Service objects. Implement as many methods as necessary.
  5. Design and implement a class called OrderProcessor. You must implement at least the following methods:

    accept; // this method accepts a GenericOrder or any of its subclass objects and stores it in any internal collection of OrderProcessor.
    
    
    process; // this method sorts all accepted orders in the internal collection of GenericOrder into collections of ComputerPart, Peripheral, Cheese, Fruit, and Service. You must associate each object with the unique identifier. You may refer to the TwoTuple.java example in the text book.
    
    
    dispatchXXX; // this method simulates the dispatch of the sorted collections. For example, the method dispatchComputerParts() should produce this output:
    
    
    Motherboard  name=Asus, price=$37.5, order number=123456
    
    
    Motherboard  name=Asus, price=$37.5, order number=987654
    
    
    RAM  name=Kingston, size=512, price=$25.0, order number=123456
    
    
    You may overload each of the above methods as you think necessary.
    
  6. Create a client class to test OrderProcessor. You will need to create a datagenerator for testing purpose. It is not mandatory but you may use a variation of Data Generator in TIJ pages 637 to 638.

  7. Pack the above codes into one single zip file and send it to your tutor.

I have done the 1 requirment, but I have trouble to understand 2nd one. Here is the code I did:

public class GenericOrder<T> {
    private static long counter = 1;
    private final long id = counter++;
    private List<T> products;
    private T theClass;

    public GenericOrder() 
    {
        products = new ArrayList<T>();
    }

    public long getId() {
        return id;
    }

    public void addProduct(T newProduct) {
        products.add(newProduct);

    }

    public int getNumberOfProducts() {
        return products.size();
    }

    public List<T> getProducts()
    {
        return products;
    }

    public void setProducts(List<T> products)
    {
        this.products = products;
    }

    public T ge开发者_运维百科t()
    {
        return theClass;
    }

    public void set(T theClass)
    {
        this.theClass = theClass;
    }
}
public class ComputerOrder<T> extends GenericOrder<T> {
    private List<T> products; 
    private T theClass;

    public ComputerOrder() 
    {
        products = new ArrayList<T>();
    }


    public void addProduct(T newProduct) {
        products.add(newProduct);

    }

    public int getNumberOfProducts() {
        return products.size();
    }

    public List<T> getProducts()
    {
        return products;
    }

    public void setProducts(List<T> products)
    {
        this.products = products;
    }

    public T get()
    {
        return theClass;
    }

    public void set(T theClass)
    {
        this.theClass = theClass;
    }
}

Please help me out here, see if anyone understand 2nd requirement better than I do. Thanks all.


I think part two is looking for a class that has an upper bound type restriction, so perhaps something like this:

public class ComputerOrder<T extends Product> extends GenericOrder<T> {
    // etc...
}

Edit

I missed the Fruit and Cheese classes. In order for type bounding to work, you would most likely need to code your computer products to an interface and use the following:

public class ComputerOrder<T implements MyInterface> extends GenericOrder<T> {
    // etc...
}

For more information on bounded type parameters, read here.


Tyler beat me to it, but I just wanted to add that you don't need to override all those methods... they're already provided for you in the base class.

What you should add are methods that are common to the ComputerParts and Peripherals, etc.


Try this:

interface CO{} //for computer orders 
interface PTO{} //party tray orders
        
class ComputerPart implements CO{} 
class Peripheral implements CO{}      
class Fruits implements PTO{} 
class Cheese implements PTO{}     
class Service implements CO, PTO{}
public static class ComputerOrders<T extends CO> extends GenericOrder{}  
public static class PartyTrayOrder <T extends PTO> extends GenericOrder {}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜