2

When I try to compile I get recompile with -Xlint unchecked for details. When I run it says: "T extends Product declared in class GenericOrder.

I thought "T" meant I could shove any product in my array. My array also does not give an orderID. I like to check things before moving forward but I can't figure out how to create a GenericOrder with some computer parts.

abstract class Product {
    protected float price;

    // return the price of a particular product
    abstract float price();
}

class ComputerPart extends Product {
    public ComputerPart(float p) {
        price = p;
    }

    @Override
    public float price() {
        return price;
    }
}

class GenericOrder<T> {
    private static int ID = 0;
    private String orderID;
    List<T> order;

    public GenericOrder() {
        orderID = "order ID " + ID++;
        order = new ArrayList<>();
    }

    public String getOrderID() {
        return orderID;
    }

    public void addToOrder(T newProduct) {
        order.add(newProduct);
    }

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

    public static void main(String args[]) {
        GenericOrder  order = new GenericOrder ();
        order.getOrderID();
        order.addToOrder(new ComputerPart(5));
        order.getProducts();
    }
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
user2899211
  • 63
  • 1
  • 8
  • 1
    1. indent your code. 2. Post the exact and complete messages from the compiler instead of paraphrasing them. 3. Don't use raw types (that's probably what the warning is telling you). GenericOrder is generic, so you should dpecify its generic type. – JB Nizet Sep 13 '17 at 05:36
  • `GenericOrder order` - don't use raw types. Use `GenericOrder`. – Eran Sep 13 '17 at 05:37
  • 2
    (as a side note, don't use `float` for prices, see https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) –  Sep 13 '17 at 05:40
  • *Curious:* What is the point of calling `getOrderID()` and `getProducts()` when you don't use the return values? – Andreas Sep 13 '17 at 05:45

2 Answers2

2

In your main simply specify type of the generic:

public static void main(String args[]) {
    GenericOrder<ComputerPart> order = new GenericOrder<>();
    order.getOrderID();
    order.addToOrder(new ComputerPart(5));
    order.getProducts();
}
xenteros
  • 15,586
  • 12
  • 56
  • 91
2

List<T> does not mean that the list can store anything. It means that the list can store T, and T can be any one type. For example, if T is Integer, List<T> can only store integers.

The problem here is that GenericOrder is a generic type. Generally when you use generic types, you specify all the generic arguments, but you didn't:

GenericOrder  order = new GenericOrder ();

In this case, the compiler treats this as having a generic parameter of Object. This means that the inner list can store Objects or anything that extends Object. This is clearly not what you wanted. Therefore, you should add the generic arguments to say that you want a GenericOrder of ComputerPart:

GenericOrder<ComputerPart> order = new GenericOrder<>();
xenteros
  • 15,586
  • 12
  • 56
  • 91
Sweeper
  • 213,210
  • 22
  • 193
  • 313