Advertisement
Guest User

SpaceStationRecruitment

a guest
Oct 17th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. //Asttronaut
  2.  
  3. package SpaceStationRecruitment;
  4.  
  5. public class Astronaut {
  6. private String name;
  7. private int age;
  8. private String country;
  9.  
  10. public Astronaut (String name, int age, String country) {
  11. this.name = name;
  12. this.age = age;
  13. this.country = country;
  14. }
  15.  
  16. public String getName() {
  17. return name;
  18. }
  19.  
  20. public int getAge() {
  21. return age;
  22. }
  23.  
  24. public String getCountry() {
  25. return country;
  26. }
  27.  
  28. @Override
  29. public String toString() {
  30. return String.format("Astronaut: %s, %d (%s)",getName(),getAge(),getCountry());
  31. }
  32.  
  33.  
  34. }
  35. //SpaceStation
  36. package SpaceStationRecruitment;
  37.  
  38.  
  39. import java.util.ArrayList;
  40. import java.util.List;
  41.  
  42. public class SpaceStation {
  43. private String name;
  44. private int capacity;
  45. private List<Astronaut> data;
  46.  
  47.  
  48. public SpaceStation(String name, int capacity) {
  49. this.name = name;
  50. this.capacity = capacity;
  51. this.data = new ArrayList<>();
  52. }
  53.  
  54. public String getName() {
  55. return name;
  56. }
  57.  
  58. public int getCapacity() {
  59. return capacity;
  60. }
  61.  
  62. public int getCount() {
  63. return data.size();
  64. }
  65.  
  66. public void add(Astronaut astronaut) {
  67. if (capacity == 0) {
  68.  
  69. } else {
  70. for (int i = 0; i < capacity; i++) {
  71. if (!data.contains(astronaut)) {
  72. data.add(astronaut);
  73. }
  74. }
  75. }
  76. }
  77.  
  78. public boolean remove(String name) {
  79.  
  80. if (capacity == 0) {
  81. return false;
  82. } else {
  83. for (int i = 0; i < data.size(); i++) {
  84. if (data.get(i).getName().equals(name)) {
  85. data.remove(i);
  86. return true;
  87. }
  88. }
  89. }
  90. return false;
  91. }
  92.  
  93. public Astronaut getOldestAstronaut() {
  94. return data.stream().max((f, s) -> Integer.compare(f.getAge(), s.getAge())).get();
  95. }
  96.  
  97. public Astronaut getAstronaut(String name) {
  98. Astronaut givenName = null;
  99. for (Astronaut astronaut : this.data) {
  100. if (astronaut.getName().equals(name)) {
  101. givenName = astronaut;
  102. }
  103. }
  104. return givenName;
  105. }
  106.  
  107.  
  108. public String report() {
  109. StringBuilder sb = new StringBuilder();
  110. sb.append(String.format("Astronauts working at Space Station %s:\n", getName()));
  111. int counter = 0;
  112. for (Astronaut astronaut : data) {
  113. if (counter == data.size() - 1) {
  114. sb.append(String.format("Astronaut: %s, %d (%s)", astronaut.getName(), astronaut.getAge(), astronaut.getCountry()));
  115. } else {
  116. sb.append(String.format("Astronaut: %s, %d (%s)\n", astronaut.getName(), astronaut.getAge(), astronaut.getCountry()));
  117. }
  118. }
  119. return sb.toString().trim();
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement