You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
683 B
31 lines
683 B
package de.srsoftware.web4rail.moving; |
|
|
|
import java.util.Vector; |
|
|
|
public class Train { |
|
private Vector<Locomotive> locos = new Vector<Locomotive>(); |
|
private Vector<Car> cars = new Vector<Car>(); |
|
private String name = null; |
|
|
|
public Train(Locomotive loco) { |
|
add(loco); |
|
} |
|
|
|
public void add(Car car) { |
|
if (car == null) return; |
|
if (car instanceof Locomotive) { |
|
locos.add((Locomotive) car); |
|
} else cars.add(car); |
|
} |
|
|
|
public int length() { |
|
int result = 0; |
|
for (Locomotive loco : locos) result += loco.length; |
|
for (Car car : cars) result += car.length; |
|
return result; |
|
} |
|
|
|
public String name() { |
|
return name != null ? name : locos.firstElement().name(); |
|
} |
|
}
|
|
|