简体   繁体   中英

Java collections and objects assignment

I have Floor class and it has following methods.

Passenger class has a constructor that sets current floor of residence to 1.

Following line adds passengers to the floor.

This is where I am struggling. Both of the following statements return true when I never loaded passenger on floor3.

Again, thank you for all the help.

You only ever do anything with the ground floor... I suspect there are other problems in the code as well.

public boolean isResident(Passenger passenger) {
  int floor = building.getFloor();
  return building.floor(0).resident.contains(passenger);
}

This method calls builder.getFloor() , then proceeds to completely disregard the output, and check if the ground floor contains the passenger.

public void enter(Passenger passenger) {     
  Floor.enterGroundFloor(passenger);    
}

This method, also, summarily puts passengers on the ground floor.

So:

Building.floor(1).enter(p1) 

Building.floor(1).isResident(p1) --> true    
Building.floor(3).isResident(p1) --> true   

This is natural because you are putting p1 on the ground floor, and then checking if he is on the ground floor twice.

You appear to have some fundamental misconceptions about what static means, and how object hierarchy should be structured. If you link the rest of your code, I am sure we can give you additional advice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM