java.lang.Object
edu.ntnu.idatt2001.carljgu.battle.Army
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 Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Add unit if it does not exist from before.void
Add units.boolean
Checks if two armies are equal based on name and list of units.Gets all units.int
Calculates the strength of an army by combining health, attack and armor of every unit.Returns a list of cavalry units using stream and filter.Returns a list of commander units using stream and filter.Returns a list of infantry units using stream and filter.getName()
Gets the army's name.Gets a random unit from the list.Returns a list of ranged units using stream and filter.int
hashCode()
Hashes name and units for the equals methodboolean
hasUnits()
Checks if the list of units is emptyboolean
Remove a specified unit.void
Sets the army's nameAnother method for return a representation of the army.toString()
Overrides the toString() method from Object.
-
Field Details
-
name
-
units
-
-
Constructor Details
-
Army
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
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
Gets the army's name.- Returns:
- name - String - the name of the army.
-
setName
Sets the army's name- Parameters:
name
- - String - the name of the army- Throws:
IllegalArgumentException
- thrown if name is not valid.
-
addUnit
Add unit if it does not exist from before.- Parameters:
unit
- Unit - an instance of Unit.
-
addUnits
Add units. Iterates over units and adds them if they do not exist from before.- Parameters:
units
- List - a list of units.
-
remove
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
Gets all units.- Returns:
- units - List - a list of units.
-
getInfantryUnits
Returns a list of infantry units using stream and filter.- Returns:
- List - a list of infantry units
-
getRangedUnits
Returns a list of ranged units using stream and filter.- Returns:
- List - a list of ranged units
-
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
Returns a list of commander units using stream and filter.- Returns:
- List - a list of commander units
-
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
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
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 ------------------------ -
equals
Checks if two armies are equal based on name and list of units. -
hashCode
public int hashCode()Hashes name and units for the equals method
-