0

I am attempting to edit values of liftobject from LiftObject.java in a array list called lift. I'm trying to use setDirection() to change this value from 0 to 22 (supposed to be -1 but i was just testing).

LiftObject.java

package system;

import java.io.Serializable;

public class LiftObject implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    int capacity;
    int direction;
    int currentFloor;
    int LiftNum;
    int id;

    public LiftObject(int capacity, int direction, int currentFloor, int LiftNum){
        this.capacity = capacity;
        this.direction = direction;
        this.currentFloor = currentFloor;
        this.LiftNum = LiftNum;
    }

    public int getCapacity(){return this.capacity;}
    public int getDirection(){return this.direction;}
    public int getCurrentFloor(){return this.currentFloor;}
    public int getLiftNum(){return this.LiftNum;}

    public int setCapacity(int capacity){return this.capacity;}
    public int setDirection(int direction){return this.direction;}
    public int setCurrentFloor(int currentFloor){return this.currentFloor;}
    public int setLiftNum(int LiftNum){return this.LiftNum;}


}

Then the main file, TestSystem.java

package system;

public class TestSystem {
    static int TopFloor = 20;
    static int Capacity = 8;
    int W_PersCount = 0;
    int A_PersCount = 0;
    int T_PersCount = 0;
    public static ObjectArrayList lift = new ObjectArrayList();
    public static ObjectArrayList wPers = new ObjectArrayList();
    public static ObjectArrayList tPers = new ObjectArrayList();
    public static ObjectArrayList aPers = new ObjectArrayList();

    public void DirectLift() {
        System.out.println("Direct Lift Start.");
        //The lift only changes direction when it hits the top or bottom
        int CF =((LiftObject) TestSystem.lift.get(0)).getCurrentFloor();
        System.out.println("Floor: ");
        System.out.println(CF);
        if( CF == 0) {
            ((LiftObject) TestSystem.lift.get(0)).setDirection(+1);
            System.out.println(((LiftObject) TestSystem.lift.get(0)).getDirection());
        }else if(CF == TopFloor){
            ((LiftObject) TestSystem.lift.get(0)).setDirection(-1);
            System.out.println(((LiftObject) TestSystem.lift.get(0)).getDirection());
        }else {
            System.out.println("Continue");
        }

    }


    public void LiftGeneration(int capacity, int direction, int currentFloor, int LiftNum) {
        LiftObject obj = new LiftObject(capacity,direction,currentFloor,LiftNum);
        obj.capacity = capacity;
        obj.direction = direction;
        obj.currentFloor = currentFloor;
        obj.LiftNum = LiftNum;


        lift.add(obj);
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getCapacity());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getDirection());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getCurrentFloor());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getLiftNum());
    }

    public void System() {
        //The lift only changes direction when it hits the top or bottom
        int CF =((LiftObject) TestSystem.lift.get(0)).getCurrentFloor();
        System.out.println("Floor: ");
        System.out.println(CF);
        System.out.println("Direct Lift Start.");
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getCapacity());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getDirection());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getCurrentFloor());
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getLiftNum());
        ((LiftObject)TestSystem.lift.get(0)).setDirection(22);
        System.out.println(((LiftObject) TestSystem.lift.get(0)).getDirection());

    }

    public static void main(String[] args) {
        System.out.println("Start.");
        TestSystem myObj = new TestSystem(); // Create an object of MyClass
        myObj.LiftGeneration(Capacity, 0, 0, 1);
        System.out.println("Works here");
        myObj.System();

    }

The problem is that no change occurs when I try and change the direction from 0 to -1 Any help would be greatly appreciated, thank you.

1
  • 1
    Your setters are completly wrong. they should set the values and not return values. Most IDEs will have an option to generate getters/setters for you. Commented Mar 27, 2020 at 15:15

1 Answer 1

1

Your problem is here:

 public int setDirection(int direction){return this.direction;}

It should be:

 public void setDirection(int direction){this.direction = direction;}

All your setters are written the same as your getters. When you write a setter you don't return a value; you set it. You'll want to adjust all your other setters to be something similar to what is here. Otherwise, you'll have issues with them too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.