Advertisement
desislava_topuzakova

Untitled

Jun 7th, 2023
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. package kindergarten;
  2.  
  3. public class Child {
  4. //полета -> характеристики
  5. private String firstName;
  6. private String lastName;
  7. private int age;
  8. private String parentName;
  9. private String contactNumber;
  10.  
  11.  
  12. //методи -> действия
  13. //1. конструктор
  14.  
  15. public Child(String firstName, String lastName, int age, String parentName, String contactNumber) {
  16. //ново дете
  17. this.firstName = firstName;
  18. this.lastName = lastName;
  19. this.age = age;
  20. this.parentName = parentName;
  21. this.contactNumber = contactNumber;
  22. }
  23.  
  24. //2. getters and setters
  25. public String getFirstName() {
  26. return firstName;
  27. }
  28.  
  29. public void setFirstName(String firstName) {
  30. this.firstName = firstName;
  31. }
  32.  
  33. public String getLastName() {
  34. return lastName;
  35. }
  36.  
  37. public void setLastName(String lastName) {
  38. this.lastName = lastName;
  39. }
  40.  
  41. public int getAge() {
  42. return age;
  43. }
  44.  
  45. public void setAge(int age) {
  46. this.age = age;
  47. }
  48.  
  49. public String getParentName() {
  50. return parentName;
  51. }
  52.  
  53. public void setParentName(String parentName) {
  54. this.parentName = parentName;
  55. }
  56.  
  57. public String getContactNumber() {
  58. return contactNumber;
  59. }
  60.  
  61. public void setContactNumber(String contactNumber) {
  62. this.contactNumber = contactNumber;
  63. }
  64.  
  65. //3. toString
  66.  
  67. @Override
  68. public String toString() {
  69. //"Child: {firstName} {lastName}, Age: {age}, Contact info: {parentName} - {contactNumber}"
  70. return String.format("Child: %s %s, Age: %d, Contact info: %s - %s",
  71. this.firstName, this.lastName, this.age, this.parentName, this.contactNumber);
  72. }
  73. }
  74.  
  75.  
  76. package kindergarten;
  77.  
  78. import java.util.ArrayList;
  79. import java.util.Comparator;
  80. import java.util.List;
  81. import java.util.stream.Collectors;
  82.  
  83. public class Kindergarten {
  84. //полета -> хаарктеристики
  85. private String name;
  86. private int capacity;
  87. private List<Child> registry;
  88.  
  89. //методи
  90. //1. конструктор
  91.  
  92. public Kindergarten(String name, int capacity) {
  93. //нова детска градина
  94. this.name = name;
  95. this.capacity = capacity;
  96. this.registry = new ArrayList<>();
  97. }
  98.  
  99. public boolean addChild(Child child) {
  100. //false -> ако нямаме място
  101. if (this.registry.size() >= this.capacity) {
  102. return false;
  103. }
  104. //true -> ако имаме място
  105. this.registry.add(child);
  106. return true;
  107. }
  108.  
  109. public boolean removeChild(String firstName) {
  110. for (Child child : this.registry) {
  111. if (child.getFirstName().equals(firstName)) {
  112. this.registry.remove(child);
  113. return true;
  114. }
  115. }
  116.  
  117. //нито едно дете не е с даденото име
  118. return false;
  119. }
  120.  
  121. public int getChildrenCount() {
  122. return this.registry.size();
  123. }
  124.  
  125. public Child getChild(String firstName) {
  126. for (Child child : this.registry) {
  127. if (child.getFirstName().equals(firstName)) {
  128. return child;
  129. }
  130. }
  131. //няма такова дете
  132. return null;
  133.  
  134. //return this.registry.stream()
  135. // .filter(ch -> ch.getFirstName().equals(firstName))
  136. // .findFirst().orElse(null);
  137. }
  138.  
  139. public String registryReport() {
  140. //by age ascending, then by first name ascending, then by last name ascending
  141. List<Child> sortedListOfChildren = this.registry.stream().sorted(Comparator.comparing(Child::getAge)
  142. .thenComparing(Child::getFirstName)
  143. .thenComparing(Child::getLastName))
  144. .collect(Collectors.toList());
  145. StringBuilder sb = new StringBuilder();
  146. sb.append(String.format("Registered children in %s:", this.name));
  147.  
  148.  
  149. for (Child child : sortedListOfChildren) {
  150. sb.append(System.lineSeparator()); //нов ред на всички операционни системи
  151. sb.append("--").append(System.lineSeparator());
  152. sb.append(child.toString());
  153. }
  154.  
  155. return sb.toString();
  156. }
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement