How to access a member in a class with arraylist that adds its toString

Viewed 85

I have a class, lets say CargoShip, which is a derived class of 'Starcraft', which implements the interface IStarcraft.

I have a function public static ArrayList<String> getSpacecraftDescriptionsByCommissionYear(ArrayList<ISpacecraft> fleet)

Question: The CargoShip has toString which prints name, commissionYear, etc.. I want to do two things: First, I want to use each Ship's toString (like the one in the CargoShip), and second I want it to be sorted by CommissionYear.

Problem: I don't know how to access the commissionYear field after I've added the toString to the arrayList.

ArrayList<String> strCommissions = new ArrayList<String>();

        for(ISpacecraft flee : fleet)
        {
            strCommissions.add(flee.toString());
        }
        //Collections.sort(//What to write here??//);
        return strCommissions;
}

Here is the CargoShip class if you need it:

package starfleet;

public class CargoShip extends Spacecraft{

    private int numberOfSpaceCranes;
    static int count = 0;

    public CargoShip(String name, int commissionYear, float maximalSpeed,int cargoCapacity, int numberOfSpaceCranes)
    {
        this.name = name;
        this.commissionYear = commissionYear;
        if(MaximalSpeed())
            this.maximalSpeed = maximalSpeed;
        this.cargoCapacity = cargoCapacity;
        this.numberOfSpaceCranes = numberOfSpaceCranes;
        count++;
    }

    public int getNumberOfSpaceCranes ()
    {
        return this.numberOfSpaceCranes;
    }


    @Override
    public String getName() {

        return this.name;
    }
    @Override
    public int getCommissionYear() {
        return this.commissionYear;
    }
    @Override
    public float getMaximalSpeed() {
        if(MaximalSpeed())
            return this.maximalSpeed;
        else
            return 0f;
    }
    @Override
    public int getCargoCapacity() {
        return this.cargoCapacity;
    }
    @Override
    public int getFirePower() {
        return this.firePower;
    }

    @Override
    public int getAnnualMaintenanceCost() {
        int cost = 0;
        this.commissionYear = 2000;

        cost += getCommissionYear();
        cost += (500 * this.numberOfSpaceCranes);
        cost += (2 * getCargoCapacity()); //To check: TotalCargoWeightCapacity?
        // TODO Auto-generated method stub
        return 0;
    }

    public String toString()
    {
        return
        "Name = " + getName() + System.lineSeparator() +
        "CommissionYear = " + getCommissionYear() + System.lineSeparator() +
        "MaximalSpeed = " + getMaximalSpeed() + System.lineSeparator() +
        "CargoCapacity = " + getCargoCapacity() + System.lineSeparator() +
        "FirePower = " + getFirePower() + System.lineSeparator() +
        "AnnualMaintenanceCost = " + getAnnualMaintenanceCost() + System.lineSeparator() + 
        "numberOfSpaceCranes = " + getNumberOfSpaceCranes() + System.lineSeparator();
    }
}
2 Answers
Related