I have the following java class with constructor and getters and setters.
public class Train implements Comparable< Train >{
private String power; // "steam", "diesel" or "electric"
private int numPassengers; // total number of passengers
private Wagon freight; // linked list of freight wagons; does not include passenger carriages;
public String getPower() {
return power;
}
public void setPower(String power) {
this.power = power;
}
public int getNumPassengers() {
return numPassengers;
}
public void setNumPassengers(int numPassengers) {
this.numPassengers = numPassengers;
}
public Wagon getFreight() {
return freight;
}
public void setFreight(Wagon freight) {
this.freight = freight;
}
@Override
public int compareTo(Train o) {
return new Integer(this.getNumPassengers()).compareTo(new Integer(o.getNumPassengers()));
}
public Train(String power){
setPower(power);
setNumPassengers(0);
setFreight(null);
}
public Train(String power, int pass){
setPower(power);
setNumPassengers(pass);
setFreight(null);
}
}
And also another class, wagon which is used by the train class.
class Wagon {
private String cargo; // e.g. "wheat"
private int weight; // weight of the cargo in tonnes
private Wagon next; // the wagon that follows this one in the train; or null if there isn't one
public String getCargo() {
return cargo;
}
public void setCargo(String cargo) {
this.cargo = cargo;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Wagon getNext() {
return next;
}
public void setNext(Wagon next) {
this.next = next;
}
public Wagon(String cargo, int weight) {
setCargo(cargo);
setWeight(weight);
setNext(null);
}
public Wagon(String cargo, int weight, Wagon next) {
setCargo(cargo);
setWeight(weight);
setNext(next);
}
}
I initialise a new train like so:
Train trainC2 = new Train("steam", 0);
trainC2.setFreight(new Wagon("wheat", 400, new Wagon("wheat", 300, new Wagon("maize", 280))));
I want to write a method that takes a train as an input, iterates through the wagons, if the cargo attribute matches a supplied string, the wagon is deleted. I have written this method which creates a new linkedlist and adds the trains wagons to it. It then iterates through this list and removes any matches. I am trying to iterate again through the modified list and use the setFreight method to update the actual train object.
public static void removeWagons(Train train, String cargo) {
LinkedList<Wagon> wag8 = new LinkedList<Wagon>();
wag8.add(train.getFreight());
ListIterator<Wagon> iter = wag8.listIterator();
Wagon currentWagon = null;
while (iter.hasNext()) {
currentWagon = iter.next();
System.out.println();
if (currentWagon.getCargo() == cargo) {
System.out.println("wag8b4" + wag8);
iter.remove();
System.out.println("wag8" + wag8);
}
}
while (iter.hasNext()) {
train.setFreight(iter.next());
}
}
I think I'm close, but don't know how to write a method that will produce an output like:
trainC2.setFreight(new Wagon("wheat", 400, new Wagon("wheat", 300, new Wagon("maize", 280))));
Or maybe there's a better way altogether?