Quote:
public class AssembledPart extends Part
{
static Part part0 = new Part(null,null,0, 0);
static Part part1 = new Part(null,null, 0, 0);
public AssembledPart(String ID,String name, int stockLevel,double unitPrice,Part p0,Part p1)
{
super( ID,name,stockLevel,unitPrice);
part0=p0;
part1=p1;
}
static public int getAvailForAssembly()
{
int min;
if(part0.getStockLevel() >= part1.getStockLevel())
min=part1.getStockLevel();
else
min=part0.getStockLevel();
return min;
}
}
The first one, AssembledPart, is a sub- class of Part. In this, please pay attention to the 3 static variables and method. Quote:
public class Part{
private String ID;
private String name;
private int stockLevel;
private double unitPrice;
/**
* Constructor
*/
public Part()
{
ID = null;
name=null;
stockLevel = 1;
unitPrice=1.0;
}
public Part(String ID, String name, int stockLevel, double unitPrice){
this.ID = ID;
this.name = name;
this.stockLevel = stockLevel;
this.unitPrice = unitPrice;
}
public String getID(){
return ID;
}
public String getName(){
return name;
}
public int getStockLevel(){
return stockLevel;
}
public double getUnitPrice(){
return unitPrice;
}
/**
* Replenish the inventory
*/
public void replenish(int qty){
stockLevel += qty;
}
/**
* Suppply.
* Return the cost of the supply if there's enough in stock
* otherwise return -1.0
*/
public double supply(int qty){
if (qty <= stockLevel){
stockLevel = stockLevel - qty;
return (qty * unitPrice);
}
else if (qty <= stockLevel+AssembledPart.getAvailForAssembly()){
AssembledPart.part0.stockLevel= AssembledPart.part0.stockLevel - qty + stockLevel;
AssembledPart.part1.stockLevel= AssembledPart.part1.stockLevel - qty + stockLevel;
stockLevel=0;
return (qty * (unitPrice + AssembledPart.part0.unitPrice + AssembledPart.part1.unitPrice));
}
else
return -1;
}
}
Please note the supply method, in which used the static method and variable in the AssembledPart class. And I think it is necessary. Quote:
public class Main {
public static void main(String[] args) {
Part[] P = new Part[4];
P[0] = new Part("p101", "Crank", 218, 12.20);
P[1] = new Part("p102", "Pedal", 320, 14.30);
P[2] = new Part("p103", "HandleBar", 120, 35.50);
P[3] = new Part("p104", "Stem", 90, 20.00);
AssembledPart[] AP = new AssembledPart[2];
AP[0] = new AssembledPart("p183", "Crank-Pedal", 80, 3.50, P[0], P[1]);
AP[1] = new AssembledPart("p184", "HandleBar-Stem", 30, 1.50, P[2], P[3]);
......
}
}
The problem is here: When I declare an array for Assembled Part, please notice the last 2 arguments, which are P[0] and P[1], then P[2] and P[3]. because of static variable I declared earlier in AssembledPart class, when I declare AP[1] with P[2] and P[3], AP[0]'s 2 last arguments, which suppose to be P[0] and P[1], change in to P[2] and P[3].