1

I am working on elevator problem in Java. One of the problems I am coming across is how to assign object to objects. For example there are 7 floors and each floor is an object. So...

1
  • I would have a field in the passenger list that lists what floor they are a part of. That or you can have each floor hold a list of passengers that belong on that floor. The second solution seems more natural, as what happens if you have a passenger that doesn't live on any floors? You seem to be mostly right on track Commented Mar 22, 2014 at 20:25

4 Answers 4

4

You could make the resident set a member of the Floor class, so each instance keeps track of it's own residents:

public class Floor {
    private Set<Passenger> resident = new HashSet<>();

    public boolean isResident(Passenger p) {
        return resident.contains(p);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I feel that residence floor is a field that belongs to the resident. Simply, hold a Floor object in your Passenger object.

public class Passenger {
  private Floor residenceFloor;

  public void setResidenceFloor(Floor residenceFloor) {
    this.residenceFloor = residenceFloor;
  }

  public Floor getResidenceFloor() {
    return residenceFloor;
  }
}

When evaluating whether a resident belongs to a certain floor simply use an overriden equals method for your Floor object.

Floor f1 = new Floor();
Passenger p1 = new Passenger();
p1.setResidenceFloor(f1);

if (f1.equals(p1.gerResidenceFloor()) {
  // p1 is a resident of f1
}

You could also hold a Set of residents in your Floor objects. These are just two different OOP styles.

Comments

0

The short answer is: Yes.

Since you have a wrapper function isResident, you could also implement a wrapper addResident and declare resident as private

Comments

0

Not sure what your overall goal or usage is, but it seems like giving the Floor class a field floorNumber that is set by a constructor could give some added flexibility.

Floor floor1 = new Floor(1);

Then just have residenceFloor be a field of Passenger:

public class Passenger {
  private int residenceFloor;

  public void setResidenceFloor(Floor residenceFloor) {
    this.residenceFloor = residenceFloor.floorNumber;
  }

  public Floor getResidenceFloor() {
    return residenceFloor;
  }
}

Having floor number as a field just seems more useful to me, as it allows an easy way to have other methods/fields refer to it. Passengers could have currentFloor fields for where they are at the moment, or residents might move to a different floor, and any method that needed to change or set a floor state could just use floorNumber of whatever floor you passed it.

1 Comment

It's hard to tell for sure what's happening with just the snippets shown, but perhaps some of the issue has to do with where you have floor(0), floor(1), which seem to be passing an integer value to a method floor(), but it seems you are actually wanting to reference the resident hashSet of a specific floor instance. So perhaps you want building.floor0.resident.add(passenger) instead? floor1.resident.add(p1) would add passenger object p1 to the resident hashSet of the floor1 object. I'm not sure what building.floor(1).resident.add(p1) would do since I don't see code for a floor() method.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.