java.lang.Object
edu.ntnu.idatt2001.carljgu.battle.Army

public class Army extends Object
public class Army. An army object stores a list of units meant to fight another army.
Version:
1.4 18.05.2022
Author:
Carl Gützkow
  • Field Details

  • Constructor Details

    • Army

      public Army(String name, List<Unit> units) throws IllegalArgumentException
      Instantiates a new Army. Used if a list of units already exists.
      Parameters:
      name - String - the name of the army.
      units - List - the units of the army.
      Throws:
      IllegalArgumentException - thrown if name is not valid.
    • Army

      public Army(String name)
      Instantiates a new Army. Second constructor if a list of units does not exist from before.
      Parameters:
      name - String - the name of the army
  • Method Details

    • getName

      public String getName()
      Gets the army's name.
      Returns:
      name - String - the name of the army.
    • setName

      public void setName(String name) throws IllegalArgumentException
      Sets the army's name
      Parameters:
      name - - String - the name of the army
      Throws:
      IllegalArgumentException - thrown if name is not valid.
    • addUnit

      public void addUnit(Unit unit)
      Add unit if it does not exist from before.
      Parameters:
      unit - Unit - an instance of Unit.
    • addUnits

      public void addUnits(List<Unit> units)
      Add units. Iterates over units and adds them if they do not exist from before.
      Parameters:
      units - List - a list of units.
    • remove

      public boolean remove(Unit unit)
      Remove a specified unit.
      Parameters:
      unit - Unit - an instance of Unit.
      Returns:
      boolean - true if the unit was removed
    • hasUnits

      public boolean hasUnits()
      Checks if the list of units is empty
      Returns:
      boolean - true if the list is not empty.
    • getAllUnits

      public List<Unit> getAllUnits()
      Gets all units.
      Returns:
      units - List - a list of units.
    • getInfantryUnits

      public List<Unit> getInfantryUnits()
      Returns a list of infantry units using stream and filter.
      Returns:
      List - a list of infantry units
    • getRangedUnits

      public List<Unit> getRangedUnits()
      Returns a list of ranged units using stream and filter.
      Returns:
      List - a list of ranged units
    • getCavalryUnits

      public List<Unit> getCavalryUnits()
      Returns a list of cavalry units using stream and filter. Makes sure units are not commander units
      Returns:
      List - a list of cavalry units
    • getCommanderUnits

      public List<Unit> getCommanderUnits()
      Returns a list of commander units using stream and filter.
      Returns:
      List - a list of commander units
    • getRandom

      public Unit getRandom()
      Gets a random unit from the list. If there are no units it returns a null object.
      Returns:
      Unit - an instance of Unit.
    • getCalculatedStrength

      public int getCalculatedStrength()
      Calculates the strength of an army by combining health, attack and armor of every unit.
      Returns:
      int - the army's calculated strength
    • strengthAndUnitRepresentation

      public String strengthAndUnitRepresentation()
      Another method for return a representation of the army. Includes the calculated strength and how many of each unit type. Example of a string representation: Strength: 302 Infantry: 1 Ranged: 5 Cavalry: 1 Commander: 0
      Returns:
      String - representation of the army.
    • toString

      public String toString()
      Overrides the toString() method from Object. iterates over all units and calls their toString() method. Uses StringBuilder to avoid making new Strings. Example of a string representation: Army Human army ------------------------ Infantry: 10 Ranged: 23 Cavalry: 4 Commander: 0 ------------------------
      Overrides:
      toString in class Object
      Returns:
      String - representation of the army.
    • equals

      public boolean equals(Object o)
      Checks if two armies are equal based on name and list of units.
      Overrides:
      equals in class Object
      Parameters:
      o - Object - object checking validity to.
      Returns:
      boolean - true if name and list of units are equal.
    • hashCode

      public int hashCode()
      Hashes name and units for the equals method
      Overrides:
      hashCode in class Object
      Returns:
      int - hashcode to the army