Reputation: 51
I'm trying to make a setter method (setAvailForAssembly) that sets the assembledstocklevel but for some reason my if conditions don't seem to be making any effect. Why this is happening?
Main Class
public class TestAssembledPart {
public static void main(String args[]) {
// Constructing two Part objects (the base parts)
Part part0 = new Part("p101", "Crank", 218, 12.20);
Part part1 = new Part("p102", "Pedal", 320, 14.30);
// Constructing AssembledPart object assembled from p101 & p102
AssembledPart part2 = new AssembledPart("p183", "Crank & Pedal", 80, 3.50, part0, part1);
// replenishing stock by 100 items
part2.replenish(100);
System.out.println(part2);
// Supplying the maximum possible assembled parts - combination
// of existing parts and base parts available for assembly
System.out.println("Supplying max number of (assembled) part");
int totalAvail = part2.getStockLevel() + part2.getAvailForAssembly();
System.out.println("part2 stocklevel: " + part2.getStockLevel() + " part2 available for assembly: " + part2.getAvailForAssembly());
}
}
AssembledPart Class
public class AssembledPart extends Part {
private Part basica;
private Part basicb;
private int assembledstocklevel;
public AssembledPart(String id, String name, int stocklevel, double unitprice,
Part part0, Part part1) {
super(id, name, stocklevel, unitprice);
this.basica = part0;
this.basicb = part1;
}
public void setAvailForAssembly() {
if(basica.getStockLevel() >= basicb.getStockLevel()){
assembledstocklevel = basica.getStockLevel();
} else assembledstocklevel = basicb.getStockLevel();
}
public int getAvailForAssembly() {
return assembledstocklevel;
}
}
Part Class
public class Part {
protected int stocklevel;
public Part(String id, String name, int stocklevel, double unitprice) {
this.id = id;
this.name = name;
this.stocklevel = stocklevel;
this.unitprice = unitprice;
}
public int getStockLevel() {
return stocklevel - qty;
}
public void setStockLevel(int stocklevel) {
this.stocklevel = stocklevel;
}
}
Upvotes: 0
Views: 70
Reputation: 28106
Due to your main method, you just forget to call setAvailForAssembly()
method, that is the reason, you are getting 0 as the result of caling gette method.
Furthermore, getters and setters are usualy used to access fields and doesn't provide any additional logic. In your case, setter method without an argument, could possibly confuse someone in the future. Just call it like calculateAvailForAssembly
or something like this.
Upvotes: 1