Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Barracks {
- public static void main(String[] args) {
- Barracks barracks = new Barracks();
- barracks.createFighters('a', Combat.MELEE, 3);
- barracks.createFighters('b', Combat.MELEE, 3);
- barracks.createFighters('c', Combat.MELEE, 3);
- barracks.callFighters("aabbcc", Combat.MELEE);
- }
- private ArrayList<Fighter> fighters = new ArrayList<Fighter>();
- /**
- * Creates a fighter and puts him in the barracks
- *
- * @param lr the first initial of the fighter
- * @param type the combat style the fighter mastered
- */
- public void createFighter(char lr, Combat type) {
- Fighter r = new Fighter(Character.toUpperCase(lr), type);
- fighters.add(r);
- }
- /**
- * Creates fighters and puts them into the barracks
- *
- * @param lr the first initial of the fighter
- * @param type the combat style the fighter mastered
- * @param amount the amount of fighters
- */
- public void createFighters(char lr, Combat type, int amount) {
- for (int i = 0; i < amount; i++)
- createFighter(lr, type);
- }
- public class Fighter {
- private char initial;
- private Combat combatStyle;
- /**
- *
- * @param _initial
- * The first initial of the fighter
- * @param _combatStyle
- * The combat style the fighter mastered
- */
- public Fighter(char _initial, Combat _combatStyle) {
- initial = _initial;
- combatStyle = _combatStyle;
- }
- public char getInitial() {
- return initial;
- }
- public Combat getCombatStyle() {
- return combatStyle;
- }
- }
- public enum Combat {
- MAGIC, MELEE, RANGED
- }
- /**
- * Calls all fighters that have initials that are in the name. Example: name
- * = war. All fighters with initials 'w','a', or 'r' will be called and
- * removed from the barracks.
- *
- * @param phrase
- * The phrase with initials
- * @param combatStyle
- * The combat style the fighter is specialized in
- */
- public ArrayList<Fighter> callFighters(String phrase, Combat combatStyle) {
- phrase = phrase.toUpperCase();
- ArrayList<Fighter> tempBarracks = new ArrayList<Fighter>();
- ArrayList<Fighter> calledFighters = new ArrayList<Fighter>();
- tempBarracks.addAll(fighters);
- ArrayList<Character> nameInChars = new ArrayList<>();
- for (char chr : phrase.toCharArray())
- nameInChars.add(chr);
- System.out.println("We have " + tempBarracks.size() + " fighters.");
- // This way ensures if we do something like "aabbcc" we get 3 of each
- // type, else it would get whatever came first that contained one of
- // the chars
- for (char c : nameInChars) {
- // Find the first fighter with this name, remove from temp barracks
- for (int f = 0; f < tempBarracks.size(); f++) {
- Fighter fgt = tempBarracks.get(f);
- if (fgt.getInitial() == c) {
- System.out.println("Removing " + c + ":" + fgt.getCombatStyle()+" from barracks");
- // Add to our called fighters
- calledFighters.add(fgt);
- // Remove from the barracks
- tempBarracks.remove(fgt);
- break;
- }
- }
- }
- System.out.println("We removed " + calledFighters.size()
- + " fighters, leaving us with " + tempBarracks.size()
- + " in the barracks.");
- return tempBarracks;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement