0

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;
        }


    }

setter not working!

danielb
  • 51
  • 1
  • 6
  • 1
    You haven't shown any code *calling* your `setAvailForAssembly` method. Note that it's also pretty odd for a setter not to have a parameter... I'd probably just put that logic into the getter, given that you don't need any new state. – Jon Skeet Oct 16 '15 at 03:12
  • how can i add the logic into the getter method? because it only takes a return statement and adding conditions causes error – danielb Oct 16 '15 at 03:38

1 Answers1

1

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.

Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • Yes i think i got it. I created a method called calulateAvailForAssembly(). Added in if-else conditions and returned assembledstocklevel; Then just passed this method as the return for getAvailForAssembly(); – danielb Oct 16 '15 at 03:48