I want to print all the items of an arrayList of objects. But it only prints the last item's values only. Here is my code:
List<items> boxes = new ArrayList<items>();
boxes.add(new items("1", 0.1f, 0.2f, 0.1f));
boxes.add(new items("2", 0.1f, 0.4f, 0.1f));
boxes.add(new items("3", 0.1f, 0.1f, 0.2f));
boxes.add(new items("4", 0.1f,0.1f, 0.3f));
boxes.add(new items("5", 0.2f, 0.1f, 0.1f));
boxes.add(new items("6", 0.1f, 0.1f, 0.1f));
boxes.add(new items("7", 0.2f, 0.3f, 0.1f));
boxes.add(new items("8", 0.1f, 0.3f, 0.1f));
boxes.add(new items("9", 0.2f, 0.2f, 0.2f));
for (items box: boxes){
System.out.println();
System.out.println("length: " +box.dimension.get("length"));
System.out.println("breadth: " +box.dimension.get("breadth"));
System.out.println("height: " +box.dimension.get("height"));
}
this is the items class:
import java.util.*;
public class items {
String boxNumber;
public static Map<String, Float> dimension = new HashMap<String, Float>();
public double volume;
public items(float l, float b, float h) {
volume = l * b * h;
dimension.put("length", l);
dimension.put("breadth", b);
dimension.put("height", h);
}
public items(String boxName, float i, float j, float k) {
boxNumber = boxName;
volume = i * j * k;
dimension.put("length", i);
dimension.put("breadth", j);
dimension.put("height", k);
}
public static Map<String, Float> getDimension() {
return dimension;
}
public static void setDimension(Map<String, Float> dimension) {
items.dimension = dimension;
}
public items rotateBox() {
Set<String> keySet = this.dimension.keySet();
String[] sides = keySet.toArray(new String[3]);
dimension.put(sides[1],
dimension.put(sides[2], dimension.get(sides[1])));
dimension.put(sides[0],
dimension.put(sides[2], dimension.get(sides[0])));
return this;
}
public double getVolume() {
// TODO Auto-generated method stub
return dimension.get("length") * dimension.get("breadth")
* dimension.get("height");
}
}
And this is what I always get:
length: 0.2
breadth: 0.2
height: 0.2
length: 0.2
breadth: 0.2
height: 0.2
length: 0.2
breadth: 0.2
height: 0.2
length: 0.2
breadth: 0.2
height: 0.2
length: 0.2
breadth: 0.2
height: 0.2
length: 0.2
breadth: 0.2
height: 0.2
length: 0.2
breadth: 0.2
height: 0.2
length: 0.2
breadth: 0.2
height: 0.2
length: 0.2
breadth: 0.2
height: 0.2
Can anyone please tell what is wrong with my code?