Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Barracks {
  4.  
  5. public static void main(String[] args) {
  6. Barracks barracks = new Barracks();
  7. barracks.createFighters('a', Combat.MELEE, 3);
  8. barracks.createFighters('b', Combat.MELEE, 3);
  9. barracks.createFighters('c', Combat.MELEE, 3);
  10. barracks.callFighters("aabbcc", Combat.MELEE);
  11. }
  12.  
  13. private ArrayList<Fighter> fighters = new ArrayList<Fighter>();
  14.  
  15. /**
  16. * Creates a fighter and puts him in the barracks
  17. *
  18. * @param lr the first initial of the fighter
  19. * @param type the combat style the fighter mastered
  20. */
  21.  
  22. public void createFighter(char lr, Combat type) {
  23. Fighter r = new Fighter(Character.toUpperCase(lr), type);
  24. fighters.add(r);
  25. }
  26.  
  27. /**
  28. * Creates fighters and puts them into the barracks
  29. *
  30. * @param lr the first initial of the fighter
  31. * @param type the combat style the fighter mastered
  32. * @param amount the amount of fighters
  33. */
  34. public void createFighters(char lr, Combat type, int amount) {
  35. for (int i = 0; i < amount; i++)
  36. createFighter(lr, type);
  37. }
  38.  
  39. public class Fighter {
  40. private char initial;
  41. private Combat combatStyle;
  42.  
  43. /**
  44. *
  45. * @param _initial
  46. * The first initial of the fighter
  47. * @param _combatStyle
  48. * The combat style the fighter mastered
  49. */
  50. public Fighter(char _initial, Combat _combatStyle) {
  51. initial = _initial;
  52. combatStyle = _combatStyle;
  53. }
  54.  
  55. public char getInitial() {
  56. return initial;
  57. }
  58.  
  59. public Combat getCombatStyle() {
  60. return combatStyle;
  61. }
  62. }
  63.  
  64. public enum Combat {
  65. MAGIC, MELEE, RANGED
  66. }
  67.  
  68. /**
  69. * Calls all fighters that have initials that are in the name. Example: name
  70. * = war. All fighters with initials 'w','a', or 'r' will be called and
  71. * removed from the barracks.
  72. *
  73. * @param phrase
  74. * The phrase with initials
  75. * @param combatStyle
  76. * The combat style the fighter is specialized in
  77. */
  78. public ArrayList<Fighter> callFighters(String phrase, Combat combatStyle) {
  79. phrase = phrase.toUpperCase();
  80.  
  81. ArrayList<Fighter> tempBarracks = new ArrayList<Fighter>();
  82. ArrayList<Fighter> calledFighters = new ArrayList<Fighter>();
  83. tempBarracks.addAll(fighters);
  84.  
  85. ArrayList<Character> nameInChars = new ArrayList<>();
  86. for (char chr : phrase.toCharArray())
  87. nameInChars.add(chr);
  88.  
  89. System.out.println("We have " + tempBarracks.size() + " fighters.");
  90.  
  91. // This way ensures if we do something like "aabbcc" we get 3 of each
  92. // type, else it would get whatever came first that contained one of
  93. // the chars
  94. for (char c : nameInChars) {
  95. // Find the first fighter with this name, remove from temp barracks
  96. for (int f = 0; f < tempBarracks.size(); f++) {
  97. Fighter fgt = tempBarracks.get(f);
  98. if (fgt.getInitial() == c) {
  99. System.out.println("Removing " + c + ":" + fgt.getCombatStyle()+" from barracks");
  100. // Add to our called fighters
  101. calledFighters.add(fgt);
  102. // Remove from the barracks
  103. tempBarracks.remove(fgt);
  104. break;
  105. }
  106. }
  107. }
  108.  
  109. System.out.println("We removed " + calledFighters.size()
  110. + " fighters, leaving us with " + tempBarracks.size()
  111. + " in the barracks.");
  112.  
  113. return tempBarracks;
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement