Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Asttronaut
- package SpaceStationRecruitment;
- public class Astronaut {
- private String name;
- private int age;
- private String country;
- public Astronaut (String name, int age, String country) {
- this.name = name;
- this.age = age;
- this.country = country;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- public String getCountry() {
- return country;
- }
- @Override
- public String toString() {
- return String.format("Astronaut: %s, %d (%s)",getName(),getAge(),getCountry());
- }
- }
- //SpaceStation
- package SpaceStationRecruitment;
- import java.util.ArrayList;
- import java.util.List;
- public class SpaceStation {
- private String name;
- private int capacity;
- private List<Astronaut> data;
- public SpaceStation(String name, int capacity) {
- this.name = name;
- this.capacity = capacity;
- this.data = new ArrayList<>();
- }
- public String getName() {
- return name;
- }
- public int getCapacity() {
- return capacity;
- }
- public int getCount() {
- return data.size();
- }
- public void add(Astronaut astronaut) {
- if (capacity == 0) {
- } else {
- for (int i = 0; i < capacity; i++) {
- if (!data.contains(astronaut)) {
- data.add(astronaut);
- }
- }
- }
- }
- public boolean remove(String name) {
- if (capacity == 0) {
- return false;
- } else {
- for (int i = 0; i < data.size(); i++) {
- if (data.get(i).getName().equals(name)) {
- data.remove(i);
- return true;
- }
- }
- }
- return false;
- }
- public Astronaut getOldestAstronaut() {
- return data.stream().max((f, s) -> Integer.compare(f.getAge(), s.getAge())).get();
- }
- public Astronaut getAstronaut(String name) {
- Astronaut givenName = null;
- for (Astronaut astronaut : this.data) {
- if (astronaut.getName().equals(name)) {
- givenName = astronaut;
- }
- }
- return givenName;
- }
- public String report() {
- StringBuilder sb = new StringBuilder();
- sb.append(String.format("Astronauts working at Space Station %s:\n", getName()));
- int counter = 0;
- for (Astronaut astronaut : data) {
- if (counter == data.size() - 1) {
- sb.append(String.format("Astronaut: %s, %d (%s)", astronaut.getName(), astronaut.getAge(), astronaut.getCountry()));
- } else {
- sb.append(String.format("Astronaut: %s, %d (%s)\n", astronaut.getName(), astronaut.getAge(), astronaut.getCountry()));
- }
- }
- return sb.toString().trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement