i am trying to create an ArrayList of product, i have three types of product:
ProductLine(mainClass), PerishableProductLine(extends ProductLine), ItemisedProductLine(extends ProductLine).
ProductLine Class
Fields: Product Code, recRetPrice, Sale Price, Quantity.
PerishableProductLine Class Fields: Days to Expiry
ItemisedProductLine Class
Fields: String[] Serial Numbers
I am trying to hard code the product in the Inventory Class but i don't know how to insert an array into an ArrayList as one of the parameter, String [] Serial Numbers;
public Inventory()
{
products = new ArrayList<ProductLine>();
products.add(new ProductLine("A0001", 10.90, 9.90, 100, "N/A"));
products.add(new ProductLine("B0010", 12.00, 7.50, 125, "5"));
products.add(new ProductLine("C0100", 3.00, 2.30, 1000, "16"));
products.add(new ItemisedProductLine("D1000", 2600, 2490, 2, Arrays.asList("a b d", "e f g h", "i j k l"))));
products.add(new ProductLine("E0001", 699, 509, 3, Arrays.asList(new serialNumbers("CCCC333333"),new serialNumbers("DDDD444444"), new serialNumber("AAAA222222"))));
}
As you can see in line 7 and 8 i tried to hard code the array as suggested in other similar questions on Stack Overflow but it does not compile.
The errors are generic(stuff like "missing ';' or expecting ')'...")don't know if this can help, I hope i described well the scenario.
ProductLine class
public class ProductLine
{
private String productCode; // Format X9999
private double recRetPrice;
private double salePrice;
private int quantity;
private String description;
public ProductLine(String productCode, double recRetPrice, double salePrice, int quantity, String description)
{
this.productCode = productCode;
this.recRetPrice = recRetPrice;
this.salePrice = salePrice;
this.quantity = quantity;
this.description = description;
}
public void sold(int no){
}
public String getProductCode(){
return productCode;
}
public double getPrice(){
return salePrice;
}
/*public String getSerialNumber(){
return ItemisedProductLine.serialNumbers;
}*/ //Does not compile
}
ItemisedProductLine
public class ItemisedProductLine extends ProductLine
{
public static String[] serialNumbers;
public ItemisedProductLine(String productCode, double recRetPrice, double salePrice, int quantity, String[] SerialNumber){
super( productCode, recRetPrice, salePrice, quantity, description);
this.serialNumbers = serialNumbers;
}
public void sold(int no){}
}
ItemisedProductLine
andProductLine
constructors. in itself your code is fine. – Marko Topolnik May 10 '12 at 8:48