7 changed files with 76 additions and 72006 deletions
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
package de.srsoftware.web4rail.moving; |
||||
|
||||
public class Car { |
||||
public int length; |
||||
private String name; |
||||
|
||||
public Car(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
String name(){ |
||||
return name; |
||||
} |
||||
} |
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
package de.srsoftware.web4rail.moving; |
||||
|
||||
public class Locomotive extends Car { |
||||
|
||||
public Locomotive(String name) { |
||||
super(name); |
||||
} |
||||
} |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
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(); |
||||
} |
||||
} |
Loading…
Reference in new issue