3

Context:

So basically I've got a class Map which contains a name and another class called Room. Room contains an int for the amount of doors it has, and a class called Door. Door contains a char for the position of the door, which can be N(orth), E(ast), S(outh), W(est).

Question:

This is how I've setup my Room class:

public class Room {
    int doors;
    Door door;


    public Room(int doors, Door door) {
        this.doors = doors;
        this.door = door;
    }
}

But this way I can only give it 1 door, and I need to be able to assign an amount of doors that's equivalent to the value of the int doors. Any ideas?

lospejos
  • 1,976
  • 3
  • 19
  • 35
Jesse
  • 41
  • 2
  • Additional info: This is my first ever question so my apologies if i made any stupid mistakes or if this sounds like a stupid question. Please feel free to point out any mistakes i've made. – Jesse Sep 21 '19 at 14:08
  • 2
    It depends on your purpose and future usage, but basically you need to have a _collection_ of `Door`s. It could be achieved using arrays, `List`s, etc. – lealceldeiro Sep 21 '19 at 14:10

4 Answers4

5

simply you must use a array or list which can contain multiple doors

public class Room {
        Door doors[];
    }

Or you can create an array-list if you are not sure with the number of doors when you create the room object because arrays are fixed in size.

public class Room {
    List<Door> doors = new ArrayList<Door>();
}

If you are using arrays you can initialize the door objects like this using the constructor

public Room(int num) {
        this.doors = new Door[num];
        Arrays.fill(this.doors,new Door());
    }
1

You can use a List to store the Doors in Room class and add multiple constructors to support creating a Room with an array of Doors or with a List of Doors. Also additionally you can define a method to add Doors to the Room after the instantiation.

public class Room {

    private final List<Door> doors = new ArrayList<>();

    public Room(Door... doors) {
        this.doors.addAll(Arrays.asList(doors));
    }

    public Room(List<Door> doors) {
        this.doors.addAll(doors);
    }

    public void addDoor(Door door) {
        this.doors.add(door);
    }

    public List<Door> getDoors() {
        return doors;
    }
}

If you are interested in some suggestions on your Door class, please read the below.

Since the direction can be either N, E, S or W, it's better to use an enum rather than a general char value to restrict the direction value only to those 4 values.

Udith Gunaratna
  • 2,091
  • 1
  • 13
  • 17
0

Use an array of doors. You dont need the number of doors as it will equal the length of the array

Patrick
  • 552
  • 5
  • 17
  • 4
    This post is really vague and seems more like a comment (there is a posted comment with more info than this answer). You should put examples, code snippet to help the OP understand your point better. – lealceldeiro Sep 21 '19 at 14:12
0

You could for example use an array of Doors:

public class Room {
    Door[] doors;


    public Room(int n) {
        this.doors = new Door[n];
        for (int i = 0; i < n; i++) {
            this.doors[i] = new Door();
        }
    }
}

This way you don't even need to store the amount of doors, since it's just the length of the doors array.

ruohola
  • 21,987
  • 6
  • 62
  • 97