Guest User

Untitled

a guest
May 18th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1.  
  2. abstract class Race{}
  3. class MordorUnit extends Race{}
  4. class MiddleEarthUnit extends Race{}
  5.  
  6. interface Unit<T> {}
  7.  
  8. interface Infantry<T> extends Unit{}
  9. interface Cavalry<T> extends Unit{}
  10.  
  11.  
  12. public class Army<T extends Race> {
  13. private ArrayList<Cavalry<T>> cavalry = new ArrayList<>();
  14. private ArrayList<Infantry<T>> infantry = new ArrayList<>();
  15. private ArrayList<Unit> army = new ArrayList<>();
  16.  
  17. public ArrayList<Cavalry<T>> getCavalry() {
  18. return cavalry;
  19. }
  20.  
  21. public ArrayList<Infantry<T>> getInfantry() {
  22. return infantry;
  23. }
  24.  
  25. public ArrayList<Unit> getArmy() {
  26. return army;
  27. }
  28.  
  29. public boolean recruit(Unit<T> unit) {
  30. if (unit instanceof Infantry){
  31. army.add(unit);
  32. return infantry.add((Infantry) unit);
  33. }
  34. if (unit instanceof Cavalry){
  35. army.add(unit);
  36. return cavalry.add((Cavalry) unit);
  37. }
  38.  
  39. return false;
  40. }
  41.  
  42. public boolean release(Unit unit) {
  43. if (cavalry.contains(unit)){
  44. army.remove(unit);
  45. return cavalry.remove(unit);
  46. }
  47. if (infantry.contains(unit)){
  48. army.remove(unit);
  49. return infantry.remove(unit);
  50. }
  51. return false;
  52. }
  53.  
  54. public void print() {
  55. army.stream().filter(x -> x instanceof Cavalry).forEach(x -> System.out.print(x.toString() + "\n"));
  56. army.stream().filter(x -> x instanceof Infantry).forEach(x -> System.out.print(x.toString() + "\n"));
  57. }
  58.  
  59. public Unit getRandomUnit() {
  60. return army.get((int) (Math.random() * army.size()));
  61. }
  62.  
  63. public Unit getRandomUnit(Unit unit) {
  64. if (unit instanceof Cavalry) cavalry.get((int) (Math.random() * cavalry.size()));
  65. if (unit instanceof Infantry) infantry.get((int) (Math.random() * infantry.size()));
  66. return null;
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment