1

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++;
    }
}

1 Answer 1

1

Your Wagon should not point to the next Wagon. Create an inner class Node in the LinkedList<T> class that has a next pointer as well as others. The Node class should be something like the following:

class Node  {
   T object;

   Node next;
   Node previous; // optional depending on linked list type.
}

The LinkedList would be of type Wagon.
E.g. LinkedList<Wagon> list = new LinkedList<>(); and the Wagon instance would be placed in the object field of the node.

Sign up to request clarification or add additional context in comments.

2 Comments

and the linked list is implemented in the Locomotive constructor? (i need a new list for every new train) and then i add the append method in the Node class and use it in the private void class of wagon.java?
For a given linkedList you keep adding nodes with the Wagon object and then linking them via the next reference This means you need to keep a reference to the first node so you can iterate thru the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.