I've got:
- interface - ISpacecraft
- abstract class - Spacecraft (implements the interface above)
- Classes - 4 Kind of ships (derived children of Spacecraft)
Task: Return the count of every ship, if I have 2 cargoShips, then return 2.
Problem: When I iterate through the ArrayList It prints '2' twice, because I have 2 cargoShips.
Expected output: 2,1,1,2
Current output: 2,2,1,1,2,2
Question: How can I iterate through the number of types of ships, instead of all instances of them?
Note: I can't change the signature of 'printInstanceNumberPerClass'.
Code:
StarFleetManagerTester
public static void printInstanceNumberPerClass (ArrayList<ISpacecraft> fleet)
{
ArrayList<ISpacecraft> objects = new ArrayList<>(fleet);
ArrayList<Integer> cnt = new ArrayList<Integer>();
for(ISpacecraft obj : objects)
{
cnt.add(obj.getCount());
}
for(ISpacecraft obj : objects)
System.out.println(obj.getCount() +" "+obj.getName());
}
Spacecraft
protected static int countCruiser;
ISpacecraft
int getCount();
cargoShip, ReserarchShip, etc..
private static int count
@Override
public int getCount() {
return this.count;
}