Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package spaceStationRecruitment;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.stream.Collectors;
  6.  
  7. public class SpaceStation {
  8. private String name;
  9. private int capacity;
  10. private Map<String, Astronaut> data;
  11.  
  12. public SpaceStation(String name, int capacity) {
  13. this.name = name;
  14. this.capacity = capacity;
  15. this.data = new LinkedHashMap<>();
  16. }
  17.  
  18. public String getName() {
  19. return name;
  20. }
  21.  
  22. public int getCapacity() {
  23. return capacity;
  24. }
  25.  
  26. public int getCount() {
  27. return data.size();
  28. }
  29.  
  30. public void add(Astronaut astronaut) {
  31. if(this.data.size()<this.getCapacity()){
  32. String name = astronaut.getName();
  33. data.put(name,astronaut);
  34. }
  35. }
  36.  
  37. public boolean remove(String astronaut_name) {
  38. if (!data.containsKey(astronaut_name)) {
  39. return false;
  40. } else{
  41. data.remove(astronaut_name);
  42. return true;
  43. }
  44. }
  45.  
  46. public Astronaut getOldestAstronaut() {
  47. return data.values().stream().sorted((f,s)->s.getAge()-f.getAge())
  48. .collect(Collectors.toList())
  49. .get(0);
  50. }
  51.  
  52. public Astronaut getAstronaut(String name) {
  53. return data.values().stream().filter(f->f.getName().equals(name))
  54. .collect(Collectors.toList())
  55. .get(0);
  56. }
  57.  
  58. public String report() {
  59. return String.format("Astronauts working at Space Station " + this.getName()+":%n"+
  60. this.data.values()
  61. .stream()
  62. .map(Astronaut::toString)
  63. .collect(Collectors.joining("%n")));
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement