Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kindergarten;
- public class Child {
- //полета -> характеристики
- private String firstName;
- private String lastName;
- private int age;
- private String parentName;
- private String contactNumber;
- //методи -> действия
- //1. конструктор
- public Child(String firstName, String lastName, int age, String parentName, String contactNumber) {
- //ново дете
- this.firstName = firstName;
- this.lastName = lastName;
- this.age = age;
- this.parentName = parentName;
- this.contactNumber = contactNumber;
- }
- //2. getters and setters
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getParentName() {
- return parentName;
- }
- public void setParentName(String parentName) {
- this.parentName = parentName;
- }
- public String getContactNumber() {
- return contactNumber;
- }
- public void setContactNumber(String contactNumber) {
- this.contactNumber = contactNumber;
- }
- //3. toString
- @Override
- public String toString() {
- //"Child: {firstName} {lastName}, Age: {age}, Contact info: {parentName} - {contactNumber}"
- return String.format("Child: %s %s, Age: %d, Contact info: %s - %s",
- this.firstName, this.lastName, this.age, this.parentName, this.contactNumber);
- }
- }
- package kindergarten;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.stream.Collectors;
- public class Kindergarten {
- //полета -> хаарктеристики
- private String name;
- private int capacity;
- private List<Child> registry;
- //методи
- //1. конструктор
- public Kindergarten(String name, int capacity) {
- //нова детска градина
- this.name = name;
- this.capacity = capacity;
- this.registry = new ArrayList<>();
- }
- public boolean addChild(Child child) {
- //false -> ако нямаме място
- if (this.registry.size() >= this.capacity) {
- return false;
- }
- //true -> ако имаме място
- this.registry.add(child);
- return true;
- }
- public boolean removeChild(String firstName) {
- for (Child child : this.registry) {
- if (child.getFirstName().equals(firstName)) {
- this.registry.remove(child);
- return true;
- }
- }
- //нито едно дете не е с даденото име
- return false;
- }
- public int getChildrenCount() {
- return this.registry.size();
- }
- public Child getChild(String firstName) {
- for (Child child : this.registry) {
- if (child.getFirstName().equals(firstName)) {
- return child;
- }
- }
- //няма такова дете
- return null;
- //return this.registry.stream()
- // .filter(ch -> ch.getFirstName().equals(firstName))
- // .findFirst().orElse(null);
- }
- public String registryReport() {
- //by age ascending, then by first name ascending, then by last name ascending
- List<Child> sortedListOfChildren = this.registry.stream().sorted(Comparator.comparing(Child::getAge)
- .thenComparing(Child::getFirstName)
- .thenComparing(Child::getLastName))
- .collect(Collectors.toList());
- StringBuilder sb = new StringBuilder();
- sb.append(String.format("Registered children in %s:", this.name));
- for (Child child : sortedListOfChildren) {
- sb.append(System.lineSeparator()); //нов ред на всички операционни системи
- sb.append("--").append(System.lineSeparator());
- sb.append(child.toString());
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement