Added iteration logic.

This commit is contained in:
Ilkka Seppala 2015-05-08 21:01:06 +03:00
parent 41b818771e
commit fba664ba49
5 changed files with 44 additions and 9 deletions

View File

@ -1,7 +1,16 @@
package com.iluwatar;
import java.util.ArrayList;
import java.util.List;
public class App {
public static void main( String[] args ) {
List<GameObject> objects = new ArrayList<>();
objects.add(new FlamingAsteroid(0, 0, 5, 5));
objects.add(new SpaceStationMir(1, 1, 4, 4));
objects.add(new Meteoroid(10, 10, 15, 15));
objects.add(new SpaceStationIss(12, 11, 14, 15));
objects.stream().forEach(o1 -> objects.stream().forEach(o2 -> { if (o1 != o2) System.out.println(String.format("%s -> %s", o1, o2)); } ));
}
}

View File

@ -1,8 +0,0 @@
package com.iluwatar;
public class Asteroid extends Meteoroid {
public Asteroid(int left, int top, int right, int bottom) {
super(left, top, right, bottom);
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public class FlamingAsteroid extends Meteoroid {
public FlamingAsteroid(int left, int top, int right, int bottom) {
super(left, top, right, bottom);
setOnFire(true);
}
}

View File

@ -2,12 +2,32 @@ package com.iluwatar;
public abstract class GameObject extends Rectangle {
private boolean damaged;
private boolean onFire;
public GameObject(int left, int top, int right, int bottom) {
super(left, top, right, bottom);
}
@Override
public String toString() {
return this.getClass().getSimpleName();
return String.format("%s at %s damaged=%b onFire=%b", this.getClass().getSimpleName(),
super.toString(), isDamaged(), isOnFire());
}
public boolean isOnFire() {
return onFire;
}
public void setOnFire(boolean onFire) {
this.onFire = onFire;
}
public boolean isDamaged() {
return damaged;
}
public void setDamaged(boolean damaged) {
this.damaged = damaged;
}
}

View File

@ -30,4 +30,9 @@ public class Rectangle {
boolean intersectsWith(Rectangle r) {
return !(r.getLeft() > getRight() || r.getRight() < getLeft() || r.getTop() > getBottom() || r.getBottom() < getTop());
}
@Override
public String toString() {
return String.format("[%d,%d,%d,%d]", getLeft(), getTop(), getRight(), getBottom());
}
}