I need to implement a combination of 2 methods that add wagon objects to a linked list of wagons. But i dont understand how to build it. Every wagon object has an attribute that references the next wagon( null at start). So i guess i have to change that reference when i add a new waggon. But how can i know where and how to add the waggon and how do i get a waggon in between of 2 others... if the train can be added is checked by the public method but how do i actually add the train with the helpermethod? Or do i actually put it in the locomotive class?
i cant put a dangerousgoods=true waggon together with passengerwagons and passengerwagons are always at the start of the train
package edu.hm.cs.swe2.trains;
public class Wagon {
private int wagonNumber;
private boolean isPassengerWagon;
private boolean carriesDangerousGoods;
public Wagon nextWagon;
public Wagon(boolean isPassengerWagon, boolean carriesDangerousGoods) {
if (isPassengerWagon == true && carriesDangerousGoods == true) {
this.carriesDangerousGoods = false;
System.out.println("passengerwagon cant handle dangerous goods");
} else
this.isPassengerWagon = isPassengerWagon;
this.carriesDangerousGoods = carriesDangerousGoods;
this.setWagonNumber(0);
this.setNextWagon(null);
}
public boolean isPassengerWagon() {
return isPassengerWagon;
}
public boolean isCarriesDangerousGoods() {
return carriesDangerousGoods;
}
public void setWagonNumber(int wagonNumber) {
this.wagonNumber = wagonNumber;
}
public void setNextWagon(Wagon nextWagon) {
this.nextWagon = nextWagon;
}
public void printWagon(int level) {
if (level == 3) {
if (nextWagon == null)
System.out.println("**********");
} else if (level == 2) {
if (this.isPassengerWagon == false && this.carriesDangerousGoods == true) {
System.out.println("* f" + this.wagonNumber + " dg *");
} else if (this.isPassengerWagon == false && this.carriesDangerousGoods == false) {
System.out.println("* f" + this.wagonNumber + " *");
} else if (this.isPassengerWagon == true) {
System.out.println("* p" + this.wagonNumber + " *");
}
} else if (level == 1) {
System.out.println("**********->");
} else if (level == 0) {
System.out.println(" *** *** ");
}
}
public boolean trainCarriesDangerousGoods() {
if (this.carriesDangerousGoods == true) {
return true;
} else if (this.nextWagon == null) {
return false;
} else {
return nextWagon.trainCarriesDangerousGoods();
}
}
public boolean trainHasPassengerWagons() {
if (this.isPassengerWagon == true) {
return true;
} else if (this.nextWagon == null) {
return false;
} else {
return nextWagon.trainHasPassengerWagons();
}
}
public Wagon addWagon(Wagon newWagon) {
if (this.trainCarriesDangerousGoods() == true && newWagon.isPassengerWagon() == true) {
System.out.println("cant add Passenger wagon to a train with dangerous goods");
}
if (newWagon.carriesDangerousGoods == true && this.trainHasPassengerWagons() == true) {
System.out.println("cant add dangerous goods to a train with passengerwaggons");
} else {
addWagon(this.wagonNumber, newWagon);
}
return newWagon;
}
private void addWagon(int wagonNumber, Wagon newWagon) {
}
}
package edu.hm.cs.swe2.trains;
public class Locomotive {
public int locomotiveCount;
public int steamLocomotiveCount;
MotivePower Propulsion;
public Locomotive(MotivePower Propulsion) {
this.Propulsion = Propulsion;
locomotiveCount++;
}
}