public static void main(String[] args) {
List<Room> rooms = new ArrayList<Room>();
// make new rooms and add to the list
House house = makeHouse(1, rooms);
}
// i want to refactor make house method, bsically the for loop to look much better
public static House makeHouse(int houseNo, List<Room> rooms) {
House house = new House();
house.setHouseNo(houseNo);
for (int i = 0; i < rooms.size(); i++) {
if (i == 0) {
house.setMasterRoom(rooms.get(i));
} else if (i == 1) {
house.setRoom2(rooms.get(i));
} else {
house.setRoom3(rooms.get(i));
}
}
return house;
}
}
I have the above classes and I want to refactor the makehouse method because i do not like the way i have written the for loop. Note I cannot change the method signature or basically I cannot change how the House class is structured. The house class is given to me so i have no control over it.
I don't see the point of using a for-loop at all. Since your design is hard-coded to have exactly three rooms, what's wrong with just having three lines of code which assign those three rooms to the House?
andy kumar
Ranch Hand
Joined: Jun 08, 2009
Posts: 44
posted
0
it is not necessary that it will have 3 rooms....it will have a master room but the rest can be null
andy kumar
Ranch Hand
Joined: Jun 08, 2009
Posts: 44
posted
0
so i guess...there is no way i can improve the for loop right?....any suggestions will be appreciated