implement and integrate VehicleTimeRecord

This commit is contained in:
Waetschker Daniel (uib17511) 2020-10-29 13:37:08 +01:00
parent e149a5c342
commit a3dd3b6585
3 changed files with 23 additions and 8 deletions

View File

@ -12,12 +12,14 @@ import java.util.Observer;
public class MainActivity extends AppCompatActivity implements Observer { public class MainActivity extends AppCompatActivity implements Observer {
protected Track track; protected Track track;
protected VehicleTimeRecord records;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
this.track = new Track(20); this.track = new Track(20);
this.records = null;
this.track.addObserver(this); this.track.addObserver(this);
} }
@ -32,14 +34,19 @@ public class MainActivity extends AppCompatActivity implements Observer {
+ "\n"; + "\n";
; ;
} }
String t = ""; //vtr arraylist to string
String text = s + "\n" + t;
TextView view = (TextView) findViewById(R.id.debugTextView); TextView view = (TextView) findViewById(R.id.debugTextView);
view.setText(s); view.setText(text);
view.invalidate(); view.invalidate();
} }
public void onStepButtonClick (View view) { public void onStepButtonClick (View view) {
for (int j = 0; j < 3000; j++) { for (int j = 0; j < 1; j++) {
this.track.timeElapse(50); this.track.timeElapse(50);
} }
} }
} }

View File

@ -1,11 +1,13 @@
package de.hems.trafficsim; package de.hems.trafficsim;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Observable; import java.util.Observable;
public class Track extends Observable { public class Track extends Observable {
protected List<Vehicle> vehicles; protected List<Vehicle> vehicles;
protected List<List<VehicleTimeRecord>> vtr_list;
public List<Vehicle> getVehicles() { public List<Vehicle> getVehicles() {
@ -15,6 +17,7 @@ public class Track extends Observable {
public Track(int numberVehicles) { public Track(int numberVehicles) {
this.vehicles = createVehiclesList(numberVehicles); this.vehicles = createVehiclesList(numberVehicles);
this.vtr_list = new LinkedList<>();
} }
protected List<Vehicle> createVehiclesList(int numberVehicles){ protected List<Vehicle> createVehiclesList(int numberVehicles){
@ -27,7 +30,7 @@ public class Track extends Observable {
return result; return result;
} }
public void timeElapse(float timeStep) { public void timeElapse(int timeStep) {
for(int i=0; i<vehicles.size();i++) { for(int i=0; i<vehicles.size();i++) {
Vehicle v = vehicles.get(i); Vehicle v = vehicles.get(i);
int forerunnerIndex = i + 1; int forerunnerIndex = i + 1;
@ -41,9 +44,12 @@ public class Track extends Observable {
} }
v.updateVelocity(distanceForerunner); v.updateVelocity(distanceForerunner);
} }
List <VehicleTimeRecord> records = new ArrayList<>(vehicles.size());
this.vtr_list.add(records);
for(Vehicle v: vehicles){ for(Vehicle v: vehicles){
v.timeElapse(50); v.timeElapse(50);
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, timeStep);
records.add(vtr);
} }
this.setChanged(); this.setChanged();

View File

@ -13,12 +13,14 @@ public class VehicleTimeRecord {
return timestep; return timestep;
} }
protected int id;
protected float position; protected float position;
protected float velocity; protected float velocity;
protected float timestep; protected int timestep;
public VehicleTimeRecord(float position, float velocity, float timestep) { public VehicleTimeRecord(int id, float position, float velocity, int timestep) {
this.position = position; this.id = id;
this.position = position; //needed???
this.velocity = velocity; this.velocity = velocity;
this.timestep = timestep; this.timestep = timestep;
} }